hs.graphicsDir = '/js/highslide/graphics/';
hs.outlineType = 'rounded-white';
hs.wrapperClassName = 'draggable-header';
hs.showCredits = false;

var sourceHref = location.href;
var selectedParamsCount = 0;
var reset = 0;

function searchProducts(val) {

    $('#simpleAJAXSearchResult').fadeOut();
    
    var req = new JsHttpRequest();
    req.onreadystatechange = function(){

        if (req.readyState == 4)
        {   
            if(req.responseJS.html) {                
                
                $('#simpleAJAXSearchResult').html(req.responseJS.html);
                $('#simpleAJAXSearchResult').fadeIn();
            }
        }
    }
    req.caching = false;
    req.open("POST", "/ajax.php?searchstring=", true);
    req.send({'type': 'searchProducts', 'searchstring': val});

}

function showCart() {
    
    disp = $('.cart-content', '.cart').css('display');
    if (disp == 'none') {
    
        $('.cart').animate({
            width: 0
        }, {
            duration: 'normal',
            queue: false,
            complete: function(){
                $('.cart-tab', '.cart').hide();
                $('.cart-content', '.cart').show();
                $('.cart').animate({
                    width: $('.cart-content', '.cart').width()
                }, {
                    duration: 'normal',
                    queue: false
                });
            }
        });
    }
}    

function discussPager_request(productID, offset){

    $('#discussions').fadeOut();
    
    var req = new JsHttpRequest();
    req.onreadystatechange = function(){

        if (req.readyState == 4)
        {   
            if(req.responseJS.html) {
                
                
                $('#discussions').html(req.responseJS.html);
                $('#discussions').fadeIn();
            }
        }
    }
    req.caching = false;
    req.open("POST", "/ajax.php", true);
    req.send({'type': 'loadDiscussion', 'productID': productID, 'offset': offset});
}
    
function add2cart_request(formData){
         
	elDiv = document.getElementById('divAdd2cartProcess');
	if(document.documentElement&&document.documentElement.scrollTop){
		t=document.documentElement.scrollTop;
	}else{
		if(document.body){
		t=document.body.scrollTop;
		}
	}
	elDiv.style.top=t + (screen.height / 2) - 100+'px';
	elDiv.style.left=(screen.width / 2) - 100+'px';
	elDiv.style.display='block';

	var req = new JsHttpRequest();
	req.onreadystatechange = function(){

	    if (req.readyState == 4)
	    {  
			if(req.responseJS.added) {
			    
                loadCart();
			    $('#divAdd2cartProcess').hide();
                showCart();
                
			    if((/MSIE (5\.5|6).+Win/.test(navigator.userAgent)))$('.select2hide').hide();
			}
        }
	}
	req.caching = false;
	req.open("POST", "/ajax.php", true);
	req.send({'type': 'add2cart', 'formData': formData});
	}

function showHideAdvSearchForm_request(state){
	var req = new JsHttpRequest();
	req.onreadystatechange = function(){
		if (req.readyState == 4){
		}
	}
    
	req.caching = false;
	req.open("POST", "/ajax.php", true);
	req.send({'type': 'showHideAdvSearchForm', 'state': state});
}

function loadAdvSearchProductsCount(updatePrice){
    
	elForm = document.getElementById('AdvancedSearchInCategory');
	var req = new JsHttpRequest();

	var height = $(document).height();
	var txt = '<div id="jquery-overlay" style="background-color:rgb(255,255,255); height:'+height+'px; line-height:0pt; position:absolute; text-align:center; top:0px; z-index:999; opacity:0.6; left:0px; width:100%; display:none;"></div>';
	$("body").append(txt);
	$("#jquery-overlay").animate({opacity:0.6},50).show(50);

	if(document.documentElement&&document.documentElement.scrollTop){
		t=document.documentElement.scrollTop;
	}else{
		if(document.body){
		t=document.body.scrollTop;
		}
	}
	elDiv = document.getElementById('divPriceApply');
	elDiv.style.top=t + (screen.height / 2) - 150+'px';
	elDiv.style.left=(screen.width / 2) - 100+'px';
	elDiv.style.display='block';

	req.onreadystatechange = function(){
				
 		if (req.readyState == 4){

			$('#AdvancedSearchInCategoryDIV').html(req.responseJS.form);
			$('#productListResult').html(req.responseJS.products);

			$('#divPriceApply').css('display','none');

	  	$("#jquery-overlay").remove();

			if(!reset){
				var paramsStr = new Array();
				paramsStr[paramsStr.length] = 'brandID='+escape($('#brandID').attr('value'));
				paramsStr[paramsStr.length] = 'lineID='+($('#brandID').attr('value')!=0?escape($('#lineID').attr('value')):'0');
				paramsStr[paramsStr.length] = 'priceID='+escape($('#priceID').attr('value'));

				for(var i=0;i<=selectedParamsCount;i++){

					if($('#param_'+i).attr('name') != null){

						name_ = new String($('#param_'+i).attr('id'));
						paramsStr[paramsStr.length] = name_+'='+escape($('#param_'+i).attr('value'));
					}
				}

				sourceHref_ = sourceHref.indexOf('#filter')==-1 ? sourceHref : sourceHref.substr(0,sourceHref.indexOf('#filter'));
				location.href = sourceHref_+'#filter'+paramsStr.join('+q');
                //location.replace(sourceHref_+'#filter'+paramsStr.join('+q'));

			} else {
				sourceHref_ = sourceHref.indexOf('#filter')==-1 ? sourceHref : sourceHref.substr(0,sourceHref.indexOf('#filter'));
				location.href = sourceHref_+'#filter';
                //location.replace(sourceHref_+'#filter');
				reset = 0;
			}

		}
	};
		  
	req.caching = true;
	req.open("POST", "/ajax.php", true);
	req.send({'type': 'advSearchProductsCount', 'formData': elForm, 'updatePrice': updatePrice});
}

function loadCart(){

	var req = new JsHttpRequest();

	req.onreadystatechange = function(){
				
 		if (req.readyState == 4){

			$('#html-cart').html(req.responseJS.html);
            //$('.cart').cartToggle({tabSpeed: 150, contentSpeed: 330});

			//checkCartAvailableWidth();


            $('.cart-close', '.cart').click(function(){
                
                var $cart = $('.cart'),
                    $cartTab = $('.cart-tab', $cart),
                    cartTabWidth = $cartTab.width(),
                    $cartClose = $('.cart-close', $cart),
                    $cartContent = $('.cart-content', $cart),
                    cartContentWidth = $cartContent.width();

                    $cart.animate({
                        width: 0
                    }, {
                        duration: 'normal',
                        queue: false,
                        complete: function(){
                            $cartTab.show();
                            $cartContent.hide();
                            $cart.animate({
                                width: cartTabWidth
                            }, {
                                duration: 'normal',
                                queue: false
                            });
                        }
                    });


            });
            
            
		}
	};
		  
	req.open("POST", "/ajax.php", true);
	req.send({'type': 'loadCart'});
}

function removeFromCart(remove){

	var req = new JsHttpRequest();

	req.onreadystatechange = function(){
	
 		if (req.readyState == 4){
			loadCart();
		}
	};
		  
	req.open("POST", "/ajax.php", true);
	req.send({'type': 'removeFromCart', 'remove':remove});
}

function updateCart(){

	elForm = document.getElementById('cartForm');
	var req = new JsHttpRequest();

	req.onreadystatechange = function(){
				
 		if (req.readyState == 4){
			loadCart();
		}
	};
		  
	req.open("POST", "/ajax.php", true);
	req.send({'type': 'updateCart', 'formData': elForm});
}

var updateAdvSearchPriceTimer = 0;
function updateAdvSearchPrice(){
	clearInterval(updateAdvSearchPriceTimer);
	updateAdvSearchPriceTimer = setInterval('clearInterval(updateAdvSearchPriceTimer);loadAdvSearchProductsCount(0);',1000);
}



/* Shop-Script functions */
function open_window(link,w,h) //opens new window
{
	var win = "width="+w+",height="+h+",menubar=no,location=no,resizable=yes,scrollbars=yes";
	newWin = window.open(link,'newWin',win);
	newWin.focus();
}

function open_printable_version(link) //opens new window
{
	var win = "menubar=no,location=no,resizable=yes,scrollbars=yes";
	newWin = window.open(link,'perintableWin',win);
	newWin.focus();
}

function confirmDelete(id, ask, url) //confirm order delete
{
	temp = window.confirm(ask);
	if (temp) //delete
	{
		window.location=url+id;
	}
}



function confirmUnsubscribe() //unsubscription confirmation
{
	temp = window.confirm('{/literal}{$smarty.const.QUESTION_UNSUBSCRIBE}{literal}');
	if (temp) //delete
	{
		window.location="index.php?killuser=yes";
	}
}

function validate() // newsletter subscription form validation
{
	if (document.subscription_form.email.value.length<1)
	{
		alert("{/literal}{$smarty.const.ERROR_INPUT_EMAIL}{literal}");
		return false;
	}
	if (document.subscription_form.email.value == 'Email')
	{
		alert("{/literal}{$smarty.const.ERROR_INPUT_EMAIL}{literal}");
		return false;
	}
	return true;
}
function validate_disc() // review form verification
{
	if (document.formD.nick.value.length<1)
	{
		alert("{/literal}{$smarty.const.ERROR_INPUT_NICKNAME}{literal}");
		return false;
	}

	if (document.formD.topic.value.length<1)
	{
		alert("{/literal}{$smarty.const.ERROR_INPUT_MESSAGE_SUBJECT}{literal}");
		return false;
	}

	return true;
}
function validate_search()
{

	if (document.Sform.price1.value!="" && ((document.Sform.price1.value < 0) || isNaN(document.Sform.price1.value)))
	{
		alert("{/literal}{$smarty.const.ERROR_INPUT_PRICE}{literal}");
		return false;
	}
	if (document.Sform.price2.value!="" && ((document.Sform.price2.value < 0) || isNaN(document.Sform.price2.value)))
	{
		alert("{/literal}{$smarty.const.ERROR_INPUT_PRICE}{literal}");
		return false;
	}

	return true;
}
