if(JQ.browser.msie && JQ.browser.version.substr(0,1) == "6") {
	DD_belatedPNG.fix('#but_search_area');
	DD_belatedPNG.fix('.btn_close');
	DD_belatedPNG.fix('#videoCloseBtn');
	DD_belatedPNG.fix('#content-home');
}

JQ(function(){ 
	/* Rounded Corners */
	var cornerSetting = { radius: 4 };
	var settings = {
		  tl: cornerSetting,
		  tr: cornerSetting,
		  bl: cornerSetting,
		  br: cornerSetting,
		  antiAlias: true,
		  autoPad: true,
		  validTags: ["div"]
	};
	if(JQ('#feeds').get(0) != null) {
		JQ('#feeds').corner(settings);
	}
	if(JQ('#footer').get(0) != null) {
		JQ('#footer').corner(settings);	
	}
});

/*
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
	SEARCH
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
*/
JQ(function(){ 	
	var $searchArea  	= JQ("#but_search_area"),
		$btnSearch	 	= JQ("#but_search"),
		$searchField	= JQ("#but_search_field"),
		searchClassOn	= "but_search_on",
		searchClassOff	= "but_search_off";
	
	function showSearch(e) {
		e.preventDefault(e);
		$btnSearch.removeClass(searchClassOff).addClass(searchClassOn);
		$searchArea.show();
		$searchField.focus();
	}
	
	$btnSearch.click(function(e) {
		e.preventDefault(e);
		var $this = JQ(this);
		
		if($this.hasClass(searchClassOff)) {
			showSearch(e);
		} else {
			$this.removeClass(searchClassOn).addClass(searchClassOff);
			$searchArea.hide();
		}
	});
	
	JQ("#but_search_area_submit").click(function () {
		$btnSearch.removeClass(searchClassOn).addClass(searchClassOff);
		$searchArea.hide();
		tracking.track({pageName:'>search', prop18: 'search', events : 'event2, event13'});
		JQ("#but_search_area_in form").submit();
	});
			
	// If the tab key is clicked the first time
	var $current_tab = 0; 
	JQ(this).keypress(function(e){
		if (e.keyCode == 9 && $current_tab == 0) {
			showSearch(e);
		} 
		
		$current_tab++;
	});
	
	JQ("#but_search_area_in form").submit(function(e) {
		// append value to hidden search field
		var $hiddenSearchField = JQ("#but_search_field_hidden");
		// remove any old search terms that might get stuck in this field
		$hiddenSearchField.get(0).value = $hiddenSearchField.get(0).value.split("-")[0] + "-" + encodeURIComponent( $searchField.val() );
	});
});

function displayVideo(guid, containerID) {
	NIKEOS.insertSWF({
		element_id : containerID,
		type : 'video_player',
		version : 'latest',
		width : '908',
		height : '510',
		params : {
			BASE : '/global-landing/global/swf/'
		},
		vars : $merge(
			{},
			{
				currentURL : NIKEOS.currentURL,
				guid: guid,
				locale : site_data.platypus_region,
				regionpath : 'na/en_US/',
				siteHost : NIKEOS.siteHost,
				autoPlay: "true"
			}
		)
	});
}

JQ(function() {
	var $feature = JQ("#featured");
	JQ.featuredContent = $feature.html();
	
	var $panels = JQ(".panel"),
		$panelHolder = JQ("#whatsNewPanels"),
		$videoClose = JQ("#videoCloseBtn"),
		$topVideoHolder = JQ("#topVideoHolder"),
		$whatsNewButtons = JQ("#whatsNewButtons a"),
		wNB_length = $whatsNewButtons.length,
		
		// all possible style classes that should be removed when a tab is not active
		dyn_styles = "active btn_0_alt btn_0 btn_1_alt btn_1 btn_2";	
	
	// If there are only 2 tabs, widen the nav buttons appropriately
	if(wNB_length == 2) {
		$whatsNewButtons.addClass("alt");
	}

	// If preview images fail on the blogs panel, show the default swoosh
	JQ("#hotNewsPanel a img", "#whatsNewPanels").error(function() {
		JQ(this).attr("src", "/global-landing/global/images/btns/btn_blankPanelEntry.png");							   
	});
	
	// If preview images fail on the video panel, show the _wall_ image, stretch, then crop to fit.
	JQ("#videoPanel a img", "#whatsNewPanels").error(function() {
		
		var imgSrc = JQ(this).attr("src"),
			replacementSrc = imgSrc.replace("_wide_","_wall_");
		
		JQ(this).addClass("resize_img");
		JQ(this).wrap("<span class=\"mask\"></span>");
		JQ(this).attr("src", replacementSrc);		
	});

	// Optimize images
	var panelSelected = [], panelNames = [];
	
	JQ("a", "#whatsNewButtons").each(function() {
		panelNames.push(this.id.split("-")[1]);
	});
	
	var panelCount = panelNames.length;
	
	for(var i=0;i<panelCount;i++) {
		panelSelected[i] = {
			name : panelNames[i],
			imageCount : JQ("ul li","#"+panelNames[i]+"").length,
			imageSrc : []
		};

		// Store all the image srcs in the imageSrc array
		for(var x=0; x<panelSelected[i].imageCount; x++) {
			panelSelected[i].imageSrc[x] = JQ("ul li:eq("+ x +") a img","#"+panelSelected[i].name+"").attr("src");
		}

		// Stop the images from loading initially
		JQ("ul li a img","#"+panelSelected[i].name+"").attr({src:"/global-landing/global/images/trans.png"});
	}

	// Make the Order Status section flexible
	var os_entry = JQ("a","#but_orderstatus").html();
	
	if(os_entry !== null) {
		var os_char_length = os_entry.length;
		var os_word_length = os_entry.split(' ').length;
		
		if(os_char_length > 23 && os_word_length == 1) {
			var os_trim = [ os_entry.slice(0,17), 
			                '<br />', 
			                os_entry.slice(17)
			              ];
						
			JQ("a","#but_orderstatus").html(os_trim.join(""));
			JQ("a","#but_orderstatus").addClass("extend");
		}
		
		else if(os_char_length > 23 && os_word_length > 1) {
			JQ("a","#but_orderstatus").addClass("extend");
		}
	}
	
	// Basic function to hide and deactivate panel
	function hidePanel() {
		if($feature.data("videoOpen")) {
			$feature.html(JQ.featuredContent);
			$feature.data("videoOpen", false);
		}
		
		JQ(".panel").animate({ top: -550 }, function() {
			$panelHolder.hide();
			$whatsNewButtons.removeClass(dyn_styles);
			$topVideoHolder.hide();
		});
	}
	
	// Tab is selected
	$whatsNewButtons.click(function(e) {
		
		var whatsNewIndex = $whatsNewButtons.index(this),
			parentId = this.id,
			panelName = parentId.split("-")[1],
			$target = JQ(e.target);

		e.preventDefault();
		if(! $target.hasClass("active")) {
			$whatsNewButtons.removeClass(dyn_styles);
			
			$topVideoHolder.hide();
			
			// Properly position the active state style of the button depending on how many tabs there are
			switch(whatsNewIndex) {
				case 0:
					if(wNB_length == 2) {
						JQ(this).addClass("btn_0_alt");
					}
					else {
						JQ(this).addClass("btn_0");
					}
					break;
				case 1:
					if(wNB_length == 2) {
						JQ(this).addClass("btn_1_alt");
					}
					else {
						JQ(this).addClass("btn_1");
					}
					break;
				case 2:
					JQ(this).addClass("btn_2");
					break;
				default:
					break;
			}
			
			$target.addClass("active");

			$panelHolder.show();
			
			$panels.hide().css({top: -550});
			JQ("#" + panelName).show().animate({ top: 0 });
			
			// Last step of image optimization - Load them in
			for(var i= 0; i<panelSelected[whatsNewIndex].imageCount; i++) {
				JQ("ul li:eq("+ i +") a img","#"+panelSelected[whatsNewIndex].name+"").attr({src : ""+panelSelected[whatsNewIndex].imageSrc[i]+""});
			}
		} else {
			hidePanel();
		}
	});
	
	// Show hover states on links when hovering over product images
	JQ("#productPanel li a img").hover(function() {
		var $elemParent = JQ(this).parents("li");
		JQ(".prod_info_contain a", $elemParent).addClass("hovered");
	}, function() {
		var $elemParent = JQ(this).parents("li");
		JQ(".prod_info_contain a", $elemParent).removeClass("hovered");
	});
	
	// Show hover states on links when hovering over blog images
	JQ("#hotNewsPanel li a img").hover(function() {
		var $elemParent = JQ(this).parents("li");
		JQ("h3.sport a", $elemParent).addClass("hovered");
	}, function() {
		var $elemParent = JQ(this).parents("li");
		JQ("h3.sport a", $elemParent).removeClass("hovered");
	});
	
	
	
	// Close tabs
	JQ(".btn_close").click(function(e) {
		e.preventDefault();
		hidePanel();
	});
	
	$videoClose.click(function(e) {
		e.preventDefault();
		$topVideoHolder.hide();
		$panelHolder.show();
		JQ("#videoPanel").show().animate({ top: 0 });
		JQ("#topVideoPlayer").html("");
	});
	
	// Play video
	JQ(".video").click(function(e) {
	
		e.preventDefault(e);
		var $target = JQ(e.target);
		JQ("#topVideoPlayer").html("");
		
		if(JQ(e.target).get(0).nodeName.toLowerCase() == "img" || JQ(e.target).get(0).nodeName.toLowerCase() == "span") {
			$target = $target.parents(".video");
		}
		
		JQ("#videoPanel").animate({ top: -550 }, function() {
			$panelHolder.hide();
			$topVideoHolder.show();
		});
		
		$feature.html("");
		
		if(typeof video_wall != "undefined") {
			var vp = video_wall.front_end.video_player;
			var guid = vp.get_guid();
			vp.guid = null;
			vp.change_video({guid: guid});
		}
		
		setTimeout(function() {
			$feature.data("videoOpen", true);
			displayVideo($target.attr("id"), "topVideoPlayer");		
		}, 300);
		
	});
});

