window.onload = setBehaviours;

function inputOnFocus (oObj, sTxt) {
	if (oObj.value == sTxt) oObj.value = '';
}
function inputOnBlur (oObj, sTxt) {
	if (oObj.value == '') oObj.value = sTxt;
}

function sendSearchBoxForm(oForm) {
	var s = oForm.q.value;
	s = s.replace(/^\s+/, '').replace(/\s+$/, '');

	if (s != '') {
		s = encodeURIComponent(s).replace(/%20/g, "+").replace(/%2C/g, ' ').replace(/%2F/g, '/');

		window.location.href = oForm.action + '/q:' + s + '.html';
	}
	return false;
}

function sendSearchForm(oForm) {
	var s = oForm.q.value;
	
	var sLocation = '';

	sLocation = oForm.action + '/';
	if(window.location.href.indexOf("zaawansowane") != -1) sLocation = sLocation + 'zaawansowane,';
	
	var sAdv = document.getElementById("adv").value != '' ? document.getElementById("adv").value : "none";

	if (s != ''){
		sLocation = sLocation + 'q:' + s + ',';
	}
	for( i = 0; i < oForm.dest.length; i++ ) {
		if( oForm.dest[i].checked == true )
			dest = oForm.dest[i].value;
	}

	sLocation += 'dest:' + dest + ',';
	

	sLocation += oForm.adv.value == "0" ? 'adv:0,' : 'adv:1,';

	
	if (sLocation.charAt(sLocation.length - 1) == ','){
		sLocation = sLocation.slice(0,-1);
	}
	if(document.getElementById("adv").value == '1'){
		for (var i = 0; i < oForm.length; i++) {

			if (oForm[i].type == 'checkbox' && oForm[i].checked) {

				//oForm[i].name = oForm[i].name.replace(/\[[0-9]\]/, "");
				sLocation += ',' + oForm[i].name + ':' + oForm[i].value;
			}
			if (oForm[i].type == 'select-one' && oForm[i].value != '') {
				sLocation += ',' + oForm[i].name + ':' + oForm[i].value;
			}
			if (oForm[i].type == 'radio' && oForm[i].checked && oForm[i] != '') {
				sLocation += ',' + oForm[i].name + ':' + oForm[i].value;
			}
			if (oForm[i].type == 'text' && oForm[i].value != '' && oForm.name != 'q') {
				sLocation += ',' + oForm[i].name + ':' + oForm[i].value;
			}
		}
	}

		window.location.href = sLocation + '.html';

	return false;
}

function sendNewsletterBoxForm(oForm, sTxt) {
	var sEmail = oForm.n_email.value;
	sEmail = sEmail.replace(/^\s+/, '').replace(/\s+$/, '');
	
	if (sEmail == '' || sEmail == sTxt) {
		return false;
	}
}


function sendSorterForm(oForm) {
	/*var sLocation = (navigator.userAgent.indexOf('MSIE') != -1 ? ''  : '') + oForm.action;*/

	var sLocation = oForm.action;
	sLocation += '/sort:' + oForm.sort.options[oForm.sort.options.selectedIndex].value;
	window.location.href = sLocation + '.html';

}

function sendSorterCatsForm(oForm) {
	/*var sLocation = (navigator.userAgent.indexOf('MSIE') != -1 ? ''  : '') + oForm.action;*/

	//var sLocation = oForm.action;
	var sLocation = '/' + oForm.cats.options[oForm.cats.options.selectedIndex].value;
	window.location.href = sLocation;

}

function sendSearchSorterForm(oForm) {
	var sLocation = window.location.href;
	var sNewLoc = "";
	
	if(sLocation.indexOf(".html") != -1){
		if(sLocation.indexOf("sort") != -1){
			sLocation = sLocation.split(",sort");
			sNewLoc=sLocation[0];
		} else {
			sLocation= sLocation.split(".html");
			sNewLoc =sLocation[0];
		}
		sLocation = sNewLoc + ',sort:' + oForm.sort.options[oForm.sort.options.selectedIndex].value+'.html';
		
	} else {
		if(sLocation.indexOf("sort") != -1){
			sLocation = sLocation.split("&sort");
			sNewLoc=sLocation[0];
		} else {
			sNewLoc = sLocation;
		}
		sLocation = sNewLoc + '&sort=' + oForm.sort.options[oForm.sort.options.selectedIndex].value;
		
	}
	window.location.href = sLocation ;
}

function hoverMenuButton(oLink, bHover) {
	oLink.style.backgroundPosition = bHover ? "19px 0" : "0 0";
}

function setBehaviours() {
	// preload obrazkow

	
	if (document.getElementById) {
		var isOpera, isIE = false;
		if(typeof(window.opera) != 'undefined'){isOpera = true;}
		if(!isOpera && navigator.userAgent.indexOf('Internet Explorer')){isIE = true;}

		//fix both IE and Opera (adjust when they implement this method properly)
		if(isOpera || isIE){
		  document.nativeGetElementById = document.getElementById;
		  //redefine it!
		  document.getElementById = function(id){
		    var elem = document.nativeGetElementById(id);
		    if(elem){
		      //verify it is a valid match!
		      if(elem.id == id){
		        //valid match!
		        return elem;
		      } else {
		        //not a valid match!
		        //the non-standard, document.all array has keys for all name'd, and id'd elements
		        //start at one, because we know the first match, is wrong!
		        for(var i=1;i<document.all[id].length;i++){
		          if(document.all[id][i].id == id){
		            return document.all[id][i];
		          }
		        }
		      }
		    }
		    return null;
		  };
		}
		// WYSZUKIWARKA
		// onsubmit
//		if (oSearchBoxForm = document.getElementById('searchBoxForm')) {
//			oSearchBoxForm.onsubmit = function() {return sendSearchBoxForm(oSearchBoxForm);};
//		}
//		if (oSearchForm = document.getElementById('searchForm')) {
//			oSearchForm.onsubmit = function() {return sendSearchForm(oSearchForm);};
//		}
		
		// NEWSLETTER
		// onfocus, onblur
		if (oNewsletterInp = document.getElementById('n_email')) {
			oNewsletterInp.value = sNewsletterTxt;
			oNewsletterInp.onfocus = function() {inputOnFocus(oNewsletterInp, sNewsletterTxt);};
			oNewsletterInp.onblur = function() {inputOnBlur(oNewsletterInp, sNewsletterTxt);};
		}
		// logowanie
		// onfocus, onblur
		if (oLoginInp = document.getElementById('b_login')) {
			oLoginInp.value = 'Login';
			oLoginInp.onfocus = function() {inputOnFocus(oLoginInp, 'Login');};
			oLoginInp.onblur = function() {inputOnBlur(oLoginInp, 'Login');};
		}
		// onfocus, onblur
		if (oPasswdInp = document.getElementById('b_passwd')) {
			oPasswdInp.value = 'hasło';
			oPasswdInp.onfocus = function() {inputOnFocus(oPasswdInp, 'hasło');};
			oPasswdInp.onblur = function() {inputOnBlur(oPasswdInp, 'hasło');};
		}
		// onsubmit
		if (oNewsletterBoxForm = document.getElementById('newsletterBoxForm')) {
			oNewsletterBoxForm.onsubmit = function() {return sendNewsletterBoxForm(oNewsletterBoxForm, sNewsletterTxt);};
		}
		
		// FORMULARZ SORTOWANIA
		if (oSorterForm = document.getElementById('sorterForm')) {
			oSorterForm.sort.onchange = function() {sendSorterForm(oSorterForm)};
		}
		
		// FORMULARZ SORTOWANIA - KAtegorie
		if (oSorterCatsForm = document.getElementById('sorterCategoriesForm')) {
			oSorterCatsForm.cats.onchange = function() {sendSorterCatsForm(oSorterCatsForm)};
		}
		
		// FORMULARZ SORTOWANIA W WYSZUKIWARCE
		if (oSorterSearcherForm = document.getElementById('sorterSearchForm')) {
			oSorterSearcherForm.sort.onchange = function() {sendSearchSorterForm(oSorterSearcherForm)};
		}
		
		// hover prawego bocznego paska z menu
		if (oPraweMenu = document.getElementById('menuPrawe')) {
			aLinks = oPraweMenu.getElementsByTagName('A');
			for (var i=0; i < aLinks.length; i++) {
				aLinks[i].onmouseover = function() {hoverMenuButton(this, true)};
				aLinks[i].onmouseout = function() {hoverMenuButton(this, false)};
			}
		}
	}
}


