// Global Definitions
var pretend_param=getParameterByName('pretend'); //e.g. youth_festivals/2012/?pretend=1/9/12 (or even Jan 9, 2012 5:00 pm)
var tccurdate=new Date();
if (typeof pretend_param == 'string' && pretend_param)
	tccurdate=new Date(pretend_param);
var strTcCurDate=tccurdate.toLocaleString();	// local string version of the current date
var tccuryear=tccurdate.getFullYear();
var tccurts=Math.round(tccurdate.getTime() / 1000);  // secs not msecs
var tcActive=1;   // do not change
var aryPanelIds=['', 'yfestbox', 'venuebox'];  // index 0 is set to div currently not hidden

// Add to favorites (bookmark)
// location.href,document.title
function addBookmark() {
   var BookmarkURL=location.href
   var BookmarkTitle=document.title
   if (document.all) {  // browser is IE
      window.external.AddFavorite(BookmarkURL,BookmarkTitle)
   }
   else {  // non-IE (e.g. Mozilla Firefox)
      window.sidebar.addPanel(BookmarkTitle, BookmarkURL, ''); 
   }
}

// Insert profile data after ajax html is loaded
//    Assumes triggering anchor element has well-defined information in the title and href attributes.
//    The reason that profile files lack image and title is to allow the file to also be php "included" in a
//       different format page, not just a popup. It should also simplify generation of those files.
//    See http://dev.iceburg.net/jquery/jqModal/ for more information.
var prloaded = function(hash) {
   trigobj = hash.t; // the triggering DOM object, e.g. DOM obj of <a href="bios/doej.htm" title="John Doe, piano" ...>
   var trigattrs=trigobj.attributes;
   var title = trigattrs.getNamedItem("title").value; // e.g. "John Doe, piano" or "Doe Quartet"
   $('#profname').text(title);
}

// support functions for tickets form
function get_form_num(formid) {
   var retint=0;
   var elobj=document.getElementById(formid);
   if (elobj) {
      var elval=elobj.value;
      if (elval) {
         if (!isNaN(parseFloat(elval)) && isFinite(elval)) // is a number 
            retint=parseInt(elval);
         else  // blank out user mistake
            elobj.value="";
      }
   }
   return retint;
}
// refresh cost and total fields on tickets form
function uptot() {
   var ci,ti,quanid,costid,quan,cost,contribamt;
   var totcost=0;
   for (ci=0; ci<concnt; ci++) {
      tcnt=arytktcnts[ci];
      for (ti=0; ti<tcnt; ti++) {
         quanid='c'+ci+'quan'+ti;
         costid='c'+ci+'cost'+ti;
         quan=get_form_num(quanid);
         cost=get_form_num(costid);
         totcost+=quan*cost;
      }
   }
   strcost=totcost ? totcost : "";
   contribamt=get_form_num("contrib");
   totcheck=totcost+contribamt;
   strcheck=totcheck ? totcheck : "";
   document.getElementById("tktcost").value = strcost;
   document.getElementById("totamt").value = strcheck;
}

// Usage: if(hasClass(document.getElementById("test"), "test")){//do something};
function hasClass(ele,cls) {
	return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
}
// Usage: addClass(document.getElementById("test"), "test");
function addClass(ele,cls) {
	if (!hasClass(ele,cls)) ele.className += " "+cls;
}
// Usage: removeClass(document.getElementById("test"), "test")
function removeClass(ele,cls) {
	if (hasClass(ele,cls)) {
    	var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
		ele.className=ele.className.replace(reg,' ');
	}
}
// Hide all but the next concert according to timestamps in aryts
// Assumes div id naming convention: 'tccon'+i where i is concert index
//    and 'tcconx' contains "season is over" message.
function unhide_next_concert() {
   var divid,divobj;
   aryPanelIds[0]='';	// set to id of current non-hidden concert div
   for (var i=0; i<aryconts.length; i++) {
      divid='tccon'+i;
      divobj=document.getElementById(divid);
      if (divobj) {
         conts=aryconts[i]+(4*3600);  // (assume concert lasts 4 hours)
         if (conts>tccurts && aryPanelIds[0]=='') {  // show only this one
			aryPanelIds[0]=divid;
			removeClass(divobj,'hideit');
         }
         else {   // hide this one
            addClass(divobj,'hideit');
         }
      }
   }
   if (aryPanelIds[0]=='') { // all concerts are past
      divobj=document.getElementById('tcconx');
      if (divobj) {
		 removeClass(divobj,'hideit');
		 aryPanelIds[0]='tcconx';
      }
   }
}

// Get query string param, e.g. .../youth_festivals/2012/?show=b2011
// Usage: strshow=getParameterByName('show');
function getParameterByName(name) {
  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  var regexS = "[\\?&]" + name + "=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.href);
  if(results == null)
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}

// make all 3 main DIVs on home page equal height
function equalize_box_heights() {
   var tallestdiv = 0;
   var i, divid, curHeight;
   for (i=0; i<aryPanelIds.length; i++) {
	  divid=aryPanelIds[i];	// e.g. 'tccon2', 'yfestbox', or 'venuebox'
      divobj=document.getElementById(divid);
      if (divobj) {
		 curHeight=divobj.offsetHeight;
		 //alert(divid+' has offsetHeight='+curHeight+', style.height='+divobj.style.height);
		 if (curHeight > tallestdiv)
			tallestdiv=curHeight;
	  }
   }
   // now set them all to the same height
   for (i=0; i<aryPanelIds.length; i++) {
	  divid=aryPanelIds[i];	// e.g. 'tccon2', 'yfestbox', or 'venuebox'
      divobj=document.getElementById(divid);
      if (divobj) {
		 divobj.style.height=tallestdiv+'px';
	     //alert(divid+' now has offsetHeight='+divobj.offsetHeight+', style.height='+divobj.style.height);
	  }
   }
}

// onLoad function called by home page
function init_home_page() {
   unhide_next_concert();
   equalize_box_heights();  // call after unhide_next_concert updates aryPanelIds 
}

