// JavaScript Document

    // Array Remove - By John Resig (MIT Licensed)
    Array.prototype.remove = function(from, to) {
        var rest = this.slice((to || from) + 1 || this.length);
        this.length = from < 0 ? this.length + from : from;
        return this.push.apply(this, rest);
    };
	
	Array.prototype.exists = function(value) {
		for(var i = 0; i < this.length; i++){
		   if(this[i] === value)
			 return true;
		}
		return false;
	}
		
	Array.prototype.idLocation = function(value) {
		for(var i = 0; i < this.length; i++){
		   if(this[i].id === value){
			 return i;
		   }
		}
		return -1;
	}
	

	
	function getGoogleMapApi() {
		var alarmKey = "ABQIAAAAFfa8J836o_PB5SroSFNVSxRJdyOc2CcvHvjRKZi12yXiJMZ88BS5spjLCV72TnWzErSXbZ08-FBLxg";
		var insiderKey = "ABQIAAAAFfa8J836o_PB5SroSFNVSxSAWLEVWeL3KRzI9NwR5lidXtVMKRSWZKz79CFIa7u-mkmbIOgY3tJ72A";
		var localKey = "ABQIAAAAFfa8J836o_PB5SroSFNVSxQj54pbLatCWwMLcVxQgl1fDL_KwhTmrOSTi6wDdakUdv98FsvQGSvz-g";
		var useKey; 
		
		var d = getDomainFromHost(location.hostname);
		
		switch(d) {
			case "apxalarm.com":
				useKey = alarmKey;
				break;
			case "apxinsider.com":
				useKey = insiderKey;
				break;
			case "apex.local":
				useKey = localKey;
				break;
			default:
				useKey = localKey;
				break;
		}
		
		loadScript('http://maps.google.com/maps?file=api&amp;v=2&amp;key=' + useKey, null);
		
	}
	
	function loadScript(scriptSrc) {
		var head= document.getElementsByTagName('head')[0];
   		var script= document.createElement('script');
//		script.setAttribute("rel","javascript");
//		script.setAttribute("type", "text/javascript");
  		script.type= 'text/javascript';
  		script.src= getStaticDomain() + scriptSrc;
//		script.onload = function() { callback(); };
/*		script.onreadystatechange = function() {
										if(script.readystate == 0) alert("Ready State 0");
										if(script.readystate == 1) alert("Ready State 1");
										if(script.readystate == 2) alert("Ready State 2");
										if(script.readystate == 3) alert("Ready State 3");
										if(script.readystate == 4) alert("Ready State 4");
										//callback();
									};*/
   		head.appendChild(script);
    }
	
	function loadCss(cssScript){
		if($.isArray(cssScript)){
			for(var i = 0; i < cssScript.length; i++){
				var head = document.getElementsByTagName("head")[0];
				var fileref = document.createElement('link');
				fileref.setAttribute("rel","stylesheet");
				fileref.setAttribute("type", "text/css");
				fileref.setAttribute("href", getStaticDomain() + cssScript[i]);
				head.appendChild(fileref);
			}
		}
		else{
			var head = document.getElementsByTagName("head")[0];
			var fileref = document.createElement('link');
			fileref.setAttribute("rel","stylesheet");
			fileref.setAttribute("type", "text/css");
			fileref.setAttribute("href", getStaticDomain() + cssScript);
			head.appendChild(fileref);
		}
	}
	
	function addCssRule(selector, declaration) {
        // test for IE
        var ua = navigator.userAgent.toLowerCase();
        var isIE = (/msie/.test(ua)) && !(/opera/.test(ua)) && (/win/.test(ua));
		var isCreated = false;
		var style_node;
		
		// find existing style node
		
		style_node = $("#dynStyle")
		if(style_node.length > 0) isCreated = true;
		else {
			$("head").append('<style id="dynStyle" text="text/css"></style>');
			style_node = $("#dynStyle");	
		}
		
		
        // append a rule for good browsers
        if (!isIE) { style_node.append(selector + declaration); }
		

        // use alternative methods for IE
        if (isIE && document.styleSheets && document.styleSheets.length > 0) {
                var last_style_node = document.styleSheets[document.styleSheets.length - 1];
                if (typeof(last_style_node.addRule) == "object") last_style_node.addRule(selector, declaration);
		
        }
		
	};

    function delayHide(target, speed, delay) {
        setTimeout('$("' + target + '").hide(' + speed + ')', delay);
    }
	
	function parseUrl(data) {
		var e=/^((http|ftp):\/)?\/?([^:\/\s]+)((\/\w+)*\/)([\w\-\.]+\.[^#?\s]+)(#[\w\-]+)?$/;
	
		if (data.match(e)) {
			return  {url: RegExp['$&'],
					 protocol: RegExp.$2,
					 host:RegExp.$3,
					 path:RegExp.$4,
					 file:RegExp.$6,
					 hash:RegExp.$7};
		}
		else {
			return  {url:"", protocol:"",host:"",path:"",file:"",hash:""};
		}
	}

	
	function getDomainFromHost(host) {
	   if (host == null || host.length == 0)
		  return "";
	   
	   str = host;
	   
	   var i = str.indexOf("/");
	   if (i > -1)
		  str = str.substring(0, i);
		  
	   var parts = str.split('.');
	   
	   var len = parts.length;
	   
	   if (len < 3)
		  return str;
	
	   var lastPart = parts[len-1];
	   var secondPart;
			 
	   secondPart = parts[len-2];
	   
	   var two = 2;
	   
	   if (lastPart == "uk" && secondPart == "co")
		  ++two;
	   
	   if (len >= 0)
		  return parts.splice(len-two, two).join('.');
	   
	   return "";
	}
	
	function getStaticDomain(){
		var host = document.location.host;
		if(host == "localhost:3367"  || host == "localhost") host = "static.apex.local";
		var domain = getDomainFromHost(host);
		if(domain != "")
			return document.location.protocol + "//static." + domain;
		else
			return "";
		
	}
	function MM_swapImgRestore() { //v3.0
 		var i,x,a=document.MM_sr; 
		for(i = 0 ; a && i< a.length && (x = a[i]) && x.oSrc ; i++)
			x.src=x.oSrc;
	}
	function MM_preloadImages() { //v3.0
		var d=document; 
		if(d.images){ 
	  		if(!d.MM_p) {
				d.MM_p=new Array();
			}
			  var i,j=d.MM_p.length,a=MM_preloadImages.arguments; 
			  for(i = 0 ; i < a.length ; i++){
				if (a[i].indexOf("#")!=0){ 
					d.MM_p[j]=new Image; 
					d.MM_p[j++].src=a[i];
				}
			  }
		}
	}

	function MM_findObj(n, d) { //v4.01
		var p,i,x;  
		if(!d) d=document; 
		if((p = n.indexOf("?")) > 0 && parent.frames.length) {
			d = parent.frames[n.substring(p+1)].document;
			n=n.substring(0,p);
		}
		if(!(x = d[n]) && d.all) 
			x=d.all[n]; 
		for (i = 0; !x && i < d.forms.length ; i++) 
			x=d.forms[i][n];
		for(i = 0 ; !x && d.layers && i < d.layers.length ; i++) 
			x = MM_findObj(n, d.layers[i].document);
		if(!x && d.getElementById) 
			x = d.getElementById(n); 

		return x;
	}

	function MM_swapImage() { //v3.0
		var i,j=0,x,a=MM_swapImage.arguments; 
		document.MM_sr=new Array; 
		for(i = 0 ; i < (a.length-2) ; i += 3)
			if ((x = MM_findObj(a[i])) != null){
				document.MM_sr[j++] = x;
				if(!x.oSrc)
					x.oSrc = x.src; 
				x.src=a[i+2];
			}
	}
	
	
	//this is the scroll object for the stories page (story's page initially)
	var scroller = {
		init:   function() {
			scroller.docH = document.getElementById("story").offsetHeight;
		    scroller.contH = document.getElementById("container").offsetHeight;
		    scroller.scrollAreaH = document.getElementById("scrollArea").offsetHeight;
		      
		    scroller.scrollH = (scroller.contH * scroller.scrollAreaH) / scroller.docH;
		    if(scroller.scrollH < 15) scroller.scrollH = 15;
			if(scroller.scrollH > 412.5) scroller.scrollH = 0;
		    document.getElementById("scroller").style.height = Math.round(scroller.scrollH) + "px";
		    
		    scroller.scrollDist = Math.round(scroller.scrollAreaH - scroller.scrollH);
	
		    Drag.init(document.getElementById("scroller"),null,0,0,-1,scroller.scrollDist);
		    
		    document.getElementById("scroller").onDrag = function (x,y) {
		      var scrollY = parseInt(document.getElementById("scroller").style.top);
		      var docY = 0 - (scrollY * (scroller.docH - scroller.contH) / scroller.scrollDist);
			  docY = docY - 2;
		      document.getElementById("story").style.top = docY + "px";
		    }
	  	}
	}
	
	function popup(url, title, numW, numH) {
		var numL = (screen.width - numW) / 2;
		var numT = (screen.height - numH) / 2;
		var params = 'width='+numW+',height='+numH+',left='+numL+',top='+numT;
		winPopup = window.open(url, 'popup', 'toolbar=no,titlebar=no,scrollbars=no,location=no,status=no,menubar=no,resizable=no,' + params);
		winPopup.focus();
	}

	$.extend($.ui.tabs.prototype, {
		getIndexFromId: function(tabId) {
			return this.anchors.index(this.anchors.filter('[href$=' + tabId + ']'));	
		}
	});
	
function getFlashMovie(movieName) {
  var isIE = navigator.appName.indexOf("Microsoft") != -1;
  return (isIE) ? window[movieName] : document[movieName];
}

function callToActionscript(str) 
{
     getFlashMovie("apxVideoStoriesPlayerV2Flash").sendToActionscript(str);
}

function onVideoMenuClick(linkNum) {
	for (i = 1; i<6;i++)
	{
		document.getElementById('link' + i).className = "notCurrent";
	}
	var elmName = 'link' + linkNum;

	document.getElementById(elmName).className = "current";
	callToActionscript(linkNum);
}
function videoMenuLoad() {

	var fullurl = window.location.href;

	var index = fullurl.substring(fullurl.length - 1, fullurl.length);



	if (index != parseInt(index))

	{

		index = 1;

	}

	onVideoMenuClick(index);

}