// cufon elements to replace
Cufon.replace("h1");
Cufon.replace("h2");
Cufon.replace("h3");
Cufon.replace("h4");
Cufon.replace("div#keyOverview h2");

Cufon.replace("div#subIntros a");
Cufon.replace("div#header a", { hover: true });
Cufon.replace("div#subNavigation a", { hover: true });
Cufon.replace("div#navContent ul a", { hover: true });

Cufon.replace("div#innerImage a:not(.noCufon)");
Cufon.replace("div#productLanding a");

Cufon.replace("a.bookCourse");

Cufon.replace("div#finderContent form fieldset legend");

Cufon.replace(".bliss", { hover: true });


/** jQuery defaultText plugin **/
$.fn.defaultText = function(inText){
                return this.each(function(){
                                var self = $(this);
                                var theText = inText;
                                
                                if (self.val() == '' || self.val() == theText) self.css({color:'#b3b3b3'}).val(theText);
                                
                                self.focus(function(){ if (self.val() == theText) self.css({color:'#676767'}).val(''); });
                                self.blur(function(){ if (self.val() == '' || self.val() == theText) self.css({color:'#b3b3b3'}).val(theText); });
                });
};


$(document).ready(function() {
						   
		

		

 $("ul.thumb_team img").each(
    function(intIndex){
		$("ul.thumb_team div").css({'display' : 'none'});
		$("div.infoThumb").css ({'position' : 'absolute', 'left' : '0px', 'top' : '180px'});	
		
		var timer;

		$(this).mouseover(
            function(){
				$("div.infoThumb").hide();
				$("ul.thumb_team img").animate({opacity:1 },0);
				
                $("ul.thumb_team img").not(this).animate({opacity:0.4 },0);
                $(this).siblings().show();
    		}
        );
    }
);
 
	
	// get the last element and .lastElement (stops extra padding on the slice device)
	$("div#firstContent > :last").addClass("lastElement"); 
	$("div#secondContent > :last").addClass("lastElement");
	
	$("div#subNavigation > :last").addClass("lastElement");
	

	// make sure the slice device equals nav height if content isn't as tall
		// first content is on the page
		if ($("#firstContent, #innerImage").length > 0 && $("#subNavigation ul").length > 0) {
			var contentHeight = $("#firstContent, #innerImage").height() + 61; // get the height of first content + padding
			var lastNavPos = $("#subNavigation a:last").position(); // position of last link in the navigation
	
			lastNavPos = Math.round(lastNavPos.top) + 10; // pos from top of window, minus height of header, plus height of link
			
			if (lastNavPos > contentHeight) { // nav is taller than firstContent
				$("div#firstContent, #innerImage").css("height", lastNavPos - 61); // set the height of firstContent, minus padding top
			}
			//console.log(contentHeight);
			//console.log(lastNavPos);
		}
	
	// toggle content
		$("div.toggleContent p:not(:first)").hide();
		$("div.toggleContent").css("padding-bottom", "8px");
		$("div.toggleContent:last").css("padding-bottom", "0");
		$("div.toggleContent p").css("margin-bottom", "0");
		
		$("div.toggleContent h2").hover(function () {
        	$(this).css("cursor", "pointer");
      	});
		
		$("div.toggleContent h2").click( function() {
				$("div.toggleContent p").hide();
				$(this).next().show();
		});
		
	// move the equipment div bg
		$("div.equipmentCards").hover( function() {
			$(this).css("background-position", "0 -136px");									
		}, function() {
			$(this).css("background-position", "0 0");	
		});
	
	// ZOOM PRODUCTS
		// if the product zoomer is present
		if ($("div#zoomer").length > 0) {
			// set up vars
			var moveWidth; 
			var moveHeight;
			
			var zoomerPos; // pos of zoomer on screen
			
			var zoomerWidth = $("div#zoomer").width(); // width of zoomer area
			//var zoomerHeight = $("div#zoomer").height(); // height of zoomer area
            var zoomerHeight = 300;
		}
		$("div#zoomer img#mainProduct").load( function() {	
													   
			$("img#loadingIcon").css("display", "none"); // hide the loading icon
			$("div#zoomer img#mainProduct").fadeIn("slow"); // fade in the high res image
			
			moveWidth = ($("div#zoomer img#mainProduct").width() - zoomerWidth) / zoomerWidth;
			moveHeight = ($("div#zoomer img#mainProduct").height() - zoomerHeight) / zoomerHeight;
			
			zoomerPos = findXY($("div#zoomer")[0]); // pos of zoomer on screen
		});
		
		// when the mouse is moved over the zoomer area
		$("div#zoomer").mousemove(function(e){
			// if its the highres image
			if( $("div#zoomer img#mainProduct").width() > zoomerWidth) {
				// maths to calculate the pos of the image in relation to the mouse
				var newHeightPos = Math.round((e.clientY + $(window).scrollTop() - zoomerPos.top) * moveHeight) * -1;
				var newWidthPos = Math.round((e.clientX + $(window).scrollLeft() - zoomerPos.left) * moveWidth) * -1;
				
				// move the image
				$("div#zoomer img#mainProduct")	.css("top", newHeightPos + "px")
												.css("left", newWidthPos + "px");
			}
		});
	
		// click the zoom link or the lowres image...
		$("a#zoomLink, div#zoomer").click(function () { 
			// zoom in
			if( $("div#zoomer img#mainProduct").width() < zoomerWidth) {
				
				// show the loading icon
				$("img#loadingIcon").css("display", "block");
				
                $('#zoomer').animate({height:300},"fast",
                    function(){
                        // get the current filename and remove the extension
                        var currentImg = $("div#zoomer img#mainProduct").attr("src").slice(0, -4);
                        // swap it for the high res version
                        var newImg = currentImg + "-highres.jpg";
                        // swap the image
                        $("div#zoomer img#mainProduct").attr("src", newImg);
                        // hide it until it's loaded
                        $("div#zoomer img#mainProduct").css("display", "none");
                        
                        // setup the tooltip
                        var tooltip = "<span id=\"zoomInfo\">move your mouse over the image</span>";
                        // show the tooltip
                        $(tooltip).insertBefore("a#zoomLink");
                        
                        // add classes
                        $("a#zoomLink").addClass("zoomOut");
                        $("div#zoomer").addClass("zoomActive");
                        
                        // remove classes
                        $("a#zoomLink").removeClass("zoomIn");
                        $("div#zoomer").removeClass("showHand");
                        
                        // reset the pos absolute values
                        $("div#zoomer img#mainProduct")	.css("bottom", "auto")
                                                        .css("right", "auto")
                                                        .css("top", "0")
                                                        .css("left", "0");
                    }
                );
			// zoom out
			} else {
				// get the filename and remove highres
				var currentImg = $("div#zoomer img#mainProduct").attr("src").slice(0, -12);
				// swap it for the low res version
				var newImg = currentImg + ".jpg";
				
				// swap the image
				$("div#zoomer img#mainProduct").attr("src", newImg);
				// hide it
				$("div#zoomer img#mainProduct").hide();
                
                $('#zoomer').animate({height:153},"fast",
                    function(){
                        
                        // fade it in
                        $("div#zoomer img#mainProduct").fadeIn("slow");
                        
                        // remove the tooltip
                        $("span#zoomInfo").remove();
                        
                        // add classes
                        $("a#zoomLink").addClass("zoomIn");
                        $("div#zoomer").addClass("showHand");
                        
                        // remove classes
                        $("a#zoomLink").removeClass("zoomOut");
                        $("div#zoomer").removeClass("zoomActive");	
                        
                        // reset the pos absolute values
                        $("div#zoomer img#mainProduct")	.css("top", "auto")
                                        .css("left", "auto")
                                        .css("bottom", "0")
                                        .css("right", "0");
                    });
			}
			
			return false;	
		});
        
        if ($('#collectionContainer')){
            var startPos = 0;
            $('#collectionThumbnails img').css('cursor','pointer').css('position','absolute').each(
                function(i){
                    $(this).css('left',startPos * 97);
                    startPos++;
                }
            );
            $('#collectionThumbnails img').mousemove( function(){ $(this).css('opacity',1); } );
            $('#collectionThumbnails img').mouseout( function(){ $(this).css('opacity',0.4); } );
            $('#collectionThumbnails').css({'overflow':'hidden','height':79,'width':676}).hover(
                function(){ $(this).find('img').fadeTo('fast',0.4); },
                function(){ $(this).find('img').fadeTo('slow',1); }
            );
            setInterval(
                function(){
                    $('#collectionThumbnails img').each(
                        function(){
                            if (parseInt($(this).css('left')) % 97 != 0){
                                var offset = parseInt($(this).css('left')) % 97;
                                $(this).css({'left':'-='+offset+'px'});
                            }
                            $(this).animate({"left":"-=97px"},"slow","swing",function(){
                                if (parseInt($(this).css("left")) < -100){
                                    var maxX = 0;
                                    $('#collectionThumbnails img').css('position','absolute').each(
                                        function(){
                                            if (parseInt($(this).css('left')) > maxX) maxX = parseInt($(this).css('left'));
                                        }
                                    )
                                    $(this).css('left', maxX + 97);
                                }
                            });
                        }
                    );
                },2000
            );
            $('#collectionThumbnails img').click(
                function(e){
                    $('#collectionMainPicture img').unbind('load');
                    var oldImage = $('#collectionMainPicture img').attr('src');
                    
                    var splitPos = parseInt($(this).attr('rel'));
                    splitPos = (splitPos > 0) ? splitPos : 160;
                    
                    $('#collectionMainPicture div').css('backgroundImage','url('+oldImage+')');
                    $('#collectionMainPicture #left').css({'width':splitPos});
                    $('#collectionMainPicture #right').css({'width':676 - splitPos});
                    
                    var imgSrc = $(this).attr('src');
                    var glue = (imgSrc.indexOf('-') > 0) ? '-' : '_';
                    var picName = imgSrc.split(glue)
                    var largeName = picName[0]+glue+'large'+glue+picName[2];

                    $('#collectionMainPicture img').attr('src',largeName).load(
                        function(){
                            $('#collectionMainPicture #left').animate({'width': 0},'slow','swing');
                            $('#collectionMainPicture #right').animate({'width': 0},'slow','swing');
                        }
                    );
                }
            );
        }
	
    // Academy hide schedules
    $('.schedulelink').each(
        function(){
            $(this).next().hide();
            var togLink = $('<div>Click Here For Course Dates</div>'); 
            togLink.toggle( 
                function(){$(this).next().show('slow');$(this).html('Click Here To Hide Course Dates');},
                function(){$(this).next().hide('fast');$(this).html('Click Here For Course Dates');}
            ).css({cursor:'pointer',marginBottom:'20px'}).hover(
                function(){$(this).css({textDecoration:'underline'});},
                function(){$(this).css({textDecoration:'none'});}
            );
            $(this).after(togLink);
        }
    );
	
	// Salon Finder
	var location = $('#fldLocation');
	var locationDefault = 'City / Zip / Postcode';
	if(location != null)
	{
		if(location.val() == '') location.val(locationDefault);
		location.bind('focus', function(){if(location.val() == locationDefault) location.val('');});
		location.bind('blur', function(){if(location.val() == '') location.val(locationDefault);});
	}


    if ($('.bookCourse').length > 0){
        var printPageLink = $('<a href="#" class="printPageLink">Print this page</a>').click(function(){window.print();});
        
        $('#secondContent').append(printPageLink);
        $('#secondContent').append($('.bookCourse').remove());
    }
	
	
});

function findXY(obj) {
    var curX = 0;
    var curY = 0;
    if (obj.offsetParent) {
        do { curX += obj.offsetLeft; curY += obj.offsetTop; } while (obj = obj.offsetParent);
    }
    return {left: curX, top: curY};
}
