
$j(document).ready( 
	function(){
		$j('#caliLargeImagesBillboard').innerfade({ speed: 'slow', timeout: 5000, type: 'sequence', containerheight: '300px' });
		$j('#latestProductMiniPlayer').innerfade({ speed: 'slow', timeout: 2000, type: 'random', containerheight: '100px' });
		$j("#brands>a").click(OpenAllBrandsAZ);
  	    $j("#C-STORE-TABS").tabs();
  	    
  	    /* login */
  	    $j("#login").click(function(){
  	    	$j("#userLogin").toggle();
	  	    $j("#userRegister").hide();
	  	    $j("#uname").focus();
	  	    return false;
   			});
   			
   		$j("#loginform").submit(UserLogin);
   		/* VIP info */
		$j("#btnPartner").click(function(e){
			$j("#partnerContainer").toggle();
		});
		$j("#partnerContainer").click(function(e){
			$j("#partnerContainer").toggle();
		});
   		/* register */
  	    $j("#sign-up").click(function(){
  	    	$j("#userRegister").toggle();
  	    	$j("#userLogin").hide();
  	    	$j("#newsletterEmail").focus();
  	    	return false;
		   	});
		   	
		/* subscription */
  	    $j("#newsletterform").submit(SubscribeToNewsletter);
  	    
  	    /* frontpage */
  	    $j("#hTopCali").mouseover(function(){$j("#hBot").attr("src", "/images/2010/homestar-cali.jpg");});
  	    $j("#hTopCali").mouseout(function(){$j("#hBot").attr("src", "/images/2010/homestar-bottom.jpg");});
  	    $j("#hTopSFD").mouseover(function(){$j("#hBot").attr("src", "/images/2010/homestar-sfd.jpg");});
  	    $j("#hTopSFD").mouseout(function(){$j("#hBot").attr("src", "/images/2010/homestar-bottom.jpg");});
  	    $j("#hTopCstore").mouseover(function(){$j("#hBot").attr("src", "/images/2010/homestar-c-store.jpg");});
  	    $j("#hTopCstore").mouseout(function(){$j("#hBot").attr("src", "/images/2010/homestar-bottom.jpg");});
  	    
  	    /* search brands toggle */
  	    $j("#search-brand-filter a.selector").click(function() {$j("#search-brand-filter-box").toggle();return false;});
  	    
  	    /* add to cart */
  	    $j("#AddToCartForm").submit(Add2Cart);
  	    $j("#AddToCartButton").click(Add2Cart);
  	    
  	    /* search */ 

		$j("#TextSearchBox").autocomplete('/system/search/autocomplete.asp',
		{
			highlight: false,
			delay:100,
			minChars:2,
			matchSubset:1,
			matchContains:1,
			cacheLength:10,
			autoFill:true,
			onItemSelect:function(li)
			{
				var manuid = ( !!li.extra ) ? li.extra[0] : 0;
				if(manuid)
				{
					top.location.href = "/system/search/search.asp?manuid="+ manuid;
				}
				else
				{
					top.location.href = "/system/search/text_search_gss.asp?q="+ query;
				}
			}			
		});
		

		
	} );

(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)

function Add2Cart(e)
{
	var msg = $j("#AddToCartForm").attr("panagora:errmsg");
	
	e.preventDefault();
	
	
	if ($j("#AddToCartForm > .p_size_select")[0].selectedIndex == 0 && $j("#AddToCartForm > .p_size_select").get(0).length > 1)
	{
		alert(msg);
	}
	else
	{
		$j("#AddToCartButton").addClass("add-to-cart-wait");
		
		$j.ajax({
			url: "/system/pb/pb.asp",
			dataType: 'html',
			cache:false,
			contentType: "text/html; charset=iso-8859-1",
 		    type: 'GET',
			data: $j("#AddToCartForm").serialize(),
			success: function(data) 
			{ 
				if(data != "NO" && data.indexOf("<!DOCTYPE") != -1)
				{
					location.reload();
				}
				else if(data == "NO")
				{
					$j("#AddToCartButton").removeClass("add-to-cart-wait");
					alert("Sorry, there are no more sizes available.");
				}
				else
				{
					$j("#cartbox").html(data); 
					$j("#AddToCartButton").effect("transfer", { 
						to: $j("#cartbox"),
						complete: function()
						{
							$j("#AddToCartButton").removeClass("add-to-cart-wait");
						}
					}, 700);
				}
			}
		});
	}
  	return false;
}
function SubscribeToNewsletter(e)
{
	e.preventDefault();
	/* check email before subscribe */
	if(this.email.value.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) != -1)
	{
		var data = $j("#newsletterform").serialize();
		$j("#newsletterform:parent").html("<img style='float:left;margin-right:5px;'  src='/images/2010/user-box-loading.gif'><p style='float:left;margin:2px 0 0'>Please wait...</p><div class='clearboth'></div>"); 
		
		$j.ajax({
			url: "/newsletter/subscribe.asp",
			dataType: 'html',
			contentType: "text/html; charset=iso-8859-1",
			cache: false,
			type: 'GET',
			data: data,
			success: function(data) 
			{ 
				if(data.indexOf("<!DOCTYPE") != -1)
				{
					location.reload();
				}
				else
				{
					$j("#newsletterform:parent").html(data); 
					
					$j("#userRegister").delay(2000).slideUp(300);//fadeTo("slow",0.33);
				}
			}
		});
	}
	else
	{
		$j(this.email).addClass("error-input");
		$j(this.email).focus().select();
	}
	
  	return false;
}

function UserLogin(e)
{
	e.preventDefault();
	if(this.uname.value.length>0 && this.pass.value.length>0)
	{
		this.submit();
	}
	else
	{
		$j(this.uname).addClass("error-input");
		$j(this.pass).addClass("error-input");
	}
	return false;
}

function OpenAllBrandsAZ(e)
{
	e.preventDefault();
	
	$j("body").append('<div class="overlay"></div>');
	$j("body > div.overlay").height($j(document).height());
	$j("body > div.overlay").click(OpenAllBrandsAZ);
	
	if ($j("#TopBrandBoxAZ").is(':visible') || $j("#TopBrandBoxAZ").html().length > 5) 
	{
		$j("#TopBrandBoxAZ").toggle();
		
		if($j("#TopBrandBoxAZ").is(':hidden'))
			$j("body > div.overlay").remove();
	} 
	else 
	{
		$j.ajax({
			url: "/system/search/brands.inc.asp",
			dataType: 'html',
			contentType: "text/html; charset=iso-8859-1",
			success: function(data) {
				if(data.indexOf("<!DOCTYPE") != -1)
				{
					location.reload();
				}
				else
				{				
		    		$j("#TopBrandBoxAZ").html(data);
		    		$j("#TopBrandBoxAZ>a.close").click(OpenAllBrandsAZ);
		    		/* get max height of the boxes */
		    		var maxHeight = 0;
					$j("#TopBrandBoxAZ div").each(function () { var myHeight = $j(this).height(); if (myHeight > maxHeight) maxHeight = myHeight; });
					$j("#TopBrandBoxAZ div").height(maxHeight);
		    		
		    		$j("#TopBrandBoxAZ").show();
		    	}
	  		}
	  	});
	}
	
  	return false;
}
function OpenCheckoutTerms(e)
{
	open_popup("/system/user/terms.asp?xml=terms", "info", 300, 250);
	return false;
}
function OpenPaymentInfo(paymentType)
{
	open_popup("/system/user/terms.asp?xml="+paymentType, "info", 300, 250);
	return false;
}
function CheckoutSelectPayment(payment_method, e)
{
	//e.preventDefault();
	e.cancelBubble = true;
		
	//selected item - orange border
	$j(".payment_type").each(function()
	{
		$j(this).removeClass("payment_type_selected");
	} );
	//check radio
	$j(".payment_type_radio").each(function()
	{
		$j(this).removeAttr("checked");
		//this.checked=false;
	} );
	// new radio checked
	$j("#selected_payment_type").val(payment_method);

	//$j("#pt_radio_"+payment_method).get(0).setAttribute("checked", "");
	$j("#pt_radio_"+payment_method).attr("checked","checked");
	
	// selected new item
	$j("#pt_"+payment_method).addClass("payment_type_selected");
	if(payment_method == "delaupp" || payment_method == "faktura_campaign")
	{
		$j("#delaupp-income").show();
		$j('#dub-terms').show();
	}
	else
	{
		$j("#delaupp-income").hide();
		$j('#dub-terms').hide();
	}
	
	// get new totals
	var country = $j("#uCountry").val();
	var shipment_id = $j(".payment_type_selected .payment_type_radio").val();
	var freight_id = $j(".freight_type_selected .freight_type_radio").val();
	
	//update totals
	$j.ajax({
		cache:false,
		url:"/system/quotation/checkout_get_totals.asp",
		data:{a:1, shipment:shipment_id, freight:freight_id, country:country},
		success:function(data)
		{
			if (data.search('<html>') == -1)
				$j(".checkout_totals").html(data);
		}
	});
	
	// update freight options
	$j.ajax({
		cache:false,
		url:"/system/quotation/checkout_get_freight.asp",
		data:{shipment:shipment_id, freight:freight_id, country:country},
		success:function(data)
		{
			if (data.search('<html>') == -1)
				$j("#freight-form-container").html(data);
		}
	});

	return false;
}

function CheckoutSelectFreight(freight_name, e)
{
	//e.preventDefault();
	e.cancelBubble = true;
		
	//selected item - orange border
	$j(".freight_type").each(function()
	{
		$j(this).removeClass("freight_type_selected");
	} );
	//check radio
	$j(".freight_type_radio").each(function()
	{
		$j(this).removeAttr("checked");
		//this.checked=false;
	} );
	// new radio checked
	$j("#selected_freight_type").val(freight_name);

	//$j("#pt_radio_"+payment_method).get(0).setAttribute("checked", "");
	$j("#ft_radio_"+freight_name).attr("checked","checked");
	
	// selected new item
	$j("#ft_"+freight_name).addClass("freight_type_selected");
	
	// get new totals
	var country = $j("#uCountry").val();
	var freight_id = $j(".freight_type_selected .freight_type_radio").val();
	var shipment_id = $j(".payment_type_selected .payment_type_radio").val();
	
	
	$j.ajax({
		cache:false,
		url:"/system/quotation/checkout_get_totals.asp",
		data:{shipment:shipment_id, freight:freight_id, country:country},
		success:function(data)
		{
			if (data.search('<html>') == -1)
				$j(".checkout_totals").html(data);
		}
	});

	// update payment options
	$j.ajax({
		cache:false,
		url:"/system/quotation/checkout_get_payments.asp",
		data:{shipment:shipment_id, freight:freight_id, country:country},
		success:function(data)
		{
			if (data.search('<html>') == -1)
				$j("#payment-form-container").html(data);
		}
	});
	// /system/quotation/checkout_get_totals.asp?shipment=&country
	
	return false;
}
var errorTimeout;

function getAddressFromKreditor(e)
{
	//e.preventDefault();
	e.cancelBubble = true;
	
	var pno = $j("#uPno").val();
	var country = $j("#uCountry").val();

	if(pno.length>0 && country.length==2)
	$j.ajax({
		url: "/system/quotation/kreditor_get_address.asp?pno="+pno+"&country="+country,
		success: function(json) { 
			if (json instanceof Array) {
				$j("#uFname").val(json[0].firstName);
				$j("#uLname").val(json[0].lastName);
				$j("#uAddress").val(json[0].streetAddress);
				$j("#uZip").val(json[0].zipCode);
				$j("#uCity").val(json[0].city);
				$j("#uEmail").focus();
			}
		},
		error: function() {
			if (errorTimeout)
				clearTimeout(errorTimeout);
			$j('#address-fetch-error').show();
			errorTimeout = setTimeout(function () { $j('#address-fetch-error').fadeOut(); }, 2000);
		},
		dataType: 'json'
	});
	
	return false;
}

function doTextSearch()
{
	var query = $j("#TextSearchBox").val();
	top.location.href = "/system/search/text_search_gss.asp?q="+ query;
	
	
}
function doTextSearchOld()
{
	var query = $j("#TextSearchBoxOld");
	top.location.href = "/system/search/text_search.asp?q="+ query.value;
}

function ImageObj (_a,_b,_c)
{
	Img=new Object();
	Img.imagepath = _a;
	Img.imagedesc = _b;
	Img.thumbnail = _c;
	return Img;
}
	function printImages()
	{
		this.TotalImages = imagearr.length;
		this.ImagePosition=0;
		this.swap=Fswap;
		this.next=FswapNext;
		this.prev=FswapPrev;

		if(this.TotalImages > 0)
		{

			if(this.TotalImages>1)
			{
				document.write("<a href='javascript:pima.next()'>");
			}

			document.write("<img border='0' id='imagecontainer' src='"+ imagearr[0].imagepath +"'><span class='imageBorder'></span>");

			if(this.TotalImages>1)
			{
				document.write("</a>");
			}
			
			//document.write("<div id='imagedesc' style='width:320px;'>"+unescape(imagearr[0].imagedesc)+"&nbsp;</div>")

		}

		return this;

		function Fswap()
		{
			//imagedescok = unescape(imagearr[this.ImagePosition].imagedesc).replace(/^\s*/,  '').replace(/\s*$/, '');
			document.getElementById("imagecontainer").src = imagearr[this.ImagePosition].imagepath;
			//document.getElementById("imagedesc").childNodes[0].nodeValue = imagedescok; 
		}

		function FswapNext()
		{
			if(this.ImagePosition >= this.TotalImages-1)
				this.ImagePosition = 0;
			else
				this.ImagePosition++;
			this.swap();

		}

		function FswapPrev()
		{
			if(this.ImagePosition < 0)
				this.ImagePosition = this.TotalImages-1;
			else
				this.ImagePosition--;
			this.swap();
		}
	}





function dotextsearch()
{
	top.location.href='/system/search/text_search.asp?q='+ document.getElementById("textsearchbox").value
}
function open_xml_edit()
{
	matrix_window = window.open("matrix.asp", "matrix", "status=yes,scrolling=yes,width=400,height=400");
}

function CheckoutSelectShipping(val)
{
	location.href='create.asp?shipment='+val;
}
function validate_form(id)
{
	var formObj = document.forms[id];
	if(formObj.address && !isValidString(formObj, "address", 2))
	{
		alert("Please check your address")
	}
	else if(formObj.zip && !isValidString(formObj, "zip", 3))
	{
		alert("Please check your zip code")
	}
	else if(formObj.city && !isValidString(formObj, "city", 2))
	{
		alert("Please check your city")
	}
	else
	{
		formObj.submit();
	}
}

function confirm_del(msg, url)
{
	if(confirm(msg))
		location.href = url;
}

function chk_form(form, msg)
{
	var msg = new String(msg)
	this.form = form;
	if(this.form.size)
	{
		if(this.form.size.value != 0)
			return true;
		else
		{
			alert(msg);
			return false
		}
	}
	else
	{
		return true;
	}
}


function CheckAll(formObj)
{
	for (var i=0;i<formObj.elements.length;i++)
	{
		var e = formObj.elements[i];
		if ((e.name != 'allbox') && (e.type=='checkbox'))
			e.checked = !e.checked
	}
}

function checkKey(e, func)
{
	if(e.keyCode == 13)
		eval(func);
}

function open_login(str, url)
{
	if(confirm(str))
		location.href = url;
}

function open_print(url, page, query)
{
	if(query)
		query = "&"+query;

	var y = 100;
	var x = (screen.availWidth - 680) / 2;
	new_win = window.open(url+"?printpage="+escape(page)+query,'rootwin','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=yes, top='+y+',screenY='+y+',left='+x+',screenX='+x)
	new_win.resizeTo(680, screen.availHeight-200);
}

function open_popup(url, handler, w, h)
{
	var wa = screen.Width;
	var ha = screen.Height;
	ha = (ha/2)-(h/2)
	wa = (wa/2)-(w/2)
	if(parent.wHander)
	{
		var handler = parent.wHandler;		
	}
	else
	{
		var handler
	}
	handler = window.open(url, handler ,'width='+w+',height='+h+',location=no,menubar=no,directories=no,toolbar=no,scrollbars=yes,resizable=yes,status=yes,top='+ha+',left='+wa);
	handler.focus();

}

function open_popup_noscroll(url, handler, w, h)
{
	var wa = screen.Width;
	var ha = screen.Height;
	ha = (ha/2)-(h/2)
	wa = (wa/2)-(w/2)
	if(parent.wHander)
	{
		var handler = parent.wHandler;		
	}
	else
	{
		var handler
	}
	parent.wHandler = window.open(url, handler ,'width='+w+',height='+h+',location=no,menubar=no,directories=no,toolbar=no,scrollbars=no,resizable=no,status=no,top='+ha+',left='+wa);
	parent.wHandler.focus();
}

function submit_form(id)
{
		document.forms[id].submit();
}

function open_user_message(msg, data, dir)
{
	dir = new String(dir)
	if(dir != "undefined")
			dir = "&dir="+ dir;
	else
			dir = "";

	if(data == 'OF')
		location.href='/pa/system/sv/customer/tools/offert/view_offert.asp';
	else if (data == 'O')
	{	
		location.href='/pa/system/sv/user/offert/view_offert.asp';
	}
	else
		open_popup("/pa/system/sv/user/messenger/msg_view.asp?msgid="+msg+dir, "msg_item", "410", "300")
}

function small_win(link)
{
		open_popup(link, "small_win", "300", "200");
}

function custom_win(link, width, height)
{
	open_popup(link, "custom_win", width, height);
}

function show(object) 
{

	if (document.getElementById) 
		document.getElementById(object).style.visibility = 'visible';
	else if (document.layers && document.layers[object] != null) 
		document.layers[object].visibility = 'visible';
	else if (document.all) 
			document.all[object].style.visibility = 'visible';
	return false;
}


function toggle(object) 
{

	if (document.getElementById) 
	{
		if (document.getElementById(object).style.visibility == 'visible')
			document.getElementById(object).style.visibility = 'hidden';
	    else
			document.getElementById(object).style.visibility = 'visible';
	}

	else if (document.layers && document.layers[object] != null) 
	{
		if (document.layers[object].visibility == 'visible' || document.layers[object].visibility == 'show' )
			document.layers[object].visibility = 'hidden';
		else
			document.layers[object].visibility = 'visible';
	}

	else if (document.all) 
	{
		if (document.all[object].style.visibility == 'visible')
			document.all[object].style.visibility = 'hidden';
		else
			document.all[object].style.visibility = 'visible';
	}
	return false;
}


function hide(object) 
{

	if (document.getElementById) 
		document.getElementById(object).style.visibility = 'hidden';
	else if (document.layers && document.layers[object] != null) 
		document.layers[object].visibility = 'hidden';
	else if (document.all) 
			document.all[object].style.visibility = 'hidden';
	return false;
}


// form handling
function setFocus(formObject, itemObject, isSelect)
{
	eval("formObject." + itemObject +".focus()");
	if(isSelect)
		eval("formObject."+ itemObject +".select()");
}
function isValidEmail(formObject, itemObject)
{
	var string;
	string = eval("formObject." + itemObject +".value");
	if (string.search(/^\w+(((-|\+)\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) != -1)
		return true;
	else
		setFocus(formObject, itemObject, 1);
		return false;
}
function isValidSelect(formObject, itemObject)
{
	var val;
	val = eval("formObject." + itemObject + ".value");
	if(val == 0)
		{
			setFocus(formObject, itemObject, 0)
			return false;
		}
	return true;
}
function isValidString(formObject, itemObject, stringLength)
{
	var my_string;
	my_string = eval("formObject." + itemObject + ".value");
	if((my_string.length) > stringLength)
		return true;
	setFocus(formObject, itemObject, 1);
	return false;
}

function removeText(field) 
{
	  field.value = '';
}

function CompareFields(formObject, itemObjectA, itemObjectB)
{
		var A = eval("formObject." + itemObjectA + ".value");
		var B = eval("formObject." + itemObjectB + ".value");

		if(A == B)
			return true
		return false
}

function isChecked(formObject, itemObject)
{
	if( eval("formObject."+ itemObject +".checked") )
		return true
	return false
}