var cutBy = 27; 

function buildAjax () {
    return false;
}

function isParentLightbox(node) {
	if(node.parentNode.id != undefined) {
		if(node.parentNode.id.substring(0,8) == "lightbox") {
		return true;
		}
		else {
		return isParentLightbox(node.parentNode);
		}
	}
return false;
}

var baseUrl;
function setBaseUrl(base) {
baseUrl = base;
}
function goToPosition () {
var hash = location.hash;
	buildAjax();

	if(hash.length > 5) {
	//window.location(hash);
	loadAjax(baseUrl + "/ajax/" + hash.substring(1), 'content', "GET");
	} else if (hash.length > 0){
	document.getElementById('content').style.visibility='visible';
	setHash(window.location);
	} else {
	document.getElementById('content').style.visibility='visible';
	}

}

function setHash(url) {
var snip = cutBy;
	if((url+"").indexOf("ajax") != -1 ) { snip += 5; } 
	
	var curHash = location.hash;
	var cur;
		if(url == 0) {
		cur = location.hash;
		} else {
		cur = (url + "").substring(snip);
		dhtmlHistory.add(cur,  null);
//		cur = cur.substring(0, cur.length - curHash.length);
		location.hash = "#" + cur;
		}
		
}

function loadPage (url, destination) {
setHash(url);
url = url.href;
var urlLength = 27;
//url = url.substring(urlLength);
loadAjax(url, destination, "GET");
}
function loadAjax(link, destination, type)
{

	var base = baseUrl;
	var ajaxRequest;  // The variable that makes Ajax possible!
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong, browser may not support AJAX, make sure content is visible.
				document.getElementById(destination).style.visibility='visible';
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			_gaq.push(['_setAccount', 'UA-16758594-3']);
			_gaq.push(['_trackPageview', link.substring(31)]);
//			stripLinks(41);
			document.getElementById(destination).innerHTML = ajaxRequest.responseText;
			buildAjax ();
			document.getElementById('content').style.visibility='visible';
							if(link == base + "/ajax/contact.php") {
				initialize();
				}



		}
	}

	ajaxRequest.open("GET", link, true);
	ajaxRequest.send(null); 
}

function stripLinks (cutBy) {
    var links = document.getElementsByTagName("a");
    for (i=0; i<links.length; i++){
        var link = links[i];
		if(isParentLightbox(link)) { break; }
			if(link.onclick = undefined) {
		link.onclick = function () { loadPage(this, 'content'); return false; };
		link.href = link.href.substring(cutBy); 
			}
	}
}

function swapImage(image) {
	var url = image.src;
	var main = document.getElementById("productMainImage");
	image.src = main.src;
	main.src = url;
	document.getElementById("mainImageLink").href = url;
}


function updatePrice(select, prices, baseprice, label) {
/**
var base;
if(window.basePrice == null) {
window.basePrice = baseprice;
}

if(window.labelPrices == null) {
window.labelPrices = new Array();
}

	if(window.labelPrices[label] == null) {
	window.labelPrices[label] = 0;
	}

base = window.basePrice;

var newprice = parseFloat(base);
newprice -= window.labelPrices[label];

		for (var i = 0; i < select.options.length; i++) {
			var price = prices[i];
			if (select.options[i].selected){
			newprice += parseFloat(price);
			window.labelPrices[label] = price;
			} else {
			
			
			}
		}
	basePrice = newprice;
	newprice = newprice.toFixed(2);
	document.getElementById("productPriceTwo").innerHTML ="&pound;" + newprice;
	return;
*/

var additionsPrice = 0;

	for(count = 0; count < jQuery("select option:selected").length; count ++) {
	curOpt = jQuery("select option:selected")[count];
	additionsPrice += parseFloat(jQuery(curOpt).attr( 'rel' ));
	}
	
	var totalPrice = parseFloat(baseprice) + parseFloat(additionsPrice);

	totalPrice = parseFloat(totalPrice).toFixed(2);
	document.getElementById("productPriceTwo").innerHTML ="&pound;" + totalPrice;

}


