var agency = '';
var path = '';
var separatorChar = '_';
var actualContentHeight;
var swfName = 'vasava.swf';
var bgName = 'bg_vasava.jpg';
var jsReady = false;
var menu_old;
var bodyClicked = false;
var section = '';
var urlPrefix = "#";
var environment;
var launchFlarFlag = false;
var CDN_Path = 'http://wwwimages.adobe.com/www.adobe.com/';
//var CDN_Path = 'http://wwwimages.stage.adobe.com/www.stage.adobe.com/';

if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)){ //test for Firefox/x.x or Firefox x.x (ignoring remaining digits);
	var ffversion=new Number(RegExp.$1) // capture x.x portion and store as a number
	if (ffversion>=1){
	isFirefox=true;
	}
}else{
	isFirefox=false;
}

secure = false;

if (location.protocol == 'https:') {
	secure = true;
}

flashPath = '';
if (secure)
{
	flashPath = 'https://max.adobe.com/assets/flash/';
}
else
{
	flashPath = 'http://assets.max.adobe.com/flash/';
}


//-------------------------------------------------------------------------
// FUNCTION: isReady
//-------------------------------------------------------------------------

function isReady() 
{
	return jsReady;
}


//-------------------------------------------------------------------------
// FUNCTION: thisMovie
//-------------------------------------------------------------------------

function thisMovie(movieName) 
{
	if (navigator.appName.indexOf("Microsoft") != -1)
	{
		return window[movieName];
	}
	else 
	{
		return document[movieName];
	}
}

//-------------------------------------------------------------------------
// FUNCTION: thisMovieInFrame
//-------------------------------------------------------------------------

function thisMovieInFrame(movieName) 
{
	SWFMovie = $('#contentFrame').contents().find('object').get(0);
	return SWFMovie;
}

//-------------------------------------------------------------------------
// FUNCTION: pauseWelcomeVideo
//-------------------------------------------------------------------------

function pauseWelcomeVideo()
{
	movie = thisMovieInFrame("welcomeVideoSWF");
	movie.sendToActionScript('stop');
}

//-------------------------------------------------------------------------
// FUNCTION: resumeWelcomeVideo
//-------------------------------------------------------------------------

function resumeWelcomeVideo()
{
	movie = thisMovieInFrame("welcomeVideoSWF");
	movie.sendToActionScript('resume');
}

//-------------------------------------------------------------------------
// READY?
//-------------------------------------------------------------------------

$(document).ready(function()
{
	$heading = $(this);
	
	
  	// facebox
  	$('a[rel*=facebox]').facebox({
                           loading_image : '/javascripts/facebox/loading.gif',
                           closeImage   : '/javascripts/facebox/closelabel.gif',
													 opacity: 0.5
                         });
	
	switch (location.pathname)
	{
		case '/agenda/': 
		case '/registration/': 
		case '/sessions/': 
		case '/sponsor/': 
		case '/awards/': 
		case '/archives/': 
			$(function(){
				$heading.find('ul').slideDown(800);
			});
		break;

		default:
			$(function(){
				$(this).find('#nav>li>ul').css({'display' : 'block'});
			});
	}

	resizeContent();
	$(window).resize(function () { resizeContent(); });

	path = location.pathname;
	
	if (!jsReady)
	{
		if (location.href.indexOf('?') > 0)
		{
			agency = location.href.substring(location.href.indexOf('?') + 1);
		}
			
		// if (agency != 'vasava' && agency != 'red' && agency != 'akqa' && path != '/')
		if (agency != 'vasava' && agency != 'akqa' && path != '/')
		{
			agency = getCookie('agency');			
		}
		
		if (agency != 'vasava' && agency != 'akqa')
		{
			var rand_no = Math.random();

			// if (rand_no < 0.33)
			// {
			// 	agency = 'vasava';
			// }
			// else if (rand_no < 0.67)
			// {
			// 	agency = 'red';
			// }
			// else
			// {
			// 	agency = 'akqa';
			// }

			if (rand_no < 0.5)
			{
				agency = 'vasava';
			}
			else
			{
				agency = 'akqa';
			}

		}
		
		setCookie('agency', agency, 1);
		
		pic1= new Image(1980,1200); 
		pic2= new Image(1980,1200); 

		if (secure)
		{
			pic1.src="https://max.adobe.com/assets/flash/bg_" + agency + ".jpg"; 
			pic2.src="https://max.adobe.com/assets/flash/bg_" + agency + "_over.jpg"; 
		}
		else
		{
			pic1.src="http://assets.max.adobe.com/flash/bg_" + agency + ".jpg"; 
			pic2.src="http://assets.max.adobe.com/flash/bg_" + agency + "_over.jpg"; 			
		}
		
		$('body').click(function(e)
		{
			if (!secure)
			{
				if (e.target.id == 'main' || e.target.id == 'container' || e.target.id == 'content' || e.target.id == 'body' || e.target.id == 'contentFrame')
				{
					if (!bodyClicked)
					{
						if (section == 'welcome' && swfobject.hasFlashPlayerVersion('9.0'))
						{
							pauseWelcomeVideo();
						}
						launchFlash();
						bodyClicked = true;
					}
				}
			}
		});

		swfName = agency + '.swf';
		bgName = 'bg_' + agency + '.jpg';
		bgOverName = 'bg_' + agency + '_over.jpg';
	
		$('body').mouseover(function(e)
		{
			if (!secure)
			{
				if (e.target.id == 'main' || e.target.id == 'container' || e.target.id == 'content' || e.target.id == 'body' || e.target.id == 'contentFrame')
				{
					$('body').css("cursor", "pointer");
					$("body").css("backgroundImage", "url(http://assets.max.adobe.com/flash/" + bgOverName +")");
				}
			}
		});

		$('body').mouseout(function(e)
		{
			if (!secure)
			{
				if (e.target.id == 'main' || e.target.id == 'container' || e.target.id == 'content' || e.target.id == 'body' || e.target.id == 'contentFrame')
				{
					$('body').css("cursor", "auto");
					$("body").css("backgroundImage", "url(http://assets.max.adobe.com/flash/" + bgName +")");
				}
			}
		});
	
		//set the background image
		$("body").css("backgroundImage", "url(" + flashPath + bgName +")");
		jsReady = true;
		
		checkURL();
		
		path = location.pathname;
	
		if ((path.indexOf("/register") >= 0) || (path.indexOf("/sponsorlist") >= 0) || (path.indexOf("/mailinglist") >= 0) || environment == 'development')
		{
			actualContentHeight = 2000;
		}
		else
		{
			actualContentHeight = ($("#mainContent").height());
		}

		
		if ((path == '/') && (isFirefox))
		{
			launchWelcomeVideo();
		}

		resizeContentDiv();
		$(window).resize(function () { resizeContentDiv() });
	}
});


//-------------------------------------------------------------------------
// FUNCTION: getBasePath
//-------------------------------------------------------------------------

function getBasePath()
{
	var basePath = '';
	
	if (launchFlarFlag)
	{
		basePath = CDN_Path + 'events/max2009/flar/';
	}
	else
	{
		switch (agency)
		{
			case 'red': 
				// basePath = CDN_Path + 'events/max2009/red/';
				basePath = CDN_Path + 'events/max2009/vasava/';
			break;
		
			case 'akqa': 
				basePath = CDN_Path + 'events/max2009/akqa/';
			break;
		
			case 'vasava': 
				basePath = CDN_Path + 'events/max2009/vasava/';
			break;
		
			default:
					basePath = flashPath;
		}
	}
	
	return basePath;
}


//-------------------------------------------------------------------------
// FUNCTION: getSwfName
//-------------------------------------------------------------------------

function getSwfName()
{
	var basePath = getBasePath();
	var fullPath = '';
	
	if (launchFlarFlag)
	{	
		fullPath = basePath + 'MaxFlar.swf';
		launchFlarFlag = false;
	}
	else
	{
		switch (agency)
		{
			case 'red': 
				fullPath = basePath + swfName;
			break;
		
			case 'akqa': 
				fullPath = basePath + swfName;
			break;
		
			case 'vasava': 
				fullPath = basePath + swfName;
			break;		
		}
	}
	
	return fullPath; 
}


//-------------------------------------------------------------------------
// FUNCTION: sendToJavaScript
//-------------------------------------------------------------------------

function sendToJavaScript(value)
{
	//this function is called when the MAX shell close button is clicked
	$("#flash-foreground").css("visibility", "hidden");

	currentPage = $('#contentFrame').attr("src");

	if (section == 'welcome')
	{
		resumeWelcomeVideo();
	}else{
		enableAllFormFields();	
	}

	bodyClicked = false;
}

//-------------------------------------------------------------------------
// FUNCTION: launchFlash
//-------------------------------------------------------------------------

function launchFlash()
{
	if (section != 'welcome')
	{
		disableAllFormFields();	
	}
	
	switch (agency)
	{
		case 'vasava': 
		case 'red': 
		case 'akqa': 
		case 'flar':
			shellPath = CDN_Path + 'events/max2009/maxshell.swf';
		break;

		default:
		{
			shellPath = CDN_Path + 'events/max2009/maxshell.swf';
		}
	}

	var vars = {};

	var params = { scale:'noorder', salign:'lt', bgcolor:'#ffffff', wmode:'transparent', allowFullScreen:'true', quality:'best', allowScriptAccess:'always'};
	
	var attributes = { id:'flash-foreground', name:'flash-foreground' };
	swfobject.embedSWF(shellPath, "flash-foreground", "100%", "100%", "10.0.0", flashPath + "/expressInstall.swf", vars, params, attributes );

	$("#flash-foreground").height($(window).height());
	$("#flash-foreground").width($(window).width());
}


//-------------------------------------------------------------------------
// FUNCTION: launchFlar
//-------------------------------------------------------------------------

function launchFlar()
{
	if (swfobject.hasFlashPlayerVersion('10.0'))
	{
		launchFlarFlag = true;
		shellPath = CDN_Path + 'events/max2009/maxshell.swf';
	
		var flashvars = { CDN_path: CDN_Path + "events/max2009/flar/", FLV_path: CDN_Path + "events/max2009/flar/video/" };
	
		var params = { scale:'noorder', salign:'lt', bgcolor:'#ffffff', allowFullScreen:'true', quality:'best', allowScriptAccess:'always'};
		
		var attributes = { id:'flash-foreground', name:'flash-foreground' };
		swfobject.embedSWF(shellPath, "flash-foreground", "100%", "100%", "10.0.0", flashPath + "/expressInstall.swf", flashvars, params, attributes );
	
		$("#flash-foreground").height($(window).height());
		$("#flash-foreground").width($(window).width());
	}else{
		document.getElementById('launchFlar').src="/images/download-flash-10.png";
		document.getElementById('launchFlarLink').href="http://get.adobe.com/flashplayer/";
		document.getElementById('launchFlarLink').target="_blank";
		document.getElementById('launchFlarLink').onclick=null;		
		document.getElementById('noFlashFlar').innerHTML="<font style='color:red;'>Please install Flash player 10 or higher.</font>";		
	}
}


//-------------------------------------------------------------------------
// FUNCTION: checkURL
//-------------------------------------------------------------------------

function checkURL()
{
	url = location.pathname;

	if (url == '/')
	{
		section = 'welcome';
	}else{
		section = 'otherSection';
	}

	if (location.pathname != '/awards/finalists/') // don't check on this page since it has hash tags
	{
		if (location.href.indexOf(urlPrefix) > 0)
		{
			querystring = "";
			
			if (location.href.indexOf('?') > 0)
			{
				querystring = location.href.substring(location.href.indexOf('?'), location.href.indexOf(urlPrefix));
			}
	
			section = location.href.substring(location.href.indexOf(urlPrefix) + 1);
			if (location.href.indexOf(separatorChar) > 0)
			{
				firstLevel = location.href.substring(location.href.indexOf(urlPrefix) + 1, location.href.indexOf(separatorChar));
				secondLevel = location.href.substring(location.href.indexOf(separatorChar) + 1);
				location.href = "/" + firstLevel + "/" + secondLevel + "/" + querystring;
			}
			else if (location.href.substring(location.href.indexOf(urlPrefix) + 1) != '')
			{
				firstLevel = location.href.substring(location.href.indexOf(urlPrefix) + 1);
				
				//Hack for Sabina
				if ((firstLevel != 'hotels') && (firstLevel != 'general') && (firstLevel != 'registrations') && (firstLevel != 'press'))
				{
					location.href = "/" + firstLevel + "/" + querystring;
				}
			}
			else
			{
				location.href = "/" + querystring;
			}
		}
	}
}


//-------------------------------------------------------------------------
// FUNCTION: resizeContent
//-------------------------------------------------------------------------

function resizeContent()
{
	var windowHeight = ($(window).height());
	var windowWidth = ($(window).width());
	var contentHeight =	($("#contentFrame").height());
	var navHeight =	($("#navContainer").height());

	var isIE6;
	isIE6 = navigator.userAgent.toLowerCase().indexOf('msie 6') != -1;

	if(!isIE6){
		if($(window).width() > 1252){
			$("#contentFrame").width(1020);
		}else if($(window).width() > 952) {
			$("#contentFrame").width(windowWidth - 232);
		}else{
			$("#contentFrame").width(720);
		}
	}

	if(windowHeight > (navHeight + 60)){
		$("#contentFrame").height(windowHeight - 60);
	}else{
		$("#contentFrame").height(navHeight);
	}


/*	}
	else if($("#contentFrame").height() > 533)
	{
		//$("#content").height(navHeight);
		$("#contentFrame").height(windowHeight - 60);
	}else{		
		$("#contentFrame").height(534);
	}
*/
	
	//set Flash height
	$("#flash-foreground").height(windowHeight);
	$("#flash-foreground").width(windowWidth);
}


//-------------------------------------------------------------------------
// FUNCTION: setCookie
//-------------------------------------------------------------------------

function setCookie(c_name, value, expiredays)
{
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie=c_name + "=" + escape(value) + ((expiredays==null) ? "" : ";expires=" + exdate.toGMTString());
}

//-------------------------------------------------------------------------
// FUNCTION: getCookie
//-------------------------------------------------------------------------

function getCookie(c_name)
{
	if (document.cookie.length > 0)
  {
  	c_start = document.cookie.indexOf(c_name + "=");
  	if (c_start != -1)
    {
    	c_start = c_start + c_name.length + 1;
    	c_end = document.cookie.indexOf(";", c_start);
    	if (c_end == -1) 
			{
				c_end = document.cookie.length;
			}
   		return unescape(document.cookie.substring(c_start, c_end));
    }
  }
	return "";
}


//-------------------------------------------------------------------------
// FUNCTION: enableAllFormFields
//-------------------------------------------------------------------------

function enableAllFormFields()
{
	$('select').css("visibility","visible");
	$('input').attr("disabled", false);
}


//-------------------------------------------------------------------------
// FUNCTION: disableAllFormFields
//-------------------------------------------------------------------------

function disableAllFormFields()
{
	$('select').css("visibility","hidden");
	$('input').attr("disabled", true);
}



//-------------------------------------------------------------------------
// FUNCTION: showTips
//-------------------------------------------------------------------------

function showTips()
{
	$('#tips').show();
	$('#tiplink').hide();
	$("#mainContent").height($("#mainContent").height());
}



//-------------------------------------------------------------------------
// FUNCTION: resizeContentDiv
//-------------------------------------------------------------------------


function resizeContentDiv()
{

	var window = $(window);
	var mainContent = $("#mainContent");
	var contentFrame = $("#contentFrame");

	var navHeight = $("#navContainer").height();

	var headerHeight = 82;

	switch (location.pathname)
	{
		case '/agenda/': 
		case '/agenda/events/': 
		case '/agenda/community/': 
		case '/registration/': 
		case '/sessions/': 
		case '/sessions/tracks/': 
		case '/sessions/discussions/': 
		case '/sessions/russellbrown/': 
		case '/sponsor/': 
		case '/awards/': 
			headerHeight = 232;
		break;

		case '/':
		  if (environment != 'development')
		  {
	      headerHeight = 415;
	    }
		break;
	}


  if(mainContent.height() < (navHeight-headerHeight))
  {
    mainContent.height(navHeight-headerHeight);
  }

  if((contentFrame.height()-headerHeight) < actualContentHeight+20)
  {
    mainContent.height(contentFrame.height()-headerHeight);
  }

}


//-------------------------------------------------------------------------
// FUNCTION: launchWelcomeVideo
//-------------------------------------------------------------------------

function launchWelcomeVideo()
{
	var vars = {};
	var params = { scale:'noorder', salign:'lt', bgcolor:'#cccccc', wmode:'transparent', allowFullScreen:'false', quality:'best', allowScriptAccess:'always'};
	var attributes = { id:'welcomeVideoSWF', name:'welcomeVideoSWF' };
	swfobject.embedSWF("/flash/WelcomeVideo.swf", "welcomeVideo", "100%", "414", "10.0.0", "http://assets.max.adobe.com/flash/expressInstall.swf", vars, params, attributes );
}

//-------------------------------------------------------------------------
// FUNCTION: launchFlarAnimation
//-------------------------------------------------------------------------

function launchFlarAnimation()
{
	var vars = {};
	var params = { scale:'noorder', salign:'lt', bgcolor:'#cccccc', wmode:'transparent', allowFullScreen:'false', quality:'best', allowScriptAccess:'always'};
	var attributes = { id:'animation', name:'animation' };
	swfobject.embedSWF(CDN_Path + "events/max2009/max2009_ar_anim.swf", "animation", "300", "300", "10.0.0", "http://assets.max.adobe.com/flash/expressInstall.swf", vars, params, attributes );
}


//-------------------------------------------------------------------------
// FUNCTION: getFlarAsset
//-------------------------------------------------------------------------

function getFlarAsset()
{
	var assetPath = CDN_Path + "events/max2009/flar/";
	return assetPath; 
}

//-------------------------------------------------------------------------
// FUNCTION: getFlarFlv
//-------------------------------------------------------------------------

function getFlarFlv()
{
	var flvPath = CDN_Path + "events/max2009/flar/video/";
	return flvPath; 
}    
