$(document).ready(function(){
	highlightNavigation("#nav a", function(){
		$(this).addClass("highlight");
	});
	
	if($.browser.msie && $.browser.version == "6.0"){
		$("ul li:first-child").addClass("first-child");
	}
});

function copyInfo(){

	document.getElementById("payment_first_name").value = document.getElementById("first_name").value;
	document.getElementById("payment_last_name").value = document.getElementById("last_name").value;
	document.getElementById("payment_address").value = document.getElementById("address").value;
	document.getElementById("payment_address2").value = document.getElementById("address2").value;
	document.getElementById("payment_town").value = document.getElementById("town").value;
	document.getElementById("payment_county").value = document.getElementById("county").value;
	document.getElementById("payment_postcode").value = document.getElementById("postcode").value;
	document.getElementById("payment_country_id").value = document.getElementById("country_id").value;
	
}

function initFeaturedSlider(){
	
	var sliderWindow = document.getElementById("featured-slider-window");
	
	var elements = [];
	$(".featured-product", sliderWindow).each(function(){
		elements.push(this);
	});
	var currentProductIndex = 0;
	
	var timeoutID = null;
	var runTimer = true;
	
	var slider = document.getElementById("featured-slider");
	
	var sliderControl = document.getElementById("featured-slider-control");
	$(sliderControl).empty();
	
	var prevButton = document.createElement("div");
	prevButton.id = "featured-slider-prev";
	prevButton.onclick = function(){
		runTimer = false;
		slideLeft((currentProductIndex -1 < 0 ? elements.length -1 : currentProductIndex - 1));
		return false;
	}
	sliderControl.appendChild(prevButton);
	
	var nextButton = document.createElement("div");
	nextButton.id = "featured-slider-next";
	nextButton.onclick = function(){
		runTimer = false;
		slideRight((currentProductIndex + 1) % elements.length);
		return false;
	}
	sliderControl.appendChild(nextButton);
	
	var featuredProduct = $(".featured-product:first", slider);
	
	function slideLeft(index){
		var nextFeaturedProduct = $(elements[index]).clone();
		featuredProduct.before(nextFeaturedProduct);
		$(slider).css("marginLeft", -featuredProduct.width() + "px");
		var position = nextFeaturedProduct.position();
		$(slider).stop(true, true).animate({
			marginLeft:"0px"
		}, function(){
			featuredProduct.detach();
			$(slider).css({marginLeft:"0px"});
			featuredProduct = nextFeaturedProduct;
			resetTimer();
		});
		currentProductIndex = index;
	}
	
	function slideRight(index){
		var nextFeaturedProduct = $(elements[index]).clone();
		featuredProduct.after(nextFeaturedProduct);
		var position = nextFeaturedProduct.position();
		$(slider).stop(true, true).animate({
			marginLeft:-position.left + "px"
		}, function(){
			featuredProduct.detach();
			$(slider).css({marginLeft:"0px"});
			featuredProduct = nextFeaturedProduct;
			resetTimer();
		});
		currentProductIndex = index;
	}
	
	function resetTimer(){
		if(timeoutID){
			clearTimeout(timeoutID);
			timeoutID = null;
		}
		if(!runTimer) return;
		timeoutID = setTimeout(function(){
			timeoutID = null;
			slideRight((currentProductIndex + 1) % elements.length);
		},7000);
	}
	
	resetTimer();
}

function parseAbsoluteURL(urlString) {
	var absoluteUrlPattern = /^([^:\/?#]+):\/\/(([^@\/?#]*)@)?([^:\/?#]*)(:([0123456789]*))?([^?#]*)(\?([^#]*))?(#(.*))?/g;
    var components = {};
    var a = absoluteUrlPattern.exec(urlString);
    absoluteUrlPattern.lastIndex = 0;
    if(!a) return null;
    components.scheme   = a[1];
    components.userinfo = a[3];
    components.hostname = a[4];
    components.port     = a[6];
    components.path     = a[7];
    components.query    = a[9];
    components.fragment = a[11];
    components.host = components.hostname;
    if(components.port) components.host += ":" + components.port;
    return components;
}

function isSubDir(subDir, parentDir) {
    return subDir.indexOf(parentDir) == 0;
}

function isParentDir(parentDir, childDir){
	var parentDir_dirs = parentDir.match(/[^\/]+/g) || [];
	var childDir_dirs = childDir.match(/[^\/]+/g) || [];
	if(!childDir_dirs.length || childDir_dirs.length - 1 != parentDir_dirs.length) return false;
	for(var i = childDir_dirs.length - 2; i >= 0; i--){
		if(childDir_dirs[i] != parentDir_dirs[i]) return false;
	}
	return true;
}

function highlightNavigation(selector, highlighter){
	function trimTrailingSlash(path){
		if(path.charAt(path.length - 1) == '/'){
			path = path.substr(0, path.length - 1);
		}
		return path;
	}
	var currentLocationPath = trimTrailingSlash(window.location.pathname);
	$(selector).each(function(){
		var href_comps = parseAbsoluteURL(this.href);
		if(!href_comps || href_comps.host != window.location.host) return;
		href_comps.path = trimTrailingSlash(href_comps.path);
		if(href_comps.path == currentLocationPath){
			highlighter.apply(this,[]);
			return;
		}
		if(currentLocationPath == '/' && href_comps.path.match(/^index\.(php|htm|html)$/g)){
			highlighter.apply(this,[]);
			return;
		}
		if(isParentDir(href_comps.path, currentLocationPath)){
			highlighter.apply(this,[]);
			return;
		}
	});
}

