if (top.location != location) {
	top.location.href = document.location.href;
}

// sIFR (Scalable Inman Flash Replacement) version 1.1.4
// Contributions by Mike Davidson, Shaun Inman, and Tomas Jogin
// Associated Article: http://www.mikeindustries.com/blog/archive/2004/08/sifr

var required = 7;
var hasFlash = false;
if (navigator.appVersion.indexOf("MSIE") != -1 && navigator.appVersion.indexOf("Windows") != -1) {
	document.write('<SCR' + 'IPT LANGUAGE=VBScript\> \n');
	document.write('on error resume next \n');
	document.write('hasFlash = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash." & required))) \n');  
	document.write('<'+'/scr' + 'ipt\> \n');
} else {
	var plugin = (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"])?navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin:0;
	if (plugin) {
		var isVersion2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
		var flashDescription = navigator.plugins["Shockwave Flash" + isVersion2].description;
		var flashVersion = parseInt(flashDescription.charAt(flashDescription.indexOf(".") - 1));
		hasFlash = flashVersion >= required;
	}
}
 
if (hasFlash && document.getElementsByTagName && document.getElementsByTagName('html')[0]) {
	document.getElementsByTagName('html')[0].className += (document.getElementsByTagName('html')[0].className=='')?'hasFlash':' hasFlash';
}	
 
function SI_normalizeWhiteSpace(txt) {
	var rE = /\s+/gi;
	return txt.replace(rE,' ');
}
function SI_forceRedraw() {
	var d = document;
	if (d.body && d.body.style) {
		d.body.style.height = "1px";
		d.body.style.height = "auto";
	}
}
 
function TJ_replaceElement(elem,swf,w,h,textcolor,bgcolor,afv) {

	/*
		Author: Tomas Jogin <http://jogin.com/weblog/>
		Description: Iterates through elements, looking for ones to be replaced with flash, see above comment for description on arguments.
	*/

	if (!hasFlash) {
		return;
	}

	var d = document;

	elemarr = elem.split(">");
	
	PE = new Object();
	RE = new Object();
	
	has_parent = elemarr.length > 1;
	
	for (i=0; i<elemarr.length; i++)
	{
		es = elemarr[i];
		E = new Object();

		E.id = false;
		E.tagName = false;
		E.className = false;

		if (es.indexOf("#") >= 0) {
			E.id = es.substr(es.indexOf("#")+1, es.length);
			E.tagName = es.substr(0, es.indexOf("#"));
		} else if (es.indexOf(".")>=0) {
			E.className = es.substr(es.indexOf(".")+1, es.length);
			E.tagName = es.substr(0, es.indexOf("."));
		} else {
			E.tagName = es;
		}
		
		if (has_parent && i == 0) {
			PE = E;
		} else {
			RE = E;
		}	
	}


	if (afv != '') {
		afv = SI_normalizeWhiteSpace(afv);
		afv = '&'+afv;
	}

	var elems = d.getElementsByTagName(RE.tagName);
	var count = elems.length;

	if (!count) {
		return;
	}	
	
	i = 0;
	
	switches = 0;
	skips = 0;
	total = count;

		if (!h) {
			var noheight = true;
		}
		if (!w) {
			var nowidth = true;
		}

	while (count)
	{
		e = elems[i];
		i++;

		if (has_parent) {
			if (PE.className) {
				if (PE.className != e.parentNode.className) {
					count--; skips++; continue;
				}	
				if (PE.id) {
					if (PE.id != e.parentNode.id) {
						count--; skips++; continue;
					}
				}
			}		
		}
		
		if (RE.className) {
			if (e.className != RE.className) {
				count--; skips++; continue;
			}
			if (RE.id) {
				if(e.id != RE.id) {
					count--; skips++; continue;
				}
			}
		}

		if(e.getAttribute("ifrskip"))
		{	continue;	}

		if (noheight) {
			h = e.offsetHeight;
		}
		if (nowidth) {
			w = e.offsetWidth;
		}
		
		var txt;
		var bSkip = false;
		txt = SI_normalizeWhiteSpace(e.innerHTML);
		
		for (var c = 0; c < txt.length; c++)
		{
			if (txt.charCodeAt(c) > 255)
			{
				bSkip = true;
				break;
			}
		}
		
		if (bSkip)
		{
			count--; skips++; continue;
		}
		
		var txtEncoded = txt;
		txtEncoded = txtEncoded.replace(/%/g, '%25');
		txtEncoded = txtEncoded.replace(/[ ]/g, "%20");
		txtEncoded = txtEncoded.replace(/&amp;|&/g, "%26");
		txtEncoded = txtEncoded.replace(/\"/g, '%22');
		//var txtEncoded = escape(txt);
		
		var c = d.createElement('div');
		c.className = 'replaced-'+RE.tagName;
		e.parentNode.replaceChild(c,e);
		
		if(RE.tagName == "div")
			i++;

		count--;
		switches++;
		var wmode = (bgcolor == 'transparent') ? 'transparent' : 'opaque';
		var fv = 'txt='+txtEncoded+afv+'&w='+w+'&h='+h+'&textcolor='+textcolor;
		var swfHTML;
		swfHTML  = '<object class="sIFRobject" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="'+w+'" height="'+h+'">';
		swfHTML += '	<param name="movie" value="'+swf+'" />';
		swfHTML += '	<param name="wmode" value="'+wmode+'" />';		
		swfHTML += '	<param name="bgcolor" value="'+bgcolor+'" />';		
		swfHTML += '	<param name="flashvars" value="'+fv+'" />';
		swfHTML += '	<embed class="sIFRobject" src="'+swf+'" flashvars="'+fv+'" width="'+w+'" height="'+h+'" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer" wmode="'+wmode+'" bgcolor="'+bgcolor+'" />';
		swfHTML += '<'+'/object>';
		swfHTML += '<'+e.nodeName.toLowerCase()+((e.id)?' id="'+e.id+'"':'')+((e.className)?' class="'+e.className+'"':'')+' style="display: none" ifrskip="1">'+txt+'<'+'/'+e.nodeName.toLowerCase()+'>';
		c.innerHTML = swfHTML;
		txt='';
	}

}

function sIFR () {
	TJ_replaceElement('h1','content/astonmartin/flash/sIFR.swf','','','#c7b964','#ffffff','');
	SI_forceRedraw();
}

// Class name management
function matchClassName(ptrEl, strClassName) {
	var arrClasses = ptrEl.className.split(" ");
	var i, intIndex = -1;
	
	for (i = 0; i < arrClasses.length; i++) {
		if (arrClasses[i] == strClassName) intIndex = i;
		break;
	}
	
	return intIndex;
}

function addClassName(ptrEl, strClassName) {
	if (matchClassName(ptrEl, strClassName) == -1) {
		ptrEl.className += " " + strClassName;
	}
}

function removeClassName(ptrEl, strClassName) {
	var arrClasses = ptrEl.className.split(" ");
	var i;
	
	for (i = arrClasses.length; i >= 0; i--) {
		if (arrClasses[i] == strClassName) arrClasses.splice(i,1);
	}
	
	ptrEl.className = arrClasses.join(" ");
}

function searchClick() {
	if (this.value == "Search") {
		this.value = "";
	}
}

function searchBlur() {
	if (this.value == "") {
		this.value = "Search";
	}
}

function searchSubmit() {
	if (ptrTxtSearch.value == "Search") {
		ptrTxtSearch.value = "";
	}
	return true;
}

function duplicateControls() {
	var intViewHeight = 0;
	var intPageHeight = 0;
	var intMaxScroll = 224;
	var arrDivs = new Array();
	var i = 0;
	var intMatchedSequences = 0;
	var ptrContent, ptrSequence;
	
	if (!document.getElementsByTagName) return;
	
	if (!!window.innerHeight && !!document.body.clientHeight) {
		intPageHeight = document.body.clientHeight;
		intViewHeight = window.innerHeight;
	} else if (document.documentElement && document.body.clientHeight) {
		if (document.documentElement.clientHeight > 0) {
			intPageHeight = document.body.clientHeight;
			intViewHeight = document.documentElement.clientHeight;
		}
	}
	
	if (intPageHeight - intViewHeight > intMaxScroll) {
		// duplicate page controls
		arrDivs = document.getElementsByTagName("div");
		for (i = 0; i < arrDivs.length; i++) {
			if (matchClassName(arrDivs[i], "sequence") > -1) {
				if (intMatchedSequences == 0) ptrSequence = arrDivs[i];
				intMatchedSequences++;
			}
		}
		
		// only duplicate if there's a single occurance of the sequence
		if (intMatchedSequences > 1) {
			return;
		} else if (intMatchedSequences == 1) {
			ptrContent = document.getElementById("content");
			
			if (ptrContent) ptrContent.innerHTML += "<div class=\"sequence\">" + ptrSequence.innerHTML + "</div>";
			SI_forceRedraw();
		}
	}
}


var ptrFrmSearch;
var ptrTxtSearch;

function initialise() {
	ptrFrmSearch = document.getElementById("frmSearch");
	ptrTxtSearch = document.getElementById("txtSearch");
	
	if (ptrTxtSearch) {
		ptrTxtSearch.onclick = searchClick;
		ptrTxtSearch.onblur = searchBlur;
	}
	
	if (ptrFrmSearch) ptrFrmSearch.onsubmit = searchSubmit;
	
	duplicateControls();
	sIFR();
}