/*
Site-wide JS
*/

var flash_duration = 6000;
var currBannerID = null;
var tempBannerID = null;
var currBanner = null;
var tempBanner = null;
var btn_origX = 0;
var pageID;
var pageSlideShow

/*$(function(){
  setTimeout('hideMessages()', flash_duration);
});*/
$(document).ready(function(){
	$('html').resize(function(){
	});
		// fix the size of the title so that it does not overlap the banner image
	// gives us the width of the text even if it goes outside the parent container
	$.fn.textWidth = function(){
	  var html_org = $(this).html();
	  var html_calc = '<span>' + html_org + '</span>'
	  $(this).html(html_calc);
	  var width = $(this).find('span:first').width();
	  $(this).html(html_org);
	  return width;
	};
	// set the original width
	var page_title = $('.section-panel-active');
	var origWidth = page_title.outerWidth() * 0.80;
	var fontSize = 37;
	// decrement the font-size until we hit a width that is not overflowing the parent container
	while(page_title.textWidth() > origWidth){
		fontSize -= 1;
		page_title.css('font-size', fontSize+'px');
	};
setProd();
$(window).resize(function() {
	adjBG();
});
adjBG();
});

function setProd(){
		if(!pageSlideShow && pageID != 11) {
			$(".link-interactive").remove();
		} else {
			$(".link-interactive").bind('click', function() {
				if($("#section-wrapper2").css('margin-top') != '444px') {
					openCloseSlideshow("open");
				} else {
					openCloseSlideshow("close");
				}
			});
		}
}

function hideMessages() {
	$("div.message").filter(function(){
		return $(this).css("display") != "none"; // don't do the fade action if it's already hidden!
	})
	.animate({
		top: '-=200px'
	}, 'normal', 'swing', function(){
		$(this).remove();
	});
}

/* HOMEPAGE HIGHLIGHT BANNERS */

var setupBanner = function(theClip) {
	var self = theClip;
	var seemore = theClip.children('.highlight-banner-seemore');
	var color_img = theClip.children('.color-image');
	var img_minus = theClip.children('.highlight-banner-seemore').children('#'+ self.attr('id') + "_minus");
	var content = theClip.children('.content');
	seemore.hide();
	color_img.hide();
	img_minus.hide();
	content.hide();
}

var homeBannerExp = function (theClip) {
	tempBannerID = theClip.parent().attr('id');
	tempBanner = theClip;
	if(currBanner) {
		 bannerClose(currBanner);
	} else {
		if(theClip.parent().height() < 444) {
		   bannerOpen(theClip);
		} else {
			 bannerClose(theClip);
		}
	}
}

var bannerOpen = function (theClip) {
	var self = theClip;
	var parent = theClip.parent();
	var color_img = theClip.parent().find('.color-image');

	color_img.show();
  color_img.animate({ opacity: 1 }, 250, function() { /* Animate end */});
	parent.animate({ height: 444 }, 250, function() {
			currBannerID = parent.attr('id');
			currBanner = theClip
			parent.children('.content').show();
			$('html, body').animate({scrollTop: (position.top + 100) + 'px'}, 400);
	});

	btn_origX = parent.children('.highlight-banner-seemore').css('left');

	//parent.children('.highlight-banner-seemore').animate({ left: 25 }, 250, function() { /* Animation complete.*/ });

	parent.css ('overflow', 'visible');
	self.children('#'+ parent.attr('id') + "_minus").show();
	self.children('#'+ parent.attr('id') + "_plus").hide();

	var position = parent.position();
	$('#banner-black-bg').css('top', (position.top + 143));
	$('#banner-black-bg').animate({ height: 444}, 250);
}

var bannerClose = function(theClip) {
	var self = theClip;
	var parent = theClip.parent();
	var color_img = theClip.parent().find('.color-image');

	parent.css ('overflow', 'hidden')
  parent.children('.content').hide();
	parent.animate({ height: 148 }, 250, function() {
		if(currBannerID != tempBannerID) {
			bannerOpen(tempBanner);
		} else {
			$('html, body').animate({scrollTop: '0px'}, 400);
		}
		currBanner = null;
	});
	//parent.children('.highlight-banner-seemore').animate({ left: btn_origX }, 250, function() { /* Animation complete.*/ });
	color_img.animate({ opacity: 0 }, 250, function() { color_img.hide(); });
	self.children('#'+ parent.attr('id') + "_minus").hide();
	self.children('#'+ parent.attr('id') + "_plus").show();
	$('#banner-black-bg').animate({ height: 0}, 250);
}

var homeBannerOver = function (theClip, theImg) {
	var self = theClip;
	var seemore = theClip.children('.highlight-banner-seemore');
	self.css ('background', 'url('+theImg+') no-repeat')
	if(theClip.height() == 148) {
		seemore.show();
	}
	theClip.children('.highlight-banner-title').css('color', '#FFFFFF');
}

var homeBannerOut = function (theClip, theImg) {
	var self = theClip;
	var seemore = theClip.children('.highlight-banner-seemore');
	self.css ('background', 'url('+theImg+') no-repeat')
  if(theClip.height() == 148) {
		seemore.hide();
	}
	theClip.children('.highlight-banner-title').css('color', '#b7b7b7');
}

var adjustBreadcrumb = function() {
	var idCtr = 0;
	$.each($("#breadcrumb li"), function(i, item){
		$(this).css('margin-left', '6px');
		if(idCtr < $("#breadcrumb li").length - 1){
			$(this).append('<span style="margin-left: 5px;">></span>');
		}
		idCtr++;
	})

}



var RemoveDuplicates = function(arr){
    //get sorted array as input and returns the same array without duplicates.
    var result=new Array();
    var lastValue="";
    for (var i=0; i<arr.length; i++) {
 	  	var curValue=arr[i];
	 	  if (curValue != lastValue) {
	 		 result[result.length] = curValue;
	 	  }
	 	  lastValue=curValue;
    }
    return result;
}

var adjBG = function(){
	if($(window).width() < 1410) {
		$('body').css('width', 1410);
		if($('#interactive-tour').length != 0){
			 $('#interactive-tour').css('width', 1410);
		}
	} else {
		$('body').css('width', '100%');
		if($('#interactive-tour').length != 0){
			 $('#interactive-tour').css('width', '100%');
		}
	}
}









