var x =0;
var y =0;
bool_flag = false;

/*window.onload = init;
function init() {
  if (window.Event) {
    document.captureEvents(Event.MOUSEMOVE);
  }
  document.onmousemove = getXY;
}*/

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() { 
	if ( window.innerWidth ) { return window.innerWidth; }
	return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth; 
}

function getClientHeight() {
	if ( window.innerHeight ) { return window.innerHeight; }
	return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
}


function getClientCenterX() { return parseInt(getClientWidth()/2)+getBodyScrollLeft(); }
function getClientCenterY() { return parseInt(getClientHeight()/2)+getBodyScrollTop(); }

function getXY() {
	x = getClientCenterX();
	y = getClientCenterY();
}

function doLoad(force,p_id) {

        var val = document.getElementById('product_number_'+p_id).value.match(/\b(^([1-9]+)$)\b/gi);
        if (!val){
            alert('Введите количество продукта');
            return false;
        }
        else
            var product_number = '' + document.getElementById('product_number_'+p_id).value;

        var znak = document.getElementById('znak').value;

        switch_loading_box('loading');
        var product_id = '' + document.getElementById('product_id_'+p_id).value;
        //var product_number = '' + document.getElementById('product_number_'+p_id).value;
        var req = new Subsys_JsHttpRequest_Js();

        req.onreadystatechange = function() {
            if (req.readyState == 4) {
                if (req.responseJS) {
            obj_number = document.getElementById('basket_items');
            obj_total = document.getElementById('basket_total');
            obj_currency = document.getElementById('basket_currency');

            if (typeof(obj_number)) {obj_number.innerHTML = '<span style=color:#FFFFFF;>' + (req.responseJS.basket_number||'') + '</span>';}
            if (typeof(obj_total)) obj_total.innerHTML = '<span style=color:#FFFFFF;>' + (req.responseJS.basket_total||'') + '</span>';
            //if (typeof(obj_currency)) obj_currency.innerHTML = '<span style=color:#FFFFFF;>' + (req.responseJS.currency||'') + '</span>';
            if (typeof(obj_currency)) obj_currency.innerHTML = '<span style=color:#FFFFFF;>' + znak + '</span>';
               }

        setTimeout("switch_loading_box('loading')",400);
            }
   }

        req.caching = false;
			module_url = window.location.toString();
			module_url = module_url.replace("http://","");
			module_url = module_url.split("/");
			module_url = "http://"+module_url[0]+"/"+shop_url;

        req.open('GET', module_url+'/basket_add/', true);
        req.send({product_id:product_id, product_number:product_number});
        return false;
}

function removeDiv(id)
    {
        var body = document.getElementsByTagName('body').item(0);
        var div = document.getElementById(id);
        body.removeChild(div);
        return false;
    }


function switch_buy_box(box_id)
    {
        if (document.getElementById(box_id).style.display!="none") {
            document.getElementById(box_id).style.display="none";
        } else {
            document.getElementById(box_id).style.display="block";
            var objwidth=document.getElementById(box_id).offsetWidth;
            var objheight=document.getElementById(box_id).offsetHeight;
            getXY();
            document.getElementById(box_id).style.left=x-objwidth/2+"px";
            document.getElementById(box_id).style.top=y-objheight/2+"px";
          }
        
          return false;
    }

$(document).ready(function() {
	anchor.init();
	buy.init();
});

anchor = {
	init : function()  {
		if ( $('span.header').hasClass("anchor") ) {
			destination = $("span.header").offset().top;
			$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, 1100 );
		}
	}
}
buy = {
	init : function() {
		$('a.buy').click(function(){
			var p_id = $(this).attr("title");
			var doc = document.documentElement, body = document.body;
			var pageTop = $(window).height() / 2 + (doc && doc.scrollTop || body && body.scrollTop || 0) - (doc.clientTop || 0);
			$('#loading').hide();
		    $('#loading').html('<p>Загрузка товара в корзину...</p>')
				.addClass('loading')
				.animate({ opacity: "show", top: pageTop - 38, left: $(window).width() / 2 + 116, fontSize: '12px'}, 500); 
			var req = new Subsys_JsHttpRequest_Js();
			req.onreadystatechange = function() {
				if (req.readyState == 4) {
					if (req.responseJS) {
						$('#loading').html('<p>Товар добавлен в корзину...</p><a href="/catalog/basket" title="">Оформить заказ</a><a href="#" title="" class="close">Продолжить покупки</a>')
							.removeClass('loading')
							.css({lineHeigh: '40'})
							.children("a.close").click(function(){
							    $('#loading').animate({ opacity: "hide", top: 0, left: $(window).width() * 0.95, fontSize: '4px'}, 500); 
								return false;
							});
						if($('#basket:not(.not_empty)')) { $('#basket').addClass('not_empty')};
						$('#basket_items').text("Всего товаров: " + (req.responseJS.basket_number || ''));
					}
				}
			}

			req.caching = false;
			module_url = window.location.toString();
			module_url = module_url.replace("http://","");
			module_url = module_url.split("/");
			module_url = "http://"+module_url[0]+"/catalog";
//			href = href.split("/");

			req.open('GET', module_url+'/basket_add/', true);
			req.send({product_id:p_id, product_number:1});

			return false;
		});
	}
}
