// *************************** jQuery Initialisation **************************** //

$(document).ready(function() {

	$("#priNav img").imghover();
	$('.readMore img').imghover();
	$('a.mailto img').imghover();

	// mailto obsfucation
	$('a.mailto').mailtoObsfucator();

	// lightbox
	$("a[rel^=lightbox]").lightBox();

	if ( typeof(menu) !== "undefined") {
		// add the menus to the DOM
		for(key in menu) {
			var str = '<div class="subMenu">';
			var subMenu = menu[key];
			for(subKey in subMenu) {
				str += '<div><a href="' + subMenu[subKey] + '">' + subKey + '</a></div>';
			}
			str += '</div>';
			$("#" + key).append(str);
		}
		// show and hide the sub-menus on hover 
		$("#priNav .menu").hover( function () {
			$(this).find(".subMenu").show();
			t = $(this).find("img").attr("title"); // hide the images title as was
			$(this).find("img").attr("title", ""); // causing mouseout probs in safari
		}, function () {
			$(this).find(".subMenu").hide();
			$(this).find("img").attr("title", t); // restore the image title
		} );
		// add a hover class - because ie doesn't support 
		// css :hover styles on anything other than an anchor
		$("#priNav .menu .subMenu div").hover( function () {
			$(this).addClass("hover");
		}, function () {
			$(this).removeClass("hover");
		} );
		// trigger the image hover on the parent menu image
		$("#priNav .menu .subMenu").hover( function () {
			$(this).prev("a").find("img").trigger('mouseover');
		}, function () {
			$(this).prev("a").find("img").trigger('mouseout');
		} );
	}

	$("#margin #latestNews .news").click(function(){
		window.location = $(this).find("a:first").attr("href");
		return false;
	});

});


// NOTE this needs to be done on LOAD to ensure images are loaded last
$(window).load(function() {

	// equalise height of homepage columns
	var maxHeight = 0;
	$('#products .panel:even').each(function(){
		var thisHeight = $(this).height();
		var nextHeight = $(this).next().height();
		if(thisHeight > nextHeight){
			$(this).next().height(thisHeight + 'px');
		} else {
			$(this).height(nextHeight + 'px');
		}
	});

});



// *************************** KP GALLERY SLIDESHOW v1.1 [START BLOCK] **************************** //

// NOTE this needs to be done on LOAD to ensure images are loaded last
$(window).load(function() {

	/* 
	 * Setup
	 * -----------------------
	 */
	// load images from the href's of the navigation
	$('#mainPicNav > div:not(:first)').each(function(){
		// extract the data we need to build the other headers
		var linkHref = $(this).children('a.link').attr('href');
		var imgSrc = $(this).children('a.pic').attr('href');
		var imgAlt = $(this).children('a.pic').text();
		// clone the current header and change the relevant details
		$('#mainPicImg > a:first')
			.clone()
			.attr('href',linkHref)
			.children('img').attr({
				src: imgSrc,
				alt: imgAlt,
				title: imgAlt
			})
			.end()
			.removeClass("active")
			.appendTo('#mainPicImg');
	});
	// if there isn't an active class, add one onto the first item
	if ( $('#mainPicNav > div.active').length == 0 ) {
		$('#mainPicNav > div:first').addClass('active');
	}
	if ( $('#mainPicImg > a.active').length == 0 ) {
		$('#mainPicImg > a:first').addClass('active');
	}
	// bind click events to nav
	$('#mainPicNav > div > a.link').click(function() {
		thumbAction( $(this).parent('div') );
		return false;
	});

});


function thumbAction(jQ){
	// cancel timer and stop the auto rotation
	window.clearInterval(kpGalTimer);
	// find the position/order of the clicked nav-item
	var clickedIndex = $('#mainPicNav > div').index( jQ );
	// show the main item (also adds class to current nav)
	showImg( clickedIndex , 200);
	// prevent the link from working
	//return false;
}

/* 
 * findIndex()
 * ---------------------
 * returns the index of the main image with the class of current
 */
function findIndex() {
	// find the current active main image
	var currentNav = $('#mainPicImg > a.active');
	// find its position
	var currentIndex = $('#mainPicImg a').index( currentNav );
	return currentIndex;
}
/* 
 * nextImg()
 * ---------------------
 * finds the next image, and passes its index to showImg()
 */
function nextImg() {
	// find the current tab
	var currentIndex = findIndex();
	// find the next tab
	var nextIndex = currentIndex + 1;
	// if its passed the end, restart at the first one
	if(nextIndex >= $('#mainPicImg > a').length) {
		nextIndex = 0;
	}
	if(nextIndex == currentIndex) {
		// console.log('only 1 image found-killing rotation');
		window.clearInterval(kpGalTimer);
		return false;
	}
	// show the correct image
	showImg(nextIndex, 1000);
}
/* 
 * showImg(imgIndex)
 * ---------------------
 * displays the image and higlights the navigation at the position imgIndex
 */
function showImg(imgIndex, fadeSpeed) {
	// check if its already showing
	if( imgIndex == findIndex() ){
		// console.log('skipped showImg as this is already current');
		return false;
	}
	// un-hilight current nav
	$('#mainPicNav > div').removeClass('active');
	// hilight the current nav
	$('#mainPicNav > div:eq('+imgIndex+')').addClass('active');
	// grab references to the active and next-active images
	var $activeImg = $('#mainPicImg > a.active');
	var $nextImg = $('#mainPicImg > a:eq('+ imgIndex +')');
	// drop the active image to 2nd position in pile (nothing in top position)
	$activeImg.removeClass('active').addClass('last-active');
	// make the next image transparent
	// make it top of the pile
	// fade it up
	// remove the last active class
	$nextImg.css({opacity: 0.0})
		.addClass('active')
		.animate({opacity: 1.0}, fadeSpeed, function() {
			$activeImg.removeClass('last-active');
		});
}
// start the timer running
var kpGalTimer = setInterval("nextImg()",7500);

// *************************** KP GALLERY SLIDESHOW [END BLOCK] **************************** //

