// Jquery no conflict mode
$j = jQuery.noConflict();

/* ******************************************************************************************
 * Bootstrap
 * ******************************************************************************************/
$j(document).ready(function() {
	
	InitSlider();
	
	InitWidgetSlider();
	
	InitHeaderEffects();
	
	InitTabs();
});

/* ******************************************************************************************
 * AnythingSlider Init
 * ******************************************************************************************/
function InitTabs(){
	$j('.slider-tabs > div.hc1').first().addClass('active');
	try{
		Cufon.refresh();
	}catch(e){}
	
	$j('.slider-tabs > div').click(function(){
		$j('.slider-tabs > div').removeClass('active');
		$cl = $j(this).attr('class');
		//alert($cl);
		$j('.header-container').css('display','none');
		$j('.header-container.'+$cl).css('display','block');
		$j('.slider-tabs > div.'+$cl).addClass('active');
		Cufon.refresh();
	});
	
	//$j('.slider-container > ').each
	
	$j('.loader').css('display','none')
}

function InitSlider() {
	if($j('body').has('ul.slider').length){

	  var delay = parseInt($j('.slider-delay').html());
	  var animTime = parseInt($j('.slider-animTime').html());
	  var sliderHeight = parseInt($j('.slider-height').html());
          
	  //alert(delay);

	  var actualSlide = 0;

	  $j('.slider').css("width","100%");
	  $j('.slider').css("height",sliderHeight+"px");
	  
	  $j('.header-container ').first().css('display','block');
	  var t = 1;
	  
	 $j('.header-container').each(function(){
		$j(this).addClass('hc'+t++);
	 });
	  
	  $j('.slider').anythingSlider({
	  	  easing: 'linear',
	  	  autoPlay: true,
	  	  pauseOnHover: true,
	  	  autoPlayLocked: true,
	  	  resumeOnVideoEnd    : true,
	  	  autoPlayDelayed     : true,
		  delay               : delay,
		  animationTime       : animTime,
		  resumeDelay         : delay,
		 onInitialized       : function(e, slider){
			
			var newBackground = slider.$currentPage.find("div.background-image").text();
			if(newBackground){
				$j(".main-background-slider-top").show();
				$j(".main-background-slider-bottom").show();
				$j(".main-background-slider-top img").attr("src",newBackground);
			}
			if($j('.slider li.activePage').hasClass('video')){
    	 		$j(".anythingSlider .anythingControls").css("bottom","45px");
    	 	} else {
    	 		$j(".anythingSlider .anythingControls").css("bottom","10px");
    	 	}
    	 	
		 },
    	 onSlideInit         : function(e, slider){
    	  	// set bottom background image
    	  	var newBackground = slider.$targetPage.find("div.background-image").text();
			if(newBackground){
				$j(".main-background-slider-bottom").show();
				$j(".main-background-slider-bottom img").attr("src",newBackground);
				$j(".main-background-slider-top img").fadeOut(animTime);
				$j(".main-background-slider-bottom img").fadeIn(animTime);

			} else {
				$j(".main-background-slider-top img").fadeOut(animTime);
				$j(".main-background-slider-bottom").hide();
				$j(".main-background-slider-top").show();
			}
			
    	 },
    	 onSlideComplete     : function(slider){
			// set top background image
			var newTopBackground = slider.$currentPage.find("div.background-image").text();
			if(newTopBackground){
				//$j(".main-background-slider-bottom").show();
				$j(".main-background-slider-top img").attr("src",newTopBackground);
				$j(".main-background-slider-top").show();
				$j(".main-background-slider-top img").fadeIn(100);
			} else {
				$j(".main-background-slider-top img").hide();
				$j(".main-background-slider-top").hide();
			}

    	 	if($j('.slider li.activePage').hasClass('video')){
    	 		$j(".anythingSlider .anythingControls").css("bottom","45px");
    	 	} else {
    	 		$j(".anythingSlider .anythingControls").css("bottom","10px");
    	 	}
    	 	
    	 }
	  });

	 // $j('div.anythingSlider .forward').hide();
	  //$j('div.anythingSlider .back').hide();
	  $j('div.anythingSlider .start-stop').hide();

	  $j('div.anythingSlider')/*.anythingSliderFx({
	 '.fade' : [ 'fade' ],
	 // '.selector' : [ 'caption', 'distance/size', 'time', 'easing' ]
	 // 'distance/size', 'time' and 'easing' are optional parameters
	 '.caption-top'    : [ 'caption-Top', '50px' ],
	 '.caption-topfull': [ 'caption-Top', '50px' ],
	 '.caption-right'  : [ 'caption-Right', '150px', '1000', 'easeOutBounce' ],
	 '.caption-bottom' : [ 'caption-Bottom', '50px' ],
	 '.caption-bottomfull': [ 'caption-Bottom', '50px' ],
	 '.caption-left'   : [ 'caption-Left', '130px', '1000', 'easeOutBounce' ],
	 '.expand'         : [ 'expand', '20%', '800', 'easeOutBounce' ],
	 '.fromTop'		 : [ 'top', '300px', '1500', 'easeOutElastic' ]
	})*/
	/* add a close button (x) to the caption */
	.find('div[class*=caption]')
	  .css({ position: 'absolute' })
	  .prepend('<span class="close">x</span>')
	  .find('.close').click(function(){
		var cap = $j(this).parent(),
		 ani = { bottom : -50 }; // bottom
		if (cap.is('.caption-top')) { ani = { top: -50 }; }
		if (cap.is('.caption-left')) { ani = { left: -150 }; }
		if (cap.is('.caption-right')) { ani = { right: -200 }; }
		cap.animate(ani, 400, function(){ cap.hide(); } );
	  });
	}
}
/*
 * Widget Slider
 */
function InitWidgetSlider() {
	if($j('body').has('ul#home-widget-slider').length){
		
		$j('ul#home-widget-slider').css("width","500px");
		$j('ul#home-widget-slider').css("height","300px");
		
		$j('ul#home-widget-slider').anythingSlider({
	  	  	autoPlay: true,
	  	  	pauseOnHover: true,
	  	    autoPlayLocked: true,
	  	    autoPlayDelayed: true,
	  	  	delay : 5000,
	  	  	animationTime : 300
		}).anythingSliderFx({
			'.fade' : [ 'fade' ]
		});
		
		$j('.widget-slider').show();
	}
}

function InitHeaderEffects() {
	if($j('.header-container').has('#cluster-container').length){
		
		cluster.density = parseInt($j('#cluster-density').text()); // frequency of circles
		cluster.radius = parseInt($j('#cluster-radius').text());   // the radius of the circles
		cluster.speed = parseInt($j('#cluster-speed').text());  // the speed of the fade in
		cluster.colors = [$j('#cluster-color-1').text(),$j('#cluster-color-2').text(),$j('#cluster-color-3').text(),$j('#cluster-color-4').text()]
		cluster.timeout =  -parseInt($j('#cluster-timeout').text());  //wait before animation timeout 

		if ( $j.browser.msie && ( parseInt($j.browser.version) == 6 || parseInt($j.browser.version) == 7 || parseInt($j.browser.version) == 8 )) {
  			$j('#to_cluster').css("text-indent","0px");
  			$j('#to_cluster').css('color',$j('#cluster-alternate-color').text());
  			Cufon.replace('#to_cluster');
		} else {
			cluster.init('to_cluster');
		}
	}
	if($j('.header').has('#punchout-container').length){
		
		punchout.image = $j('#punchout-image').text();
		
		/*if ( $j.browser.msie && ( parseInt($j.browser.version) == 6 || parseInt($j.browser.version) == 7 || parseInt($j.browser.version) == 8 )) {

		} else {*/
			punchout.init('to_punch');
		//}
	}
}