/*------------------------------------------------------------------
[Global Javascript]

Copyright: Adam Foster - Codefor 2010
Client: Cherished Trinkets
Project: Cherished Trinkets Site
Created by: Adam Foster
Last Updated: 09 - 01 - 10
-------------------------------------------------------------------*/

Cufon.replace('h1:not(.welcome)', { hover: true })('h2', { hover: true });

$jq = jQuery.noConflict();

/* When dom is ready ----------------------------------------------*/

$jq(document).ready(function(){
	init();
	hoverStates();
	if( $jq("#banner").html() ) homeScroller();
});

/* Init -----------------------------------------------------------*/

function init(){
	$jq('body').addClass('js');
	$jq('a[rel="external"]').attr("target", "_blank");	
}

/* Nav hover states -----------------------------------------------*/

function hoverStates() {
		
	$jq('#nav li.page-item-7').append('<span></span>').hover(
		function(){
			$jq(this).children('ul').show();
		},
		function(){
			$jq(this).children('ul').hide();
		}
	);

}

/* Homepage image viewer -----------------------------------------*/

function homeScroller() {

	var o = {
		init : function() {
			o.banner = $jq('#banner');
			o.container = o.banner.children('#bannerImage');
			o.items = $jq('#bannerNav li a');
			o.fadeCount = 0;
			o.imgSrc = "";
			o.pageLink = "";
			o.inputDelayer = o.delayTimer(4500);
			o.inputDelayer(function(){o.monitor()})
		},
		monitor : function(){
			o.container.children('a').fadeOut('fast', function() {
				
				o.items.eq(o.fadeCount).parent().removeClass('sel');
				o.fadeCount++;
				if(o.fadeCount == 5) o.fadeCount = 0;
				o.items.eq(o.fadeCount).parent().addClass('sel');
				
				o.pageLink = o.items.eq(o.fadeCount).attr("href");
				o.imgSrc = o.items.eq(o.fadeCount).attr("rel");
				
				o.container.empty();
				o.container.append("<a href='" + o.pageLink + "'><img src='" + o.imgSrc + "'/></a>");
				o.container.css({opacity: 0}).stop().animate({opacity: 0},700).animate({opacity: 1},300);
				o.inputDelayer(function(){o.monitor()});
			});
		}, 
		delayTimer : function (delay) {
			 var timer;
			 return function(fn){
				  timer=clearTimeout(timer);
				  if(fn)
					   timer=setTimeout(function(){
					   fn();
					   },delay);
				  return timer;
			 }
		}
	}
	o.init();
	
}

/* Clear text -----------------------------------------------------*/

function clearText(thefield){
	if (thefield.defaultValue==thefield.value){
		thefield.value = "";
	}
}
function replaceText(thefield){
	if (thefield.value == ""){
		thefield.value = thefield.defaultValue;
	}
}