document.cookie = "resol=" + screen.width;

/*
Object.prototype.attachEvent = function (sEvent, fnHandler, bUseCapture) {
        this.addEventListener(sEvent.indexOf('on') == 0 ? sEvent.replace('on', '') : sEvent, fnHandler, bUseCapture);
}
*/
function showpicture(url,title) {
        var newwin = window.open('images/popup.html?' + url,null,0);
        newwin.focus();
}
function showpopup(title) {
        var newwin = window.open('/discount.html',null,1);
        newwin.focus();
}
function showpopup1(title) {
        var newwin = window.open('/vacancy.html',null,1);
        newwin.focus();
}

function getTop(elem) {
        var y = elem.offsetTop;
        while (elem) {
                y += elem.offsetTop;
                elem = elem.offsetParent;
        }
        return y;
}

function showDesc(pid, show) {
        var layout = getLayout();
        if ((layout != 'very-low') && (layout != 'mobile')) {
                var x = this.offsetLeft;
                var y = this.offsetTop;
                desc = document.getElementById('product-desc-' + pid);
                if (desc != null) {
                        if (getTop(desc) + desc.offsetHeight - 120 > document.height) {
                                desc.style.top = "-" + (desc.offsetHeight - 40) + "px";
                        }
                        if (show) {
                                desc.style.visibility = 'visible';
                        } else {
                                desc.style.visibility = 'hidden';
                        }
                }
        }
}

function lowResHack() {
        var centerWidth = screen.width - 400;
        var elemCount = Math.floor(centerWidth / 200);
        var elemWidth = centerWidth / elemCount;
        var mos = document.getElementsByTagName('div');
        for(i = 0; i < mos.length; i++) {
                elem = mos[i];
                if (hasClassName(elem, 'product-mosaic')) {
                        elem.style.width = elemWidth + 'px';
                }
        }
}

function showAuth() {
        window.scrollTo(0, 0);
        if ($('account_login') != null) {
                $('account_login').focus();
        }
        new Effect.Pulsate('auth', {
                duration: 2
        });
        return false;
}

function animateLoad() {
}

//window.attachEvent('onload', lowResHack, true);
//window.attachEvent('onload', animateLoad, true);

function clientCoord(elem) {
        var x = 0;
        var y = 0;
        do {
                x += elem.offsetLeft;
                y += elem.offsetTop;
                elem = elem.offsetParent;
        } while(elem);
        return {
                x : x,
                y : y
        };
}

function addToCart(element, pid) {
        var cartDiv;
        var loc;
        if (element != null) {
                if ($('product-detail') == null) {
                        cartDiv = element.parentNode.parentNode.parentNode.parentNode;
                } else {
                        cartDiv = $('product-detail');
                }
                loc = clientCoord(cartDiv);
        }
        if (cartDiv != null || element == null) {
                if (cartDiv != null) {
                        loc = clientCoord(cartDiv);
                }
                var xmlHttpReq = false;
                // Mozilla/Safari
                if (window.XMLHttpRequest) {
                        xmlHttpReq = new XMLHttpRequest();
                }
                // IE
                else if (window.ActiveXObject) {
                        xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
                }
                xmlHttpReq.open('GET', 'http://www.bombo.ru/?action=add_to_cart&mode=ajax&item=' + pid, true);
                xmlHttpReq.onreadystatechange = function() {
                        if (xmlHttpReq.readyState == 4) {
                                var elem = document.getElementById('sli');
                                elem.innerHTML = xmlHttpReq.responseText;
                                if (pid > 0) {
                                        var curGood = cartDiv.cloneNode(true);
                                        document.body.appendChild(curGood);
                                        curGood.style.zIndex = '5000';
                                        curGood.className = 'product-mosaic aniCart';
                                        curGood.style.backgroundColor = '#fff';
                                        curGood.style.width = cartDiv.offsetWidth + "px";
                                        curGood.style.height = cartDiv.offsetHeight + "px";
                                        curGood.style.left = loc.x + "px";
                                        curGood.style.top = loc.y + "px";
                                        curGood.style.visibility = 'visible';
                                        var cart = document.getElementById('cart-place');
                                        var locD = clientCoord(curGood);
                                        var locC = clientCoord(cart);
                                        var dx = (locC.x - locD.x);
                                        var dy = (locC.y - locD.y);
                                        new Effect.Parallel([
                                                new Effect.MoveBy(curGood, dy, dx, {
                                                        sync: true
                                                }),
                                                new Effect.Scale(curGood, 20, {
                                                        sync: true
                                                }), new Effect.ScrollTo('cart-small', {
                                                        sync: true,
                                                        offset: -20
                                                })],
                                                {
                                                        fps: 30,
                                                        duration: 2,
                                                        afterFinish: function() {
                                                                curGood.style.visibility = 'hidden';
                                                                curGood.style.top = '0px';
                                                                curGood.style.left = '0px';
                                                                //curGood.remove();
                                                                curGood = null;
                                                                new Effect.Pulsate('cart-place', {
                                                                        duration:2
                                                                });
                                                        }
                                                });
                                }
                        }
                }
                xmlHttpReq.send(null);
        }
        return false;
}

function initSearchFocus() {
        var search = document.getElementById('search-text');
        search.onblur = function() {
                this.style.color = '#999';
                if(!this.value) this.value = this.getAttribute('title');
        };
        search.onfocus = function() {
                this.style.color = '#333';
                if(this.value == this.getAttribute('title')) this.value = '';
        };
        search.onblur();
}

if (window.addEventListener) {
        window.addEventListener('load', initSearchFocus, true);
} else {
        window.attachEvent('onload', initSearchFocus, true);
}

function deleteItemFromCart(pid) {
        var xmlHttpReq = false;
        // Mozilla/Safari
        if (window.XMLHttpRequest) {
                xmlHttpReq = new XMLHttpRequest();
        }
        // IE
        else if (window.ActiveXObject) {
                xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
        }
        if ($('cart-loading') != null) {
                $('cart-loading').style.top = '-100px';
                $('cart-loading').style.visibility = 'visible';
        }
        xmlHttpReq.open('GET', 'http://www.bombo.ru/?action=update_cart&mode=ajax&to_del=' + pid, true);
        xmlHttpReq.onreadystatechange = function() {
                if (xmlHttpReq.readyState == 4) {
                        var elem = document.getElementById('cart');
                        elem.innerHTML = xmlHttpReq.responseText;
                        addToCart(null, -1);
                }
        }
        xmlHttpReq.send(null);
        return false;
}

function updateCart() {
        var xmlHttpReq = false;
        // Mozilla/Safari
        if (window.XMLHttpRequest) {
                xmlHttpReq = new XMLHttpRequest();
        }
        // IE
        else if (window.ActiveXObject) {
                xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
        }
        var fields = $('update-cart').getElementsByTagName('input');
        var req = '';
        for (i = 0; i < fields.length; i++) {
                if (fields[i].getAttribute('type') == 'text') {
                        req += fields[i].getAttribute('name') + '=' + encodeURIComponent(fields[i].value) + '&';
                }
        }
        fields = $('update-cart').getElementsByTagName('select');
        for (i = 0; i < fields.length; i++) {
                req += fields[i].getAttribute('name') + '=' + encodeURIComponent(fields[i].value) + '&';
        }
        if (req != '') {
                if ($('cart-loading') != null) {
                        $('cart-loading').style.top = '-100px';
                        $('cart-loading').style.visibility = 'visible';
                }
                xmlHttpReq.open('GET', 'http://www.bombo.ru/?action=update_cart&mode=ajax&' + req + '&' + (new Date()).getTime(), true);
                xmlHttpReq.onreadystatechange = function() {
                        if (xmlHttpReq.readyState == 4) {
                                var elem = document.getElementById('cart');
                                elem.innerHTML = xmlHttpReq.responseText;
                                addToCart(null, -1);
                        }
                }
                xmlHttpReq.send(null);
        }
        return false;
}

function compliteCart(mode) {
        var xmlHttpReq = false;
        // Mozilla/Safari
        if (window.XMLHttpRequest) {
                xmlHttpReq = new XMLHttpRequest();
        }
        // IE
        else if (window.ActiveXObject) {
                xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
        }
        var req = '';
        if (mode == 'process' || mode == undefined) {
                if ($('complite-cart') != null) { // вводим данные
                        var fields = $('complite-cart').getElementsByTagName('input');
                        for (i = 0; i < fields.length; i++) {
                                if ((fields[i].getAttribute('type') == 'text') || (fields[i].getAttribute('type') == 'password') || (fields[i].getAttribute('name') == 'page')) {
                                        req += fields[i].getAttribute('name') + '=' + encodeURIComponent(fields[i].value) + '&';
                                } else if (fields[i].getAttribute('type') == 'radio' && fields[i].checked == true) {
                                        req += fields[i].getAttribute('name') + '=' + encodeURIComponent(fields[i].value) + '&';
                                }
                        }
                        fields = $('complite-cart').getElementsByTagName('select');
                        for (i = 0; i < fields.length; i++) {
                                req += fields[i].getAttribute('name') + '=' + encodeURIComponent(fields[i].value) + '&';
                        }
                        fields = $('complite-cart').getElementsByTagName('textarea');
                        for (i = 0; i < fields.length; i++) {
                                req += fields[i].getAttribute('name') + '=' + encodeURIComponent(fields[i].value) + '&';
                        }
                } else { // с подтверждения вернулись назад
                        req = 'page=1';
                }
        } else if (mode == 'finish') { // подтверждаем окончитально
                req = 'complite=1&page=2';
        } else { // затычка от хрени
                req = '';
        }
        //alert('http://www.bombo.ru/?action=complite_cart&mode=ajax&' + req);
        if (req != '') {
                if ($('cart-loading') != null) {
                        $('cart-loading').style.top = '0';
                        $('cart-loading').style.visibility = 'visible';
                }
                xmlHttpReq.open('GET', 'http://www.bombo.ru/?action=complite_cart&mode=ajax&' + req + '&' + (new Date()).getTime(), true);
                xmlHttpReq.onreadystatechange = function() {
                        if (xmlHttpReq.readyState == 4) {
                                var elem = document.getElementById('cart');
                                elem.innerHTML = xmlHttpReq.responseText;
                                var scriptRegExp = /<script[^>]*>([\s\S]+?)<\/script>/mgi;
                                var script;
                                do {
                                        script = scriptRegExp.exec( xmlHttpReq.responseText );
                                        if ( script != null ) {
                                                try {
                                                        eval( script[ 1 ] );
                                                } catch(error) {
                                                        alert(error);
                                                }
                                        }
                                } while(script != null);
                                addToCart(null, -1);
                                if ($('complite-cart') == null) {
                                        window.scroll(0, clientCoord($('cart')).y - 10);
                                } else {
                                        window.scroll(0, clientCoord($('complite-cart')).y - 30);
                                }
                        }
                }
                xmlHttpReq.send(null);
        }
        return false;
}

var allowed = new Array();
var shiptypeCost = new Array();
var shiptypeZones = new Array();
var init = true;

function redrawShipTypes() {
        var zoneId = $( 'zone-selector' ).value;
        $$( '.shiptype-info' ).each( function( s, idx ) {
                s.style.display = 'none';
        });
        var selected = false;
        var zoneIdx = shiptypeZones[ zoneId ];
        if (allowed[ zoneIdx ] != null) {
                if (init) {
                        init = false;
                        selected = true;
                }
                allowed[ zoneIdx ].each( function (s, idx ) {
                        if (!selected) {
                                $( 'shiptype-radio-' + s ).checked = true;
                                selected = true;
                        }
                        $( 'shiptype-' + s ).style.display = '';
                        if (s == 16)
                                $( 'shiptype-cost-' + s ).innerHTML = shiptypeCost[ zoneId ] + ' руб.';
                });
        }
        return false;
}

function getBodyScrollTop(){
        return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
}

function getBodyScrollLeft() {
        return self.pageXOffset || (document.documentElement && document.documentElement.scrollLeft) || (document.body && document.body.scrollLeft);
}

function getClientWidth() {
        return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
}

function getClientHeight() {
        return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
}

window.onscroll = window.onresize = function () {
        var block = document.getElementById("online-help");
        //if (block.style.display == "block") {
        block.style.top = Math.floor(getBodyScrollTop() + getClientHeight() - 90) + "px";
        block.style.left = Math.floor(getClientWidth() + getBodyScrollLeft() - 160) + "px";
//block.style.margin = "0px 0px 0px -"+ Math.floor(150 / 2)+"px";
//}
}
