﻿// Load the document onLoad event
$(document).ready(function() {
	// pin the primary nav, if applicable (find the item with a matching path and replace the styles)
	//the commented code below didn't not mesh well with the Ektron Polls
	/*$('.menu li a[@href=' + document.location.pathname + ']').each(function() {
        alert("test");
        if ($(this).attr('class') == "first") {
            $(this).removeClass('first').addClass('firstPinned'); // the first item has a slightly different background
        } else {
            $(this).addClass('pinned');
        }
	});*/
	
	//manually set link classname because jQuery is not playing well with Ektron
	var menuDiv=document.getElementById("mainMenu");
	if(menuDiv!=null){
	    var lis=menuDiv.getElementsByTagName("li");
        for(i=0;i<lis.length;i++){
		    var aLink=lis[i].getElementsByTagName("a");
    			
		    if(aLink[0].href=="http://dev.focalpress.com"+document.location.pathname){
			    if(aLink[0].className=="first"){
				    aLink[0].className="firstPinned";
			    } else {
				    aLink[0].className=aLink[0].className+"pinned";
			    }
		    }
	    }
	}
	
	// Set the region picker selection
	regionState();
    
    var allLinks=document.getElementsByTagName('a');
    for(i=0;i<allLinks.length;i++)
    {
      // if the class contains 'video'
      //need to upgrade to"if contains
        if(allLinks[i].className=='dot video')
        {
            allLinks[i].onclick=function() {
	            var url = '/video.aspx?id=' + this.getAttribute('asset');
	            //var url = '/Content.aspx';
	            var features = 'toolbar=no,status=no,height=520,width=600,resizable=no';
	            window.open(url, 'video', features);
	            return false;
	        }
        } 
    }
    	
	/*
	// update the format of news items
	$('.newsItem').each(function() {
	    newsDate(this.getAttribute('date'),this.getAttribute('id'));
	});
	
	//update the format of book date items
	$('.bookdate').each(function() {
	    bookDate(this.getAttribute('date'),this.getAttribute('id'));
	});*/
});
function openLinkFromFlash(url){
	var newWindow=window.open(url,'_blank','toolbar=yes,location=yes,status=yes,menubar=yes');
}
function toggleDescriptions(id,style){
    var divElement=document.getElementById("description"+id);
    if(style=="on"){
        divElement.style.display="block";
    } else {
        divElement.style.display="none";
    }
}
function toggleDiv(id){
    var divElement=document.getElementById(id);
    if(divElement.style.display=="block"){
        divElement.style.display="none";
    } else {
        divElement.style.display="block";
    }
}
function regionState() {
    var pos = document.cookie.indexOf("region=");
    
    if (pos != -1) {
        var start = pos + 7;
        var end = document.cookie.indexOf(";", start);
        if (end == -1) end = document.cookie.length;
        var value = unescape(document.cookie.substring(start, end));
        
        if (value == "UK") {
        if (document.getElementById('UK_region')){
           document.getElementById('UK_region').checked = true;
            //console.log("set to UK");
            }
        }
    }
    
}

function ektLocalizeDate(date, id) {
	if (document.getElementById && 10 == date.length) {
		var oTempDate = new Date(date.substr(0,4), parseInt(date.substr(5,2),10)-1, date.substr(8,2));
		document.getElementById(id).innerHTML=(oTempDate.toLocaleDateString ? oTempDate.toLocaleDateString() : oTempDate.toLocaleString());
	}
}
var months = ['Janauary','February','March','April','May','June','July','August','September','October','November','December'];

function bookDate(date, id) {
	if (document.getElementById && 10 == date.length) {
		var oTempDate = new Date(date.substr(0,4), parseInt(date.substr(5,2),10)-1, date.substr(8,2));
		document.getElementById(id).innerHTML= months[oTempDate.getMonth()] + ", " + oTempDate.getFullYear();
	}
}

function newsDate(date, id) {
	if (document.getElementById && 10 == date.length) {
		var oTempDate = new Date(date.substr(0,4), parseInt(date.substr(5,2),10)-1, date.substr(8,2));
		document.getElementById(id).innerHTML= months[oTempDate.getMonth()] + " " + oTempDate.getDay() + ", " + oTempDate.getFullYear();
	}
}
