// *************************** jQuery Initialisation **************************** //


$(document).ready(function() {

	// hide the sub-menus
	$("#pri_nav ul").hide().css("top", "33px");
	// onclick - show the sub emnu
	$("#pri_nav li").hover( function () {
		$(this).find("ul").show();
	}, function () {
		$(this).find("ul").hide();
	} );
	// toggle the :hover class - for ie6 compatability
	$("#pri_nav ul li").hover( function () {
		$(this).addClass("hover");
	}, function () {
		$(this).removeClass("hover");
	} );

	$(".imghover").imghover();
	$("#changeCurrency img").imghover();
	$("#search_btn").imghover();
	$('.mailtoLink img').imghover();
	$('.viewNow img').imghover();

	// standard usage
	$('.mailto').mailtoObsfucator();
	$('.mailtoLink').mailtoObsfucator();

	$("#search_productCode").click(function() {
		if ($(this).is(":checked"))	{
			$("#search_ipt").val("enter product code...");
			$("#search").attr("action",relPath + "scripts/search_prod.asp");
		} else{
			$("#search_ipt").val("keyword search...");
			$("#search").attr("action",relPath + "scripts/search.asp");
		}
	});

	$('#navigation').fadeOut();
	$('#product').hover(function(){
		$('#navigation').fadeIn("slow");
	},function(){
		$('#navigation').fadeOut("slow");
	});

	jQuery(function($) {
		$("img[src$=png]").pngfix();
	});

	$(".snippet").hover(
		function(){ $(this).addClass("hover"); },
		function(){ $(this).removeClass("hover"); }
	);
	$(".snippet").click(function() { 
		window.location = $(this).find("h3 > a").attr("href"); 
		return false;
	});

	$(".listItem").hover(
		function(){ $(this).addClass("hover"); },
		function(){ $(this).removeClass("hover"); }
	);
	$(".listItem").click(function() { 
		window.location = $(this).find("h3 > a").attr("href"); 
		return false;
	});



	//Review Panel Display
	//References
	var linksToUse = $(".reviewLinks span");
	//Underline review action click links
	linksToUse.hover( function () {
		$(this).addClass("underline");
	}, function () {
		$(this).removeClass("underline");
	} );
	

	//Manage click events
	linksToUse.click(function(){
		var productId = document.getElementById("productReference").value;
		//load selected links
		switch(this.id){
			case "show3Reviews":
				var contentToReplace = $("#moreReviewsPanel");
				contentToReplace.slideUp();
				contentToReplace.load("../include/ajaxLoadReviews.asp", toggleReviews("Top3"));
				contentToReplace.slideDown();
				break;
			case "showAllReviews":
				var contentToReplace = $("#moreReviewsPanel");
				contentToReplace.slideUp();
				contentToReplace.load("../include/ajaxLoadReviews.asp?productId=" + productId, toggleReviews("All"));
				contentToReplace.slideDown();
				break;
			case "writeReview":
				var contentToReplace = $("#writeReviewPanel"); 
				contentToReplace.slideUp();
				contentToReplace.load("../include/ajaxWriteReview.asp?productId=" + productId + "&sectionId=" + document.getElementById("relationId").value + "&sectionType=" + document.getElementById("sectionType").value);
				contentToReplace.slideDown();
				break;
			default:
				break;
		}
	});

	//Question Panel Display
	//References
	var linksToUse2 = $(".showAskAQuestion");
	var contentToReplace2 = $("#questionPanel");

	//Manage click events
	linksToUse2.click(function(){
		//load selected links
		contentToReplace2.slideUp();
		contentToReplace2.load("../info/question.asp");
		contentToReplace2.slideDown();
	});

	/* 
	 * -----------------------
	 * HOMEPAGE TAB TIMER - Part 1 of 2
	 * -----------------------
	 */
	$('#tabNav span').hover(function(){
		$(this).addClass('hover');
	},function(){
		$(this).removeClass('hover');
	});
	$('#tabNav a').click(function() {
		// cancel timer
		window.clearInterval(tabTimer);
		// find the index of the clicked tab
		var clickedTab = $(this);
		// find its position within all the tabs
		var clickedIndex = $('#tabNav a').index(  clickedTab );
		// show the clicked on tab
		showTab( clickedIndex );
		// prevent the link from working
		return false;
	});
	/*
	 * HOMEPAGE TAB TIMER : end 
	 */


	 
	 /* 
	 * -----------------------
	 * HOMEPAGE NEW ARRIVALS CAROUSEL - Part 1 of 2
	 * -----------------------
	 */	 
		if(!maxPos) karlousel_init();
 		$("#karlousel_prev").click(function(){
			if(currentPos == 0) return false;
			currentPos--;
			var newPos = currentPos * moveAmount;
			$("#karlousel_slider").animate( { left: "-" + newPos + "px" }, 500 );
			checkNav();
			return false;
		});
		$("#karlousel_next").click(function(){
//			if(!maxPos) karlousel_init();
			if(currentPos + onView == maxPos) {
				return false;
			}
			currentPos++;
			var newPos = currentPos * moveAmount;
			$("#karlousel_slider").animate( { left:"-" + newPos + "px" }, 500 );
			checkNav();
			return false;
		});	
	 /*
	 * HOMEPAGE NEW ARRIVALS CAROUSEL : end 
	 */

	 
	// lazy loading of large animated banner on homepage
	// $("#banner_dj-flight-case").html('<a href="http://www.djflightcase.co.uk"><img src="pics/banner_dj-flight-case.gif" width="560" height="100" border="0" alt="DJ Flightcases" title="DJ Flightcases" /></a>');
	
	
	/* 
	 * -----------------------
	 * HOMEPAGE SELECTION OF CUSTOMERS - Part 1 of 2
	 * -----------------------
	 */
	setInterval( "slideSwitch()", 2800 );
	/*
	 * HOMEPAGE SELECTION OF CUSTOMERS : end 
	 */

	
});




/* 
 * -----------------------
 * HOMEPAGE TAB TIMER - Part 2 of 2
 * -----------------------
 * this needs to be outside of the onload events 
 */
/* 
 * findTab()
 * ---------------------
 * returns the index of the tab with the class of current
 */
function findTab() {
	// find the current tab
	var currentTab = $('#tabNav span.current a');
	// find its position within all the tabs
	var currentIndex = $('#tabNav a').index( currentTab );
	// alert('currentIndex='+currentIndex);
	return currentIndex;
}
/* 
 * nextTab()
 * ---------------------
 * finds the next tab, and passes its index to showTab()
 */
function nextTab() {
	// find the current tab
	var currentIndex = findTab();
	// find the next tab
	var nextIndex = currentIndex + 1;
	// if its passed the end, restart at the first one
	if(nextIndex >= $('#tabNav a').length) {
		nextIndex = 0;
	}
	// trigger the click event, to show the next tab
	//$('#tabNav a:eq(' + nextIndex + ')').trigger("click");
	// show the correct tab
	showTab(nextIndex);
}
/* 
 * showTab(tabIndex)
 * ---------------------
 * displays the tab at the position tabIndex
 */
function showTab(tabIndex) {
	// un-hilight all tabs
	$('#tabNav span').removeClass('current');
	// hilight the current tab
	$('#tabNav span:eq('+tabIndex+')').addClass('current');
	// hide all panels
	$('#tabContainer .panel').addClass('hide');
	// show current panel
	var thisContainer = $('#tabContainer .panel:eq('+ tabIndex +')');
	/* no longer using the ajax loading
	if( !thisContainer.hasClass('loaded') ){
		thisContainer.load('include/ajaxHomeBanner.asp', { 'bannerToShow' : tabIndex }, function(){
			// initialise the carousel, once its loaded
			if(tabIndex == 3){
				$('#mycarousel1').jcarousel({ 
					scroll: 1
				});
			}
		}).addClass('current').addClass('loaded');
	}
	*/
	// show the current container
	thisContainer.removeClass('hide');
}
// start the timer running
var tabTimer = setInterval("nextTab()",5000);
/*
 * HOMEPAGE TAB TIMER : end 
 */



/* 
 * -----------------------
 * HOMEPAGE SELECTION OF CUSTOMERS - Part 2 of 2
 * -----------------------
 * this needs to be outside of the onload events 
 */
function slideSwitch() {
	// console.log("slideSwitch");
	var $active = $('#fader div.active');
	if ( $active.length == 0 ) $active = $('#fader div:last');
	var $next =  $active.next().length ? $active.next() : $('#fader div:first');
	$active.addClass('last-active');
	$next.css({opacity: 0.0})
			.addClass('active')
			.animate({opacity: 1.0}, 1000, function() {
					$active.removeClass('active last-active');
			});
}
/*
 * HOMEPAGE SELECTION OF CUSTOMERS : end 
 */

 
 
 /* 
 * -----------------------
 * HOMEPAGE NEW ARRIVALS CAROUSEL - Part 2 of 2
 * -----------------------
 * this needs to be outside of the onload events 
 */
	var onView = 3; // number of items that can be seen at any one time
	var moveAmount = 150; // distance in px to move the items by
	var currentPos = 0; // always 0
	var maxPos = false; // total number of elements in the carousel - gets set on first run
	function karlousel_init() {
		maxPos = $("#karlousel #karlousel_slider .item").length;
		// console.log("maxPos: "+maxPos);
		if(currentPos + onView >= maxPos) {
			$("#karlousel_next").addClass("inactive");
		}
		// stretch the slider accordingly
		// $("#karlousel #karlousel_slider").css("width", (maxPos * moveAmount) + "px");
		$("#karlousel #karlousel_slider").css("width", "1000px");
	}
	function checkNav(){
		$("#karlousel_nav span").removeClass("inactive");
		if(currentPos + onView == maxPos) {
			$("#karlousel_next").addClass("inactive");
			return false;
		}
		if (currentPos == 0) {
			$("#karlousel_prev").addClass("inactive");
		}
		return false;
	}
 /*
 * HOMEPAGE NEW ARRIVALS CAROUSEL : end 
 */
