var browserWidth;
var browserHeight;
var browserHeightIncludes = 70 + 24; // 70 for top nav, 19 for the first line of the bottom nav


// called when page is loaded or resized
COACH.pageConfig.doResize=function()
{	
	
	
	
	//check initially for override values;
	this.checkOverride();
	
	var footerOffset = this.getFooterOffset();

	// Initialize Browser H & W
	browserWidth = $(window).width();
	browserHeight = $(window).height();
	
	// establish min sizes
	if (browserHeight < 500){
		browserHeight = 500;
	}
	if (browserWidth < 975){
		browserWidth = 975;
	}
	//calculate top edge of flashcontent	
	
	browserHeightIncludes = $('#flashHolder').css('top').replace(/[^-\d\.]/g, '');
	//trace('new browserHeightIncludes = '+browserHeightIncludes);
	
	
	if (this.scalePriority == 'maintainAspectRatio'){	
		
		var ratio = this.nativeHeight/this.nativeWidth;
		
		var newDivWidth = (browserWidth - this.leftEdge)-this.rightEdge;
		var newDivHeight = Math.floor(newDivWidth * ratio);
		
		var maxDivWidth = newDivWidth;

		var newDivBottom = newDivHeight + browserHeightIncludes;
		
		if (this.stayAboveFold === true && newDivBottom > browserHeight){

			newDivHeight = browserHeight - browserHeightIncludes;
			
			var ratio = this.nativeWidth/this.nativeHeight;
			newDivWidth = Math.floor(newDivHeight * ratio);
			
			if(newDivWidth > maxDivWidth)
			{
				newDivWidth = maxDivWidth;
				ratio = this.nativeHeight/this.nativeWidth;
				newDivHeight = Math.floor(newDivWidth * ratio);
			}
			
			//trace('fit by height newDivWidth :'+newDivWidth +' && maxDivWidth: '+maxDivWidth);
		}
		
		trace('browserWidth: ' + browserWidth + ' newDivWidth: ' + newDivWidth );

		// Set Footer Placement
		
	
		//var top = $('#flashHolder').css('top').replace(/[^-\d\.]/g, '');
		var totalFooterOffset = Number(browserHeightIncludes)+Number(footerOffset);
		//trace('totalFooterOffset == '+totalFooterOffset);
		//console.log('flashHolder top = '+top);
		var newSupplementaryY = String( Number(newDivHeight + totalFooterOffset)+'px');
		
		//trace('newSupplementaryY == '+newSupplementaryY);
		$('#supplementary').css({'position':'absolute'}).css({'top':newSupplementaryY});
		
		
		
		/*
		var top = Number($('#flashHolder').css('top').replace(/[^-\d\.]/g, ''))
		var height = Number($('#flashHolder').css('height').replace(/[^-\d\.]/g, ''));
		trace('top = '+top);
		trace('height = '+height);
		
		var newY  = top+height;
		newY = newY+'px';
		
		
		$('#supplementary').css({'position':'absolute'}).css({'top':newY});
		trace('newY = '+newY)
		*/

	} else if (this.scalePriority == 'maxHeight'){ // defaulting to preserve the ratio
	
		
		
		var ratio = this.nativeWidth/this.nativeHeight;
		
		var newDivHeight = Number(browserHeight - 110);
		var newDivWidth = (browserWidth - this.leftEdge)-this.rightEdge;
		
		// Set Footer Placement
		var newSupplementaryY = String( Number(browserHeight - 40) + 'px');
		$('#supplementary').css({'position':'absolute'}).css({'top':newSupplementaryY});
		
	}
	
	// apply div changes
	var newnativeWidthString = newDivWidth + 'px';
	var newnativeHeightString = newDivHeight + 'px';
	
	// Update Flash SWF size
	$('#flashHolder').css({'height':newnativeHeightString}).css({'width':newnativeWidthString});
	
	// override placeFooter function
	window.placeFooter = function(){ };
	
	// alight div if we've maintained aspect ratio
	if (this.scalePriority == 'maintainAspectRatio' && this.stayAboveFold === true){
		
		var diff = browserWidth - (newDivWidth+this.leftEdge+this.rightEdge);
		var newX; 
		
		
		if (this.divAlign == 'center'){ // centering
			var centerOffset = Math.floor(diff/2); 
			newX = this.leftEdge+centerOffset;
			
			/*
			if (newX < this.leftEdge){ // check to see we haven't overlapped the left edge
				newX = this.leftEdge;
			}
			
			if (newX+newDivWidth < browserWidth-this.rightEdge){ // check to see we haven't overlapped the left edge
				trace('too wide')
				newX = this.leftEdge;
			}
			*/
			
			/*
			trace('flashholder position left = '+($('#flashHolder').position().left))
			trace('flashHolder width = '+($('#flashHolder').width()));
			
			trace('window.width() = '+$(window).width())
			trace($('#flashHolder').position().left+$('#flashHolder').width());
			*/
		
		} else if (this.divAlign == 'right'){
			
			newX = diff-this.rightEdge;
		} else if (this.divAlign == 'left'){
			var newX = this.leftEdge;
		}
	}
	else if(this.scalePriority == 'maxHeight'){
		var newX= this.leftEdge;
	}
	

	// set new X position
	newX = newX+'px';
	$('#flashHolder').css({'left':newX});
	
	if (this.reportResize === true){
		getFlashMovie("CoachHP").updateFlashDimensions(newDivWidth, newDivHeight);
	}
	
	
	if(COACH.pageConfig.firstRun === false)
	{
		COACH.pageConfig.firstRun = true
		COACH.pageConfig.doResize();

	}
}

// set doResize when ready and on resize


$(document).ready(function()  {
	
	COACH.pageConfig.firstRun = false;
	COACH.pageConfig.doResize()
});
$(window).resize(function() {
	COACH.pageConfig.firstRun = false;
	COACH.pageConfig.doResize()
});


COACH.pageConfig.setDivAlignment = function(newAlign){
	this.divAlign = newAlign;
	COACH.pageConfig.doResize()
}




COACH.pageConfig.getFlashMovie = function(movieName) {
	return obj = $('#flashTarget')[0];
}


COACH.pageConfig.checkOverride = function(){
	
	var hashParams =  COACH.pageConfig.getHashParams();	
	
	for (var n in COACH.pageConfig)
	{	
		var overrideValue = hashParams[n];
		
		if(overrideValue != undefined && overrideValue!=COACH.pageConfig[n])
		{
			COACH.pageConfig[n] = overrideValue;			

		}		
	}
}

COACH.pageConfig.getHashParams = function() {

    var hashParams = {};
    var e,
        a = /\+/g,  // Regex for replacing addition symbol with a space
        r = /([^&;=]+)=?([^&;]*)/g,
        d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
        q = window.location.hash.substring(1);

    while (e = r.exec(q))
       hashParams[d(e[1])] = d(e[2]);

    return hashParams;
}

COACH.pageConfig.getFooterOffset = function()
{
	var offset;
	var country = this.country.toLowerCase();
	var pageType = this.pageType.toLowerCase();
	switch(country)
	{
		case 'us':		
		if(pageType=='lp')
		{
			offset = 26//16;
		}
		else if(pageType=='hp')
		{
			offset = 6;
		}
		break;
		case 'jp':
		if(pageType=='lp')
		{
			offset = 3;
		}
		else if(pageType=='hp')
		{
			offset = -13;
		}
		break;
		case 'cn':
		if(pageType=='lp')
		{
			offset = 4;
		}
		else if(pageType=='hp')
		{
			offset = -10;
		}
		break;
		default:
		break;
		
	}
	//trace('offset = '+offset);
	return offset;
}



function trace(s) {
	try { 
		console.log(s) 
	} catch (e) { 
		//alert(s) 
	}
};

