var windowHeight;
var winW;
var iphone=false;

//used on home.html and video_landing.html to open a lightbox
function openVideo(trigger, linkLocation, width, height){
	$j('#'+trigger).fancybox({
		'hideOnContentClick': false,
		'overlayColor': '#000',
		'overlayOpacity' : 0.65,
		'centerOnScroll': true,
		'transitionIn': 'elastic',
		'transitionOut': 'fade',
		'padding':5,
		'width' : width,
		'height' : height,
		'href': linkLocation,
		'type' : 'iframe'
	});
	//triggers hidden a tag on page
	$j('#'+trigger).trigger('click');
}

//Used on guided_search.html to add a clicked class to elements selected .  Array is returned from guided_search.html inline js
function activateSelections(){
	$j('.selection_list li', '#selections_container').each(function(){
		$j(this).click(function(){
			if($j(this).hasClass('clicked')){
			$j(this).removeClass('clicked');
			}
			else{
			$j(this).parent().find('li').removeClass('clicked');
			$j(this).addClass('clicked');
			}
		});
	});
}

// Used on guided_search.html to navigate through selections.  Arrows appear/dissapear as req.
function scrollSelections(){
	//container height of holding html element
	var containerHeight=465;	

	$j('.nav_arrow_down', '#hsb_brand_copy_body').each(function(){
		var currentArrow=$j(this);
		var whatToScroll='.'+$j(currentArrow).parent().attr('class') +'_scroller';	
		var originalHeight=$j(whatToScroll).height();
		
		var timeOut=0;	
		if(originalHeight<containerHeight){
			$j(currentArrow).css({'display' : 'none'}).parent().children('.nav_arrow_up').css({'display' : 'none'});
		}
		else{
		//if global iphone var (set in iphoneCheck())is set to false use mousedown otherwise use iphone/ipad functionality
		if(!iphone){
			$j(currentArrow).mousedown(function(){
				scrollDown(currentArrow, whatToScroll, originalHeight, containerHeight, timeOut);
				});
		}
		else{
			$j(currentArrow).bind('touchstart', function(){
				scrollDown(currentArrow, whatToScroll, originalHeight, containerHeight, timeOut);
			});			
		}		
		}
	});
	$j('.nav_arrow_up', '#hsb_brand_copy_body').each(function(){
		var currentArrow=$j(this);
		var whatToScroll='.'+$j(currentArrow).parent().attr('class') +'_scroller';	
		var originalHeight=$j(whatToScroll).height();
		var timeOut=0;	
		$j(currentArrow).addClass('nav_arrow_up_disabled');
		//if global iphone var (set in iphoneCheck())is set to false use mousedown otherwise use iphone/ipad functionality
		if(!iphone){
			$j(currentArrow).mousedown(function(){
				scrollUp(currentArrow, whatToScroll, originalHeight, containerHeight, timeOut);
				});
		}
		else{
			$j(currentArrow).bind('touchstart', function(){
				scrollUp(currentArrow, whatToScroll, originalHeight, containerHeight, timeOut);
			});			
		}
	});
}
//helper for scrollSelections() function.  Scrolls content up
	function scrollUp(currentArrow, whatToScroll, originalHeight, containerHeight, timeOut){
				$j(currentArrow).bind('mouseup mouseleave touchend', function() {
							clearInterval(timeOut);
					});
					timeOut= setInterval(function(){
					var currentHeight=parseInt($j(whatToScroll).css('top'), 10);
					if(currentHeight<0){
					$j(currentArrow).parent().children('.nav_arrow_down').removeClass('nav_arrow_down_disabled');
					$j(whatToScroll).animate({'top' : '+='+15}, 1);
					}
					else{
						$j(whatToScroll).stop(true).css({'top' : 0});
						clearInterval(timeOut);
						$j(currentArrow).addClass('nav_arrow_up_disabled');
					}
				}, 10);
	}
//helper for scrollSelections() function.  Scrolls content down
	function scrollDown(currentArrow, whatToScroll, originalHeight, containerHeight, timeOut){	
				$j(currentArrow).bind('mouseup mouseleave touchend', function() {
							clearInterval(timeOut);
					});
					timeOut= setInterval(function(){
					var currentHeight=parseInt($j(whatToScroll).css('top'), 10);
					if(currentHeight>(originalHeight-containerHeight)*-1){
					$j(currentArrow).parent().children('.nav_arrow_up').removeClass('nav_arrow_up_disabled');
					$j(whatToScroll).animate({'top' : '-='+15}, 1);
					}
					else{
						$j(whatToScroll).stop(true).css({'top' : (originalHeight-containerHeight)*-1});
						clearInterval(timeOut);
						$j(currentArrow).addClass('nav_arrow_down_disabled');
					}
				}, 10);
	}

// Binds javascript hover dropdowns to no flash content if flash is not installed
function dropDownNavs(){
	$j('#autobots_nav', '#expandable_flash_area').hover(function(){
		if($j(this).children('img').length ==0){
			$j(this).append('<img class="autobot_indicator" src="/transformers/dotm/images/generic/noflash/autobot_indicator.png" />');
		}
		},
		function(){
		$j(this).children('img').remove();
		});
		
	$j('.html_nav', '#expandable_flash_area').each(function(){
		var flyout=$j(this).children('.html_flyout');
		var origHeight=flyout.css('height');
		$j(flyout).css({'height' : 0, 'opacity' : 0});
		
		$j('#decepticons_nav', '#expandable_flash_area').hover(function(){
		if($j(this).children('img').length ==0){
			$j(this).append('<img class="decepticon_indicator" src="/transformers/dotm/images/generic/noflash/decepticon_indicator.png" />');
		}
		},
		function(){
		$j(this).children('img').remove();
		});
		$j(this).hoverIntent(function(){
			$j(flyout).css({'display':'block'}).stop(true).animate({'height' : origHeight, 'opacity' : 1}, 400, 'easeOutQuad');
		},
		function(){
			
			$j(flyout).animate({'height' : 0, 'opacity' : 0}, 200, 'easeInQuad', function(){
				$j(this).css({'display':'none'});
			});
		});
	});

}

//Checks to see if current user is using iphone/ipad/ipod
function iphoneCheck(){
	if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
	    iphone=true;
	}
	else{
		iphone=false;
	}
}

// enlarges flash area to accomodate swf flyout on all pages that have the flash character nav.
function characterNavEnlargePlease() {
	$j('#expandable_flash_area').css({
			height : 237
		});
}
// shrinks flash area to accomodate swf flyout on all pages that have the flash character nav.
function characterNavShrinkPlease() {
	$j('#expandable_flash_area').css({
			height : 101
		});
}

// takes copy and shortens it for use with the expandableTitle() function.
function shortenCopy(originalText, maxLines, width, object) {
	var brokenString = originalText.split(" ");
	var lengthOfString = brokenString.length;
	var testString = "";
	var testString2 = "";
	var shortenedString = "";
	var lineNumber = 0;
	var newStringWidth = 0;
	var oldStringWidth = 0;

	for(var i = 0; i < lengthOfString; i++) {
		testString += brokenString[i];
		
		if(brokenString[i + 1] == undefined) {
			shortenedString = originalText;			
			return 'false';
		}
		else if(lineNumber==maxLines-1){
			testString2 = testString + " " + brokenString[i + 1] + "....";
		}
		else{
			testString2 = testString + " " + brokenString[i + 1];
		}

		oldStringWidth = $j(object).text(testString).width();
		newStringWidth = $j(object).text(testString2).width();

		if(oldStringWidth == newStringWidth) {

			shortenedString += " " + testString;
			
			testString = "";
			
			lineNumber += 1;
			
			if(lineNumber == maxLines) {
				
				shortenedString += '...';
				return shortenedString;
			}
		}
		else{
		testString += " ";
		}
	}
}

//creates an expandable title with selector given
function expandableTitle(selector, maxLines, style) {
	$j(selector).each(function() {
			var originalText = $j(this).text();			
			var originalHeight = $j(this).parent().height();
			var width = $j(this).width();
			var link = $j(this).attr('href');
			var endHeight = $j(this).height() + 5;

			var shortenedCopy = shortenCopy(originalText, maxLines, width, $j(this));
		
		$j(this).css({
			display : 'block',
			'height' : originalHeight
			}); 
			
			if(shortenedCopy != 'false') {
				
					$j(this).text(shortenedCopy);
					if(iphone){
					$j(this).bind('touchstart', function(){
					hoverTitleIn($j(this), link, style, originalText, originalHeight, endHeight)
					});					
							
					}
					else{
					var config = {    
					over: function(){
					var thisTitle=$j(this);
					hoverTitleIn(thisTitle, link, style, originalText, originalHeight, endHeight);
					}, // function = onMouseOver callback (REQUIRED)    
					timeout: 500, // number = milliseconds delay before onMouseOut    
					out: hoverTitleOut // function = onMouseOut callback (REQUIRED)    
					};
					
					$j(this).hoverIntent(config);
					} 
			}
			else {
				$j(this).text(originalText); 
			}
		});
}

// helper function for expandableTitle()  places an absolute positioned element over current element and populates it with the original string passed in 
// the expandableTitle() function.

function hoverTitleIn(thisTitle, link, style, originalText, originalHeight, endHeight){
					var linkString="";
						if(!$j(thisTitle).is('span')){
						linkString = '<a class="'+ style + ' tooltip" href="'+ link + '">' + originalText + '</a>';
						}
						else{
						linkString = '<span class="'+ style + '">' + originalText + '</span>';
						}
						
						$j('#hsb_content_bg').append(linkString);
						
						var offset = $j(thisTitle).offset();
						var parentOffset= $j('#hsb_container').offset();
						$j('.'+style).css({
								height : originalHeight,
								top : offset.top,
								left : offset.left-parentOffset.left
							}).stop(true).animate({
								'height' : endHeight
							}, 500, 'easeInOutQuad');
						
						$j('.'+style).hover(function() {
								
							},
							function() {
								$j('.'+style).animate({
										height : originalHeight
									}, 200, 'easeInOut', function() {
										$j(this).remove();
									});
							}); 
	}
	
function hoverTitleOut(){
	//req for hoverintent
}

// helper for setUpScrollPanes() function renables the up arrow if the user clicks down or content is above it.
function rebindUpArrow(selector) {
	$j(selector).find('.nav_arrow_up').data('active', true).click(function() {
			
			var container=$j(this).parent().find('.hsb_brand_sect_content')
			var scrollAmount = $j(container).height();
			var totalHeight = $j(container).css({'height' : 'auto'}).height();
			$j(container).css({'height' : scrollAmount});
			
			var scrolled = $j(this).parent().find('.hsb_brand_sect_body_scroller').data('totalScrolled') - scrollAmount;
			
			var buttonUp = $j(this).parent().find('.nav_arrow_up');
			var buttonDown = $j(this).parent().find('.nav_arrow_down');
			
			$j(this).parent().find('.hsb_brand_sect_content').animate({
					top : '+=' + scrollAmount
				}, 600, 'easeInOutQuad', function() {
				}); 
			if(scrolled - scrollAmount < 0) {
				$j(buttonUp).data('active', false).addClass('nav_arrow_up_disabled').unbind('click');
			}
			if($j(buttonDown).data('active') == false) {
				$j(buttonDown).removeClass('nav_arrow_down_disabled');
				rebindDownArrow(selector);
			}
			
			$j(this).parent().find('.hsb_brand_sect_body_scroller').data('totalScrolled', scrolled);
			
		});
	
}

// helper for setUpScrollPanes() function renables the down arrow if the user clicks down or content is below it.
function rebindDownArrow(selector) {
	
	$j(selector).find('.nav_arrow_down').data('active', true).click(function(e) {
			var container=$j(this).parent().find('.hsb_brand_sect_content')
			var scrollAmount = $j(container).height();
			var totalHeight = $j(container).css({'height' : 'auto'}).height();
			$j(container).css({'height' : scrollAmount});
			var buttonUp = $j(this).parent().find('.nav_arrow_up');
			var buttonDown = $j(this).parent().find('.nav_arrow_down');
			
			var scrollArea = $j(this).parent().find('.hsb_brand_sect_body_scroller');
			
			var scrolled = $j(scrollArea).data('totalScrolled') + scrollAmount;
			
			$j(this).parent().find('.hsb_brand_sect_content').animate({
					top : '-=' + scrollAmount
				}, 600, 'easeInOutQuad', function() {
					
				});
			
			if(scrolled + scrollAmount >= totalHeight) {
				$j(buttonDown).data('active', false).addClass('nav_arrow_down_disabled').unbind('click');
			}
			
			if($j(buttonUp).data('active') == false) {
				$j(buttonUp).removeClass('nav_arrow_up_disabled');
				rebindUpArrow(selector);
			}
			$j(this).parent().find('.hsb_brand_sect_body_scroller').data('totalScrolled', scrolled); 
			
		});
}

//Sets up scroll panes on any module that requires scrolling page content.  Takes height of container and amount of elements to generate pages.
function setUpScrollPanes() {
	var contentScroller = $j('.hsb_brand_sect_body_scroller', '#hsb_content_bg').data('totalScrolled', 0);
	$j('.content_module', '#hsb_content_bg').each(function() {
			rebindDownArrow($j(this)); 
		});
	
	var disableArrows = contentScroller.filter(function() {
			var content = $j(this).find('.btn_215_100_left')
			var len = $j(content).length;
			var height=$j(content).height();
			if($j(this).height() < len*height) {
				return false;
			} else {
				return true;
			}
		}).parent().parent();
	$j(disableArrows).find('.nav_arrow_down').hide().unbind('click');
	$j(disableArrows).find('.nav_arrow_up').hide().unbind('click');
	
	$j('.nav_arrow_up', '#hsb_content_bg').addClass('nav_arrow_up_disabled').data('active', false);
	
}
//Activates pulses for navigation rollovers
function pulseOff(pulserAnimation) {
	$j(pulserAnimation).css({
			opacity : 1
		}).animate({
			opacity : 0
		}, 500, 'easeInOutSine', function() {
			pulseOn(pulserAnimation);
		});
}
//Activates pulses for navigation rollovers
function pulseOn(pulserAnimation) {
	$j(pulserAnimation).css({
			opacity : 0
		}).animate({
			opacity : 1
		}, 500, 'easeInOutSine', function() {
			pulseOff(pulserAnimation);
		});
}


//Activates pulses for navigation rollovers
function bindNavClicks(selector) {
	$j(selector).each(function() {
			if(!$j(this).find('a').hasClass('active')) {
				$j(this).removeClass("nav_link");
				$j(this).find('a').append("<img src='/transformers/dotm/images/generic/hdr/pulser.png' class='pulser' />");
	
				if(!iphone){
				$j(this).hover(				
					function(e) {
						e.stopPropagation();
						$j(selector).find('.pulsing').removeClass('pulsing');
						$j(this).find('.pulser').addClass('pulsing').css({
								opacity : 0
							});
						$j(this).find('img').fadeIn(200);
						pulseOn($j(this).find('.pulsing'));
						
					}, function(e) {
						e.stopPropagation();
						$j('.pulsing', '#hsb_topnav').stop(true).removeClass('pulsing');
						$j(this).find('img').eq(0).stop(true, true).fadeOut(200);
						$j(this).find('img').eq(1).css({
								opacity : 0
							});
					});
					}
					else{
					$j(this).bind('touchstart', function(){
					$j(this).find('img').fadeIn(200);
					});		
					
					}
			} else {
				$j(this).removeClass("nav_link");
				$j(this).find('a').removeClass("nav_link").append("<img src='/transformers/dotm/images/generic/hdr/pulser.png' class='pulser' />").find('img').eq(1).css({
						opacity : 1
					});
				$j(this).hover(
					function(e) {
					
						e.stopPropagation();
						$j(this).find('.pulser').addClass('pulsing');
						pulseOff($j(this).find('.pulsing'));
					},
					function() {
						$j('.pulsing').stop(true).css({
								opacity : 1
							}).removeClass('pulsing');
					});
				
			}
		});
}
//resizes tabs to fit entire window as the user increases/decreases tabs
function resizeTabs() {
	var width,
	position,
	tileWidth;
	
	width = (winW - 990) / 2; //990 because 960 is width + 15 padding for each side
	if(width < 0) {
		width = 0;
	}
	position = -306 + width;
	tileWidth = width - 306 + 1;
	
	if(tileWidth < 0) {
		tileWidth = 0;
	}
	
	$j('#tabLeft').css({
			left : position
		});
	$j('#tabLeftTile').css({
			width : tileWidth,
			left:0
		});
	
	$j('#tabRight').css({
			left : width + 990
		});
	$j('#tabRightTile').css({
			width : tileWidth,
			right:0
		});
}
//resiszes window background.
function resizeWindow(e) {
	windowHeight = $j(window).height();
	var h,
	winH,
	imgH;
	
	h = $j("body").height() + 80;
	winW = $j(window).width();
	winH = $j(window).height();
	
	if(winW < 960) {
		winW = 960;
	}
	imgH = $j("#bgImage").height();
	
	resizeTabs();
	
	if(h > winH) {
		$j("#bodyContainer").css("height", h);
	} else {
		$j("#bodyContainer").css("height", winH);
	}
	
	if(winW >= imageMinW) {
		$j("#bgImage").css("width", winW);
		$j("#bgImage img").css("width", winW);
	} else {
		$j("#bgImage").css("width", winW);
		$j("#bgImage img").css("width", "1200px");
	}
}
function activateShare() {
	//This is for the fancy box footer share link.  Will need to go on every page eventually.
	$j('#activateSharebox').fancybox({
		'hideOnContentClick': false,
		overlayColor: '#000',
		overlayOpacity:.65,
		centerOnScroll: true,
		transitionIn: 'elastic',
		transitionOut: 'fade',
		padding:5
	});
	$j('#hsb_copy_input').val(document.URL);
	$j('#activateCopybox').fancybox({
		'hideOnContentClick': false,
		overlayColor: '#000',
		overlayOpacity:.65,
		centerOnScroll: true,
		transitionIn: 'elastic',
		transitionOut: 'fade',
		padding:5
	});
	$j('#activateCopybox2').fancybox({
		'hideOnContentClick': false,
		overlayColor: '#000',
		overlayOpacity:.65,
		centerOnScroll: true,
		transitionIn: 'elastic',
		transitionOut: 'fade',
		padding:5
	});
}

function setTheStyleToggle(control, target) {
	var controlDivID = "#"+control;
	var targetDivID = "#"+target;
	
	$j(document).find(controlDivID).click(function(){
		if ($j(document).find(targetDivID).css('display') == "block") {
			$j(document).find(targetDivID).css({'display' : 'none'});
		} else {
			$j(document).find(targetDivID).css({'display' : 'block'});
		}
	});
}  

