var productId = 0;
var m1 = '5929247';
var m2 = '4516718';
function addBasketAjax(aObject, ajaxURL) {
    new Ajax.Request(ajaxURL,
    {
        method:'post',
        onSuccess: function(transport){
            $('ajaxbasketid').show();
            $('ajaxbasketid').innerHTML = transport.responseText;
            aObject.innerHTML = '<span style="color: blue;">в корзине&nbsp;</span>';
        },
        onFailure: function(){ alert('Ошибка ajax-корзины.') }
    });
    return false;
}

function refreshBasket(){
    new Ajax.Request('/ajaxbasket.html?add2basketid=0',
    {
        method:'post',
        onSuccess: function(transport){
            $('ajaxbasketid').show();
            $('ajaxbasketid').innerHTML = transport.responseText;
            if (typeof isBasketPage != 'undefined'){
                $('basket_order').onclick = $('basket_order_main').onclick;
            }
            if (typeof isMakeOrderPage != 'undefined'){
                $('basket_order').hide();
            }
        },
        onFailure: function(){ alert('Ошибка ajax-корзины.') }
    });
}

function sendQuestionRequest(pars){
    var url = '/faqsblock.html';
    new Ajax.Request(url,
    {
        method: 'post',
        parameters: pars,
        onSuccess: function( transport ){
            var responseStr = transport.responseText;
            if (responseStr) {
                $('faqsblock').innerHTML = responseStr;
            }
        },
        onFailure: function(){
            alert( 'Ошибка...' )
        }
    });
}

function addQuestion(question, email){
    sendQuestionRequest({question: question, id: productId, email: email});
}

function editQuestion(faqid){
    var field = $('adminfield_'+faqid);
    var editBlock = $('editblock_'+faqid);
    editBlock.hide();
    field.show();
    field.select();
    field.update($('uquestion_'+faqid).innerHTML);
    field.onchange = function(){
        sendQuestionRequest({faqid: faqid, editquestion: field.value, category: catId});
        editBlock.show();
        field.hide();
    }
    field.onblur = function(){
        editBlock.show();
        field.hide();
    }
}

function editAnswer(faqid){
    var field = $('adminfield_'+faqid);
    var editBlock = $('editblock_'+faqid);
    field.show();
    editBlock.hide();
    field.select();
    field.update($('uanswer_'+faqid).innerHTML);
    field.onchange = function(){
        sendQuestionRequest({faqid: faqid, editanswer: field.value, category: catId});
        editBlock.show();
        field.hide();
    }
    field.onblur = function(){
        editBlock.show();
        field.hide();
    }
}

function deleteQuestion(faqid){
    sendQuestionRequest({faqid: faqid, deletequestion: true, category: catId});
}

function sendMessage2Friend(pars){
    var url = '/ajaxmessage.html';
    new Ajax.Request(url,
    {
        method: 'post',
        parameters: pars,
        onSuccess: function( transport ){
            var responseStr = transport.responseText;
            if (responseStr) {
            	if ($('send_admin_msg')) $('send_admin_msg').hide();
            	if ($('want_description_msg')) $('want_description_msg').hide();
            }
        },
        onFailure: function(){
            alert( 'Ошибка...' )
        }
    });
}
function centeredMessage(mesIdName){
    var messDiv = $(mesIdName);
    var ws = getWindowSize();
    var windowWidth = ws.windWidth;
    var windowHeight = ws.windHeight;
    var yScroll = ws.yScroll;
    var alertWidth = messDiv.getWidth();
    var alertHeight = messDiv.getHeight();
    var heightCenter = (windowHeight - alertHeight) / 2 + yScroll;
    var widthCenter = (windowWidth - alertWidth) / 2;
    messDiv.style.top = heightCenter+'px';
    messDiv.style.left = widthCenter+'px';
}


function getWindowSize(){
    var y = 0;
    if (self.pageYOffset) {
        yScroll = self.pageYOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {
        yScroll = document.documentElement.scrollTop;
    } else if (document.body) {
        yScroll = document.body.scrollTop;
    }
    y += yScroll;
    var windowWidth, windowHeight;
    if (self.innerHeight) {
        windowWidth = self.innerWidth;
        windowHeight = self.innerHeight;
        if (yScroll > windowHeight)	windowWidth -= 16;
    } else if (document.documentElement && document.documentElement.clientHeight) {
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) {
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }
    var a = new Array();
    a['windWidth'] = windowWidth;
    a['windHeight'] = windowHeight;
    a['yScroll'] = yScroll;
    a['allHeight'] = windowHeight + yScroll;
    return a;
}

var bigImg = null;

function zoomIn(img, bigImage){
	var offset = img.cumulativeOffset();
	var width = img.getWidth();
	var height = img.getHeight();
	var wSize = getWindowSize();
	var maxHeight = wSize.windHeight / 2;
	bigImg = new Element('img');
	bigImg.src = bigImage;
	bigImg.hide();
	bigImg.className = 'big_img';
	bigImg.style.border = '1px solid gray';
	bigImg.style.top = offset.top + height + 'px'; 
	bigImg.style.left = offset.left + width + 8 + 'px'; 
	bigImg.style.zIndex = '100000000'; 
	document.body.appendChild(bigImg);
	bigImg.onload = function(){
		var bigImgHeight = bigImg.getHeight();
		var bigImgWidth = bigImg.getWidth();
		var prop = bigImgWidth / bigImgHeight; 
		if (bigImgHeight > maxHeight){
			bigImg.style.height = maxHeight + 'px';
			bigImg.style.width = (maxHeight * prop) + 'px';
		}
		var y = wSize.windHeight + wSize.yScroll - bigImg.getHeight();
		if (offset.top > y){
			bigImg.style.top = y + 'px'; 
		}
		bigImg.show();
	}
	bigImgHeight = bigImg.getHeight();
}

function zoomOut(img){
	if (bigImg) {
		bigImg.remove();
	}
}

function showSend2AdmindMsg(){
	$('send_admin_msg').show();
	centeredMessage('send_admin_msg');
	new Draggable('send_admin_msg', {handle: 'amsg_header'});
}

function hideSend2AdminMsg(){
	$('send_admin_msg').hide();
}

function sendAdminMsg(){
	var user_email = $F('xuser_email');
	var user_text = $F('xuser_msg');

	if (!user_email){
		alert('Не заполнен Ваш e-mail');
	}
	if (!user_text){
		alert('Не заполнено поле текста сообщения');
	}
	
	var pId = null;
	if (typeof productId != 'undefined'){
		pId = productId;
	}
	
	sendMessage2Friend({
		'user_email': user_email,
		'user_text': user_text,
		'to_admin': 'yes',
		'product_id': pId
		})
}


Event.observe(window, 'load', function(){
    refreshBasket();
})

function ajaxCurrency(url) {
    if (!currentURL) return true;
    new Ajax.Request(url, {
        method:'get',
        onSuccess: function(transport){
            ajaxPage(currentURL);
        }
    });
    return false;
}

stuHover = function() {
	var cssRule;
	var newSelector;
	for (var i = 0; i < document.styleSheets.length; i++)
		for (var x = 0; x < document.styleSheets[i].rules.length ; x++)
			{
			cssRule = document.styleSheets[i].rules[x];
			if (cssRule.selectorText.indexOf("LI:hover") >= 0)
			{
				 newSelector = cssRule.selectorText.replace(/LI:hover/gi, "LI.iehover");
				document.styleSheets[i].addRule(newSelector , cssRule.style.cssText);
			}
		}
	var getElm = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<getElm.length; i++) {
		getElm[i].onmouseover=function() {
			this.className+=" iehover";
		}
		getElm[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" iehover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", stuHover);

var currentURL = '';
var loader = '<img src="/_images/loader.gif" style="margin-left: 30%;" />';
function ajaxMenu(menuType, menuID) {
	if (!($('submenu'+menuID))) return false;
	if (menuType == 'category') {
		if ($('submenu'+menuID).innerHTML != '') {
			$('submenu'+menuID).innerHTML = '';
			$('cat_'+menuID).className = 'a1plus';
			if ($('cat_'+menuID).up().hasClassName('left_menu_top_level')){
			    $('cat_'+menuID).up().removeClassName('left_menu_sel');
			}
			return false;
		}
		$('submenu'+menuID).innerHTML = loader;
		new Ajax.Request('/ajaxmenu.html',
		{
			method:'get',
			parameters: {
				categoryid: menuID
			},
			onSuccess: function(transport){
				if ($('submenu'+menuID)){
					$('submenu'+menuID).innerHTML = transport.responseText;
					$('cat_'+menuID).className = 'selected';
					if ($('cat_'+menuID).up().hasClassName('left_menu_top_level')){
					    $('cat_'+menuID).up().addClassName('left_menu_sel');
					}
				}
			},
			onFailure: function(){ alert('Ошибка ajax-меню.') }
		});
	} else {
		if ($('subvendor'+menuID).innerHTML != '') {
			$('subvendor'+menuID).innerHTML = '';
			$('ven_'+menuID).className = 'a1plus';
			return false;
		}
		$('subvendor'+menuID).innerHTML = loader;
		new Ajax.Request('/ajaxmenu.html',
		{
			method:'get',
			parameters: {
				vendorid: menuID
			},
			onSuccess: function(transport){
				$('subvendor'+menuID).innerHTML = transport.responseText;
				$('ven_'+menuID).className = 'selected';
			},
			onFailure: function(){ alert('Ошибка ajax-меню.') }
		});
	}
}
function ajaxMenu2(categoryID, vendorID, divID) {
	if ($(divID).innerHTML != '') {
		$(divID).innerHTML = '';
		$('cv_'+divID).className = 'a1plus';
		return false;
	}
	new Ajax.Request('/ajaxmenu.html',
	{
		method:'get',
		parameters: {
			categoryid2: categoryID,
			vendorid2: vendorID
		},
		onSuccess: function(transport){
			$(divID).innerHTML = transport.responseText;
			$('cv_'+divID).className = 'selected';
		},
		onFailure: function(){ alert('Ошибка ajax-меню.') }
	});
}

function ajaxPage(url) {
	var d = getWindowSize();
	var h = d.windHeight / 2 + d.yScroll - 10;
	var img = '<img src="/_images/ajax-loader.gif" style="position: absolute; left: 50%; top: '+h+'px;" />';
	$('ajaxcontentid').innerHTML = 'Загрузка...'+img;
	new Ajax.Request(url, {
		method:'post',
		onSuccess: function(transport){
			currentURL = url;
			$('ajaxcontentid').innerHTML = transport.responseText;
			Effect.ScrollTo('ajaxcontentid');
		},
		onFailure: function(){ alert('Ошибка ajax-content.') }
	});
	return false;
}
function toggleDisp() {
	for (var i=0;i<arguments.length;i++){
		var d = $(arguments[i]);
		if (d.style.display == 'none')
		d.style.display = 'block';
		else
		d.style.display = 'none';
	}
}
/*-----------------------------------------------------------
Toggles tabs - Closes any open tabs, and then opens current tab
Input:     1.The number of the current tab
2.The number of tabs
3.(optional)The number of the tab to leave open
4.(optional)Pass in true or false whether or not to animate the open/close of the tabs
Output: none
---------------------------------------------------------*/
function toggleTab(num,numelems,opennum,animate) {
	if ($('tabContent'+num).style.display == 'none'){
		for (var i=1;i<=numelems;i++){
			if ((opennum == null) || (opennum != i)){
				var temph = 'tabHeader'+i;
				var h = $(temph);
				if (!h){
					var h = $('tabHeaderActive');
					h.id = temph;
				}
				var tempc = 'tabContent'+i;
				var c = $(tempc);
				if(c.style.display != 'none'){
					if (animate || typeof animate == 'undefined')
					Effect.toggle(tempc,'blind',{duration:0.5, queue:{scope:'menus', limit: 2}});
					else
					toggleDisp(tempc);
				}
			}
		}
		var h = $('tabHeader'+num);
		if (h)
		h.id = 'tabHeaderActive';
		h.blur();
		var c = $('tabContent'+num);
		c.style.marginTop = '2px';
		if (animate || typeof animate == 'undefined'){
			Effect.toggle('tabContent'+num,'blind',{duration:0.5, queue:{scope:'menus', position:'end', limit: 2}});
		}else{
			toggleDisp('tabContent'+num);
		}
	}
}

function getWindowSize(){
	var y = 0;
	if (self.pageYOffset){yScroll = self.pageYOffset;}
	else if (document.documentElement && document.documentElement.scrollTop){yScroll = document.documentElement.scrollTop;}
	else if (document.body){yScroll = document.body.scrollTop;}
	y += yScroll;
	var windowWidth, windowHeight;
	if (self.innerHeight){
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
		if (yScroll > windowHeight)	windowWidth -= 16;
	}
	else if (document.documentElement && document.documentElement.clientHeight){
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	}
	else if (document.body){
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	var a = new Array();
	a['windWidth'] = windowWidth;
	a['windHeight'] = windowHeight;
	a['yScroll'] = yScroll;
	a['allHeight'] = windowHeight + yScroll;
	return a;
}

