

var arrElements = ['search', 'login'];


function unchangePass()
{
	//$('#password_unchange_wrapper').toggle();
	try {
	document.getElementById('password_unchange_wrapper').style.display = 'none';
	} catch (e) {}

	$('#ue-password_unchange').click( function()
		{
			//$('#password_unchange_wrapper').toggle('slow');
			$('#password_unchange_wrapper').show('slow');
		}
	);
}


function initPowieksz()
{
	
	if(!$("a.powieksz").is('a') || !$("div.more a").is('a'))
		return false;
		
	$("a.powieksz").click(function()
	{
		var t = this.title || this.name || null;
		var g = this.rel || false;
		TB_show(t,this.href,g);
		this.blur();
		return false;
	});

	$("a#powiekszJeden").click(function()
	{
		var t = this.title || this.name || null;
		var g = this.rel || false;
		TB_show(t,this.href,g);
		this.blur();
		return false;
	});
	$("a#powiekszDwa").click(function()
	{
		var t = this.title || this.name || null;
		var g = this.rel || false;
		TB_show(t,this.href,g);
		this.blur();
		return false;
	});
}


function initAddressForm()
{
	if(!$('form#fm-addressCommit').is('form'))
		return true;
	$('form#fm-addressCommit input.button').toggle();
		$('form#fm-addressCommit select').change( function()
				{
					$('form#fm-addressCommit').get(0).submit();
				}
			);
		
}

function initAddressMultiForm()
{
	$('form.fm-addressMultiCommit input.button').toggle();
	$('form.fm-addressMultiCommit select').change( function()
			{
/*				$('select').attr('disabled','disabled');
				$('select option').attr('disabled','disabled');*/
				$('.'+this.className).parents('form').get(0).submit();
			}
		);
		
}

function l_changeMultiDelivery()
{
	document.getElementById('formMulti').submit();
}

function initDeliveryForm()
{
	if(!$('form#fm-deliveryCommit').is('form'))
		return false;
		
	$('form#fm-deliveryCommit input.button').toggle();
	$('form#fm-deliveryCommit select').change( function()
			{
				$('form#fm-deliveryCommit').get(0).submit();
			}
		);
}


function addressBilling()
{
	if(!$('form#address').is('form'))
		return false;
		
	if(!$('form#address input#address-inv_address_copy') || !$('form#address input#address-inv_address_copy').is('input'))
		return false;
		
	// fetching parents' containers to initialize
	if( $('form#address input#address-inv_address_copy').get(0).checked )
	{
		$('div.toggle-billing').toggle();
	}
	
	$('form#address input#address-inv_address_copy').click( function()
			{
				if( $('form#address input#address-inv_address_copy').get(0).checked )
				{
					$('div.toggle-billing').slideUp();
					clearAddress();
				} else {
					$('div.toggle-billing').slideDown();
					copyAddress();
				}
			}
		);
}

function copyAddress()
{
	var arr = ['name', 'surname', 'street', 'city', 'post_code'];
	var i = 0;
	for(i=0; i<5; i++)
	{	
		if($('#address-'+arr[i]).is('input'))
		{
			var idf = 'address-inv_'+arr[i];
			if(!$('#'+idf).get(0).value)
				$('#'+idf).get(0).value = $('#address-'+arr[i]).get(0).value;
		}
	}
	if($('#address-id_country').is('select'))
	{
		$('#address-inv_id_country').get(0).selectedIndex = $('#address-id_country').get(0).selectedIndex;
	} elseif($('#address-country').is('input') && $('#address-inv_country').is('input'))
	{
		if($('#address-country').get(0).value)
		{
			$('#address-inv_country').get(0).value = $('#address-country').get(0).value;
		}
	}
}

function clearAddress()
{
	var arr = ['name', 'surname', 'street', 'city', 'post_code', 'country'];
	var i = 0;
	for(i=0; i<6; i++)
	{	
		if($('#address-inv_'+arr[i]).is('input'))
		{
			$('#address-inv_'+arr[i]).get(0).value = '';
		}
	}
	
}


function searchInit()
{
	$('div.fm-checkboxes>div.fm-checkboxes').each( 
		function(i) 
		{
			$(this).hide();
		}
	);
	
	$('div.fm-checkboxes').each( 
		function(i) 
		{
			$(this).find('label input').click(
				function()
				{
					var classHolderID = this.value;
					if(this.checked)
					{
						$('div.divHolder-'+classHolderID).show();
					} else {
						$('div.divHolder-'+classHolderID).hide();
					}
					var chck = this.checked;
					if(this.value == 0)
					{
						$('input[@name*='+this.name.replace('[]','')+']').each(
							function(i)
							{
//								this.click();
								this.checked = chck;
							}
						);
					} else {
						$('div.divHolder-'+classHolderID+'>label>input').each(
							function(i)
							{
								this.checked = chck;
							}
						);
						if(!chck)
						{
							if($(this).parents('div[@class*=divHolder-]').is('div'))
							{
								$(this).parents('div[@class*=divHolder-]').each(
									function(i)
									{
										var strClass = this.className.replace(/^.*divHolder-/i, '').replace(/[^0-9]+.*$/i,'');
										$('input[@value='+strClass+']').get(0).checked = chck;
									}
								);
							} 
							$('input[@name*='+this.name.replace('[]','')+']').each(
								function(i)
								{
									if(this.value == 0)
									{
										this.checked = chck;
									}
								}
							);
							
						}
					}
					
				}
			);
			
			$(this).find('input[@value=0]').each(
				function(i)
				{
					if(this.checked)
					{
						this.click();
						this.click();
					}
				}
			);
			
		}
	);
}


function initSizePopup()
{
	$('ul li#size-popup a').click(
         function() { 
             window.open(this.href, "_blank", "toolbar=no, menubar=no, status=yes, height=500, width=500, scrollbars=yes"); 
             return false; 
         } 
	);
	
	/* lioosys */ 
	$('p#size-popup1 a').click(
         function() { 
             window.open(this.href, "_blank", "toolbar=no, menubar=no, status=yes, height=500, width=500, scrollbars=yes"); 
             return false; 
         } 
	);
	/* /lioosys */
}

var netscape = (navigator.appName.indexOf("Netscape") != -1);
IE = true;

var hTimerZoom = null;
var arrZommImages = new Array();
var arrImagesHeight = new Array();
var zommSlideNr = 0;

function imgLargeShow(producer, title, price, arrImg, idImg, imgHeight) 
{
	zommSlideNr = 0;

	if (!arrImg.length) {
		return;
	}
	var image = document.getElementById(idImg);
	tempY = docjslib_getImageYfromTop(image);
		
		
	e=document.getElementById('obrazek-duzy');
		bottomOfDiv = e.offsetHeight + tempY;
		botomOfPage = pageHeight();
		topOfPage = posTop();
		if((bottomOfDiv - topOfPage) > botomOfPage){
			tempY = tempY - ((bottomOfDiv - topOfPage) - botomOfPage);
		}
		tempY = Math.max(310, parseInt(tempY));
		e.style.top = tempY + 'px'; 
		
 	document.getElementById('obrazek-duzy').innerHTML = '<img id="img-large" src="' +arrImg[0]+ '" style="border-color: #E8E9E9; border-style: solid; border-width: 1px; padding: 2px;" alt="" border="0" align="bottom" width="197"  /><br /><div align="left" style="padding-left: 10px;"><b style="color: 000;">' +producer+ '</b><br />' +title+ '<br />' +price+ '</div>'; 
	document.getElementById('obrazek-duzy').style.visibility = 'visible';
	
	arrZommImages = arrImg;
	arrImagesHeight = imgHeight;
	
	if (arrImg.length==1) {
		return;
	}
	
	hTimerZoom = setInterval( "imgLargeChange()", 2000 );
}

function imgLargeHide() 
{
	clearInterval(hTimerZoom); 
	hTimerZoom = 0;
	document.getElementById('obrazek-duzy').style.visibility = 'hidden';
}

function imgLargeChange()
{
	if(!hTimerZoom) {
		return;
	}

	zommSlideNr ++;
	if (arrZommImages[zommSlideNr] == '') {
		zommSlideNr ++;
	}
	if (zommSlideNr >= arrZommImages.length) {
		zommSlideNr = 0;
	}
	
	e=document.getElementById("img-large");
	e.src = arrZommImages[zommSlideNr];
	//if (arrImagesHeight[zommSlideNr] != 0) {
		//e.height = arrImagesHeight[zommSlideNr]
	//}
}

function docjslib_getImageYfromTop(imgID) {
  if (!(IE)) return eval(imgID).y
  else return docjslib_getRealTop(imgID);
}

function docjslib_getRealTop(imgElem) {
	yPos = eval(imgElem).offsetTop;
	tempEl = eval(imgElem).offsetParent;
	while (tempEl != null) {
  		yPos += tempEl.offsetTop;
  		tempEl = tempEl.offsetParent;
  	}
	return yPos;
}
function pageHeight() {return window.innerHeight != null? window.innerHeight: document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight:document.body != null? document.body.clientHeight:null;}
function posTop() {return typeof window.pageYOffset != 'undefined' ? window.pageYOffset:document.documentElement && document.documentElement.scrollTop? document.documentElement.scrollTop: document.body.scrollTop?document.body.scrollTop:0;}
/* /lioosys */


function initNotReady()
{
	try{
		$('a.ready').click(
			function()
			{
				alert('Przepraszamy - funkcja w przygotowaniu.');
			}
		);
	}catch(e){}
}

function pagesSelect()
{
	if(!$('form#pagesSelect').is('form'))
		return true;
		
	$('form#pagesSelect input.button').hide();
	$('form#pagesSelect select').change( 
		function()
		{
			$('form#pagesSelect').get(0).submit();
			return true;
		} 
	);
}


function searchParamsLoad()
{
	$('#sizeContainer').hide(250,
		function()
		{
			$('#sizeContainer').before('<p id="loadingContainer" style="display: none;">Ładuję...</p>');
			$('#loadingContainer').show(250);
		}
	);
	var catSearch = $('#categoriesSearch').get(0);
	var idParam = catSearch.options[catSearch.selectedIndex].value;
	var url = '';
	$('#sizeContainer select').each(
		function()
		{
			url = url + '&'+this.name+'='+this.options[this.selectedIndex].value;
		}
	);
	
	$('#sizeContainer input').each(
		function()
		{
			url = url + '&'+this.name+'='+this.value;
		}
	);
//	alert("index.php?ajaxParams="+idParam+"&ajax=1"+url);
	
	$('#sizeContainer').load("index.php?ajaxParams="+idParam+"&ajax=1"+url,
		{
			"ajaxParams" : idParam ,
			"ajax" : 1
		},
		function()
		{
			$('#loadingContainer').hide(
				250,
				function()
				{	
					$('#loadingContainer').remove();
					$('#sizeContainer').show(250);
				}
			);
		}
	);
}

function initSearchOptions()
{
	try {
		if(!$('#sizeContainer').is('fieldset') && !$('#sizeContainer').is('p'))
			return true;
			
		$('#categoriesSearch').change(
			function()
			{
				searchParamsLoad();
				return true;
			}
		);
		if($('#categoriesSearch').get(0).selectedIndex > 0)
		{
			searchParamsLoad();
		}
	}catch(e){}
}

var hasLoaded = false;

function initialize()
{
	initSearchOptions();
	initNotReady();
	initPowieksz();
	initSizePopup();
	searchInit();
	unchangePass();
	initDeliveryForm();
	initAddressForm();
	initAddressMultiForm();
	addressBilling();
	pagesSelect();
	hasLoaded = true;
}


try {
	$(document).ready(initialize);
} catch(e){}	

/* ambasadorka mody */

var httpReq = false;
try {
	httpReq = new ActiveXObject(Msxml2.XMLHTTP);
} catch(e) {
	try {
		httpReq = new ActiveXObject("Microsoft.XMLHTTP"); //IE
	} catch(E) {
		try {
			httpReq = new XMLHttpRequest();
		} catch(E) {
			httpReq = false;
		}
	}
}

function checkValues() {
	
		
	var login = encodeURIComponent( document.getElementById( 'am-login' ).value );	
	httpReq.open("GET", 'am_check.php?login='+login, true);
	httpReq.onreadystatechange=function() {
		if(httpReq.readyState == 4) {
			document.getElementById( 'am-message' ).innerHTML = httpReq.responseText;
		}
	}

	httpReq.send("");	

}
function checkValuesProm() {
			
	var code = encodeURIComponent( document.getElementById( 'am-login' ).value );	
	httpReq.open("GET", 'prom_check.php?code='+code, true);
	httpReq.onreadystatechange=function() {
		if(httpReq.readyState == 4) {
			document.getElementById( 'am-message' ).innerHTML = httpReq.responseText;
		}
	}

	httpReq.send("");	
}

/*
$(window).load(initialize);
*/

/* lioosys */
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function mailtoSend ( prodTitle, manTitle, arrECProductLiteID )
{
    prodTitle = prodTitle.replace(/&/g, '-');
    manTitle = manTitle.replace(/&/g, '-');
    
    subject = prodTitle+'%20%28'+arrECProductLiteID+'%29%20'+manTitle;
    message ='Mam%20pytanie%20na%20temat%20produktu:%20'+prodTitle+'%20'+manTitle+'%20-%20http:%2F%2Fwww.saltandpepper.pl%2Findex.php%3FECProduct='+arrECProductLiteID;
    
    location.href = 'mailto:obslugaklienta@saltandpepper.pl?subject='+subject+'&body='+message;
}


function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function setRemember()
{
	var login = document.getElementById('email').value; 
	var pass = MD5(document.getElementById('password').value);
	if (document.getElementById('remember').checked) {
		createCookie('sp_login',login,14);
		createCookie('sp_pass',pass,14);
	}
	
	
	document.getElementById('login').submit();
	return false;
}
function logout(href)
{
	eraseCookie('sp_login');
	eraseCookie('sp_pass');
	
	location.href = href;
}




function reg_cancel_newsletter()
{
	if (document.getElementById('cancel').checked) {
		document.getElementById('reg-kobiety').checked = false;
		document.getElementById('reg-mezczyzni').checked = false;
		document.getElementById('reg-wyprzedaze').checked = false;
		document.getElementById('reg-nowi_projektanci').checked = false;
		return false; 
	}

	document.getElementById('reg-kobiety').checked = true;
	document.getElementById('reg-mezczyzni').checked = true;
	document.getElementById('reg-wyprzedaze').checked = true;
	document.getElementById('reg-nowi_projektanci').checked = true;
	return false; 
}

