
/* ================================================================
    jQuery Code

================================================================  */
   
function equalHeight(group) {
    tallest = 0;
    group.each(function() {
        thisHeight = $(this).height();
        if(thisHeight > tallest) {
            tallest = thisHeight;
        }
    });
    group.height(tallest);
}


$(document).ready(function() {
    equalHeight($(".iteminner"));        // Forces inner backgroudn to be equal height
    equalHeight($(".itembox h3"));        // Forces all H3's to be equal height [blankspace on single line if wrapping exists on other elements]
	
	equalHeight($(".itembox"));            // Forces container box to all be equal height
	
	
    function alertBoxheight() {
        if ( ($("#alertbox_inner").length > 0) && (($("#alertbox_inner img").length > 0)) ) {
            // Check alertbox exists with an image. If so get hieght and reset the alerbox inner height to accomidate
           
            var imgHeight = $("#alertbox_inner img").height();
            var boxHeight = $("#alertbox_inner").height();
           
            if(imgHeight > boxHeight) {
                $("#alertbox_inner").height(imgHeight);
            }
        }
    }
    alertBoxheight();
	
	
	$("#presssubmit").click(function () {
	
			// alert("URL: " + document.location);
		
		   // Array: Months Names -> Numbers 
		    var monthsnum = new Array(12);
		    monthsnum["january"]  = 0;
		    monthsnum["february"]  = 1;
		    monthsnum["march"]  = 2;
		    monthsnum["april"]  = 3;
		    monthsnum["may"]  = 4;
		    monthsnum["june"]  = 5;
		    monthsnum["july"]  = 6;
		    monthsnum["august"]  = 7;
		    monthsnum["september"]  = 8;
		    monthsnum["october"]  = 9;
		    monthsnum["november"] = 10;
		    monthsnum["december"] = 11;
		    // end array 

		    // Get todays date
		    var today         = new Date();
		    var todayMonth     = today.getMonth();
		    var todayYear     = today.getYear();
		    if(todayYear < 2000) { todayYear = todayYear + 1900; }
		   
		   
		    var submitYear = document.ViewPress.pressyear.value;
		    var submitMonth = document.ViewPress.pressmonth.value;

		    if(submitYear > todayYear){
		        // If the year is in the future, give alert
		        $("table.main").replaceWith("<div style=\"padding: 20px; \"><h2>No press releases are available for the dates you selected.<br />Please try again</h2></div>");
		       
		    } else if ((submitYear >= todayYear) && (monthsnum[submitMonth] > todayMonth)) {
		        // If Year matches or greater than current year and month is greater than current month, dispaly error
				$("table.main").replaceWith("<div style=\"padding: 20px; \"><h2>No press releases are available for the dates you selected.<br />Please try again</h2></div>");

		    } else {
			
                // Make Redirect
                if (submitYear!='') {  
                    if (submitMonth!='') {submitMonth=submitMonth + '/';} else { submitMonth = '';}
                               
                    var url = $("#urlpath").text();  
                    url= url + '' + submitYear + '/' + submitMonth;
                    window.location=url;
                }
		    }		
	});
	
	
});  



/* ================================================================ 	
	Style Switcher 
		- Paul Sowden, 
		- A List Apart: http://www.alistapart.com/articles/alternate/

================================================================  */


function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);
