 $(window).load(function(){
 	// setTimeout('mi_setup_page()', 5000);
 	mi_setup_page();
});


function hideYoutube() {
	$(".youtube-player").hide();
}
function showYoutube() {
	$(".youtube-player").show();
}


function mi_setup_page() {

	$("label").inFieldLabels();
	$(".colorbox").colorbox({transition:"none"});

	
	$(".form-go-button").mouseover(function() {
		this.src=WEBSITE_ROOT+'templates/en/template_images/form-go-button-on.jpg';
		});
	$(".form-go-button").mouseout(function() {
		this.src=WEBSITE_ROOT+'templates/en/template_images/form-go-button.jpg';
		}
		);
	
	
	
	showBannerSIFR();

	if ($("#news-stories")) {
		$("#news-stories").css("display", "block");
			$('#news-stories').cycle({ 
				 fx:     'fade', 
				 timeout: 0,
				 pager:  '#news-numbers',
				 delay:  -2000
				 ,before:hideYoutube
				 ,after:showYoutube
				 });
	}
	
	
	if ($("#banners1")) {
		$("#banners1").css("display", "block");
			$('#banners1').cycle({ 
				 fx:     'fade'
				 ,random: true
				 ,timeout: 0
				 ,delay:  -2000
				 ,prev:  '#banners1-prev'
				 ,next:  '#banners1-next'
				 ,after:afterBannerTransition
				 });

	}
	if ($("#banners2")) {
		$("#banners2").css("display", "block");
			$('#banners2').cycle({ 
				 fx:     'fade'
				 ,random: true
				 ,timeout: 0
				 ,delay:  -2000
				 ,prev:  '#banners2-prev'
				 ,next:  '#banners2-next'
				 ,after:afterBannerTransition
				 });


	}


	if ($("#features")) {
		$("#features").css("display", "block");
		$("#feature-dropdowns").css("display", "block");
			$('#feature-dropdowns').cycle({ 
				 fx:     'none', 
				 pager:  '#feature-numbers',
				 pagerClick:stopFeature,
				 timeout: 6000,
				 delay:  -2000,
				 after:afterFeatureDropdown
				 });

			$('#features').cycle({ 
				 fx:     'fade', 
				 timeout: 6000, 
				 delay:  -2000,
				 pager:  '#feature-numbers',
					pagerAnchorBuilder: function(i) {
						// select the anchor created previously
						return '#feature-numbers a:eq('+i+')';
						}
			});
		$("#feature-dropdowns").css("display", "none");
			
	 }

	
	
	
	
	if ($("#feature-arrow")) {
//		$("#feature-dropdowns").css("display", "block");
//		$("#feature-dropdowns").css("display", "none");
		 $('#feature-arrow').toggle(
		function () {
			$('#features').cycle("pause");
			$('#feature-dropdowns').cycle("pause");
			$("#feature-dropdowns").css("display", "block");

			showFeatureSIFR();

			document.getElementById("feature-arrow").className = "on";
			document.getElementById("feature-expander").className = "on";
		},
		function () {
			$("#feature-dropdowns").css("display", "none");
			document.getElementById("feature-arrow").className = "off";
			document.getElementById("feature-expander").className = "off";
		}
		);

	}
	if ($("#feature-expander")) {
		 $('#feature-expander').toggle(
		function () {
			$('#features').cycle("pause");
			$('#feature-dropdowns').cycle("pause");
			$("#feature-dropdowns").css("display", "block");
			showFeatureSIFR();
			document.getElementById("feature-arrow").className = "on";
			document.getElementById("feature-expander").className = "on";
		},
		function () {
			$("#feature-dropdowns").css("display", "none");
			document.getElementById("feature-arrow").className = "off";
			document.getElementById("feature-expander").className = "off";
		}
		);
	}
}



function stopFeature(zeroBasedSlideIndex, slideElement) {
	 $("#features").cycle('pause');
	 $('#feature-dropdowns').cycle("pause");
	if ($(slideElement).find('.sIFR-replaced').html() == null) {
		showFeatureSIFR();
	}
}
function afterFeatureDropdown(currSlideElement, nextSlideElement, options, forwardFlag) {
	if ($(nextSlideElement).find('.sIFR-replaced').html() == null) {
		showFeatureSIFR();
	}
}

function afterBannerTransition(currSlideElement, nextSlideElement, options, forwardFlag) {
	if ($(nextSlideElement).find('.sIFR-replaced').html() == null) {
		showBannerSIFR();
	}
}

function showFeatureSIFR() {
	 sIFR.replace(avenirFeatureTitle, {
		selector: '.feature-title'
	 });
	 sIFR.replace(avenirFeatureTitle, {
		selector: '.feature-title-wide'
	 });

	 sIFR.replace(avenirFeatureCallout, {
		selector: '.feature-callout'
	 });
	 sIFR.replace(avenirFeatureReadMore, {
		selector: '.feature-read-more'
	 });
}

function showBannerSIFR() {

	 sIFR.replace(avenirBannerHeadline, {
		selector: '.banners .headline'
	 });
	 sIFR.replace(avenirBannerSubHeadline, {
		selector: '.banners .subheadline'
	 });
}


$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});
