// JavaScript Document
var specCookie = readCookie(specCookieName);
var categoryCookie = readCookie(categoryCookieName);

var specList = Array();
var selectedBrand = false;

function checkCategory(categoryId) {
	if (Number(categoryCookie) != 0) {
		if (Number(categoryCookie) != categoryId) {
			eraseCookie(specCookieName);
			eraseCookie(orderByCookieName);
		}
	}
	
	createCookie(categoryCookieName, categoryId);
	//return true;
}

function getSpecList() {
	var specPairList = specCookie ? specCookie.split('|') : Array();
	
	for (var n =0; n < specPairList.length; n++) {
		var specPair = specPairList[n].split(':');
		if (specPair[0] == 'brand') {
			selectedBrand = specPair[1];
		}
		else {
			specList[specList.length] = [Number(specPair[0]), Number(specPair[1])];
		}
	}
}

function addSpecItemFilter(specId, itemId, redirect) {
	getSpecList(); 
	
	if (specId == 'brand') {		
		selectedBrand = Number(itemId);
	}
	else {
		specList[specList.length] = [Number(specId), Number(itemId)];
	}
	postCookie();
	window.location = redirect;
}

function removeSpecItemFilter(specId, redirect) {
	getSpecList();
	
	if (specId == 'brand') {
		selectedBrand = false;
	}
	else {
		for(var n=0; n < specList.length; n++) {
			if (specList[n][0] == specId) {
				specList.splice(n, 1);
				break;
			}
		}
	}
	
	postCookie();
	window.location = redirect;
}

function postCookie() {
	var cookieStr = '';
	for (var n=0; n < specList.length; n++) {
		cookieStr += specList[n][0].toString() + ':' + specList[n][1].toString();
		if (n < (specList.length -1))
			cookieStr += '|';
	}
	
	if (selectedBrand != false) {
		if (specList.length)
			cookieStr += '|';
			
		cookieStr += 'brand:' + selectedBrand;
	}
	
	createCookie(specCookieName, cookieStr);
}

function eraseSpecCookie() {
	eraseCookie(specCookieName);
	window.location.reload();
}

function setOrderBy(text) {
	createCookie(orderByCookieName, text);
	createCookie("vine_por_order_by", "1");
	window.location.reload();
}

var popUpImageList = Array();

function showImagePopUp(e) {
	if (!e)
		e = window.event;
		
	var elem = (e.target) ? e.target : e.srcElement;
	
	var x, y;

	if (typeof(e.pageX) == 'number') {
		x = e.pageX + 16;
		y = e.pageY + 16;
	}
	else {
		if (typeof(e.clientX) == 'number') {
			x = (e.clientX + document.documentElement.scrollLeft) + 16;
			y = (e.clientY + document.documentElement.scrollTop) + 16;
		}
		else {
			x = 0;
			y = 0;
		}
	}
	
	if(document.getElementById('popUpImg')) {
		// make slide fit on screen
		_calculateBrowserSize();
		var popUpWidth = 300;
		var popUpHeight = 300;
		
		// calculate client x,y;
		var clientX = 0, clientY = 0;
		if (typeof(e.clientX) == 'number') {
			clientX = e.clientX;
			clientY = e.clientY;
		}
		
		if (browserWindowWidth < popUpWidth) {
			x = 0
		}
		else {
			if (clientX) {
				if ((clientX + popUpWidth) > browserWindowWidth)
					x -= (clientX+popUpWidth) - browserWindowWidth;
			}
		}
		
		if (browserWindowHeight < popUpHeight) {
			y = 0
		}
		else {
			if (clientY) {
				if ((clientY + popUpHeight) > browserWindowHeight) 
					y -= (clientY+popUpHeight) - browserWindowHeight;
			}
		}
		
		var popup = document.getElementById('popUpImg');
		popup.style.top = y + 'px';
		popup.style.left = x + 'px';
		
		while (popup.firstChild)
			popup.removeChild(popup.firstChild);
		
		var pid = Number(elem.parentNode.id.substr(7));
		
		if (!isNaN(pid)) {
			if (pid > -1 && pid < popUpImageList.length) {
				var img = document.createElement('img');
				img.setAttribute('src', popUpImageList[pid]);
				img.setAttribute('width', '300');
				img.setAttribute('height', '300');
				popup.appendChild(img);
			}
		}
		
		popup.style.display = 'block';
	}
}

function hideImagePopUp(e) {
	document.getElementById('popUpImg').style.display = 'none';
}

var browserWindowHeight = 0;
var browserWindowWidth = 0;

function _calculateBrowserSize() {
	// calculate window width - height
	if (navigator.userAgent.indexOf('MSIE') != -1) {
		if (document.documentElement.clientWidth == 0)
			browserWindowWidth = document.body.clientWidth;
		else	
			browserWindowWidth = document.documentElement.clientWidth;
			
		if (document.documentElement.clientHeight == 0)
  			browserWindowHeight = document.body.clientHeight;
		else
			browserWindowHeight = document.documentElement.clientHeight;
	}
	else {
		browserWindowWidth = window.innerWidth;
  		browserWindowHeight = window.innerHeight;
	}
	
}

function updateProductPrice() {
	var warrantyId;
	
	var warrantyIds = document.forms.buyProductForm.warrantyType;
	for (var n=0; n < warrantyIds.length; n++) {
		if (warrantyIds[n].checked)
			warrantyId = Number(warrantyIds[n].value);
	}
	
	var qty = Number(document.forms.buyProductForm.qty.value);
	
	if (isNaN(qty)) {
		document.forms.buyProductForm.qty.value = '1';
		qty = 1;
	}
	
	var prodPriceDiv = document.getElementById('curProductPrice');
	var newPrice = prodPriceDiv.firstChild.data.substr(0, prodPriceDiv.firstChild.data.indexOf(' ')+1);

	var curWar;
	for (var n=0; n < warranties.length; n++) {
		if (warranties[n].id == warrantyId) {
			curWar = warranties[n];
			break;
		}
	}
	
	var recargo;
	if ((prodPrice * qty) + ((prodPrice * qty) * (curWar.percentCharge / 100 )) > ((prodPrice * qty) + curWar.fixedCharge)) {
		recargo = (prodPrice * qty) + ((prodPrice * qty) * (curWar.percentCharge / 100 ));
	}
	else {
		recargo = ((prodPrice * qty) + curWar.fixedCharge);
	}
	
	newPrice += recargo.toFixed(0);
	
	prodPriceDiv.firstChild.data = newPrice;
}

