// ---------------------------------------------------------------------------------------
// Copyright 2011 - Brooklyn Public Library  All rights reserved.
// ---------------------------------------------------------------------------------------

$(document).ready(function() { 

$("#supersearch").bind("submit", function() { return searchchk(this); });
$("#howdoi").bind("change", function() { navhowdoi(this.options[this.selectedIndex].value); });
$("#getbranchinfo").bind("change", function() { navbranch(this.options[this.selectedIndex].value); });

if ( $("#hprotator").length ) {

$('#hprotator').cycle({timeout:6000,pager:'#hprotator-nav',speed:400 });


$.preloadImages("/images/home/hprot-play.png");
$("#hprotator-nav").append("<a id='pbtn' class='pause'>&nbsp;&nbsp;&nbsp;</a>");


$('#pbtn').click(function() {
var bplplay = true;

	if (bplplay) {
    	$('#hprotator').cycle('pause');
		$("#pbtn").removeClass("pause").addClass("play")
		bplplay = false;
	}
	else {
		$('#hprotator').cycle('resume');
		$("#pbtn").removeClass("play").addClass("pause")
		bplplay = true;
	}
});

$("#hometab ul").idTabs();

}



  var default_values = new Array();
  $("input.swaptext").focus(function() {
    if (!default_values[this.id]) {
      default_values[this.id] = this.value;
    }
    if (this.value == default_values[this.id]) {
      this.value = '';
    }
    $(this).blur(function() {
      if (this.value == '') {
        this.value = default_values[this.id];
      }
    });
  });
  
  
if ( $("#support-rotator").length ) {
	$('#support-rotator').cycle({timeout:6000,speed:1000,random:1});
}


});  // end of (document).ready



function searchchk ( supersearchbox )	{
    if (supersearchbox.ss_terms.value == "Search for..." || supersearchbox.ss_terms.value == "")
	{
  	//$("#searchalert").fadeIn("fast").fadeTo(4000, 1).fadeOut(1000);
    //$("#close_message").click(function(){ $("#searchalert").fadeOut("slow"); });
  
	// $("#searchalert").fadeIn(1000).fadeTo(5000, 1).fadeOut(1000);
 	 //$("#searchalert").fadeOut(10000);
      //$("#searchalert").animate({ opacity: 1.0}, 5000, 'linear', function () {
     // $(this).fadeOut("slow");
	 // });
        //supersearchbox.ss_terms.focus();
        return false ;
    } else
		return true ;
}

jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}


//// how do i - quicklinks select list ////
function formHandler(form){
var URL = document.howdoiform.site.options[document.howdoiform.site.selectedIndex].value;
window.location.href = URL;
}


////// popUp window /////
function popUp(strURL,strType,strWidth,strHeight,strTarget) {
	var strOptions="";
	if (strType=="console") strOptions="resizable,width="+strWidth+",height="+strHeight;
	if (strType=="fixed") strOptions="status,width="+strWidth+",height="+strHeight;
	if (strType=="elastic") strOptions="toolbar,menubar,scrollbars,resizable,location,width="+strWidth+",height="+strHeight;
	//window.open(strURL, 'newWin', strOptions);
	var newWindow = window.open(strURL, strTarget, strOptions);
	newWindow.focus();
	}
	
////// popUp window /////
function popUpsurv(strURL,strType,strWidth,strHeight,strTarget) {
	var strOptions="";
	if (strType=="console") strOptions="resizable,width="+strWidth+",height="+strHeight;
	if (strType=="fixed") strOptions="status,width="+strWidth+",height="+strHeight;
	if (strType=="elastic") strOptions="toolbar,menubar,scrollbars,resizable,location,width="+strWidth+",height="+strHeight;
	//window.open(strURL, 'newWin', strOptions);
	var newWindow = window.open(strURL, strTarget, strOptions);
	newWindow.moveTo(150,100);
	newWindow.focus();
	}

////// popUp windows /////
function popUpsm(theURL,winName,features) 
{ 
  var _W=window.open(theURL,winName,features);
	  _W.focus();
      _W.moveTo(150,100);
} 

function survpop(theURL,strWidth,strHeight) 
{ 
  var _W=window.open(theURL,'survey',"toolbar=no,menubar=no,scrollbars,resizable,location,width="+strWidth+",height="+strHeight);
	  _W.focus();
} 

function windowOpen(w,h,name,URL) {
newWin = window.open(URL,name,"resizable=no,scrollbars=no,width=" + w + ",height=" + h);
newWin.focus();
}


////// alternate text box value /////
function alterNate(elm){
	if (!elm.base) elm.base = elm.value
	if (elm.value == elm.base) elm.value = "";
	else if (elm.value == "") elm.value = elm.base;
}


////// error handling for email forms /////
function chkform ( sform )
{
    if (sform.message.value == "") {
        alert( "Please enter a message." );
        sform.message.focus();
        return false ;
    }
	return true ;
}

function emailit(whoto,domainto,title,subject,displayname)
{
	if (domainto == null || domainto == '') {domainto = 'brooklynpubliclibrary.org';}
	var writeit = whoto + '@' + domainto;
	if (displayname == null || displayname == '') {displayname = writeit;}
	
	if (subject == null || subject == '') {
		document.write("<a href='mailto:"+writeit+"' title='"+title+"'>"+displayname+"</a>");
	}
	else {
		document.write("<a href='mailto:"+writeit+"?subject="+subject+"' title='"+title+"'>"+displayname+"</a>");
	}
}

function navbranch(x)
  {
  var evid = x;
  if (evid != "") {
  window.location="branch_library_detail.jsp?branchpageid="+evid;
  }
  }
  
	function navhowdoi(x)
	   {
	   var giddyup = x;
	   if (giddyup != "") {
	   //window.location.href = '/calendar.do?dispatch=detail&eventpageid='+evid;
	   window.location.href = giddyup;
	   }
	   }
	   
	   
////// stylesheet switcher /////
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);

