/* Add Iframe Support to Prototype */
Element.addMethods('iframe', {
	document: function(element) {
	  element = $(element);
	  if (element.contentWindow)
		  return element.contentWindow.document;
	  else if (element.contentDocument)
		  return element.contentDocument;
	  else
		  return null;
	},
	$: function(element, frameElement) { 
	  element = $(element);
	  var frameDocument = element.document();
	  if (arguments.length > 2) {
		  for (var i = 1, frameElements = [], length = arguments.length; i < length; i++)
			  frameElements.push(element.$(arguments[i]));
		  return frameElements;
	  }
	  if (Object.isString(frameElement))
		  frameElement = frameDocument.getElementById(frameElement);
	  return frameElement || element;
	}
});

var isHidden=true;

function getTrackingCode() {
	var host = document.domain;
	//var host = document.location.href
	if (host.indexOf('xperimentations') >= 0) {
		gaCode = "UA-8191530-2";
	} else {
		gaCode = "UA-8191530-1";
	}
	return gaCode;
}

function openPage(link) {
	
	//alert("open page");
	isHidden=false;
	$('contentIFrame').src = link;
	thisMovie("xperimentSite").sendToActionScript("mute");
	
}
function closePage() {
	
	//alert("close page");
	isHidden=true;
	$('content').hide();
	SWFAddress.setValue("/");
	//$('xperimentSite').focusFlash();
	thisMovie("xperimentSite").sendToActionScript("unmute");

}

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



function getLocation() {
	var file = $('contentIFrame').contentWindow.location.toString();
	file = file.replace("http://","").replace("https://","");
	file = file.substr(file.indexOf("/html"));
	return file;
}

function setLocation() {
	try {
		if (getLocation() != SWFAddress.getValue()) {
			SWFAddress.setValue(getLocation());
		}
	} catch (err) { }
}

function addressChange() {
	var value=SWFAddress.getValue();
	if (value == "" || value == "/" || value == "/k") {
		if (isHidden == false) {
			closePage();
		}
	} else {
		if (value != getLocation()) {
			if (isHidden == true) {
				openPage(value.substr(value.indexOf("/")+1))
			} else {
				$('contentIFrame').src = value.substr(value.indexOf("/")+1);
			}
		} 
	}
}

function updateHeight() {
	if (getLocation().indexOf("about:blank") == -1) {
		if (isHidden == false) {
			setLocation();
		} 
		try  {
			$('content').show();
			var iframeHeight = $('contentIFrame').contentWindow.document.body.scrollHeight+50;
			$('content').hide();
			if (iframeHeight < 500)	{
				$('content').show();
				iframeHeight = $('contentIFrame').contentWindow.document.body.height;
				$('content').hide();
				if (iframeHeight < 500) {
					setTimeout(updateHeight,500);
					$('content').hide();
				} else {
					$('contentIFrame').height=iframeHeight;
					if (isHidden==false) {
						$('content').show();
					}
				}
			} else {
				$('contentIFrame').height = iframeHeight;
				if (isHidden==false) {
					$('content').show();
				}
			}
		} catch(err)  {
				setTimeout(updateHeight,500);
				$('content').hide();
		}
	}
}

Event.observe(SWFAddress,SWFAddressEvent.CHANGE, addressChange.bindAsEventListener());