/***********************************************
***  BUTTON ROLLOVER & PRELOAD  ****************
************************************************/

$(function(){
	// button rollover
	if ($('.imgBtn') != null) {
		$('.imgBtn').hover(function(){
				if(!$(this).is('.disabled'))
					$(this).attr("src", $(this).attr("src").replace(/^(.*?)(?:-hover)?(\..*?)$/i,'$1-hover$2'));
			},
			function(){
					if(!$(this).is('.disabled'))
						$(this).attr("src", $(this).attr("src").replace(/^(.*)-hover(\..*?)$/i, '$1$2'));
				});
	}
	// preload image rollovers
	if ($("img.imgBtn, img[src^='images/buttons/btn'], input[src^='images/buttons/btn']") != null) {
	    $("img.imgBtn, img[src^='images/buttons/btn'], input[src^='images/buttons/btn']").each(function(){
			    $("<img>").attr("src", $(this).attr("src").replace(/(btn-.*)(\..*?)/i, '$1-hover$2'));
		    });
	}
});

/***********************************************
***  EXTERNAL LINKS  ***************************
************************************************/

$(document).ready(function() {
	$("a[rel=external]").each(function() {
		$(this).addClass('external');
		$(this).attr({
			title: "Open this page in a new window",
			target: "_blank"
		});
	})
});

/***********************************************
***  COMPACT FORM (LABEL OVER INPUT)  **********
************************************************/

$(function(){
	$('form.compact input, form.compact select, form.compact textarea').focus(function(){
			$('label[for=' + $(this).attr('id') + ']').hide();
		});
	$('form.compact input, form.compact select, form.compact textarea').each(function(ele){
			if($(this).val().length > 0)
				$('label[for=' + $(this).attr('id') + ']').css({ display: "none" });
		});
	$('form.compact input, form.compact select, form.compact textarea').blur(function(){
			if($(this).val().length == 0)
				$('label[for=' + $(this).attr('id') + ']').show();
		});
	$('form.compact').removeClass('compact');
});

/***********************************************
***  FOOTER SLIDING  ***************************
************************************************/

$(function(){
	$('#footerLinks ul a').hover(function(){
			$(this).stop().animate({
					paddingLeft: 8
				}, 200);
		},
		function(){
				$(this).stop().animate({
						paddingLeft: 0
					}, 200);
			});
});

/***********************************************
***  TAB BUTTON SLIDING  ***********************
************************************************/

$(function(){
	$('.infoTile .desc a, .blockList .viewMore, .featuredProducts .seeRange').hover(function(){
			$(this).stop().animate({
					top: 0
				}, 200);
		},
		function(){
				$(this).stop().animate({
						top: -7
					}, 200);
			});
});

/***********************************************
***  TAB BUTTON SLIDING  ***********************
************************************************/

$(function(){
	$('.productSelection .viewMore').hover(function(){
			$(this).stop().animate({
					top: 173
				}, 200);
		},
		function(){
				$(this).stop().animate({
						top: 168
					}, 200);
			});
});

/***********************************************
***  BLOCK LIST BUTTON SLIDING  ****************
************************************************/
/*
$(function(){
	$('.blockList .viewMore').hover(function(){
			$(this).stop().animate({
					top: 0
				}, 200);
		},
		function(){
				$(this).stop().animate({
						top: 168
					}, 200);
			});
});*/

/***********************************************
***  HOME PAGE BANNER ROTATIONS  ***************
************************************************/

$(function(){
	var delay = 4000;
	var fadeTime = 500;

	var curr = 1;

	function intFunc(){
		var prev = curr;
		curr++;

		if (curr === 6){
			curr = 1;
		}
		$('#homeHero > li:nth-child(' + prev + ')').fadeOut(fadeTime);
		$('#homeHero > li:nth-child(' + curr + ')').fadeIn(fadeTime);

	}
	var interval = window.setInterval(intFunc, delay);
});

/***********************************************
***  FADE IN BOX  ******************************
************************************************/

$(function(){
	// nav functionality
	$(".changeCountry").click(function(){
			if($(this).parent().hasClass('expanded')) {
				$(this).parent().removeClass('expanded');
				$(this).parent().addClass('collapsed');
				$(".loc > ul").fadeOut("fast");
			}
			else if($(this).parent().hasClass('collapsed')) {
				$(this).parent().addClass('expanded');
				$(this).parent().removeClass('collapsed');
				$(".loc > ul").fadeIn("fast");
			}
		});
});

/***********************************************
***  CUFON  ************************************
************************************************/

$(function() {
	Cufon.replace('#homeHeader h1');
	Cufon.replace('#homeHeader p');
	Cufon.replace('#right h1');
	Cufon.replace('#right h2');
	Cufon.replace('.locations .col h3');
	Cufon.replace('.locations h2');
    	Cufon.now();
});

/***********************************************
***  HOME PAGE TILES  **************************
************************************************/

$(document).ready(function() {

	$("#homeTiles li").hover(function() { 

		var thumbOver = $(this).find("img").attr("src");
		$(this).find("a").css({'background' : 'url(' + thumbOver + ') no-repeat center bottom'});
		$(this).find("span").stop().fadeTo('fast', 0 , function() {
			$(this).hide()
		});
	} , function() { 
		$(this).find("span").stop().fadeTo('fast', 1).show();
	});

});
