$(document).ready(function() {
	
	// form button hovers
		$("input.hoverPath").hover( function() {
			var hoverPath = $(this).attr("src"); // get current image path
			var hoverPath = hoverPath.substring(0, hoverPath.length-4); // strip last 4 chars (.gif)
			
			$(this).attr("src", hoverPath + "-hover.gif"); // add path to -hover.gif
		}, function() {
			var hoverPath = $(this).attr("src"); // get current hover image path
			var hoverPath = hoverPath.substring(0, hoverPath.length-10); // strip last 10 chars (-hover.gif)
			
			$(this).attr("src", hoverPath + ".gif"); // show original image
		});

	// homepage accordion
	
		// hide info text
		$("div.eaReveal", this).hide();
	
		// each accordion info area
		$("div.eaInfo").each( function(i) {
			
			// add toggle link
			var currentHeader = $("h3", this).text();
			var toggleLink = '<a class="eaToggle tk-adelle" href="#" title="'+ currentHeader +'">'+ currentHeader +'</a>';
			$(this).prepend(toggleLink);
				
		});
		
		// reveal text...
  		$("a.eaToggle").click( function() {
  			
  			$("div.eaReveal").slideUp("slow"); // hide any currently active
  			$("a.eaToggle").removeClass("active"); // remove all active classes
  			
  			// if current clicked text area is hidden... show it
  			if ($(this).next().is(":hidden")) {
  				$(this).next().slideDown("slow"); // show clicked	
  				$(this).addClass("active"); // set active class
  			}
  			
  			return false;
  		});
	
	// clear search field on focus
	
		// get original value
		var formDefaultValue = $("input#siteSearch").val();
		
		// on focus, if default value clear input
		$("input#siteSearch").focus( function() {
			if ($(this).val() == formDefaultValue) {
				$(this).val("");
			}
		});
		
		// on blur, if not text entered reset to default
		$("input#siteSearch").blur( function() {
			if ($(this).val() == "") {
				$(this).val(formDefaultValue);
			}
		});
		
	// main nav dropdowns
		
		// give li a .over class for IE & keep hover state on parent link
		$("div#header ul#detailNav li").hover( function() {
    		$(this).addClass("over");
    		$("a:first", this).addClass("highlight");
  		}, function () {
    		$(this).removeClass("over");
    		$("a:first", this).removeClass("highlight");
  		});
  		
  	// product detail print
  		
  		// add print link here as without .js it won't work
  		$("ul.productTools li.subTools").before('<li><a class="pageNav-print" href="#print" title="Print page">Print page</a></li>');
  		
  		// print!
  		$("a.pageNav-print").click(function() {
  			window.print();
  			return false;
 		});
  		
  	// product detail zoom
  	
  		$("div#productImage a").lightBox(); // activate

	// product detail tabs
		
		// give first tech item link selected state
		$("ul#techNav li:first a").addClass("selectedTech");
		
		// hide all but first breakdown & hide all headers
		$("div#productTech-inner div.breakdown:not(:first)").hide();
		$("div#productTech-inner div.breakdown h3").hide();
		
		// disable outline focus on tabs
		$("ul#techNav li a").css("outline", "0");
		
		// click a new tab
		$("ul#techNav li a").click( function() {
			
			// remove all selected classes & add new selected
			$("ul#techNav li a").removeClass("selectedTech");
			$(this).addClass("selectedTech");
			
			// get anchor of clicked link & create div ref
			var divRef= $(this).attr("href");
			divRef = "div" + divRef;
			
			// hide all breakdowns and show clicked one
			$("div#productTech-inner div.breakdown").hide();
			$(divRef).show();
			
			return false;
		});
		
	// checkout steps
	
		// basket...
			
			$("div#newCustomers").hide(); // hide new customer login
			$("div.userType").show(); // show new / existing options
			$(".utHeader").hide(); // hide headers
			
			// toggle new user form
			$(".showNew").click(function() {
				$("div#newCustomers").show();
				$("div#existingLogin").hide();
			});
			
			$(".showExisting").click(function() {
				$("div#newCustomers").hide();
				$("div#existingLogin").show();
			});
			
			// show new user form if radio is checked (user has submitted form)
			if ($(".showNew").is(":checked")) {
				$("div#newCustomers").show();
				$("div#existingLogin").hide();
			}
			
		// shipping / billing address...
		
			$("div#shippingAddress").hide(); // hide shipping address
			$("div.shipOptions").show(); // show shipping options
			$(".shipHeader").hide(); // hide header
			
			// toggle shipping form
			$("#shipDifferent").click(function() {
				$("div#shippingAddress").show();
			});
			
			$("#shipSame").click(function() {
				$("div#shippingAddress").hide();
			});
			
			// show new user form if radio is checked (user has submitted form)
			if ($("#shipDifferent").is(":checked")) {
				$("div#shippingAddress").show();
			}
			
	// product filter select
		
		$("div.filterItems input").hide(); // hide filter submit buttons...
		
		// submit filter form when select is updated
		$("div.filterItems select").change(function() {
    		$(this).closest("form").submit();
		});
		
	// order history
	
		// make entire tr clickable
		$("table#orderHistory tr.ohRow").click(function() {
  			window.location = ($("td.ohAction a", this).attr("href"));
		});
		
		// give it a nice hover hand and change the bg colour
		$("table#orderHistory tr.ohRow").hover(function () {
    		$(this).addClass("hoverIt");
    		$("td", this).css("background", "#F2F2F0");
		}, function () {
    		$(this).removeClass("hoverIt");
    		$("td", this).css("background", "#FFF");
  		});
  		
  	// Twitter footer

		// pull JSON data from twitter API 
		// !!!! ID WILL NEED UPDATING TO THE DRAGON AUDIO ACCOUNT !!!!
		$.getJSON("http://twitter.com/statuses/user_timeline.json?screen_name=dragon_audio&count=3&callback=?", function(data){
		
			var showTweets = ""; // setup tweet list
			
			// cycle through tweets
			$.each(data, function(i,item){
				
				// grab the tweet text
				var tweetContent = item.text;
				console.log(item.created_at);
				
				// convert URLs to clickable
				tweetContent = tweetContent.replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig, "<a href=\"$1\" title=\"$1\" target=\"_blank\">$1</a>");
				// convert twitter @s too
				tweetContent = tweetContent.replace(/(^|\s)@(\w+)/g, "$1@<a href=\"http://www.twitter.com/$2\" title=\"@$2\" target=\"_blank\">$2</a>");
				
				// apend tweet to list
   				showTweets += "<div class=\"tweet\"><p>" + tweetContent + "</p></div>";
   				
			});
			
			// if we have tweets... show them
			if(showTweets != "") { $("#tweetHolder").html(showTweets); }
		});

});

$(window).load(function() {
	
	// homepage slider
 	$("div.homeBanner-active").nivoSlider({
        effect:"random", // Specify sets like: 'fold,fade,sliceDown'
        pauseTime: 5000, // How long each slide will show
        directionNavHide:false // Only show on hover
    });
    
    // homepage slider sub
 	$("div.homeBanner-sub-active").nivoSlider({
        effect:"fade", // Specify sets like: 'fold,fade,sliceDown'
        directionNavHide:false, // Only show on hover
        controlNav:false, // 1,2,3... navigation
        keyboardNav:false, // Use left & right arrows
        manualAdvance:true, // Force manual transitions
        animSpeed:250 // Slide transition speed
    });
    
    // get image total, so we can auto centre the jump links
	var bannerImages = $("div.homeBanner-active img").length;
	var controlWidth = bannerImages * 17;
	$("div.homeBanner-active div.nivo-controlNav").css("width", controlWidth);

});

