﻿/*
 *  Navigation / swf manager
 *  @author: christoffer@enedahl.com
 */

//get the querystring for this script, put it into 'var query'
var scriptElements = document.getElementsByTagName('script');
var scriptSrc = "";
for(var i = 0; i < scriptElements.length; i++){
    var script = scriptElements[i].src;
    if( script.toLowerCase().indexOf("navigation.js") > 0){
        scriptSrc = script;
    }
}
var query = getQuery( scriptSrc.replace(/.*\?/, '') );

//Start using the query
//Get swf dimentions of this page's swf if any
var contentHeight = query.Height? query.Height: 970;
var contentWidth  = query.Width ? query.Width : 350;

var flashvars = {};

//Generate a unique communication id for this page
flashvars.connectionName = "Azaya_"+ Math.random();
flashvars.PageID = query.PageID;
flashvars.user = query.ucid;
flashvars.game = query.game;
flashvars.userID = query.userID;
flashvars.email = query.email;
flashvars.screenName = query.screenName;
flashvars.lang = query.lang;

//Init flash params
var params = {};
var attributes = {allowScriptAccess: "sameDomain", scale:"noscale", id:"topMenu",bgcolor: "#400500"};

//What flashversion do we demand?
var flashVersion = "9.0.124"; //Released Apr 9 2008, security fixes

//Animation variables
//Settings:
var goalTimeSpan = 0;//1000;
var closeTimeSpan = 1000;//1000;

//Private 
var goalWidth = 0;
var goalHeight = 0;
var goalId;
var goalTime;
var startWidth;
var startHeight;

var topMenuUrl = "navigation/azayaTopMenu02.swf";
var sidebarUrl = "navigation/azayaSidebarMenu01.swf";

//Create top navigation swf embed
swfobject.embedSWF(topMenuUrl, "topMenuPlaceholder", 970, 80, flashVersion, "expressInstall.swf", flashvars, params, attributes);

//If sidebar div exist, load sidebar
try{
    var attSidebar = {scale:"noscale"}
    attSidebar.id = "flashSidebar";    
    //attributes.bgcolor = "#000000"; 
    var paramsSidebar = {};
    paramsSidebar.wmode = "transparent";
    swfobject.embedSWF(sidebarUrl, "flashSidebarPlaceholder", 210, 400, flashVersion, "expressInstall.swf", flashvars, paramsSidebar, attSidebar);

}catch(e){}

//Create main swf or hide it with an empty swf
if( query.Swf ){
    createMainSwf( query.Swf , query.SwfName, query.lang, query.ucid, query.game, query.wmode);
}else{
    contentHeight = 1;
    contentWidth = 1;    
    createMainSwf( "swf/empty.swf", "test", query.lang ); 
    
    resize( "flashContent", 0, 0);
    resize( "flashContentWrapper", 0, 0);
}

//Functions below

/**
 * getQuery
 * @param s url
 */
function getQuery(s) {
  var query = {};

  s.replace(/\b([^&=]*)=([^&=]*)\b/g, function (m, a, d) {
    if (typeof query[a] != 'undefined') {
      query[a] += ',' + d;
    } else {
      query[a] = d;
    }
  });

  return query;
}

// script included using test.js?a=10&amp;z=50
function getLastChild(el) {
  return (el.lastChild && el.lastChild.nodeName != '#text') ? getLastChild(el.lastChild) : el;
}


/**
 *  Creates the initial flashContent swf
 * @param swf the url to the swf to load
 * @param swfName the name of the application, localized to show in preloader
 */
function createMainSwf(swf, swfName, lang, ucid, game, wmode){
    swf = unescapeSwf(swf);
	var attributes2 = {bgcolor: "#400500"};
	attributes2.id = "flashContent";
	attributes2.allowScriptAccess = "sameDomain";
	if( wmode ){
	    //attributes2.wmode = wmode;
		params.wmode = wmode;
	}
	flashvars.swfUrl = swf;
	flashvars.swfName = swfName
	flashvars.user = ucid;
	flashvars.game = game;
	flashvars.lang = lang;

	swfobject.embedSWF( "ContentPreloader/ContentPreloader.swf" , "flashContentPlaceHolder", contentWidth, contentHeight, flashVersion, "expressInstall.swf", flashvars, params, attributes2);
	
}

/**
 *  Changes the flashContent swf, must
 */
function changeContentSwf(swf, swfName, flashVars){
    swf = unescapeSwf(swf);
	var att = { data: "ContentPreloader/ContentPreloader.swf" , width: contentWidth, height: contentHeight, scale: "noscale", allowScriptAccess: "sameDomain", bgcolor: "#400500" };
	var par = { flashvars: "connectionName="+ flashvars.connectionName + "&swfUrl="+ swf +"&swfName="+ swfName + "&" + flashVars};
	    
	var id = "flashContent";
	var myObject = swfobject.createSWF(att, par, id);

}

function unescapeSwf(swf){
    swf = swf.replace("{question}","?");
    swf = swf.replace("{equal}","=");
    return swf;
}

/**
 *  Resize a html element
 */
function resize(divId, newWidth, newHeight)
{
	contentWidth = newWidth;
	contentHeight = newHeight;

	try{
		document.getElementById(divId).style.width = newWidth + 'px';
		document.getElementById(divId).style.height = newHeight + 'px';
	}catch(e){}
}

/**
 *  Private self-calling animationResize function
 */
function animateResize(){

	var timeLeft = goalTime - new Date().getTime();
	var percent = 1-(timeLeft / goalTimeSpan);

	percent = sigmoid( (percent - 0.5) * 12 );
	
	if (timeLeft <= 0 ){					
		resize( goalId, goalWidth, goalHeight);
		resize( "flashContentWrapper", goalWidth, goalHeight);
		return; //done!
	}
	
	var diffW = goalWidth  - startWidth;
	var diffH = goalHeight - startHeight;
	resize( goalId, startWidth + (diffW*percent), startHeight + (diffH*percent));
	resize( "flashContentWrapper", startWidth + (diffW*percent), startHeight + (diffH*percent));
    
	setTimeout('animateResize()',33);
}

/**
 *
 */
function startResize(divId, newWidth, newHeight){
	startWidth = contentWidth;
	startHeight = contentHeight;

	goalTime = new Date().getTime() + goalTimeSpan;

	goalWidth = newWidth;
	goalHeight = newHeight;
	goalId = divId;

	setTimeout('animateResize()',50);
}

/**
 *
 */
function sigmoid(x)
{
	return 1.0 / (1.0 + Math.exp(-x));
}

/**
 *  Get a existing flashmovie object
 */
function getFlashMovie(movieName) {
    var isIE = navigator.appName.indexOf("Microsoft") != -1;
    return (isIE) ? window[movieName] : document[movieName];
}

/**
 *  Trigger close flash
 */
function closeFlashContent( swfName ){
    try{
        getFlashMovie("flashContent").closeDoor( swfName );	
    }catch(e){
        //Unable to call flashcontent
    }
}

/**
 *  Close existing flashContent, resize, changeSwf
 */
function navigateMenu( menuID, swf, width, height, swfName, flashVars){
    
    //Close (animate), name of next swf will need to be passed on close
    closeFlashContent( swfName );
    
    //Set current language
    flashVars.lang = query.lang;
	
    //Resize (animate)
	setTimeout("startResize('flashContent',"+ width +", "+ height +")", closeTimeSpan );
    
    //change swf
	setTimeout("changeContentSwf('"+ swf +"','"+ swfName +"', '"+ flashVars + "')", closeTimeSpan + goalTimeSpan );
    			    
}

/*function log(str){

    document.getElementById("out").innerHTML += str + "<br/>";
}*/