// JavaScript Document

var ADIDAS = {
	
	init : function(){
    (function($){
		
		// Mainmenu dropdown
		if(checkWMode() == false && $('#noWmodeShade').size() == 0){
			$("<div id='noWmodeShade' class='default'></div>").insertAfter('div#grayShade');			
			$("#noWmodeShade").hide();
		}
		
		$("#grayShade").hide();
		//$("#HTMLContent").hide();
	  	
		//number the li.button elements
		$("li.button").each(function (index, domEle) {
	        $(this).attr("id",'button' + (index+1));	                
	      });
		 $('#wrapper .dropdown li').hover(function(){$(this).addClass('hover')}, function(){$(this).removeClass('hover')})
	  	// submenu dropdown with images
		
		$('ul.featured-perf').accordion({
			animated:false,
			header: 'a',
			selectedClass: 'expanded',
			alwaysOpen: false,
			event: 'mouseover'
		}).bind('mouseover', function(){
			//need to keep the menu open for IE6
			$(this).parents("li.button").trigger("mouseover");
			$(this).parents(".parent").addClass('hover');
									
		}).bind("accordionchange", function(event, ui){
			$(this).parents(".button").addClass('over');
			
		}).bind('mouseleave', function(){
			$('ul.featured-perf','','').accordion('activate', -1);
								
		});
	  $('ul.featured-perf','','').accordion('activate', -1);
	  
	  
		$("li.button a.nav-button").hover(function(){
			
			$(this).parent().addClass('hover');
			
			if ($(this).hasClass('expanded')) {				
				handleMenus();				
				pauseFlash ();
			}
			else{
				handleMenus();
				$(this).addClass('expanded').next().show();	
				pauseFlash ();
			}
		},function(){});  // empty hoverout function	
		                                    
		$('div.parent').bind('mouseleave',function(){
			$(this).removeClass('hover');
			if(checkWMode() == false){
				showWOWmode('hide');				
			}
			if(jQuery.browser.msie && jQuery.browser.version == '6.0' ){
						$('select.hideable').css({visibility:'visible'});
					}	
			
			$("#grayShade").hide();
			$('ul.featured-perf','','').accordion('activate', -1);
			
			//$('li.featured div.featured').hide();
			resumeFlash ();
		});	
		
})(jQuery);
		
} // end of init()	
		
};

function handleMenus(){
	if (!detectOlderOpera()) {										
					if(checkWMode() == false){
						showWOWmode('show');						
					}
					if(jQuery.browser.msie && jQuery.browser.version == '6.0' && jQuery('select:visible').size() > 0){
						setHideable();
						jQuery('select.hideable').css({visibility:'hidden'});
					}	
					if (!detectMacXFF2()) {
						
						jQuery("#grayShade").show();
					}
				}
	
}

function setHideable(){	
	if (jQuery('select:visible').size() > 0) {
		jQuery('select:visible').filter('[class!=hideable]').addClass('hideable');
	}
}

//once we know whether or not there's flash and no wmode set, we need to show/hide flash appropriately

function showWOWmode(action){
	if (action == 'show') {
		jQuery("#FlashContent, #flashcontent").css({visibility:'hidden'}).find('embed').hide();
		jQuery("#noWmodeShade").show();
		//jQuery("#flashcontent").css({position:'absolute', zIndex: '4000', marginLeft: '-5000px'})
	}
	else {
		jQuery("#FlashContent, #flashcontent").css({visibility:'visible'}).find('embed').show();
		jQuery("#noWmodeShade").hide();
		//jQuery("#flashcontent").css({position:'static' , marginLeft: '0'})		
	}	
}

// checking for the existence of a wmode
function checkWMode(){
	
	var embeds = jQuery('embed, object');
	if (embeds.length > 0) {
		//IE type 
		if (jQuery("object").size() > 0 && ((jQuery("object")[0].innerHTML.toString().toLowerCase().indexOf('wmode') == -1)|| jQuery("object")[0].innerHTML.toString().toLowerCase().indexOf('<param name="wmode" value="window">') != -1)) {
			return false;
		}
		else if (jQuery("embed").size() > 0) {
		//FF type
			try{ 
				var j = jQuery('embed').attr('wmode').toString().length; 
				return; 
			} 
			catch(err){ return false; }			
		}
		else{
			//console.log("wmode is set")
			return; }
	}
	else {
		//console.log("no Flash on page.  WMODE irrelevant")
		return;
	}					
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

// example call: processNewNav({newWidth: 940, animDuration: 1000,easing:'Cubic.easeOut'}) 

function processNewNav(obj){
	var newWidth = obj.newWidth + 'px';
	var duration = obj.animDuration;
	var easing = obj.easing.split('.').reverse().join(''); // Cubic.easeOut -> easeOutCubic
	
	jQuery('div.header-nav,#footer dl.nav').animate({width: newWidth},duration,easing);

	if (obj.newWidth<=190) 	jQuery('#wrapper').addClass('smallWindow');
	else jQuery('#wrapper').removeClass('smallWindow');
}

function processNewNav(obj){
	var newWidth = obj.newWidth + 'px';
	var duration = obj.animDuration;
	var easing = obj.easing.split('.').reverse().join(''); // Cubic.easeOut -> easeOutCubic
	
	jQuery('div.header-nav,#footer dl.nav').animate({width: newWidth},duration,easing);

	navShift(obj.newWidth);
}

function navShift(newWidth){
	if (newWidth<=190) 	jQuery('#wrapper').addClass('smallWindow');
	else jQuery('#wrapper').removeClass('smallWindow');
}

(function($){
	navShift ( $(window).width() );
	moveFooter();
	$(window).bind('resize',function(){
		navShift ( $(window).width() );
		resizeFlash();
		moveFooter();
	})
})(jQuery);

function resizeFlash(){
	(function($){
		var footerheight = $('#footer').height() + parseInt($('#footer').css('paddingTop'), 10) + parseInt($('#footer').css('paddingBottom'), 10);
		var windowheight = $(window).height();
		var wrapper = $('#wrapper').height();
		var flashheight = windowheight - wrapper - footerheight;
	
	if (jQuery('embed, object').size() > 0) {
		if ($("#homepage").size() > 0) {
			//console.log("window height: " +  windowheight +"flash height: " + $('#flashcontent').height()+ "wrapper: " + wrapper + "footer height:" + footerheight);
			if (flashheight > 588) {
				$('#FlashContent, #flashcontent').css('height', parseInt(flashheight));
				return flashheight;
			}
			else {
				$('#FlashContent, #flashcontent').css('height', 588);
				return 588;
			}
		}
		if ($('.fullFlash').size() > 0) {
			$('#FlashContent, #flashcontent').css('height', parseInt(flashheight));
			return flashheight;
		}
	}
	})(jQuery)
}

function thisMovie ( movieName ) {
        if ( navigator.appName.indexOf("Microsoft") != -1) return window[movieName];
            else return document[movieName];
}


function pauseFlash ( ) {
      
      if(jQuery('#homepage #flashLayer').size() > 0){
            thisMovie("flashLayer").pause();          
      }
      
}

	
	
function resumeFlash ( ) {

      if(jQuery('#homepage #flashLayer').size() > 0){
            thisMovie("flashLayer").resume();
      }
      
}

function moveFooter(){
  var wrapper = jQuery('#wrapper').height();
  var footerheight = jQuery('#footer').height() + parseInt(jQuery('#footer').css('paddingTop'),10)  + parseInt(jQuery('#footer').css('paddingBottom'),10);
  var flashHeight = jQuery('#FlashContent, #flashcontent').height() + wrapper;
  var windowheight = jQuery(window).height();
  var embeds = jQuery('embed, object');
  var totalPageHeight = jQuery('#content, #homepage').height() + wrapper + footerheight;
  
  
    function getFooterOffset(){
    	var offset; 
        
		if ( jQuery('#homepage').size() > 0 && (jQuery(window).height() <  footerheight + 588) ){ // basically when the flash stops resizing to be smaller.
	      offset = 613;
		  //console.log('offset: ' + offset);
	    } else {
	      offset = jQuery(window).height() - footerheight;
	    }	
		return offset + 'px'  	
	}
	function updateFooter(type){
		switch (type){
			case "abs":
			jQuery('#footer').css({position: 'absolute',top: getFooterOffset()}) 	
			break;
			
			case "static":
			jQuery('#footer').css({position: 'static'})
			break;
		}
	}
  

  if ((jQuery('#homepage').size() > 0 && embeds.length > 0) || (embeds.length == 0 && jQuery('#content').size > 0 && (jQuery('#content').height() + wrapper + footerheight) < jQuery(window).height())) 
  {	 
  //console.log('scenario 1: embeds ' + embeds.length + "jQuery('#content').height() + wrapper + footerheight" + jQuery('#content').height() + ' ' +  wrapper + '' + footerheight + 'window.height: ' + jQuery(window).height())
  	updateFooter("abs");  	
  }
  else if ((jQuery('#homepage').size() > 0 && embeds.length == 0)){
  	updateFooter("static"); 
  }  
  else if (embeds.length > 0 && totalPageHeight < windowheight|| embeds.length == 0 && (totalPageHeight < windowheight))  
  {//console.log('scenario 2: embeds ' + embeds.length + "jQuery('#content').height() + wrapper + footerheight " + jQuery('#content').height() + ' ' +  wrapper + ' ' + footerheight + ' window.height: ' + jQuery(window).height())
	updateFooter("abs");
  }
  else if (embeds.length > 0 && totalPageHeight > windowheight || embeds.length == 0 && totalPageHeight > windowheight){
  	//console.log('scenario 3: embeds ' + embeds.length + "jQuery('#content').height() + wrapper + footerheight" + jQuery('#content').height() + ' ' +  wrapper + '' + footerheight + 'window.height: ' + jQuery(window).height())
	updateFooter("static");	
  }     
}




// Detects Firefox 2 on Mac to avoid transparency over Flash issue
function detectMacXFF2() {
  var userAgent = navigator.userAgent.toLowerCase();
  if (/firefox[\/\s](\d+\.\d+)/.test(userAgent)) {
    var ffversion = new Number(RegExp.$1);
    if (ffversion < 3 && userAgent.indexOf('mac') != -1) {
      return true;
    }
  }
  return false;
}

// detect Opera versions older than 9 which cannot handle transparencies
function detectOlderOpera()
{
	if(jQuery.browser.opera)
	{
		var userAgent = navigator.userAgent.toLowerCase();
		var operaIndex = userAgent.indexOf("opera");
		var version = new Number(userAgent.charAt(operaIndex+6));
		
		if(version < 9)
			return true;		
	}
	return false;
}

