// CHANGE CSS CLASS FUNCTION - FOR PROMO FLASH

function change_css_class(i, c) {
	$("#"+i).attr("class",c);
}

function thisMovie(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
}

// SEARCH FUNCTIONALITY

// Search box class
function searchBox (searchBoxId, gnbSearchClose) {
	// Properties
	this.userSearch = false;
	this.searchBox = $("#"+searchBoxId);
	this.gnbSearchClose = $("#"+gnbSearchClose);
	this.hiddenElements = new Array("greyScreen","gnb_search_close");
	this.defaultSearchValue = "Find";
	this.searchText = "";

	// Search box focus
	this.sbFocus = function() {
		if (!this.userSearch)
	 		$(this.searchBox).val("");
	}
	
	// Manipulate the page and run searches
	this.searchManip = function() {
		// If the user hasn't changed his search, just leave it as is
		if ($(this.searchBox).val() == this.searchText) return;
		
		this.searchText = $(this.searchBox).val();
		// If the user is typing in a search and his screen isn't darkened out yet, do that
		if ((this.searchText.length > 0) && (!this.userSearch)) {
			this.userSearch = true;
			this.toggleHidden(false);
			// Pause the SWF
			thisMovie("main_promo").pause();
			// Predictive dropdown
			$("#gnb_dd_container").fadeIn(300);
			// Otherwise, if he has cleared his search, undarken his screen
		} else if (this.searchText == "") {
			this.clearSearch(false);
		}
	}

	// Search box unfocus
	this.sbBlur = function() {
		if ($(this.searchBox).val() != "") this.userSearch = true;
		else this.closeSearch(true);
	}
	
	// Hide or unhide stage
	this.toggleHidden = function(hide) {
		for (i=0; i<this.hiddenElements.length; i++) {
			if (hide) $("#"+this.hiddenElements[i]).addClass("hidden");
			else $("#"+this.hiddenElements[i]).removeClass("hidden");
		}
	}
	
	// Clear a search and the stage
	this.clearSearch = function(clearStage) {
		// Reset variables
		this.userSearch = false;
		this.searchText = "";
		// Hide predictive dropdown
		$("#gnb_dd_container").fadeOut(300);
		
		// Clear stage if true
		if (clearStage) {
			// Hide the stage
			this.toggleHidden(true);
			// Start playing the SWF again
			thisMovie("main_promo").resume();
		}
	}
	
	this.closeSearch = function() {
		$(this.searchBox).val(this.defaultSearchValue);
		this.clearSearch(true);
	}
}

var searchBox;

$(function() {
	// Initialize search box
	searchBox = new searchBox("search_field", "gnb_search_close");

	// Attach listeners
	$(searchBox.searchBox).focus(function() { 
		searchBox.sbFocus() 
	}).blur(function() { 
		searchBox.sbBlur() 
	}).keyup(function() {
		searchBox.searchManip();
	}).click(function() {
		searchBox.searchManip();
	});
	
	searchBox.gnbSearchClose.click(function() { searchBox.closeSearch(); })
});



// GNB SUBMENU STYLING CLASS

function gnbSubmenu(subMenu, xOffset) {
	submenuDiv = $("#"+subMenu+" .gnb_submenu");
	width = ($(this.submenuDiv).find("li").size() * 110) + 36;
	xPosArrow = ($("#"+subMenu).children("a").width()/2) - 13 - xOffset;

	$(this.submenuDiv).css({
		"left" : xOffset+"px",
		"width" : width+"px"
	});
	//alert(xPosArrow);
	$(this.submenuDiv).prepend("<div><img src='/us/flash/p2main/images/gnb_subnav_left.png' width='18' height='126' /></div>");
	$(this.submenuDiv).prepend("<div class='gnb_arrow'><img src='/us/flash/p2main/images/gnb_arrow.png' width='27' height='10' /></div>");
	$(this.submenuDiv).append("<div><img src='/us/flash/p2main/images/gnb_subnav_right.png' width='18' height='126' /></div>");
	$("#"+subMenu+" .gnb_arrow").css("left", xPosArrow+"px");
}

$(function() {
	gnbSubmenu("gnb_tv",-185);
	gnbSubmenu("gnb_mob",-170);
	gnbSubmenu("gnb_pho",-145);
	gnbSubmenu("gnb_off",-420);
	gnbSubmenu("gnb_app",-475);
	gnbSubmenu("gnb_sup",-634);
})



// SECONDARY PROMO CAROUSEL

$(function() {
    // Create carousel
    // At launch, not needed as there are only 4 secondary promos
    $("#secondary_boxes").jCarouselLite({
        btnGo: ["#sb_pg1", "#jc", "#jc", "#jc", "#sb_pg2", "#jc", "#jc", "#jc", "#sb_pg3", "#jc", "#jc", "#jc"/*, "#sb_pg4", "#jc", "#jc", "#jc"*/],
        btnNext: "#jc",
        btnPrev: "#jc",
        visible: 4,
        scroll: 4,
        circular: false,
        start: 0
    });
    
    // Attach click events to pagination controls
    $(".sb_page").click(function() {
    	$(".sb_sel").removeClass("sb_sel");
    	$(this).addClass("sb_sel");
    });
    $("#secondary_boxes li").css("display","block"); 
	setTimeout('$("#sb_pg1").click()',300);

	if (!($.browser.msie && ($.browser.version < 7.0))) {
		$("#secondary_boxes li img").each(function() {
			src = $(this).attr("src");
			// Preload the hover states
			jQuery("<img>").attr("src",$(this).attr("src").replace(/\.png/,"_over.png"));
			
			$(this).mouseover(function() {
				if (!src.match("_over"))
					$(this).attr("src",$(this).attr("src").replace(/\.png/,"_over.png"));
			}).mouseout(function() {
				$(this).attr("src",$(this).attr("src").replace(/_over/,""));
			});
		})
	}
});




// SECONDARY PROMO POSITIONING

var spHt = 250;
var minTop = 518;
var minHt = 768;
var curTop = minTop;

function resizeSP() {
	newTop = -1;
	ht = document.body.clientHeight;
			
	if (ht < minHt) newTop = minTop;
	else newTop = ht - spHt;
	
	if (newTop != curTop) {
		curTop = newTop;
		$("#sp_content").css("top",newTop+"px");
	}
}

$(function() {
	resizeSP();
	window.onresize = function() { resizeSP(false) };
})

