/// Code to open and position the iframe
var generatedIFrame = null;
var flashObId = null;
var iframeTimeout;
var flashParent;

//this is called before all others - gives us the id of the flash movie
function setFlashObId(id){ flashObId = id; }

function callFlashProxy(action, args){
	//args is a string of name:value pairs, comma seperated
	document.getElementById(flashObId).callFlashProxy(action, args);
}

function restyle (object, rules){
	 for (var rule in rules){
		object.style[rule] = rules[rule];
	}
}

function findPosition(obj){
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	return {'x':curleft,'y':curtop};
}

function doIFrame(left, top, width, height, url, urlParameters){				
	// find the parent node of the flash movie				
	flashParent = document.getElementById(flashObId).parentNode;
	
	removeIFrame();

	width 	= parseInt(width);
	height 	= parseInt(height);
	left 	= parseInt(left);
	top 	= parseInt(top);

	//offsets - iframe needs to be smaller than the proxy panel
	// this offset should be incorporated inside the flash code rather than here
	width-=39;
	height-=50;
	left+= 22;
	top+=42;
	
	generatedIFrame = document.createElement("iframe");
	generatedIFrame.scrolling = 'no';
	generatedIFrame.id = "theRegIFrame1";
	//set the iframe url - including any parameters passed in
	generatedIFrame.src = url + "?" + urlParameters;
	generatedIFrame.frameBorder = 0;

	restyle (generatedIFrame, {
		'position' : 'absolute',
		'width' 	: width + 'px',
		'height' 	: height + 'px',
		'top' 	: top + 'px',
		'left' 	: left + 'px'
	});
	
	iframeTimeout = setTimeout("displayIframe()", 500);
	return true;
}

function displayIframe(){
	flashParent.appendChild(generatedIFrame);
}

function removeIFrame(){
	clearTimeout(iframeTimeout);
	if(generatedIFrame!=null){
		generatedIFrame.parentNode.removeChild(generatedIFrame);
		generatedIFrame=null;
		return true;
	}
	return false;				
}


function getIframeParent() {
	if (parent != null && parent != this && parent.getIframeParent != null) {
		return parent;
	}
	else {
		return null;
	}
}

var _emrw_lc_id=null;
function getFlashLocalConnectionId() {
	regproxy_log("getFlashLocalConnectionId _emrw_lc_id = "+_emrw_lc_id);
	// generate a local connection id which can be used
	// just for this browser window
	// this might be called specifically from the same window or from an iframe
	if (_emrw_lc_id == null) {
		if (getIframeParent() != null) {
			_emrw_lc_id = getIframeParent().getFlashLocalConnectionId();
			regproxy_log("getFlashLocalConnectionId from parent = "+_emrw_lc_id);
		}
		if (_emrw_lc_id == null) {
			_emrw_lc_id = Math.random()+"";
			regproxy_log("generated random getFlashLocalConnectionId = "+_emrw_lc_id);
		}
	}
	return _emrw_lc_id;
}


function closeWidget(mediaServerUrl) {
	// close the widget if this is not safari
	// this function is called from the actual iframe
	// therefore we have to reference "callFlashProxy" from the parent
	var ua = navigator.userAgent.toLowerCase();
	var isSafari = (ua.indexOf('safari')>-1);
	if (isSafari){
		insertSafariCookieFixFlashMovie(mediaServerUrl);
	} else {
		getIframeParent().callFlashProxy("complete", "");
	}
}


function insertSafariCookieFixFlashMovie(mediaServerUrl) {
	// insert the flash movie, which sends the refering url to the main flash movie, into the page 
	// the main flash movie then downloads the content of the url (and does nothing with it)
	// but the action of downloading the page sets the blue cookie in the main page
	// without this code safari does not "pass" cookies from the iframe to the main window
	regproxy_log("inserting insertSafariCookieFixFlashMovie");
	cookieUrl = document.referrer;
	regproxy_log("cookieUrl = "+cookieUrl);
	var fvars = true;
	var flashparams = {
		menu: "false",
		scale: "noscale",
		menu: "false",
		quality: "best",
		salign: "tl",
		base: ".",
		align: "t",
		bgcolor: "#FFFFFF",
		flashVars: "cookieUrl="+escape(cookieUrl),
		allowFullScreen: "true",
		allowScriptAccess: "always",
		wmode: "transparent"
	};
	var attributes = {
		id: "safaricookiefix",
		 name: "safaricookiefix"
	};

	swfobject.embedSWF(
		mediaServerUrl+"/dxd/swfs/safaricookiefixer.swf", 
		"safaricookiefixdiv", 
		"1", 
		"1", 
		"9.0.115",
		"http://media.international.com/EMEA/it-IT/swf/dcom/expressInstall.swf", 
		fvars, 
		flashparams, 
		attributes
		);
	regproxy_log("insertSafariCookieFixFlashMovie insert done");

}

function getConsole(){
	//need to check for console this way, testing console==null throws a reference error if it's not defined - and that stops the script running
	if (window["console"] == null){
		window["console"] = {log:function(value){}, error:function(value){}};
	}
	return window["console"];
}

function regproxy_log(msg) {
	getConsole().log("Regproxy JS:"+msg);
}

function regproxy_error(msg) {
	getConsole().error("Regproxy JS:"+msg);
}