
// ======== THE VARIABLES ======== //
// Turn this to true if you want to display a special promo box
var specialBox = false;
// The max length of allowable images
var maxLength = 20;
var itemsLength;
var newItemsLength = 0;
var startImg = 0;
var imgNum = 0;
var transTime = 7000;
var slideTimer;
var timerOn;
var oldb = $('pBox' + 0);
var start = false;

// Date and time settings for the promo box
var currentTime = new Date();
var month = currentTime.getMonth();
var day = currentTime.getDate();
var year = currentTime.getFullYear();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();

// The Arrays of links
var theLinks = new Array();
var theTrackers = new Array();
var theTargets = new Array();

// ==== MOBILE REDIRECT ==== // really needs to be done on the server side
var exempt;
var place = window.location.href;
var re = new RegExp("search");
var searchUse;

// Check to see if the word "search" is in the url
if(re.test(place)== true){
	searchUse = true;
} else {
	searchUse = false;	
}

if(navigator.userAgent.match(/xoom/i)) {
	exempt = true;	
} else {
	exempt = false;
}
if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/android/i))) {
	if((document.referrer != "http://m.ocls.info/") && (searchUse == false) && (exempt == false)){ 
		location.href='http://m.ocls.info';
	}
}


// ==== INITIALIZE ON LOAD ==== //
function init() {
	iCanHasEngage();
	switchBox('catalogsearch');
	loadItPromo(); 
}


// ========= START THE ENGINE ======= //
function loadItPromo() {
	if (specialBox == false) {
		$('promoBoxContent').style.height = '252px';
		//$('contentMain').style.height = '526px';
		$('loader2').style.display = 'block';
		startPromo();
	}
}

function startPromo(){
	// Should we not load it because there is a special promo box?
	if (specialBox == false) {
		getData();
	}
}

// ======= SET THE TIMER =========== //
function clearTimer() {
	if (timerOn == true) {
		clearInterval(slideTimer);
		timerOn = false;
	}
}

function theTimer() {
	if (timerOn == true) {
		clearInterval(slideTimer);
	} else {
		timerOn = true;
	}
	slideTimer = setInterval ( 'slideShow()', transTime );
}

// ======== GET THE DATA =========== //
function getData() {	
	new Ajax.Request('/xml/promoBoxItems.xml', {
		method: 'get',
		onSuccess: function(transport) {
			var xml = transport.responseXML;
			var img, linkie, tracker, altT, pTarget, html, html2, db, mc;
			// Time vars
			var sDate, sTime, eDate, eTime;
			
			var itemCount = 0;
			
			// The path to the images
			var imgPath = '/Images/hp/promo_box/';
			
			// Add some Divs to put in our content
			$('promoBoxContent').innerHTML = '\
				<div id="pBoxImg" onclick="goToUrl();" onMouseOver="clearTimer();" onMouseOut="theTimer();"></div>\
				<div id="pBoxPanel"></div>';
			
			var items = xml.getElementsByTagName('item');
			itemsLength = items.length;
			// RUN THROUGH ITEMS
			for (i = 0; i < maxLength; i++) {
				// START DATE
				sDate = (xml.getElementsByTagName('startDate')[i].childNodes.length) ? xml.getElementsByTagName('startDate')[i].childNodes[0].nodeValue: "";
				// START TIME
				sTime = (xml.getElementsByTagName('startTime')[i].childNodes.length) ? xml.getElementsByTagName('startTime')[i].childNodes[0].nodeValue: "";
				// END DATE
				eDate = (xml.getElementsByTagName('endDate')[i].childNodes.length) ? xml.getElementsByTagName('endDate')[i].childNodes[0].nodeValue: "";
				// END TIME
				eTime = (xml.getElementsByTagName('endTime')[i].childNodes.length) ? xml.getElementsByTagName('endTime')[i].childNodes[0].nodeValue: "";
				
				if (checkDateTime(sDate, sTime, eDate, eTime) == true)
				{
					newItemsLength++;
					
					// THE IMAGE
					img = (xml.getElementsByTagName('image')[i].childNodes.length) ? xml.getElementsByTagName('image')[i].childNodes[0].nodeValue: "";
					// THE LINK
					linkie = (xml.getElementsByTagName('link')[i].childNodes.length) ? xml.getElementsByTagName('link')[i].childNodes[0].nodeValue: "";
					// THE TARGET TAG
					pTarget = (xml.getElementsByTagName('link')[i].getAttribute("target"));
					// THE TRACKER
					tracker = (xml.getElementsByTagName('tracker')[i].childNodes.length) ? xml.getElementsByTagName('tracker')[i].childNodes[0].nodeValue: "";
					// THE ALT TEXT
					altT = (xml.getElementsByTagName('altText')[i].childNodes.length) ? xml.getElementsByTagName('altText')[i].childNodes[0].nodeValue: "";
					
					
					//add the link and the tracker to the arrays
					theLinks[itemCount] = linkie;
					theTrackers[itemCount] = tracker;
					theTargets[itemCount] = pTarget;
					
					//pick the first image to show and hide the rest
					if (itemCount == 0) {
						db = 'block';
						mc = 'background-color #9cceff;';
					} else {
						db = 'none';
						mc = 'background-color: #CCC;';
					}
					
					// Create the images and stack them on top of eachother
					html = '<div class="promoImgHolder">\
								<div id="pBox'+ itemCount + '" style="display:'+ db +';" >\
									<img src="'+ imgPath + img + '" style="border:none;" alt="' + altT + '">\
								</div>\
							</div>';
					
					// Create the bottom small box panel
					html2 = '<a style="cursor:pointer; '+ mc +'" onclick="slideShow(\''+ itemCount +'\'); theTimer();" class="pLittleBox" id="pLB'+ itemCount +'"></a>';
					
					// Add the images to here
					$('pBoxImg').innerHTML += html;
					// Add the panel to here
					$('pBoxPanel').innerHTML += html2;
					itemCount++;
				} 
			}
		},
		
		onComplete:  function() {
				// now we can go do things
				theTimer();
				if (start == false) {
					$('pLB0').style.backgroundColor = '#9cceff';
					start = true;
				}
			
		},
		
		onFailure: function() {
				return false;
		}
	})
}

// ======== CHECK DATE AND TIME ========= //
function checkDateTime(sDate, sTime, eDate, eTime) {
	var sDate, sTime, eDate, eTime;
	
	// Get the current time
	var currentDate = new Date(year,month,day,hours,minutes); 
	// Assemble the start date
	var startDate = new Date(sDate.substring(6,10),sDate.substring(0,2) - 1,sDate.substring(3,5),sTime.substring(0,2),sTime.substring(3,5)); 
	// Assemble the end date
	var endDate = new Date(eDate.substring(6,10),eDate.substring(0,2) - 1,eDate.substring(3,5),eTime.substring(0,2),eTime.substring(3,5));	
	
	//$('debug').innerHTML += currentDate + "<br/>" + startDate + "<br/>" + endDate + "<br/> NEXT: <br/>";
	
	if ((currentDate > startDate) && (currentDate < endDate)) 
	{
		return true
	}
	else 
	{
		return false
	}

}

// ======== FADE THROUGH THE SLIDES ========= //
function slideShow(n) {
	var n = n;
	var newb,newlb,oldlb;
	
	// take an arg
	if (n == '0' || parseInt(n) > 0) {
		num = imgNum;
		imgNum = n;
	// or start over	
	} else if (imgNum == newItemsLength - 1) {
		imgNum = 0;
		num = newItemsLength - 1;
	// or move forward
	} else {
		imgNum++;
		num = imgNum - 1;
	}

	newb = $('pBox' + imgNum);
	oldb = $('pBox' + num);
	newlb = $('pLB' + imgNum);
	oldlb = $('pLB' + num);
	
	// do the thing if the objects are here, or do nothing and don't throw an error
	if (newb != null && oldb != null && newb != oldb) {
		newb.appear();
		oldb.fade();
		newlb.style.backgroundColor = '#9cceff';
		oldlb.style.backgroundColor = '#CCC';
	}
}



// ========== GO TO THE URL ============ //
function goToUrl() {
	// Get the Link and the Tracker from the Arrays and put them together
	var theLink = theLinks[imgNum];
	var theTracker = theTrackers[imgNum];
	var theTarget = theTargets[imgNum];
	var url = theLink;
	
	if (theTracker != "") {
		url += '?from=' + theTracker;
	}
	
	if (theTarget == "") {
		theTarget = "_parent";	
	}
	
	// Go to the URL
	window.open(url,theTarget);
}

function highlightOn(o) {
	o.style.backgroundColor = 'rgb(226,241,250)';	
}

function highlightOff(o) {
	o.style.backgroundColor = '#fff';	
}


// ==== SEARCH BOX STUFF ==== //
var squery = '';
function iCanHasEngage() {	
	$('ich').innerHTML = '<span onclick="switchBox(\'icanhas\')" style="cursor: pointer;"><b>I Want To...</b></span>';
	$('ss1').innerHTML = '<span onclick="switchBox(\'catalogsearch\')" style="cursor: pointer;"><b>Library Catalog</b></span>';
	$('ss2').innerHTML = '<span onclick="switchBox(\'sitesearch\')" style="cursor: pointer;"><b>OCLS Site</b></span>';
}

function switchBox(n) {
	var sb = $('search');
	var ichb = $('ichb');
	var b = $('gobut');
	var selbox = $('sb');
	var h = '';
	
	
	if ($('q')) {
		squery = $('q').value;	
	} else

	if ($('searcharg')) {
		squery = $('searcharg').value;	
	} else {
		squery = squery;	
	}


	switch (n) {
	
		case 'icanhas':

			h = '<select class="icanhas" id="sb" onchange="window.location=this.value;">';
			
			for (var o in icanhasstuff) {
				if (o != "end") {
					h += '<option value="' + o + '">' + icanhasstuff[o] +'</option>';
				}
			}

			h += '</select><input type="image" src="images/hp/searchbutton.jpg" alt="" onclick="window.location=document.getElementById(\'sb\').value;" class="buttonSmall" />';

			$('ich').style.color = '#000000';
			$('ss1').style.color = '#878787';
			$('ss2').style.color = '#878787';

		break;
		
		case 'sitesearch':
			
			h = '\
                    <FORM NAME=fbsearch METHOD="get" ACTION="http://google.ocls.info/search">\
                        <input type="hidden" name="ie" value="">\
                        <input type="hidden" name="site" value="OCLS_Main">\
                        <input type="hidden" name="output" value="xml_no_dtd">\
                        <input type="hidden" name="client" value="OCLS_Main">\
                        <input type="hidden" name="num" value="10">\
                        <input type="hidden" name="restrict" value="OCLS_Public">\
                        <input type="hidden" name="lr" value="">\
                        <input type="hidden" name="proxystylesheet" value="OCLS_Main">\
                        <input type="hidden" name="oe" value="">\
                        <INPUT TYPE="text" NAME="q" class="formText" id="q" value="' + squery + '"><input type="image" src="images/hp/searchbutton.jpg" alt="Submit" class="buttonSmall">\
                    </FORM>\
			';
			
			$('ich').style.color = '#878787';
			$('ss1').style.color = '#878787';
			$('ss2').style.color = '#000000';

		break;

		case 'catalogsearch':
			h = '\
                    <FORM NAME=fbsearch METHOD="post" ACTION="http://iii.ocls.info/search/a?a">\
                        <input type="hidden" name="SORT" value="D">\
                        <input type="hidden" name="SEARCHSCOPE" value="1">\
                        <input type="hidden" name="searchtype" value="Y">\
                        <INPUT TYPE="text" NAME="searcharg" class="formText" id="searcharg" value="' + squery + '"><input type="image" src="images/hp/searchbutton.jpg" alt="Submit" class="buttonSmall">\
                    </FORM>\
			';
			
			$('ich').style.color = '#878787';
			$('ss1').style.color = '#000000';
			$('ss2').style.color = '#878787';

		break;
	}
	
	sb.innerHTML = h;
	
}











