jQuery(document).ready(function($) {	
	parseURLParams();
	
	//For flipping boxes on homepage that flipped when they shouldn't flip (can slow down android browsers)
	$("a.stopPropagation").click(function(e){e.stopPropagation();});
	
	
	//show footer
	$('#directorynav').toggle();
	$('.toggleFooter').toggle();
	
	$('#lsFlip2 .contentFlip2').bind("click",function(){
		// $(this) point to the clicked .contentFlip element (caching it in elem for speed):
		var elem = $(this);
		LOG('flip');
		// data('flipped') is a flag we set when we flip the element:
		if(elem.data('flipped'))
		{
			// If the element has already been flipped, use the revertFlip method
			// defined by the plug-in to revert to the default state automatically:
			elem.revertFlip();
			
			// Unsetting the flag:
			elem.data('flipped',false)
		}
		else
		{
			// Using the flip method defined by the plugin:
			elem.flip({
				direction:'lr',
				speed: 350,
				onBefore: function(){
					// Insert the contents of the .contentData div (hidden from view with display:none)
					// into the clicked .contentFlip div before the flipping animation starts:
					elem.html(elem.siblings('.contentData').html());
				}
			});
			
			// Setting the flag:
			elem.data('flipped',true);
			LOG('tb_init');
			tb_init("#lsFlip2 div.backContent .thickbox");
			
		}
	},false);

	
	
	
	$('#lsFlip3 .contentFlip3').bind("click",function(){
		// $(this) point to the clicked .contentFlip element (caching it in elem for speed):
		var elem = $(this);
		
		// data('flipped') is a flag we set when we flip the element:
		if(elem.data('flipped'))
		{
			// If the element has already been flipped, use the revertFlip method
			// defined by the plug-in to revert to the default state automatically:
			elem.revertFlip();
			
			// Unsetting the flag:
			elem.data('flipped',false)
		}
		else
		{
			// Using the flip method defined by the plugin:
			elem.flip({
				direction:'lr',
				speed: 350,
				onBefore: function(){
					// Insert the contents of the .contentData div (hidden from view with display:none)
					// into the clicked .contentFlip div before the flipping animation starts:
					elem.html(elem.siblings('.contentData').html());
				}
			});
			
			// Setting the flag:
			elem.data('flipped',true);
			//LOG('tb_init');
			//tb_init("#lsFlip2 div.backContent .thickbox");
			
		}
	});

	
	
	
	//when rolling downwards off a picker, close all bubbles
	$("#miniFooter").mouseover(function(){
	 	if(BrowserDetect.browser=="Explorer"){
 	 		$("#pickerBubbles div.bubble").attr("style", "display: none");
		}else{
		 	$("#pickerBubbles div.bubble").fadeOut('fast');
		}
	});

	
	$('#eventsCarousel').jcarousel({
      	scroll: 8,
       	start: 4,
		wrap: 'circular',
        //initCallback:   mycarousel_initCallback,
  		itemLastInCallback:   mycarousel_itemLastInCallback,
      	itemVisibleInCallback: {onBeforeAnimation: eventsCarousel_itemVisibleInCallback},
        itemVisibleOutCallback: {onAfterAnimation: eventsCarousel_itemVisibleOutCallback}
	});


	//iphone tap to close
	$('#pickerBubbles div.bubble').click(function() {
  		$(this).fadeOut('fast');
	});
		
	if(BrowserDetect.browser=="Explorer"){
 	  	$("#pickerBubbles div.bubble").hover(
			//ie(even 8) can't handle animating the opacity of a png with an alpha channel
      		function () {}, 
      		function () {$(this).attr("style", "display: none");}
    	);
	}else{
		$("#pickerBubbles div.bubble").hover(
			function () {}, 
			function () {$(this).fadeOut('fast');}
		);
	}


	$('img.hover').hover(
		function() {$(this).attr('src', $(this).attr('src').replace( /_off/i, '_over' ));}, 
		function() {$(this).attr('src', $(this).attr('src').replace( /_over/i, '_off' ));}
	);



	//calendar bubbles
	$(".calendarTable .cBubble","#mainColBody").hover(
		function () {
			var theData = $(this).attr('data-bubble'), theSel= "#" + theData + " div.bubble";
			if(BrowserDetect.browser=="Explorer"){$(theSel).attr("style", "display:block")}else{$(theSel).fadeIn('fast')}
		}, 
		function () {}
	);

});


function   mycarousel_itemLastInCallback(carousel, two,three,state) {
 	//hide any open bubbles
 	$("#pickerBubbles div.bubble:not(.open)").attr("style", "display: none");
	var startLocation = 1;
	//this is not a good place for the page specific start scroll
	if(state=='init'){carousel.scroll(startLocation)}
};





function eventsCarousel_itemVisibleInCallback(carousel, item, i, state, evt){
 	var idx = carousel.index(i, eventsCarousel_itemList.length);
 	var thisItem = eventsCarousel_itemList[idx - 1];  
 	//	if(window.console){console.log('eventsCarousel_itemVisibleInCallback')};

	 $("a[href*=/forms/]","#eventsCarousel").click(function() {
		//alert('hi');
		var myURL =  this.href;
		var theseParams = parseGetVars(myURL);
		var thisDept = unescape(theseParams['department']);
		var thisSubject = unescape(theseParams['subject']);
		var thisBody = unescape(theseParams['body']);
		//alert(thisDept);

			
		// Pass a value to thickbox window by updating parent page's hidden form field	
		// set the value of the hidden form field id='dept' in the calling page
		// to the 'department' value captured from the link clicked on, that is now stored in the var 'thisDept'
		//document.getElementById("dept").value = thisDept;
		document.globalStorage.field1.value = thisDept;
		document.globalStorage.field2.value = thisSubject;
		document.globalStorage.field3.value = thisBody;
	});	
 	
  	
    var thisItemHTML = eventsCarousel_getItemHTML(thisItem) ;    
    var thisWidth = item.width;
    var thisHeight = item.height;
    // Create an object from HTML
    var DOMitem = jQuery(thisItemHTML).get(0);
	
	
 	
 	//Bubbles
 	if(thisItem.bubble){
		//initialize bubble
		var theSel= "#" + thisItem.bubble + " div.bubble";
		if(BrowserDetect.browser=="Explorer"){
 	 
 	  		$(DOMitem).hover(
				//ie(even 8) can't handle animating the opacity of a png with an alpha channel
      			function () {$(theSel).attr("style", "display:block");}, 
      			function () {}
    		);
  
		}else{
	 	
			$(DOMitem).hover(
				function () {$(theSel).fadeIn('fast');}, 
				function () {}
		);
	}
	}

 	 //Thickboxes
 	if(thisItem.thickbox){
 	
 	if(thisItem.complex!="yes"){
 		//Simple thickboxes
   		tb_init(DOMitem);
  	}else{
       	 //Complex thickboxes
       	if(BrowserDetect.browser=="Safari"||BrowserDetect.browser=="Chrome"){
       		//Safari and Chrome do popup
       		$(DOMitem).click(function () {
       			var thisHref= $(this).attr('href');
   				displayWindow(thisHref, 930,490,0);
   				return false;
   			});
       	}else{
         	//Complex thickboxes, FF, IE
			tb_init(DOMitem);
		}  
  	}
  	}
  	
  	//REPLACE FLIP CONTENTS
	if(typeof(thisItem.replaceWith)!="undefined"){
		newHTML=   $(thisItem.replaceWith).html() ;
    	var DOMitem = jQuery(newHTML).get(0);
	}
  	
  	
	carousel.add(i, DOMitem);
	
	//INIT FLIP
	if(typeof(thisItem.replaceWith)!="undefined"){initFlip();}
	
}

function eventsCarousel_itemVisibleOutCallback(carousel, item, i, state, evt){carousel.remove(i);};

function eventsCarousel_getItemHTML(item){
	var isPopup=item.popup;
	var replaceSelector=item.replaceWith;
	var thisHTML="";
	
	if (isPopup=='bubble'){
	    thisHTML=  '<a href="'+item.href+'">	<img src="' + item.url + '" width="94" height="88" alt="' + item.title + '" /><p>' +item.title+'</p></a>';
	}else{
    	thisHTML=  '<a href="'+item.href+'"><img src="' + item.url + '" width="94" height="88" alt="' + item.title + '" /><p>' +item.title+'</p></a>';
	}
	return thisHTML;
}



function parseURLParams(){
	var tb = unescape($.getURLParam("tb"));
	var width = unescape($.getURLParam("width"));
	var height = unescape($.getURLParam("height"));
	if(width =="null"){width =  "900"}	
	if(height =="null"){height =  "450"}
	
	if(tb !="null"){
		tbURL = tb + "?width="+width+"&height=" + height;
		tb_show("", tbURL, null);
	}
}



function initFlip(){
	$('.contentFlip').bind("click",function(){
		// $(this) point to the clicked .contentFlip element (caching it in elem for speed):
		var elem = $(this);
		
		// data('flipped') is a flag we set when we flip the element:
		if(elem.data('flipped'))
		{
			// If the element has already been flipped, use the revertFlip method
			// defined by the plug-in to revert to the default state automatically:
			elem.revertFlip();
			
			// Unsetting the flag:
			elem.data('flipped',false)
		}
		else
		{
			// Using the flip method defined by the plugin:
			elem.flip({
				direction:'lr',
				speed: 350,
				onBefore: function(){
					// Insert the contents of the .contentData div (hidden from view with display:none)
					// into the clicked .contentFlip div before the flipping animation starts:
					elem.html(elem.siblings('.contentData').html());
				}
			});
			
			// Setting the flag:
			elem.data('flipped',true);
		}
	});
}


// doesn't work, don't know why
//		$("#pickerBubbles div.bubble").hoverIntent({
//				sensitivity: 7, 
//				interval: 700, 
//				over: showBubble, 
//				timeout: 700, 
//				out: hideBubble
//			});
function showBubble(){ $(this).show();}
function hideBubble(){$(this).hide();}


