var pageSel;
var subpageSel;
var isPageLoaded = false;
var nBrands = 0;


function startEffects(page, subpage) {
	FadeOpacity('bordoLeft', 0, 100, 2000, 10);
	FadeOpacity('bordoRight', 0, 100, 2000, 10);
	
	if(page != "Intro") {
		FadeOpacity("socialFaceBook", 100, 50, 500, 10);
		FadeOpacity("socialTwitter", 100, 50, 500, 10);
		FadeOpacity("socialMySpace", 100, 50, 500, 10);
		FadeOpacity("socialYouTube", 100, 50, 500, 10);
	
		if(page != "Home") FadeOpacity("menuHome", 100, 50, 500, 10);
		if(page != "Prodotti") FadeOpacity("menuProdotti", 100, 50, 500, 10);
		if(page != "Gallery") FadeOpacity("menuGallery", 100, 50, 500, 10);
		if(page != "Contatti") FadeOpacity("menuContatti", 100, 50, 500, 10);
		
		if(subpage != null) {
			if(subpage != "Bassi") FadeOpacity("sottomenuBassi", 100, 50, 500, 10);
			if(subpage != "Ampli") FadeOpacity("sottomenuAmpli", 100, 50, 500, 10);
			if(subpage != "Effetti") FadeOpacity("sottomenuEffetti", 100, 50, 500, 10);
			if(subpage != "Accessori") FadeOpacity("sottomenuAccessori", 100, 50, 500, 10);
		}
	} else {
		FadeOpacity('imgIntro', 0, 100, 2000, 10);
	}
	
	pageSel = page;
	subpageSel = subpage;
	
	setInterval('respira()', 8000);
	if(page == "Intro") {
		setTimeout('fadeOutIntro()', 20000);
	}
	
	isPageLoaded = true;
}


function fadeOutIntro() {
	FadeOpacity('imgIntro', 100, 0, 2000, 10);
	FadeOpacity('linkSkipIntro', 100, 0, 2000, 10);
	FadeOpacity('bordoLeft', 100, 0, 2000, 10);
	FadeOpacity('bordoRight', 100, 0, 2000, 10);
	setTimeout('goToHome()', 2500);
}


function goToHome() {
	document.location.href = "index.php";
}


function respira() {
	fadeOut();
	setTimeout('fadeIn()', 1000);
}


function fadeOut() {
	FadeOpacity('bordoLeft', 100, 50, 1000, 10);
	FadeOpacity('bordoRight', 100, 50, 1000, 10);
}


function fadeIn() {
	FadeOpacity('bordoLeft', 50, 100, 1000, 10);
	FadeOpacity('bordoRight', 50, 100, 1000, 10);
}


function setSel(element) {
	if(isPageLoaded == true) {
		if((element.id != "menu"+pageSel) && (element.id != "sottomenu"+subpageSel)) FadeOpacity(element.id, 50, 100, 250, 10);
	}
}


function setUnSel(element) {
	if(isPageLoaded == true) {
		if((element.id != "menu"+pageSel) && (element.id != "sottomenu"+subpageSel)) FadeOpacity(element.id, 100, 50, 250, 10);
	}
}


function changeTxt(textField, action, text) {
	if(action == "focus") {
		if(textField.value == text) {
			textField.value = "";
			textField.className = "input_sel";
		}
	} else if(action == "blur") {
		if(textField.value == "") {
			textField.className = "input_unsel";
			textField.value = text;
		}
	}
}


function addToNewsletter(emailField) {
	email = emailField.value;
	
	var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
	if (email.search(emailRegEx) == -1) {
		alert("Inserire un indirizzo e-mail valido!");
		emailField.focus();
		return false;
	}

	var param = 'emailaddress=' + email;

	new Ajax.Request(
		'ajax/addToNewsletter.php', 
	    { 
			method: 'post', 
			parameters: param, 
			onComplete: 
				function(response) {
	    			var mex = response.responseText.substring(1);
	    			alert(mex);
	    			if(response.responseText.charAt(0) == "Y") {
	    				emailField.value = "";
	    				emailField.blur();
	    				changeTxt(emailField, 'blur', 'e-mail address here');
	    			}
				} 
	    }
	);
}


function resetBrandSel() {
	for(var i = 1; i <= nBrands; i++) {
		$('brand' + i).className = "";
	}
}


function getResizeLocation() {
	var url = location.href;
	url = url.substring(0, url.lastIndexOf('/'));
	url += "/includes/resize.php";
	return url;
}


function getImgLocation() {
	var url = location.href;
	url = url.substring(0, url.lastIndexOf('/'));
	url += "/imgs/prodotti/";
	return url;
}


var globalCategory;
var globalBrandId;
var globalIndex;
var globalPage;
var globalNProducts;


function managePaging() {
	if(globalPage > 1) {
		$('firstPage').style.visibility = "visible";
		$('prevPage').style.visibility = "visible";
	} else {
		$('firstPage').style.visibility = "hidden";
		$('prevPage').style.visibility = "hidden";
	}
	
	var lastPage;
	if(globalNProducts % 8 == 0) {
		lastPage = globalNProducts / 8;
	} else {
		lastPage = parseInt((globalNProducts / 8), 10) + 1;
	}
	
	if(globalPage < lastPage) {
		$('nextPage').style.visibility = "visible";
		$('lastPage').style.visibility = "visible";
	} else {
		$('nextPage').style.visibility = "hidden";
		$('lastPage').style.visibility = "hidden";
	}
	
	if(globalPage > 1 || globalPage < lastPage) {
		$('pageCount').innerHTML = globalPage + " di " + lastPage;
		$('pageCount').style.visibility = "visible";
	} else {
		$('pageCount').style.visibility = "hidden";
	}
}


function loadProducts(category, brandId, index, page) {
	resetBrandSel();
	$('brand' + index).className = "sel";
	
	globalCategory = category;
	globalBrandId = brandId;
	globalIndex = index;
	globalPage = page;
	
	var offset = (page - 1) * 8;
	
	var param = 'cat=' + category + '&brandId=' + brandId;
	param += '&offset=' + offset;
	
	if($('ricercaProd') && $('ricercaProd').value != "Cerca..." && $('ricercaProd').value != "") {
		param += '&filter=' + $('ricercaProd').value;
	}
	
	new Ajax.Request(
		'ajax/getProducts.php', 
	    { 
			method: 'post', 
			parameters: param, 
			onComplete: 
				function(response) {
	    			var strProducts = response.responseText.substring(1);
	    			var tmp = strProducts.split("$");
					
	    			globalNProducts = tmp[0];
	    			strProducts = tmp[1];
	    			
	    			if(response.responseText.charAt(0) == "Y") {
	    				if($('imgMarchiProdotti_riga1').style.display != 'none') {
	    					$('imgMarchiProdotti_riga1').style.display = 'none';
	    					$('imgMarchiProdotti_riga2').style.display = 'none';
	    					$('imgProdotti_riga1').style.display = 'table-row';
	    					$('imgProdotti_riga2').style.display = 'table-row';
	    				}
	    				
	    				if(category == "Ampli") {
	    					var classOriginale = $('listaProdotti').getAttribute("class");
	    					if(classOriginale == null || classOriginale == "") {
	    						classOriginale = $('listaProdotti').getAttribute("className");
	    					}
	    					$('listaProdotti').setAttribute("class", classOriginale + " bkgroundWhite");
	    					$('listaProdotti').setAttribute("className", classOriginale + " bkgroundWhite");
	    				}
	    				
	    				var products = strProducts.split("|");
	    				var row = 2;
	    				var col = 0;
						
						var displayProduct = globalNProducts < 8 ? globalNProducts : 8;
	    				for(var i = 0; i < displayProduct; i++) {
							if(products[i] == null) {
								break;
							}
							
	    					var product = products[i].split(",");
	    					if(col % 4 == 0) {
	    						row++;
	    						col = 0;
	    					}
	    					$('listaProdotti').tBodies[0].rows[row].cells[++col].innerHTML = "<img src='" + getResizeLocation() + "?url=" + getImgLocation() + product[1] + "&size=160x200' alt='" + product[2] + "' style='cursor: pointer;' onClick='showProductDetails(this, " + product[0] + ");' />";
	    				}

	    				for(i; i <= 8; i++) {
	    					if(col % 4 == 0) {
	    						row++;
	    						col = 0;
	    					}
	    					$('listaProdotti').tBodies[0].rows[row].cells[++col].innerHTML = "&nbsp;";
	    				}

	    				managePaging();
	    			}
				} 
	    }
	);
}


function loadBrands(filter, categoria) {
	if(filter == null || filter == "Cerca...") {
		filter = "";
	}
	var param = 'filter=' + filter;
	param += '&categoria=' + categoria;
	
	new Ajax.Request(
		'ajax/getBrands.php', 
	    { 
			method: 'post', 
			parameters: param, 
			onComplete: 
				function(response) {
					var resp = response.responseText;
					var tmp = resp.split("$");
					nBrands = tmp[0];
					$('elencoMarchi').innerHTML = tmp[1];
				}
	    }
	);
}


function changeProductsPage(action) {
	switch(action) {
		case 'first':
			loadProducts(globalCategory, globalBrandId, globalIndex, 1);
			break;
		case 'prev':
			loadProducts(globalCategory, globalBrandId, globalIndex, (globalPage - 1));
			break;
		case 'next':
			loadProducts(globalCategory, globalBrandId, globalIndex, (globalPage + 1));
			break;
		case 'last':
			var lastPage;
			if(globalNProducts % 8 == 0) {
				lastPage = globalNProducts / 8;
			} else {
				lastPage = parseInt((globalNProducts / 8), 10) + 1;
			}
			loadProducts(globalCategory, globalBrandId, globalIndex, lastPage);
			break;
	}
}


function showProductDetails(obj, id) {
	var param = 'id=' + id;
	
	new Ajax.Request(
		'ajax/getProductDetails.php', 
	    { 
			method: 'post', 
			parameters: param, 
			onComplete: 
				function(response) {
	    			var docWidth = document.body.clientWidth;
	    			var docHeight = document.body.clientHeight;
	    			
	    			var width = 700;
	    			var height = 500;
	    			
	    			if(width > (docWidth - 50)) {
	    				width = (docWidth - 50);
	    			}
	    			
	    			if(height > (docHeight - 50)) {
	    				height = (docHeight - 50);
	    			}
	    			
	    		    var win = new Window({
	    		    	 
	    		    	width: width, 
	    		    	height: height, 
	    		    	minimizable: false,
	    		        maximizable: false
	    		    });
	    		    win.getContent().update(response.responseText);
	    		    win.showCenter();
				}
	    }
	);
}


