// Set this for correct link rewriting
var RES_SITE1="http://www.res-americas.com";
var RES_SITE2="http://res-americas.com";
var flashOverlay;
var newContent="";
var NEXT_URL="";
var POST_TRANSITION_ACTION=null;
var NOFLASH=false;
var DEFAULT_TRANSITION="/flash/blade.swf";

// wait for the page to fully load before initializing
window.onload = init;

function init() {
	var transition;
	
	POST_TRANSITION_ACTION=showPage;

	// Fixes a dreamweaver problem
	document.getElementById("flashLayer").style.zIndex='2000';

	transition=$("meta[name='RESFlashTransition']").attr("content");
	if (!transition) {
		transition=DEFAULT_TRANSITION;
	}

	flashembed("flashLayer", 
		{
			src:transition,
			version: [8,0],
			onFail: function() { 
				NOFLASH=true;
        				document.getElementById("container").style.visibility='visible';
        				document.getElementById("flashLayer").style.visibility='hidden';
    					},
			id:'transition',
			wmode: 'transparent',
			allowscriptaccess: 'always',
			width: '100%', 
			height: '100%'
		});
		
	rewriteLinks();
}

function rewriteLinks() {
	if (NOFLASH) { return; }
	for (var i = 0; i < document.links.length; i++) {
		if ((document.links[i].href.indexOf(RES_SITE1)==0) || (document.links[i].href.indexOf(RES_SITE2)==0)) {
			if (document.links[i].href.indexOf("#")==-1) {
				if (document.links[i].className!="noFlash") {
					document.links[i].href = 'javascript:link(\"'+document.links[i].href+'\");';
				}
			}
		}
	}
}

function transitionComplete() {
	POST_TRANSITION_ACTION();
}

function transitionStarted() {
	// called on first frame of transition
      document.getElementById("container").style.visibility='visible';
      document.getElementById("flashLayer").style.backgroundColor='';
}

function showPage() {
	// Called by flash when the transition completes
      document.getElementById("flashLayer").style.zIndex='-10';
	// If postTransition function exists then call it  
	  if(typeof postTransition == 'function') {
		postTransition();
	  } 
}

function doFadeOut() {
      document.getElementById("flashLayer").style.zIndex='2000';
	if (!flashOverlay) {
		flashOverlay=document.getElementById("transition");
	}
	flashOverlay.fadeOutTransition();
}

function loadContent() {
	POST_TRANSITION_ACTION=showPage;
	window.location.href=NEXT_URL;
}

function link(url) {
	NEXT_URL=url;
	POST_TRANSITION_ACTION=loadContent;
	doFadeOut();
}


