//web root vaqr
var theWebRoot = "http://www.madebysix.com/six_client_hold/bhh/test";
//nav
$(document).ready(function () {
	var ul = $('#catnav');
	var li = $('#catnav li a');
	//this adds the onlick function to all the a tags in the nav
	li.click(function () {
		//then finds out if the parent is one of the three that needs the drop down reveal
		if ($(this).parent().attr("id")=="cncom" || $(this).parent().attr("id")=="cncon" || $(this).parent().attr("id")=="cnamg") {
			//if it does it increses the nav, adds a class for styling (also removing it from the others so only one at a time is clicked then returns false so the link isn't followed
	  		$('#nav').animate({
				height: '89px'
			}, 350);
	  		li.parent().removeClass('clicked');
	  		$(this).parent().addClass('clicked');
	  		return false;
		} else {
			//if it isn't one of those 3 it does nothing
		};
	});
});
//news ticker
$(document).ready(function(){
	var first = 0;
	var speed = 500;
	var pause = 6000;
	function removeFirst(){
		first = $('#newsticker ul li:first').html();
		$('#newsticker ul li:first').fadeOut('slow', function() {$(this).remove();});
		$('#newsticker ul li:nth-child(2)').fadeOut('slow');
		$('#newsticker ul li:nth-child(2)').fadeIn('slow');
		addLast(first);
	}
	function addLast(first){
		last = '<li style="display:none">'+first+'</li>';
		$('#newsticker ul').append(last)
	}
	interval = setInterval(removeFirst, pause);
});
//itemImageLink rollover
$(document).ready(function(){
	$("a.itemImageLink").hover(
		function () {
			$("img", this).fadeTo("slow", 0.75);
		},
		function () {
			$("img", this).fadeTo("fast", 1);
		}
	);
});
//leftnav rollover
$(document).ready(function(){
	$("#content .col ul.leftnav li a").fadeTo(10, 0.5); // set the lighter text on all nav items
	$("#content .col ul.leftnav li.selected a").fadeTo(10, 1.0); // the change the selected to the darker colour
	//only add the hover function if it isn't li.selected
	$("#content .col ul.leftnav li a:not(#content .col ul.leftnav li.selected a)").hover(function(){
		$(this).fadeTo("fast", 1.0); // darker on rollover
	},function(){
   		$(this).fadeTo("fast", 0.5); // lighter on rollout
	});
});

//item page image swap
//set the current image number
var current = 1;
$(document).ready(function(){
	var count = $("div.item-images ul").children().length;
	//get the number
	if (count>1) {
		//if there's more than one image add a div with prev/next buttons
		for (var i=1; i < count+1; i++) {
			var sel = "div.item-images li:nth-child("+[i]+") a";
			//add a class with the current number to each item, this makes it easy to set the current var
			$(sel).attr('class',i);
			//add the function to each thumnail
			$(sel).click(function () {
				//get the link
				theImgToLoad = $(this).attr("href");
				//find the big image
				theBigImageTag = $("div.large-item img");
				//set the big image src to the link
				theBigImageTag.attr("src", theImgToLoad);
				//unfade all the items, then fade the clicked item
				$("div.item-images ul li a").animate({opacity: 1}, 10);
				$(this).animate({opacity: 0.5}, 10);
				current = parseInt($(this).attr("class"));
				return false;
			});
		};
		//add a div that contains the previous/next buttons over the image
		$("div.large-item").append('<div><a href="#" id="image-prev">prev</a><a href="#" id="image-next">next</a></div>');
		//add functions to the prev/next buttons
		//prev
		$("#image-prev").click(function () {
			if (current==1) {
				var sel = "div.item-images li:nth-child("+count+") a";
				current = count;
			} else {
				var curminus1 = current-1;
				var sel = "div.item-images li:nth-child("+curminus1+") a";
				current = curminus1;
			}
			//get the link
			theImgToLoad = $(sel).attr("href");
			//find the big image
			theBigImageTag = $("div.large-item img");
			//set the big image src to the link
			theBigImageTag.attr("src", theImgToLoad);
			//unfade all the items, then fade the clicked item
			$("div.item-images ul li a").animate({opacity: 1}, 10);
			$(sel).animate({opacity: 0.5}, 10);
			return false;
		});
		//next
		$("#image-next").click(function () {
			if (current==count) {
				var sel = "div.item-images li:nth-child(1) a";
				current = 1;
			} else {
				var curplus1 = current+1;
				var sel = "div.item-images li:nth-child("+curplus1+") a";
				current = curplus1;
			}
			//get the link
			theImgToLoad = $(sel).attr("href");
			//find the big image
			theBigImageTag = $("div.large-item img");
			//set the big image src to the link
			theBigImageTag.attr("src", theImgToLoad);
			//unfade all the items, then fade the clicked item
			$("div.item-images ul li a").animate({opacity: 1}, 10);
			$(sel).animate({opacity: 0.5}, 10);
			return false;
		});
		//fade no. 1
		$("div.item-images ul li:first-child a").animate({opacity: 0.5}, 10);
		$("#image-next").hover(
			function () {
				$('#image-next').css("background-image", "url("+theWebRoot+"/i/btn-overimage-next-hov.png)");
			},
			function () {
				$('#image-next').css("background-image", "url("+theWebRoot+"/i/btn-overimage-next.png)");
			}
		);
		$("#image-prev").hover(
			function () {
				$('#image-prev').css("background-image", "url("+theWebRoot+"/i/btn-overimage-prev-hov.png)");
			},
			function () {
				$('#image-prev').css("background-image", "url("+theWebRoot+"/i/btn-overimage-prev.png)");
			}
		);
	} else {
		//otherwise just fade no. 1 and set href to #
		$("div.item-images ul li:first-child a").animate({opacity: 0.5}, 10);
		$("div.item-images ul li:first-child a").attr('href',"#");
	}
});

