var product ="http://www.noseahead.co.uk/"// 'http://localhost:83/noseahead.com/'; //
var tab = "mih";
var LoadComplete, GetImages, AddHandlers; 
function start(){
	LoadComplete = function (){
	    document.getElementById("loading").style.display = "none";
	    document.body.style.backgroundImage = "url(resources/images/" + bg+ ")";
	    document.body.style.backgroundColor = "#fff";
	    //document.body.style.backgroundColor = "#000";
	    document.getElementById("main").style.display = "block";
	    if (window.parent && window.parent != window){window.parent.document.getElementById("frame").src = "content/home.php"};
	    if (showlightonload){showLight();}
	    setTimeout("startTicker()", 7000);
	}
	GetImages = function (){
		Images[0] = product + "resources/images/t.png";
		Images[1] = product + "resources/images/h1.png";
		Images[2] = product + "resources/images/bgb.png";
		Images[3] = product + "resources/images/bfi.png";
		Images[4] = product + "resources/images/mkr.png";
		Images[5] = product + "resources/images/mqetest.png";
		Images[6] = product + "resources/images/mis.png";
		Images[7] = product + "resources/images/mih.png";
		Images[8] = product + "resources/images/min.png";
		Images[9] = product + "resources/images/mir.png";
		Images[10] = product + "resources/images/mic.png";
		Images[11] = product + "resources/images/mim.png";
		Images[12] = product + "resources/images/mirss.png";
		Images[13] = product + "resources/images/bfi.png";
		Images[14] = product + "resources/images/sb.png";
		Images[15] = product + "resources/images/bl.png";
		Images[16] = product + "resources/images/b.png";
		Images[17] = product + "resources/images/br.png";
		Images[18] = product + "resources/images/sl.png";
		Images[19] = product + "resources/images/sr.png";
		Images[20] = product + "resources/images/l.png";
		Images[21] = product + "resources/images/lo.png";
		Images[22] = product + "resources/images/rss.png";
		Images[23] = product + "resources/images/mleft.png";
		Images[24] = product + "resources/images/mright.png";
		Images[25] = product + "resources/images/mqetest.png";
		Images[26] = product + "resources/images/miso.png";
		Images[27] = product + "resources/images/miho.png";
		Images[28] = product + "resources/images/mino.png";
		Images[29] = product + "resources/images/miro.png";
		Images[30] = product + "resources/images/mico.png";
		Images[31] = product + "resources/images/mimo.png";
		Images[32] = product + "resources/images/mirsso.png";
		Images[33] = product + "resources/images/rho.png";
		Images[34] = product + "resources/images/inputo.png";
		Images[35] = product + "resources/images/goo.png";
		Images[36] = product + "resources/images/bg.png";
		Images[37] = product + "resources/images/regs.png";
		Images[38] = product + "resources/images/regso.png";
		Images[39] = product + "resources/images/lgo.png";
		Images[40] = product + "resources/images/lgoo.png";
		Images[41] = product + "resources/images/lgn.png";
		Images[42] = product + "resources/images/lgno.png";

    }
    AddHandlers = function(){
		if (document.getElementById("mih")){
			document.getElementById("mih").onmouseover = mnu_mo;
			document.getElementById("mih").onmouseout = mnu_ml;
			document.getElementById("mih").onclick = mnu_c;
			document.getElementById("min").onmouseover = mnu_mo;
			document.getElementById("min").onmouseout = mnu_ml;
			document.getElementById("min").onclick = mnu_c;
			document.getElementById("mir").onmouseover = mnu_mo;
			document.getElementById("mir").onmouseout = mnu_ml;
			document.getElementById("mir").onclick = mnu_c;
			document.getElementById("mic").onmouseover = mnu_mo;
			document.getElementById("mic").onmouseout = mnu_ml;
			document.getElementById("mic").onclick = mnu_c;
			document.getElementById("mim").onmouseover = mnu_mo;
			document.getElementById("mim").onmouseout = mnu_ml;
			document.getElementById("mim").onclick = mnu_c;
			document.getElementById("mirss").onmouseover = mnu_mo;
			document.getElementById("mirss").onmouseout = mnu_ml;
			document.getElementById("mirss").onclick = mnu_c;
			document.getElementById("lightbox").onclick = hideLight;
			document.getElementById("lgn").onmouseover = mnu_mo;
			document.getElementById("lgn").onmouseout = mnu_ml;
			document.getElementById("lgn").onclick = function(){window.frames[0].location = "content/members.php?method=login"};
			document.getElementById("lgo").onmouseover = mnu_mo;
			document.getElementById("lgo").onmouseout = mnu_ml;
			document.getElementById("regs").onmouseover = mnu_mo;
			document.getElementById("regs").onmouseout = mnu_ml;
			document.getElementById("regs").onclick = function(){window.frames[0].location = "content/members.php?method=register&action=get"};
			document.getElementById("blin1").onclick  = function(){window.open("http://www.sportinglife.co.uk");}
			document.getElementById("blin2").onclick  = function(){window.open("http://www.racingpost.co.uk");}
			document.getElementById("blin3").onclick  = function(){window.open("http://www.oddschecker.com");}
			document.getElementById("babo1").onclick  = function(){window.open("http://www.odyssey-consulting.co.uk");}
			var c = document.getElementById("basei").children || document.getElementById("basei").childNodes;
			for(var i = 0;i < c.length;i++){
				c[i].onmouseover = function(){this.style.textDecoration='underline';}
				c[i].onmouseout = function(){this.style.textDecoration='none';}
				if (c[i].id.indexOf("bnew") != -1){
					c[i].onclick = function(){tab = "min";umkr(tab);}	
				}else if (c[i].id.indexOf("bres") != -1){
					c[i].onclick = function(){tab = "mir";umkr(tab);}	
				}else if (c[i].id.indexOf("bcon") != -1){
					c[i].onclick = function(){tab = "mic";umkr(tab);}	
				}
			}
				
		}
//		document.getElementById("rss").onmouseover = mnu_mo;
//		document.getElementById("rss").onmouseout = mnu_ml;
//		document.getElementById("rss").onclick = mnu_c;	
		window.onresize = body_onresize;
		window.onscroll = wscroll;
		document.body.onscroll = wscroll;
	} 
	body_onresize();
   	Load(); 
}

function startTicker(){

	document.getElementById("itick").innerHTML = tick;
	document.getElementById("itick2").innerHTML = tick;
	document.getElementById("itick").style.left = 705;
	document.getElementById("itick2").style.left = 705 + document.getElementById("itick").clientWidth + 250;
	setInterval(otick, 10);
	
}

function otick(){
	var w = document.getElementById("itick").clientWidth;
	var o = document.getElementById("itick");
	var o2 = document.getElementById("itick2");
	if (Number(o2.style.left.replace("px", "")) < Number(o.style.left.replace("px", ""))){
		o = document.getElementById("itick2");
		o2	 = document.getElementById("itick");
	}
	var l = Number(o.style.left.replace("px", ""));
	var l2 = Number(o2.style.left.replace("px", ""));
	if (l2 < 705 - w){
		l2 -= 1;
		l = l2 + w + 250;
	}else{
		l -= 1;
		l2 -=1;
	}
	o.style.left = l + "px";
	o2.style.left = l2 + "px";
}

function body_onresize(){
	var w = 920;
	var h = 700;
	if (document.getElementById('main').style.width == ""){document.getElementById('main').style.width =w};
	if (document.getElementById('main').style.height == ""){document.getElementById('main').style.height =h};
	var l = (document.body.clientWidth / 2) - (w / 2);
	var t = (document.body.clientHeight / 2) - (h /2);
	if (t < 20){t =20}
	document.getElementById('main').style.left = l;
	//document.getElementById('main').style.top = t;
	wscroll();
}

function mnu_mo(e){

	e = e || event;
	var id = "";
	if (e.target){id = e.target.id}else{id = e.srcElement.id}
	if (id.substr(0, 1) == "_"){id = id.substr(1, id.length - 1);}
	document.getElementById(id).style.backgroundImage = "url(" + product  + "resources/images/" + document.getElementById(id).className + "o.png)";
	
}

function mnu_ml(e){

	e = e || event;
	var id = "";
	if (e.target){id = e.target.id}else{id = e.srcElement.id}
	if (id.substr(0, 1) == "_"){id = id.substr(1, id.length - 1);}
	document.getElementById(id).style.backgroundImage = "url(" + product  + "resources/images/" + document.getElementById(id).className + ".png)";
	
}

function mnu_c(e){

	e = e || event;
	var id = "";
	if (e.target){id = e.target.id}else{id = e.srcElement.id}
	tab = id;
	umkr(id);
}

function umkr(id, dr){
	var f = document.getElementById("frame")
	switch(id){
		case "mih":
			if (!dr){f.src = "content/home.php";}
			mmkr(90);
			break;
		case "min":
			if (!dr){f.src = "content/news.php";}
			mmkr(188);
			break;
		case "mir":
			if (!dr){f.src = "content/results.php";}
			mmkr(313);
			break;
		case "mic":
			if (!dr){f.src = "content/contact.php";}
			mmkr(453);
			break;
		case "mim":
			if (!dr){f.src = "content/members.php";}
			mmkr(630);
			break;
		case "mirss":
			if (!dr){f.src = "http://www.noseahead.co.uk/blog.php?feed=rss2";Resize(1500);}
			mmkr(779);
			break;
	}
}

function mmkr(l){
	document.getElementById("mkr").style.left = l;
}

var Images;
var Complete = 0;
function Load(){
   
    if (!document.getElementById("loading")){
        document.body.innerHTML += '<div id="loading" class="loading"><div id="loadingo" class="loadingo"></div><div id="loadinge"class="loadinge"></div><div class="loadingt">loading</div><div id="loadingp" class="loadingp">0%</div></div>'
         document.getElementById('loading').style.left = (document.body.clientWidth / 2) - 100;
	    document.getElementById('loading').style.top = (document.body.clientHeight / 2) - 25;
    } 
    window.onresize = function (){
        if (document.getElementById('loading')){
            document.getElementById('loading').style.left = (document.body.clientWidth / 2) - 100;
            document.getElementById('loading').style.top = (document.body.clientHeight / 2) - 25;
        }
    }  
	Images = new Array();
	GetImages();
	PreloadImages();
	//setInterval(IncrementProgress, 100);
}

function RaiseLoadComplete(){
	if (LoadComplete){LoadComplete();}
	if (AddHandlers){AddHandlers();}
}

function GetImage(src){
    var i = new Image();
    i.src = src;
}

function PreloadImages(){
	Complete = 0;
	if (Images.length > 0){
		for (var i = 0; i<Images.length;i++)
			{PreloadImage(new Image(), Images[i]);}
	}
	else{RaiseLoadComplete();}
}

function PreloadImage(img, src){
	var img = img;
	img.onload = onLoad;
	img.onerror = onError;
	img.src = src;
	function onLoad(){img.onload = null;setTimeout(IncrementProgress, 1);}
	function onError(){img.onerror = null;setTimeout(IncrementProgress, 1);}
}

function IncrementProgress(){
	Complete += 1;
	var p = String((Complete  / Images.length) * 100);
	if (p.indexOf(".") != -1){p = p.split(".")[0]};
	document.getElementById("loadingp").innerHTML = p + "%";
	document.getElementById("loadingo").style.width = (Complete  / Images.length) * 200;
	if (Number(p) > 95){
		document.getElementById("loadinge").style.display = "none";
	} else if (Number(p) > 5){
		document.getElementById("loadinge").style.left = (Complete  / Images.length) * 200;
		document.getElementById("loadinge").style.display = "block";
	}
	 if (Complete == Images.length){RaiseLoadComplete();}		
}

function GetRequestObject(url){
	var r;
	if (XMLHttpRequest){r = new XMLHttpRequest();} else {r = new ActiveXObject("Microsoft.XMLHTTP")}
	r.open("GET", url, true);
	return r;
}

function GetPostObject(url, p){
	var r;
	if (XMLHttpRequest){r = new XMLHttpRequest();} else {r = new ActiveXObject("Microsoft.XMLHTTP")}
	r.open("POST", url, true);
	r.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	r.setRequestHeader("Content-length", p.length);
	r.setRequestHeader("Connection", "close");
	return r;
}

function GetSyncRequestObject(url){
	var r;
	if (window.XMLHttpRequest){r = new XMLHttpRequest();} else {r = new ActiveXObject("Microsoft.XMLHTTP")}
	r.open("GET", url, false);
	return r;
}

function Resize(h){

	if (document.getElementById("content")){
		document.getElementById("content").style.height = h;
		document.getElementById("sl").style.height = h + 470;
		document.getElementById("sr").style.height = h + 470;
		document.getElementById("base").style.top = h + 270;
		var xh = h + 500;
		if (h >400 || document.body.clientHeight > xh){
			document.getElementById("bgb").style.top = 887;
			if (document.body.clientHeight > xh){xh = document.body.clientHeight}
			document.getElementById("bgb").style.height = xh  - 887;
			document.getElementById("bgb").style.visibility = "visible";
		}else{
			document.getElementById("bgb").style.height = 0;
			document.getElementById("bgb").style.top = 0;
			document.getElementById("bgb").style.visibility = "hidden";
		}
		document.getElementById("lightbox").style.height = h + 500;
		//		if (h > 400){document.getElementById("base").style.height = 50;}else{document.getElementById("base").style.height = 30;}
	}

}

function showLight(){
	setTimeout('fade(0, "lightbox");document.getElementById("lightbox").style.display="block";', 0);
	setTimeout('fade(5, "lightbox")', 50);
	setTimeout('fade(10, "lightbox");', 50);
	setTimeout('fade(15, "lightbox");', 50);
	setTimeout('fade(20, "lightbox");', 50);
	setTimeout('fade(25, "lightbox");', 50);
	setTimeout('fade(30, "lightbox");', 50);
	setTimeout('fade(35, "lightbox");', 50);
	setTimeout('fade(40, "lightbox");', 50);
	setTimeout('fade(45, "lightbox");', 50);
	setTimeout('fade(50, "lightbox");', 50);
	setTimeout('showLightComplete();', 50);
}

function showLightComplete(){
	setTimeout('document.getElementById("lightboxcontent").style.display="block";wscroll();', 0);
}

function hideLight(){
	setTimeout('document.getElementById("lightboxcontent").style.display="none";document.getElementById("lightbox").style.display="none";', 0);
}

function fade(o, id) { 
    var s = document.getElementById(id).style; 
	s.display = "block";
    s.opacity = (o / 100); 
    s.mozopacity = (o / 100); 
    s.khtmlopacity = (o / 100); 
    s.filter = "alpha(opacity=" + o + "), blur(0,0,0)"; 
}


function wscroll(){
	if (document.getElementById("lightboxcontent").style.display == "block" && document.getElementById("lightboxcontent").style.width != "" && document.getElementById("lightboxcontent").style.height != ""){
		var h = document.body.clientHeight;
		var y = document.body.scrollTop;
		document.getElementById("lightboxcontent").style.top = Number(y + ((h / 2) - (document.getElementById("lightboxcontent").style.height.replace("px", "") / 2)));
		document.getElementById("lightboxcontent").style.left = (document.body.clientWidth / 2) - Number(document.getElementById("lightboxcontent").style.width.replace("px", "") / 2);
	
	}
	return true;
}

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

function icomplete(){
	if (document.getElementById("imain")){
		if (document.getElementById("imain").style.display == "none"){
			var r = window.frames[window.frames.length -1].document.body.innerHTML;
			if (r.indexOf("~") != -1){
				if (r.split("~")[0] == "1"){
					document.getElementById("lightboxcontent").innerHTML = '<div class="title">File Uploaded</div><div class="icontent">The file was successfully uploaded to the server.</div>';	
					setTimeout("window.frames[0].addimage('" + r.split("~")[1] + "');", 1000);
				}else{
					document.getElementById("lightboxcontent").innerHTML = '<div class="title">Upload Failed</div><div class="icontent">The upload to the server failed. Please try again later.</div>';	
				}
			}else{
				document.getElementById("lightboxcontent").innerHTML = '<div class="title">Upload Failed</div><div class="icontent">The upload to the server failed. Please try again later.</div>';	
			}
		}
	}
}

function urlencode (str) {
    var hexStr = function (dec) {
        return '%' + dec.toString(16).toUpperCase();
    };
 
    var ret = '',
            unreserved = /[\w.-]/; // A-Za-z0-9_.- // Tilde is not here for historical reasons; to preserve it, use rawurlencode instead
    str = (str+'').toString();
 
    for (var i = 0, dl = str.length; i < dl; i++) {
        var ch = str.charAt(i);
        if (unreserved.test(ch)) {
            ret += ch;
        }
        else {
            var code = str.charCodeAt(i);
            // Reserved assumed to be in UTF-8, as in PHP
            if (code === 32) {
                ret += '+'; // %20 in rawurlencode
            }
            else if (code < 128) { // 1 byte
                ret += hexStr(code);
            }
            else if (code >= 128 && code < 2048) { // 2 bytes
                ret += hexStr((code >> 6) | 0xC0);
                ret += hexStr((code & 0x3F) | 0x80);
            }
            else if (code >= 2048 && code < 65536) { // 3 bytes
                ret += hexStr((code >> 12) | 0xE0);
                ret += hexStr(((code >> 6) & 0x3F) | 0x80);
                ret += hexStr((code & 0x3F) | 0x80);
            }
            else if (code >= 65536) { // 4 bytes
                ret += hexStr((code >> 18) | 0xF0);
                ret += hexStr(((code >> 12) & 0x3F) | 0x80);
                ret += hexStr(((code >> 6) & 0x3F) | 0x80);
                ret += hexStr((code & 0x3F) | 0x80);
            }
        }
    }
    return ret;
}

String.noLC = new Object
  ({the:1, a:1, an:1, and:1, or:1, but:1, aboard:1,
    about:1, above:1, across:1, after:1, against:1,
    along:1, amid:1, among:1, around:1, as:1, at:1,
    before:1, behind:1, below:1, beneath:1, beside:1,
    besides:1, between:1, beyond:1, but:1, by:1, 'for':1,
    from:1, 'in':1, inside:1, into:1, like:1, minus:1,
    near:1, of:1, off:1, on:1, onto:1, opposite:1,
    outside:1, over:1, past:1, per:1, plus:1,
    regarding:1, since:1, than:1, through:1, to:1,
    toward:1, towards:1, under:1, underneath:1, unlike:1,
    until:1, up:1, upon:1, versus:1, via:1, 'with':1,
    within:1, without:1});
    
String.prototype.titleCase = function () {
  var parts = this.split(' ');
  if ( parts.length == 0 ) return '';
  var fixed = new Array();
  for ( var i in parts ) {
    var fix = '';
    if ( String.noLC[parts[i]] )
    {
      fix = parts[i].toLowerCase();
    }
    else if ( parts[i].match(/^([A-Z]\.)+$/i) )
    { // will mess up "i.e." and like
      fix = parts[i].toUpperCase();
    }
    else if ( parts[i].match(/^[^aeiouy]+$/i) )
    { // voweless words are almost always acronyms
      fix = parts[i].toUpperCase();
    }
    else
    {
      fix = parts[i].substr(0,1).toUpperCase() +
                 parts[i].substr(1,parts[i].length);
    }
    fixed.push(fix);
  }
  fixed[0] = fixed[0].substr(0,1).toUpperCase() +
                 fixed[0].substr(1,fixed[0].length);
  return fixed.join(' ');
}

function dmc(ev)
{ 
	ev = window.event || ev;   
	if (window.opera){return {x:ev.pageX, y:ev.pageY};}   
	return {x:ev.clientX + document.body.scrollLeft - document.body.clientLeft, y:ev.clientY + document.body.scrollTop  - document.body.clientTop}
}  

function include_dom(script_filename) {
    var html_doc = document.getElementsByTagName('head').item(0);
    var js = document.createElement('script');
    js.setAttribute('language', 'javascript');
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', script_filename);
    html_doc.appendChild(js);
    return false;
}
