if(window.XMLHttpRequest)
	Ajax = new XMLHttpRequest();
else if(window.ActiveXObject)
	Ajax = new ActiveXObject("Microsoft.XMLHTTP");

var form;
var word = new Array();
var photos = new Array();

window.onload = function (){
	words = words.split(';');
	for (var i2 in words) {		//		wordID	,	wordValue		;
		var t1 = words[i2].split(',');
		if (t1[0])
			word[t1[0]] = t1[1];
	}
	if (document.getElementById('prods'))
		onLoadProds();
	else if (document.getElementById('details'))
		onLoadDetails();
	else if (document.getElementById('store'))
		onLoadStore();
	else if (document.getElementById('basket2'))
		onLoadBasket(2);
	else if (document.getElementById('basket3'))
		onLoadBasket(3);
	else if (document.getElementById('basket4'))
		onLoadBasket(4);

	for (i1 in document.forms)
		if (document.forms[i1].nodeName == "FORM" && document.forms[i1].name)
				if (document.forms[i1].name == 'accounts')
					onLoadHelpAccounts(i1);
	main();

}
$(function() {
	$('#logo').click(function(){
		_gaq.push(['_setCustomVar',1,'logo','scope1',1],['_trackPageview']);
	})
	$('#menu1 a').click(function(){
		_gaq.push(['_setCustomVar',1,'naglowek',$(this).attr('title'),3],['_trackPageview']);
	})
	$('#menu2 a').click(function(){
		_gaq.push(['_setCustomVar',1,'menu gorne',$(this).html(),3],['_trackPageview']);
	})
	$('#menu3 a').click(function(){
		_gaq.push(['_setCustomVar',1,'menu lewe',$(this).attr('title'),3],['_trackPageview']);
	})
	$('#footer a').click(function(){
		_gaq.push(['_setCustomVar',1,'stopka',$(this).attr('title'),3],['_trackPageview']);
	})
	$('#youre a').click(function(){
		_gaq.push(['_setCustomVar',1,'breadcrumbs','ok',3],['_trackPageview']);
	})
	$('.pages a').click(function(){
		if($(this).parents('#pos1').length > 0)
			_gaq.push(['_setCustomVar',1,'pagination','top',3],['_trackPageview']);
		else
			_gaq.push(['_setCustomVar',1,'pagination','bottom',3],['_trackPageview']);
	})
	$('#detailphoto .mini').click(function(){
		_gaq.push(['_setCustomVar',1,'miniaturki','ok',3],['_trackPageview']);
	})
	$('.prod2 a img').click(function(){
		_gaq.push(['_setCustomVar',1,'lista produktow','zdjecie srednie',3],['_trackPageview']);
	})
	$('.prod2 .name a').click(function(){
		_gaq.push(['_setCustomVar',1,'lista produktow','nazwa produktu',3],['_trackPageview']);
	})
	$('.colors').click(function(){
		_gaq.push(['_setCustomVar',1,'kolor','ok',3],['_trackPageview']);
	})
	$('#contacts a').click(function(){
		_gaq.push(['_setCustomVar',1,'kontakt',$(this).attr('id'),3],['_trackPageview']);
	})
	$('#storeButton').click(function(){
		_gaq.push(['_setCustomVar',1,'schowek','szczegoly',3],['_trackPageview']);
	})
	$('#basket label').click(function(){
		_gaq.push(['_setCustomVar',1,'rozmiary','ok',3],['_trackPageview']);
	})
});
function main() {
	form = window.document.forms[0]['email'];
	form.required = true;
	form.removeAttribute('type');
	form.setAttribute('type','email');
}

/*			details			*/
var instore;
var pid;
var store;
var basket;
function onLoadDetails () {
	var store = document.getElementById('storeButton');
	var basket = document.getElementById('basket');
	pid = store.href.split('=');
	pid = pid[pid.length-1];
	store.removeAttribute('href');
	if(store.addEventListener)
		store.addEventListener('click',store2,false);
	else if (store.attachEvent)
		store.attachEvent('onclick',store2);
	dp = document.getElementById('detailphoto');
	for (i1 in dp.children)
		if (dp.children[i1].className == 'mini') {
			if(dp.addEventListener)
				dp.children[i1].addEventListener("click",swapPhoto2,false);
			else if (dp.children[i1].attachEvent)
				dp.children[i1].attachEvent("onclick",swapPhoto2);
			}
	if (basket) {
		if(basket.addEventListener)
			basket.addEventListener("submit",checkBasket,false);
		else if (basket.attachEvent)
			basket.attachEvent("onsubmit",checkBasket);
		
		for (i1 in basket.firstChild.children)
			if (basket.firstChild.children[i1].tagName == 'LABEL' && basket.firstChild.children[i1].className == 'temporary') {
				if(basket.addEventListener)
					basket.firstChild.children[i1].addEventListener("click",temporary,false);
				else if (basket.attachEvent)
					basket.firstChild.children[i1].attachEvent("onclick",temporary);
			}
	}
}
function swapPhoto2 (e) {
	bp = document.getElementById('bigphoto');
	if (e.srcElement)
		ph1 = e.srcElement.src.split('/');
	else
		ph1 = this.src.split('/');
	ph2 = ph1[ph1.length-1].split('.');
	ph3 = ph2[0].split('-');
	ph4 = ph3[ph3.length-1].split('m');
	bp.src = (ph4[0] < 3000) ? 'photos/'+ph4[0]+'b.jpg' : 'photo/big/'+ph4[0]+'.jpg';
}
function store2 () {
	if(Ajax) {
		Ajax.open("GET", '/set.php?section=store&product='+pid);
		Ajax.onreadystatechange = function() {
			if(Ajax.readyState == 4  && Ajax.status == 200) {
				document.body.style.cursor = 'auto';
				store = document.getElementById('storeButton');
				if (store.children.length == 2) {
					store.firstChild.value = word[157];
					store.removeChild(store.lastChild);
				}
				else {
					store.firstChild.value = word[158];
					div = document.createElement('div');
					div.innerHTML = word[179];
					store.appendChild(div);
				}
			}
			else
				document.body.style.cursor = 'wait';
		}
		Ajax.send(null);
	}
}
function checkBasket () {
	basket = document.getElementById('basket');
	items = 0;
	for (i1 in basket.firstChild.children)
		if (basket.firstChild.children[i1].firstChild && basket.firstChild.children[i1].firstChild.type == 'checkbox' && basket.firstChild.children[i1].firstChild.checked)
			items++;
	if (items)
		return true;
	else 	{
		alert(word[318]);
		return false;
	}
}
function temporary() {
	if(Ajax) {
		Ajax.open("GET", '/set.php?section=temporary&product='+pid+'&size='+this.htmlFor);
		Ajax.send(null);
	}
}

/*			products		*/
var prevnext;	// numer akutalnie wyświetlanego prevnext
function onLoadProds() {
	prods = prods.split(';');	//	pid		,		isInStore		,		photos (spacja separator)
	photos = new Array();
	for (var i in prods) {
		var prod = prods[i].split(',');
		var newdiv;
		var container = document.getElementById('p'+prod[0]);
		if (prod[0] > 0) {
			for (i1 in container.children) {
				if (container.children[i1].className == 'name') {
					for (i2 in container.children[i1].children) {
						child = container.children[i1].children[i2];
						if (child.tagName == 'H2' || child.tagName == 'H3' || child.tagName == 'P'){
							txt = child.innerHTML;
							child.innerHTML = '<a href="'+prod[0]+'" title="'+txt+'">'+txt+'</a>';
						}
						else if (child.tagName == 'A') {
							child.removeAttribute('href');
							if(child.addEventListener)
								child.addEventListener('click',store4,false);
							else if (child.attachEvent)
								child.attachEvent('onclick',store4);
						}
					}
				}
			}
			photos[prod[0]] = prod[2].split(' ');
			if (photos[prod[0]].length > 1) {	//		more then 1 photo
				newdiv = document.createElement('div'); 
				newdiv.id = 'prevnext_'+prod[0];
				newdiv.className = 'prevnext';
				container.lastChild.style.bottom = '53px';
				newdiv.innerHTML = '<div onclick="swapPhoto('+prod[0]+',0);_gaq.push([\'_setCustomVar\', 1, \'klikanie srednich\', \'poprzednie\', 3]);_gaq.push([\'_trackPageview\']);">'+word[333]+'</div><div style="margin-left:120px" onclick="swapPhoto('+prod[0]+',1);_gaq.push([\'_setCustomVar\', 1, \'klikanie srednich\', \'nastepne\', 3]);_gaq.push([\'_trackPageview\']);">'+word[258]+'</div>';
				container.insertBefore(newdiv,container.lastChild);
				if(container.addEventListener) {
					container.addEventListener("mouseover",swapShow,false);
					container.addEventListener("mouseout",swapHide,false);
				}
				else if (container.attachEvent) {
					container.attachEvent("onmouseover",swapShow);
					container.attachEvent("onmouseout",swapHide);
				}
			}
		}
	}
}
function store4 (e) {
	if (e.srcElement)
		pid = e.srcElement.parentNode.parentNode.parentNode.id.split('p');
	else
		pid = e.target.parentNode.parentNode.parentNode.id.split('p');
	pid = pid[1];
	if(Ajax && pid > 0) {
		Ajax.open("GET", '/set.php?section=store&product='+pid);
		Ajax.onreadystatechange = function() {
			if(Ajax.readyState == 4  && Ajax.status == 200) {
				document.body.style.cursor = 'auto';
				children = document.getElementById('p'+pid).lastChild;
				for (i1 in children.children) {
					if (children.children[i1].tagName == 'A') {
						if (children.children[i1].firstChild.value[0] == '+') {
							children.children[i1].firstChild.value = '- '+word[156];
							children.children[i1].title = word[158];
						}
						else {
							children.children[i1].firstChild.value = '+ '+word[156];
							children.children[i1].title = word[157];
						}
					}
				}
			}
			else
				document.body.style.cursor = 'wait';
		}
		Ajax.send(null);
	}
}
function swapShow(e) {
	if (e.currentTarget)
		k = e.currentTarget.id.split('p');
	else
		k = e.srcElement.parentElement.parentElement.id.split('p');
	if (k[1] > 0) {
		prevnext = k[1];
		document.getElementById('prevnext_'+prevnext).style.visibility='visible';
	}
}
function swapHide() {
	if (prevnext)
		document.getElementById('prevnext_'+prevnext).style.visibility='hidden';
}
function swapPhoto(prod,action) {
	img = document.getElementById('p'+prod).firstChild.firstChild;
	if (action) {
		img.src = (photos[prod][1] < 3000) ? 'photos/'+photos[prod][1]+'n.jpg' : 'photo/medium/'+photos[prod][1]+'.jpg';
		photos[prod].push(photos[prod].shift());
	}
	else {
		img.src = (photos[prod][photos[prod].length-1] < 3000) ? 'photos/'+photos[prod][photos[prod].length-1]+'n.jpg' : 'photo/medium/'+photos[prod][photos[prod].length-1]+'.jpg';
		photos[prod].unshift(photos[prod].pop());
	}
}

function onLoadStore() {
	store = document.getElementById('store');
	for (i1 in store.children)
		if (store.children[i1].className == 'prod') {
			store.children[i1].lastChild.removeAttribute('href');
			if(store.addEventListener)
				store.children[i1].lastChild.addEventListener("click",store3,false);
			else if (store.attachEvent)
				store.children[i1].lastChild.attachEvent("onclick",store3);
		}
}
function store3 (e) {
	if (e.srcElement)
		pid = e.srcElement.parentNode.parentNode.id.split('p');
	else
		pid = e.target.parentNode.parentNode.id.split('p');
	pid = pid[1];
	if(Ajax && pid > 0) {
		Ajax.open("GET", '/set.php?section=stores&product='+pid);
		Ajax.onreadystatechange = function() {
			if(Ajax.readyState == 4  && Ajax.status == 200) {
				document.body.style.cursor = 'auto';
				store.removeChild(document.getElementById('p'+pid));
				if (store.children.length < 1)
					document.getElementById('empty').style.display = 'block';
			}
			else
				document.body.style.cursor = 'wait';
		}
		Ajax.send(null);
	}
}

function onLoadBasket(step) {
	if (step == 2) {
		basket = document.getElementById('basket2');
		inputs = document.getElementsByTagName('input');
		if(document.body.addEventListener) {
			for (i1 in inputs)
				if (inputs[i1].name == 'shipping')
					inputs[i1].addEventListener('click',basketMethodChange,false);
			basket.addEventListener('submit',function(e){basketMethodCheck(e);},false);
		}
		else if (document.body.attachEvent) {
			for (i1 in inputs)
				if (inputs[i1].name == 'shipping')
					inputs[i1].attachEvent('onclick',basketMethodChange);
			basket.attachEvent('onsubmit',function(e){basketMethodCheck(e);});
		}
	}
	else if (step == 3) {
		basket = document.getElementById('basket3');
		if(basket.addEventListener)
			basket.addEventListener('submit',function(e){basketAddressCheck(e);},false);
		else if (basket.attachEvent)
			basket.attachEvent('onsubmit',function(e){basketAddressCheck(e);});
	}
	else if (step == 4) {
		basket = document.getElementById('basket4');
		if(basket.addEventListener)
			basket.addEventListener('submit',function(e){basketAcceptCheck(e);},false);
		else if (basket.attachEvent)
			basket.attachEvent('onsubmit',function(e){basketAcceptCheck(e);});
	}
}
function basketMethodChange(e) {
	inputs = document.getElementsByTagName('input');
	for (i1 in inputs)
		if (inputs[i1].name == 'shipping')
			inputs[i1].parentNode.parentNode.style.backgroundColor = null;
	if (e.srcElement)
		e.srcElement.parentNode.parentNode.style.backgroundColor = '#d5fed4';
	else
		e.target.parentNode.parentNode.style.backgroundColor = '#d5fed4';
}
function basketMethodCheck(e) {
	inputs = document.getElementsByTagName('input');
	for (i1 in inputs) {
		if (inputs[i1].name == 'shipping' && inputs[i1].checked == true) {
			return true;
		}
	}
	alert(word[316]);
	//e.returnValue = false;
	if(e.preventDefault)
		e.preventDefault();
}
function basketAddressCheck(e) {
	var warn = false;
	datas = document.getElementById('datas');
	inserts = document.getElementById('datas').getElementsByTagName('input');
	var fields1=new Array("firstname1","lastname1","firstname2","lastname2","address2","zipcode2","city2","country2","phone2");	// obowiązkowe
	var fields2=new Array("email1","phone1","skype1","gadu1");	//	conajmniej 1 z tych
	for (i1 in inserts)
		if (inserts[i1].type == 'text')
			for (i2 in fields1)
				if (fields1[i2] == inserts[i1].name) {
					if (inserts[i1].value == '') {
						inserts[i1].style.backgroundColor = '#ffc0cb';
						warn = true;
					}
					else
						inserts[i1].style.backgroundColor = null;
				}
	if (warn) {
		alert(word[314]);
		delete warn;
		e.preventDefault();
		return false;
	}
	warn = true;
	for (i1 in inserts)
		if (inserts[i1].type == 'text')
			for (i2 in fields2)
				if (fields2[i2] == inserts[i1].name) {
					if (inserts[i1].value == '')
						inserts[i1].style.backgroundColor = '#fcd48e';
					else {
						inserts[i1].style.backgroundColor = null;
						warn = false;
					}
				}
	if (warn) {
		alert(word[315]);
		delete warn;
		e.preventDefault();
	}
}
function basketAcceptCheck(e) {
	if (document.getElementById('agreed').checked == false) {
		alert(word[317]);
		e.preventDefault();
	}
}


function onLoadHelpAccounts(x1) {
	form = document.forms[x1];
	for (i1 in form.elements)
		if (form.elements[i1].tagName == "INPUT" && form.elements[i1].type == "text") {
			if(form.addEventListener)
				form.elements[i1].addEventListener("click",selectInput,false);
			else if (form.attachEvent)
				form.elements[i1].attachEvent("onclick",selectInput);
		}
}
function selectInput(e) {
	e.target.selectionStart = 0;
	e.target.selectionEnd = e.target.textLength;
}

