/**
 * @author Henry Schmieder | thoughtomatic.co.uk
 */
window.addEvent( "domready", init );
window.addEvent( "load", initLoad );
window.addEvent( "resize", resizeListener );

var DEBUG = false;

function init()
{
	new EdgeTechCheck();
	new EdgeNavigation();
	checkExtendedViewport();
	new EdgeCredits();
	calcF();
}

function initLoad()
{
	calcF();
	//new EdgeTextPreparer();
}

var FCPRE = new Class
({
	initialize: function()
	{
		$("fWarn").setStyle("display", "none");
	}
});

var EdgeTechCheck = new Class
({
	shell:null,
	outerShell:null,
	
	initialize: function()
	{
		if( !$chk($("fWarn")) ) {
			return;
		}
		$("fWarn").setStyle("display", "block");
		this.shell = $("fWarnInner");
		this.outerShell = $("fWarn");
		var fCheck = new Swiff2();
		var version = fCheck.getVersionDetails();
		var major =  version[0];
		if( major < 8 ) {
			this.resizeLayer();
			window.addEvent( "resize", this.resizeListener.bind( this ) );
			$("fWarnJs").setStyle("display", "none");
			$("fWarnFl").setStyle("display", "block");
		}else{
			if( $chk( this.outerShell ) ) {
				this.outerShell.setStyle( "display", "none" );
			}
		}	
	
	},
	resizeLayer: function()
	{
		var h = (window.getSize().y < 500) ? 500  :  window.getSize().y;
		this.outerShell.setStyle( "height", h+"px" );
		$("rootcontainer").setStyles( { height:h+"px" , overflow:"hidden" } );
		this.shell.adopt( $("footer").setStyle("top", "370px")  );
		this.shell.adopt( $("logo") );
	},
	
	requireHigherFlash: function()
	{
		//console.log( "require higher Flash" );
	},
	requireFlash: function()
	{
		//console.log( "require Flash" );
	},
	resizeListener: function()
	{
		this.resizeLayer();
	}
});


var EdgeTextPreparer = new Class
({
	
	initialize: function()
	{
		$$(".aExt a").each( function( el ) {
			var href = el.get("href");
			if( href && href.indexOf( REMOTE_BASE ) < 0 ) {
				el.set( "target", "_blank" );
			}
		});
	}
});

var EdgeCredits = new Class
({
	initialize: function()
	{
		if( !$chk( $("credit") ) ) return;
		$("credit").addEvent( "mouseenter", this.enter.bind(this) );
		$("creditHover").setStyle( "visibility", "visible" );
		$("creditHover").set( "opacity", 0 );
		
		if( !navigator.platform.match(/win/i) && navigator.userAgent.match( /Firefox/ )  ) {
			$("creditHover").setStyle( "left", "270px" );
		}
	},
	enter: function()
	{
		$("creditHover").fade(1);
		$("footer").removeEvents( "mouseenter" );
		$("footer").addEvent( "mouseleave", this.out.bind(this) );
	},
	out: function()
	{
		$("footer").removeEvents( "mouseleave" );
		$("creditHover").fade(0);
	}
});

var EdgeNavigation = new Class
({
	so: null,
	cPanel:null,
	
	initialize: function( cPanel )
	{	
		if( !$chk( $("navigation") ) ) {
			return;
		}
		var s2 = new Swiff2();
		var version = s2.getVersionDetails();
		var major =  version[0];
		
		var description = this.getDescription();
		
		var ac = ACTIVE;
		if( !ac ) {
			if( $chk( $$("#navigation li.active a")) ) {
				 ac = String( $$("#navigation li.active a").get("text") );
			}else{
				ac = null;
			}
		}
		
		if( major < 8 ) return;
		
		var wMode;
		if( major < 9 ) wMode = "window";
		else wMode = "transparent";
		var id = "navigation";
		
		//var inFl = new Element( "div", { id:"navFlIn" } ).inject( $(id) );
		//wMode = "window";
		
		 new Element( "div", {html:$( id ).get("html"), id:id, "class":"falt" } ).inject( $("contentContainer", "top") );
		 
		 
		var ref = this;
		
		var params = {
			swLiveConnect: false,
			allowScriptAccess: "always",
			wMode: wMode,
			bgcolor:"#ffffff"
		};
		var fVars = {
			active: ac,
			description: description
		};
		
		this.so = new Swiff
		(
			REMOTE_BASE + "swf/navigation.swf",
			{
				id: "fl-navigation",
				width: 746,
				height: 70,
				params: params,
				vars: fVars,
				container: $(id),
				callBacks: {
					//clickContact: ref.onContactClicked.bind( ref ),
					onLoad: ref.naviInitializeCompleteHandler.bind( ref )
				}
			}
		);
	},
	getDescription: function()
	{
		var desc = [];
		var item;
		$$("#navigation li a").each( function( el ) {
			if(  el.getParent().hasClass( "byp") ) {
				return;
			}
			item = {
				url : el.get( 'href' ),
				label : el.get( "text" )
			};
			desc.push( item );
		}.bind( this ));
		desc = JSON.encode( desc );
		return desc;
	},
	reveal: function()
	{
		Swiff.remote( this.so.toElement(), "reveal" );
	},
	onContactClicked: function()
	{
		return this.cPanel.clickOpenHandler();
		if( this.cPanel.foldOutAvail ){
			this.cPanel.slideOut();
		}else{
			this.cPanel.slideIn();
		}
	},
	naviInitializeCompleteHandler: function()
	{
		this.reveal();
	}
});

var TeaserTextMajor = new Class
({
	so:null,
	src: "swf/teaserBoxMajor.swf",
	width:0,
	height:0,
	size:70,
	
	initialize: function( line1, line2, line3, container, width, height, useMasking, size, bgColor )
	{
		this.render( line1, line2, line3, container, width, height, useMasking, size, bgColor );
	},
	
	render: function( line1, line2, line3, container, width, height, useMasking, size, bgColor )
	{
		if( Swiff2.getMajor() < 8 ) return;
		
		container = !container ? line1.getParent() : container;
		if( size ) {
			this.size = size;
			if( size == 80 ) {
				this.src = "swf/teaserBoxMajor80.swf";
			}else if( size == 60 ) {
				this.src = "swf/teaserBoxMajor60.swf";
			}
		}
		width = !width ? 315 : width;
		height = !height ? ( size == 80 ? 140 : 130 ) : height;
		this.width = width;
		this.height = height;
		
		text1 = line1.get("html");
		text2 = line2.get("html");
		
		if( line3 ) {
			text3 = line3.get("html");
		}else{
			text3 = null;
		}
		
		var color1 = line1.getStyle( "color" );
		var color2 = line2.getStyle( "color" );
	
		if( line3 ) {
			var color3 = line3.getStyle( "color" );
		}
		
		
		//line2.setStyle( "display", "none" ); check
		
		
		var params = {
			swLiveConnect: false,
			allowScriptAccess: "sameDomain",
			wMode: "transparent",
			bgcolor:"#ffffff"
		};
		
		
		var fVars = {
			text1: String(text1),
			text2: String(text2),
			text3: String( text3 ),
			color1: String(color1),
			color2 : String(color2),
			color3: String(color3),
			useMasking: useMasking
		}
	 
		this.so = new Swiff
		(
			REMOTE_BASE + this.src,
			{
				id: "flash-headteaser-copy",
				width: this.width,
				height: this.height,
				params: params,
				vars: fVars,
				container: container
			}
		);

	}
});

var TeaserTextMinorSwap = new Class
({
	Extends: TeaserTextMajor,
	src: "swf/teaserBoxMajor60Swap.swf",
	initialize: function( line1, line2, line3, container, width, height )
	{
		width = !width ? 556 : width;
		height = !height ? 67 : height;
		this.render( line1, line2, line3, container, width, height );
	}
});


var TeaserTextMinor = new Class
({
	Extends: TeaserTextMajor,
	src: "swf/teaserbox.title.swf",
	initialize: function( line1, line2, line3, container, width, height )
	{
		width = !width ? 314 : width;
		height = !height ? 67 : height;
		this.render( line1, line2, line3, container, width, height );
	}

});


/****************************************
 *  Utilities
 *  **************************************/

var Swiff2 = new Class
({
	//Implements: Swiff,
	//Extends: Swiff,
	
	initialize: function()
	{
		this.contentStack = new Object();
	},
	
	/**
	* @param versionMajor int major player version 
	* 					|| array [ major, minor, revision ]
	* @param versionSwf string path to swf file for this major version
	* @return void
	*/
	setVersionContent: function( versionMajor, versionSwf )
	{
		var versionMinor = 0;
		var versionRev = 0;
		if( typeof versionMajor == 'object' ) {
			var v = versionMajor;
			versionMajor = v[0];
			versionMinor = v[1];
			versionRev = v[2];
		}
		this.contentStack['V'+versionMajor] = versionSwf;
		this.contentStack['r'+versionMajor] = versionRev;
		this.contentStack['m'+versionMajor] = versionMinor;
	},
	
	/**
	 * returns the optimal ( highest possible due to player requirement ) swf url matching client flash player version
	 * @return Object
	 */
	getOptimalContent: function()
	{
		var v = this.getVersionDetails();
		var thisMajor = v[0];
		var thisRev = v[2];
	
		var minDiff = 1000;
		for(var i in this.contentStack) {
			var n =  Number( i.substr(1, 3) );
			var tmpDiff = thisMajor - n;
			if(tmpDiff < minDiff && tmpDiff > -1)  {
				if( tmpDiff === 0 ) {
					if( this.contentStack['r'+thisMajor] <= thisRev ) {
						minDiff = tmpDiff;
					}
				}else{
					minDiff = tmpDiff;
				}
			}
		}
		
		if( tmpDiff <= 0 ) return { fname: this.contentStack['V'+n], version:n };
		return ( thisMajor - minDiff <= thisMajor ) ? { fname: this.contentStack['V'+(thisMajor - minDiff)].toString(), version: thisMajor} : null;
	},
	
	/**
	 * @return array or int 
	 * -1 : ie automation server error - flash might be installed though
	 * 0 : no flash installed
	 * [i,j,k ] major, minor, rev
	 */
	getVersionDetails: function()
	{
		var version = [0,0,0];
		if (navigator.plugins && navigator.mimeTypes.length) {
			version = navigator.plugins["Shockwave Flash"];
			if (version && version.description) {
				version = version.description;
				version = version.replace(/([a-zA-Z]|\s)+/, "").replace(/(\s+r|\s+b[0-9]+)/, ".").split(".");
			}else{
				version = [0,0,0];
			}
		}
		
		else if (typeof window.ActiveXObject != "undefined") {
			try {
				var a = new ActiveXObject( "ShockwaveFlash.ShockwaveFlash" );
				
				if (a) { // a will return null when ActiveX is disabled
					var d = a.GetVariable("$version");
					if (d) {
						d = d.split(" ")[1].split(",");
						version = [parseInt(d[0], 10), parseInt(d[1], 10), parseInt(d[2], 10)];
					}
				}
			}
			catch(e) { version = -1}
		}
		if( DEBUG && version < 0 ) {
			version = [9,0,115];
		}
		return version;
	}
});


Swiff2.getMajor = function()
{
	return new Swiff2().getVersionDetails()[0];
}
var ImagePreloader = new Class
({
	initialize: function()
	{
		imagesToPreload.each( function( img ) {
			if( $chk(img) ) new Asset.image( REMOTE_LAYOUT_BASE + img );
		});
	}
});
var AlphaPng = new Class
({
	initialize: function(options) {
		if (Browser.Engine.trident4)
		{
			this.fixElements( this );
		}
	},
	fixElements: function( classRef )
	{
		if( classRef != this ) return;
		//var images = [ $("logo") ];
		var images = $$(".trans");
		for (var i = 0; i < images.length; i++ )
		{
			var img =  images[ i ].getStyle( "background-image" );
			var rgx = /^.*\//;
			var a = img.substring(img.lastIndexOf( rgx ),img.match(/\.png/i).length + 2)
			img = img.substring( img.indexOf(a) + 3,img.length);
			img = img.replace( "\(\"","");
			img = img.replace( "\"\)","");
			images[ i ].setStyle( "filter", 'progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=\'true\', src=\'' + img + '\', sizingMethod=\'crop\')' );
			images[ i ].setStyle( "background", "none" );
		}
	}
});


function getRequestParameter(param) 
{
	var q = document.location.search || document.location.hash;
	if(q) {
		var pairs = q.substring(1).split("&");
		for (var i=0; i < pairs.length; i++) {
			if (pairs[i].substring(0, pairs[i].indexOf("=")) == param) {
				return pairs[i].substring((pairs[i].indexOf("=")+1));
			}
		}
	}
	return "";
}
function getRequestParametersArray()
{
	var q = document.location.search || document.location.hash;
	var res = {};
	if(q) {
		var pairs = q.substring(1).split("&");
		for (var i=0; i < pairs.length; i++) {
			res[ pairs[i].substring(0, pairs[i].indexOf("=")) ] = pairs[i].substring((pairs[i].indexOf("=")+1));
		}
	}
	return res;
}
function getCurrentSite()
{
	var loc = window.location;
	loc = loc.toString().replace( REMOTE_BASE, "" );
	loc = loc.toString().replace( /\.html?((\?|#)[^\/]*)?$/, "" );
	if( !$chk( loc ) ) loc = "index";
	return loc;
}



function calcF()
{
	if( !$chk( $("footer") ) ) return;
	return;
	
	var footerH = $("footer").getSize().y;
	var contH = $("contentContainer").scrollHeight;
	var bodyH = $("rootcontainer").scrollHeight;
	var wH= window.getSize().y;
	
	var diff = wH - contH - footerH;
	if( diff < 0 ) {
		diff = 0;
	}
	var s = getCurrentSite();
	
	
	switch( s )
	{
		case "index" :
			//diff -= 60;
			break;
		
		case "news" :

			//diff = 60;
			break;
			
		case "awards" :
			
			//diff = 60;
			break;
			
		case "showcase-glaxosmithkline" :
			//diff = 60;
			break;
			
		case "email-disclaimer" :
			diff -=40;
			break;
			
		case "films" : 
		
			break;
		
		case "gcc/services" :
			if( Browser.Engine.trident ) {
				return;
			}
		case "services" :
			
			if( Browser.Engine.trident ) {
				return;
			}
			
		default :
			//diff -= footerH;
			break;
		
	}

	
	$("footer").style.marginTop = diff+"px";
}


function checkExtendedViewport()
{
	try {
		if( document.body.getSize().x > 1090 ) {
			$("rootcontainer").addClass( "ext" );
		}else{
			$("rootcontainer").removeClass( "ext" );
		}
	}catch( e ) {}
}

function resizeListener( event )
{
	checkExtendedViewport();
	calcF();
}

