/*	VAIO SPECIFIC CORE FUNCTIONS
 *	AUTHOR: Branden Thompson, Branden.Thompson@am.sony.com
 *	DATE: June 27, 2008
 *
 *	FUNCTION GUIDELINES:
 *	"If it's VAIO specific, it goes here, if not, it's a seperate file"
 *
 *	- ***DOCUMENT YOUR FUNCTIONS***
 *	- PRE-CONDITION and POST-CONDITION YOUR FUNCTIONS!
*/

/*	FUNCTION:  footnoteDispenser()
 *	----------------------------------------------------------------------------
 *	PRE-CONDITIONS:		file must be included in the calling HTML page.
 *
 *						- buttonID, contentDivID and messageID must be valid
 *						  and unique identifiers that exist on the page.
*/

function footnoteDispenser(buttonID, contentDivID, messageDivID, offClass, onClass)
{
	var ourButton = $(buttonID);
	var checkDiv = $(contentDivID);
	var ourMessage = $(messageDivID);
	//alert(ourButton.className + "\n" + checkDiv.className);
	
	if(ourButton.className == onClass)
	{
		// this means that the footnotes are not showing so....
		
		// set the class of the button to "minus"
		ourButton.className = offClass;
		checkDiv.style.display = 'block';
		ourMessage.innerHTML = 'Click to hide footnotes.';
	}
	else
	{
		ourButton.className = onClass;
		ourMessage.innerHTML = 'Click to view footnotes.';
		checkDiv.hide();
	}
}


/*	FUNCTION: shadowBoxInit()
 *	----------------------------------------------------------------------------
 *	PRE-CONDITIONS:		this file must be included in the calling HTML page.
 *						
 *						shadowbox.js must also be included in the calling HTML
 *						page BEFORE this file is linked.
 *
 *	POST-CONDITIONS:	defined Shadowbox options will be initialized when this
 *						function is called.
 *	----------------------------------------------------------------------------
*/

function shadowBoxInit()
{	
	// we're simply defining our customizable options in Shadowbox.
	// this is related to Shadowbox.js	
	var options = {
		loadingImage: '../img/ajax-loading.gif'
	};

	// initialize the options we just defined in the Shadowbox.
	Shadowbox.init(options);		
}// end function

/*	FUNCTION: showSeriesTab([string]theContainerID, [string] theSeriesTabID, [string] theDefaultTabID)
 *	----------------------------------------------------------------------------
 *	PRE-CONDITIONS:		this file must be included in the calling HTML page.
 *
 *						theContainerID must be a valid, defined element within
 *						the calling HTML document.
 *
 *						all potential IDs that will be called by theSeriesTab
 *						must be within theContainerID, WITH THE EXCEPTIOON OF
 *						theDefaultTabID.	
 *
 *						theSeriesTabID must be valid, defined element ID witin
 *						the HTML document.
 *
 *	POST-CONDITIONS:	showSeriesTab will be displayed, while all other elements
 *						within theContainerID will be hidden.  
 *
 *						If theDefaultTabID is also specified, this element 
 *						will also be hidden unless if happens to also be the 
 *						element called by theSeriesTabID.
 *	----------------------------------------------------------------------------			
*/
function showSeriesTab(theContainerID, theSeriesTabID, theDefaultTabID)
{
	 // let's make an element for our Tab:
	var theTabWeWant = document.getElementById(theSeriesTabID);
	var ourContainer = document.getElementById(theContainerID);

	// this is our default tab... if it's specified.
	if(theDefaultTabID)
	{	
		var theDefaultTab = document.getElementById(theDefaultTabID);
		ourContainer.style.display = "block";
		theDefaultTab.style.display = "none";
	}
	
	// let's look for our div container with all the inline DIVs with tab content:
	var ourTabContainer = document.getElementById(theContainerID);
	
	// let's also create a new variable for us to parse through this stuff:
	var ourInsideElements = ourTabContainer.getElementsByTagName('div');
	
	
	// let's look for all of our nested DIVs within the container...
	for(var i = 0; i < ourInsideElements.length; i++)
	{
		
		// ...but we only want the first level of DIVS, all of which have the class "hidden":
		if(ourInsideElements[i].className == "hidden" || ourInsideElements[i].className == "jsEnabled" )
		{
			// alert (ourInsideElements[i].id);  //- for debugging purposes.
			
			// let's reset our displays to "off"
			ourInsideElements[i].style.display = "none";
									
		}
		
	}
	
	// now let's turn our tab "on":
	ourContainer.style.display = "block";
	ourContainer.className = 'jsEnabled';
	theTabWeWant.style.display = "block";
	theTabWeWant.className = 'jsEnabled';
	}
// end function.


/*	FUNCTION: 	displayFlashHeader([string]divID, [int]width, [int]height, 
 *				[string/html]headerString, [string]alignment, [int]offsetX, 
 *				[int]offsetY, [string]objectAlign,[string]leading)
 * 	----------------------------------------------------------------------------
 *	PRE-CONDITIONS:		divID must exist and be a properly defined element 
 *						within the HTML document.
 *
 *						width, height, and headerString all MUST be defined.
 *
 *
 *	POST-CONDITIONS:	function will write the flash Object 
 *						
 *						specified and/or default variables will be passed to 
 *						the flash object so it can render headerString into a 
 *						smooth, visually appealling font.
 *	----------------------------------------------------------------------------
*/

// let's define a global variable that the function can simply re-use, rather
// than taking up more memory by using a new copy of the variable every function
// call.
var thisSWFObjectHeader;


function displayFlashHeader(divID, width, height, headerString, alignment, offsetX, offsetY, leading)
{
	// let's start our by doing some fatal-error exception alerting.
	if((!(divID)) || (!(width)) || (!(height)) ) {
		alert("FATAL ERROR - displayFlashHeader(): Required arguments are missing.  Please review the page and fix accordingly.");
		return(false);
	}	
	
	// let's perform some basic logic tests here for our non-required variables:
	
	// if alignment is not specified, default to left:
	if((!(alignment)) || (alignment == '')) {
		alignment = "lt";
	}
	
	// if offsetY is null, not-defined or blank, set to 0:
	if ((!(offsetX)) || (offsetX == '')) {
		offsetX = 0;
	}
	
	// if offsetY is null, not-defined or blank, set to 0:
	if ((!(offsetY)) || (offsetY == '')) {
		offsetY = 0;
	}	
	
	// if leading is null, not-defined or blank, set to 0:
	if ((!(leading)) || (leading == '')) {
		leading = 0;
	}	
	
	// Now that we've made sure all parameters are defined one way or another, let's define our flash Object
	
	thisSWFObjectHeader = new SWFObject("FLASH/headerText.swf?t=" +new Date().getTime(), "hero_"+divID, width, height, "8", "#FFFFFF", true);
	
	thisSWFObjectHeader.addParam("wmode", "transparent");			// this parameter makes the background of the swf object transparent
	thisSWFObjectHeader.addParam("scale", "noscale");				// do we want to have the swfObject be scalable?
	thisSWFObjectHeader.addVariable("TextHTML",headerString);		// this paramater is the text to be displayed. HTML 1.0 is supported. <font> <p> <a> <b><br>...etc. For the font face, please use 'ITC Avant Garde Std Bk' or 'ITC Avant Garde Std Md', Flash defaults to ITC Avant Garde Std Bk
	thisSWFObjectHeader.addVariable("TextAlignment", alignment);	// this paramater is the horizontal offset from the left in pixel. 	
																	// If TextAlignment "right" is used. the coordinate offset will start from the right
	thisSWFObjectHeader.addVariable("OffsetX",offsetX);	
	thisSWFObjectHeader.addVariable("OffsetY",offsetY);				// this paramater is the vertical offset form the top
	thisSWFObjectHeader.addVariable("TextLeading",leading);				// this paramater is the vertical offset form the top
	thisSWFObjectHeader.addParam("salign", "lt");					// how do we want this aligned?  lt = left, rt = right.		
	thisSWFObjectHeader.write(divID); 								// write the swf file into the Div with the ID passed below:
}
// end function.






// Flash will use this function the get the pageName from the template and use that for Flash tracking
function getOmniturePageName() { return s.pageName; };

/*
	Flash uses this function to locate the correct SWF movie
*/
function thisMovie(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName]
	}
	else {
		return document[movieName]
	}
}

// SERVICES TAB FUNCTIONS

var currentTabId = "gettingStartedContent";
var currentServiceId = "pcOrientationPopupContent";

// Changes the active subtab of the services area.
function changeTab(newTabId) {
	$(currentTabId).addClassName('hideThis');
	$(newTabId).removeClassName('hideThis');
	var baseCurrent = currentTabId.replace('Content','');
	var baseNew = newTabId.replace('Content','');
	
	$(baseCurrent + 'Nav').removeClassName('activeNav');
	$(baseNew + 'Nav').addClassName('activeNav');
	
	currentTabId = newTabId;
}

// Changes which popup content is active.
function activateServicesPopup(newServiceId) {
	bsResetOverlayHeight();

	$(currentServiceId).removeClassName('activePopup');
	var newServiceObj = $(newServiceId);
	if (!newServiceObj.next()) {
		$('nextServiceBtn').addClassName('inactiveBtn');
	} else {
		$('nextServiceBtn').removeClassName('inactiveBtn');
	}
	if (!newServiceObj.previous()) {
		$('previousServiceBtn').addClassName('inactiveBtn');
	} else {
		$('previousServiceBtn').removeClassName('inactiveBtn');
	}
	newServiceObj.addClassName('activePopup');
	currentServiceId = newServiceId;
}

// Advances to the next sibling, if it exists.
function nextService() {
	if ($(currentServiceId).next()) {
		$(currentServiceId).removeClassName('activePopup');
		currentServiceId = $(currentServiceId).next().id;

		$(currentServiceId).addClassName('activePopup');
		if (!$(currentServiceId).next()) {
			$('nextServiceBtn').addClassName('inactiveBtn');
		}
		if ($('previousServiceBtn').hasClassName('inactiveBtn')) {
			$('previousServiceBtn').removeClassName('inactiveBtn');
		}
	}
}

// Goes to the previous sibling, if it exists.
function previousService() {
	if ($(currentServiceId).previous()) {
		$(currentServiceId).removeClassName('activePopup');
		currentServiceId = $(currentServiceId).previous().id;
		
		$(currentServiceId).addClassName('activePopup');
		if (!$(currentServiceId).previous()) {
			$('previousServiceBtn').addClassName('inactiveBtn');
		}
		if ($('nextServiceBtn').hasClassName('inactiveBtn')) {
			$('nextServiceBtn').removeClassName('inactiveBtn');
		}
	}
}

// Since each tab is a different size, this will set the overlay height based on the current tab.
function bsResetOverlayHeight() {
	$$(".overlaybackground").each(function(e){
	 	e.style.height = ($('content').getHeight()+ 391)+"px"; // 890px is thes height of the header, menu + footer
	});
}


// ONLOAD

Event.observe(window, 'load', function() {

	var params = {
		allowScriptAccess: "always",
		scale: "noscale",
		wmode: "opaque",
		bgcolor: "#ffffff"
		};
	var flashvars = {
	    XMLFileName: "/wcsstore/SonyStyleStorefrontAssetStore/flashfiles/xmls/backstage_banner.xml"
	  }

    swfobject.embedSWF("/wcsstore/SonyStyleStorefrontAssetStore/flashfiles/swfs/backstage_banner.swf", "backstageBanner", "960", "317", "9.0.0", null, flashvars, params);


	var stateSelector = new jcCustomSelectDynamic('state', {onchange: function(e){
		storeSelector.clearOptions();
		
		storeSelector.newOptionsFromArray(populateStoreSelect(stateSelector.view.rel));
	}, 
	defaultText: "Select a state"});
	
	stateSelector.clearOptions();
	
	var storeSelector = new jcCustomSelectDynamic('storeName', {onchange: function(e){
		$('storeInformation').update(getStoreInfo(storeSelector.view.rel));
	},
	defaultText: "Select a store"});

	stateSelector.newOptionsFromArray(createStateOptionList());
	
	if (window.location.hash) {
		var winHash = window.location.hash.replace('#','');
		
		if (winHash == 'gettingStarted') {
			setTimeout(function(){
				changeTab('gettingStartedContent');
				showSeriesTab('seriesTabs', 'backstageServicesTab', 'contentCopy');
			}, 1000);
		} else if (winHash == 'upgrade') {
			setTimeout(function(){
				changeTab('upgradeContent');
				showSeriesTab('seriesTabs', 'backstageServicesTab', 'contentCopy');
			}, 1000);
		} else if (winHash =='dataServices') {
			setTimeout(function(){
				changeTab('dataServicesContent');
				showSeriesTab('seriesTabs', 'backstageServicesTab', 'contentCopy');
			}, 1000);
		} else if (winHash == 'locator') {
			setTimeout(function(){
				showSeriesTab('seriesTabs', 'backstageLocatorTab', 'contentCopy');
			}, 1000);
		} else if (winHash == 'backstagePass') {
			setTimeout(function(){
				showSeriesTab('seriesTabs', 'backstagePassTab', 'contentCopy');
			}, 1000);
		}
		
	}
}.bind(this));