// JavaScript Document
var sf; // this will be a reference to the sucker fish functions - object literal
sfHover = function() {
	var testEl=document.getElementById("nav")
	
	if(testEl != null){ // elemnt exists
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	
	
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
	}
}


if (window.attachEvent) window.attachEvent("onload", sfHover);


//var sf; // this will be a reference to the sucker fish functions - object literal
sfHover2 = function() {
	
  if( ! document.getElementById("top_nav") )
  {
    return false;
  }
	var sfEls = document.getElementById("top_nav").getElementsByTagName("LI");
	
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
}


	if (window.attachEvent) window.attachEvent("onload", sfHover2);



/*
Write email addresses with javascript

write_email("name", "domain.com"); 
	writes "<a href='mailto:name@domain.com'>name@domain.com</a>

write_email("name", "domain.com", "Email Me"); 
	writes "<a href='mailto:name@domain.com'>Email Me</a>

write_email("name", "domain.com", "Email Me", "EmailClass"); 
	writes "<a class="EmailClass" href='mailto:name@domain.com'>Email Me</a>
	
	*/

function write_email(prefix, suffix, alt, classname){

 if(alt){
  	var address_hold ="<a href='mailto:" + prefix + "@" + suffix +"' target='_blank' class='"+ classname +"' >"+ alt +"</a>";
 		}else{
 
		var address_hold ="<a href='mailto:" + prefix + "@" + suffix +"' target='_blank' class='"+ classname +"' >" + prefix + "@" + suffix +"</a>";
 	}
	address=document.write(address_hold);
 return address;
 
}


// BEGIN :: MINI CALENDAR POPUP SCRIPT
function initMiniCalendar(){
	if ($('#mini_calendar .ev_wrap').length>0){ 
		
		$("#mini_calendar .ev_wrap").each(function(){
												   
		var m_popup = $(this).find('.popup')
		var m_trigger = $(this).find('a')
		
		var m_popup_height = m_popup.height();
		$(m_popup).css('top', '-' + m_popup_height + 'px');
		
		
			$(m_trigger).mouseover(function(){
			$(m_popup).show();				  
			})
			
			$(m_trigger).mouseout(function(){
			$(m_popup).hide();				  
			})
									
		})
	}
}
	



// BEGIN :: ADD AUDIO AND VIDEO TO POP UP	
	
function audioInit(audio_src, poster_src, modal_title){
	
	modal_title = unescape(modal_title);
	
	//shorten the modal title
			// if title is longer than 65 characters, truncate and add ... 
			if(modal_title.length > 65){
				modal_title = modal_title.substring(0, 65);
				modal_title = modal_title + '...';
			}
			
	// build our audio container
	var audioHTML = '<div id="av_hold"><div id="audio_player">\
	<img src="' + poster_src + '" id="audio_poster" />\
	<div id="audio_bar">\
    <audio id="audioControl" src="' + audio_src + '" controls preload> </audio>\
    </div>\
	</div></div>';
	$('#album_name').html(modal_title);
	$('#modal_bg').html(audioHTML);
	
				

	if  (Modernizr.audio && Modernizr.audio.mp3){ 
		// if this is an html5 compatible browser that can play mp3s.
		//includes iPhone  OS3+ & iPad
		var audioObj=new Object();
		audioObj=document.getElementById("audioControl");
		$("#audio_poster").click(function(ev){
			ev.preventDefault();
				if(audioObj.paused==true){
					audioObj.play();
				}else{
			audioObj.pause();
			}
		});
	
	}else{
		// Flash support for non-html5 desktops
		var so = new SWFObject('/swf/player.swf','player','400','25','9');
		so.addParam('allowfullscreen','true');
		so.addParam('allowscriptaccess','always');
		so.addParam('flashvars','file=' + audio_src);
		so.write('audio_bar');
		var player = document.getElementById('audio_bar');
		$("#audio_poster").click(function(){player.play(true);});
	}
}
	
	
function videoInit(videoID, modal_title){
	// unescape the modal title
	modal_title = unescape(modal_title);
	
	//shorten the modal title
			// if title is longer than 65 characters, truncate and add ... 
			if(modal_title.length > 65){
				modal_title = modal_title.substring(0, 65);
				modal_title = modal_title + '...';
			}
	
	var videoHTML = '<div id="av_hold"><div id="video_player">'+ videos[videoID].embed_code +'</div></div>' ;
	$('#album_name').html(modal_title);
	$('#modal_bg').html(videoHTML);
}
 	








$(document).ready(function() {
						   
		//// LOGOUT TESS	   
		$('.logout_tess').click( function(){
					
			jQuery.post(  site_root+'/logout-tess' ,
				{
				//'id': $(this).attr('id')
				},
				function( resp ){	
				}
			);				
							
		});		
		
		
		// check if logged in
		//var logged_in = '<a href="https://tickets.harristheaterchicago.org/account/change.aspx">Update</a> <span>or</span> <a class="logout_tess">Log Out</a>';
		var logged_in = '<a class="logout_tess">Log Out</a>';
	 	$.post('/checkloggedin', function(data) {
	 		// if logged in
			if(data == 'true'){
				$('.tesslogin').html(logged_in)
			}
			
			///console.log(data);
			
		});
	
		   
						   
						   
	// focus the input if hovering over the donation tab
	$("#donation").mouseover(function(){
		$("#donation_input").focus();
	});
	
	
	// Find all inputs with class default_text and use the label as the default value of the input
	var password_input_normal;
	
	if ( $('.default_text').length > 0){ //if element exists
	
		$('.default_text').each(function(e){
			var def_text= $(this).attr('default')
			if ( $(this).val() == '' || $(this).val() == undefined || $(this).val() == null ) {
				$(this).val(def_text);
				
			} 
			// if credentials are stored in keychain and filled in by defualt
			if ( $(this).attr('id') == 'login_password' || $(this).attr('id') == 'login_email'){
				
				if($(this).val() != ''){
					
						$(this).removeClass('login_password_bg').removeClass('login_email_bg')
					}
			}
			
			
			
			$(this).focus(function(){
				if ( $(this).val() == $(this).attr('default')) {
				$(this).val('');
				
					// check if this is email or password
					if ( $(this).attr('id') == 'login_password' || $(this).attr('id') == 'login_email'){
						$(this).removeClass('login_password_bg').removeClass('login_email_bg')
					}
				
				}
			});
			
			$(this).blur(function(){
					if ($(this).val() == '') {
						$(this).val(def_text);
						// check if this is email or password
						if ( $(this).attr('id') == 'login_password'){
							$(this).addClass('login_password_bg')
						}
						if ( $(this).attr('id') == 'login_email'){
							$(this).addClass('login_email_bg')
						}
					}
			});
		});
	}
	
	
	// ADD NEIGHBOR CLASSES NEXT TO CLASS CURRENT ON ALL FILTER TAB MENUS
	
	if ($('#filter_tabs').length>0){ //if element exists
		$('#filter_tabs .current').next().addClass('current_neighbor'); //add class to next sibling el
	}
			
	
	
	
	// BEGIN :: POP UP MODAL OBJECT 
	
	Modal = {
		
		overlayID : 'overlay',
		overlayFadeDuration : 450,
		content : '',
		contentID : '',
		contentCenter : 0,
		closeID : '',
		overlay : '',
		modalFunction : '',
		docHeight : $(document).height(),
		winHeight : $(window).height(),
		winScrollTop : 0,
		overlayHeight : 0,
		winCenter : 0,
		removeContent : false,
		removableContentID : 'av_hold',
		
		
	
		addModal : function (){ //if overlay doesn't exist in the dom, add it
			
			if($('#' + Modal.overlayID).length == 0){ // look for the ID of whatever you've named your overlay
				$('body').prepend(Modal.overlay); // if it doesn't exist already, add it to the dom
			}
			
			
			// set the height of our overlay to the docuement height
			$('#' + Modal.overlayID).height(Modal.overlayHeight);
			
			
			
			$('#' + Modal.overlayID).show();
			 $('#' + Modal.overlayID).animate({ opacity: 0.6 }, Modal.overlayFadeDuration, function() {
  			//$('#' + Modal.overlayID).fadeIn(Modal.overlayFadeDuration, function(){ // fade in the overlay 
			
			
				$('#' + Modal.contentID).show(); //show the modal content by ID and run any functions
				
				//center the modal 
				Modal.winCenter=(Modal.winHeight / 2) + Modal.winScrollTop;
				Modal.contentCenter = $('#' + Modal.contentID).height() / 2 ;
			
				//offset our content depending on scrollTop
				var contentTop = Modal.winCenter - Modal.contentCenter;
				$('#' + Modal.contentID).css('top', contentTop);
				$('#' + Modal.contentID).css('marginTop', '0px'); // IF there is a top margin, get rid of it
				
				// run any function that was passed
				eval(Modal.modalFunction); 
				
				
			
			})
			
			//hide the shadowbox and reset the defaults
			$('#' + Modal.overlayID + ', #' + Modal.closeID).click(function(e){
				e.preventDefault();
				Modal.hide();
			});
			
		},
		
		show : function(contentID, closeID, removeContent, func){
			
			// reset defaults	
			Modal.docHeight=$(document).height();
			Modal.winHeight=$(window).height();
			Modal.winScrollTop=$(document).scrollTop();
			
			Modal.setOverlayHeight();
			
			Modal.removeContent = removeContent;
			Modal.removableContentWrapHTML = '<div id ="' + Modal.removableContentWrapID + '"></div>';
			Modal.overlay = '<div id="' + Modal.overlayID + '"></div>';
			Modal.contentID = contentID;
			Modal.closeID = closeID;
			Modal.modalFunction = func;	
			Modal.addModal();
			
		},
		
		hide : function()
		{
			$('#' + Modal.closeID).unbind();
			if (typeof Thumb_SS != "undefined") { // see if our object exists
       			Thumb_SS.resetDefaults(); // NOT MODULAR : reset defaults and stop the slideshow
			}
			if(Modal.removeContent){ // if we are going to clear out and reset this content every time, add our removeable content wrapper div
				$('#' + Modal.removableContentID).html('');
			}
				
				$('#' + Modal.contentID).hide(); //remove the modal content 
				//$('#' + Modal.overlayID).fadeOut(Modal.overlayFadeDuration) // fade out the overlay
				 $('#' + Modal.overlayID).animate({ opacity: 0 }, Modal.overlayFadeDuration, function(){ 
					$('#' + Modal.overlayID).hide() ;
			   });
			
		},
		
		setOverlayHeight : function()
		{
		
			(Modal.docHeight < Modal.winHeight) ? Modal.overlayHeight = Modal.winHeight : Modal.overlayHeight = Modal.docHeight ;
			
		}
		
	};

	
	// BEGIN :: ADD MODAL POP UP FOR SLIDESHOWS - INCLUDE thumbnail_slideshow.js
	
	if ($('.photo_alb').length>0){ 
		$('.photo_alb').click(function(e){
			e.preventDefault();
			var reference_el = $(this).find('h3 a');
			// .album a should have the following attributes corresponding to the album json object to reference and the title of the album.
			var album_obj = reference_el.attr('album_obj');
			//var album_title = $(this).attr('album_title');
			var album_title = reference_el.html();
			
			album_title = escape(album_title);
			
			var thumb_function_string = '"' + album_obj +'" , "' + album_title + '"';
			//alert(thumb_function_string)
			Modal.show('gallery_slideshow', 'close_button', false, 'Thumb_SS.init('+ thumb_function_string +')');
																
		});
	};
	
	
	// BEGIN :: ADD MODAL POP UP FOR AUDIO AND VIDEO
	if ($('.av_album').length>0){ 
		$('.av_album').click(function(e){
			e.preventDefault();
			var reference_el = $(this).find('h3 a');
			//var modal_title = $(this).attr('modal_title');
			var modal_title = reference_el.html();
			//alert(modal_title)
			modal_title = escape(modal_title);
			
			
		
			if (reference_el.attr('type')=='video'){
				var videoID = reference_el.attr('videoid')
				var func_string = '"' + videoID +'", "'+ modal_title +'"';
				//alert(func_string )
				Modal.show('modal', 'close_button', true, 'videoInit('+ func_string +')');
				
			} else if(reference_el.attr('type')=='audio'){
				var audioSrc = reference_el.attr('source_file')
				var audioPoster = reference_el.attr('poster')
				var func_string = '"' + audioSrc +'", "'+ audioPoster +'", "' + modal_title + '"';
			
				Modal.show('modal', 'close_button', true, 'audioInit('+ func_string +')');
			}
		});
	}
	
	
	var oops = 'Oops! You forgot a query.';
	
	
	$( '#search_submit' ).click( function(ev){
		ev.preventDefault();	
		
		var search_term = encodeURI( $( '#search_field' ).val() );
		
		
		if( search_term=='' || $( '#search_field' ).val()==$( '#search_field' ).attr('default') ){
			 $( '#search_field' ).val( oops );
			 return false;
		}
	
		window.location.href = site_root + '/search/query/' + search_term;
	  }) ;///END CLICK TO SUBMIT SEARCH
	
	
	///// SET SEARCH FIELD VALUE TO DEFAULT
	if( $( '#search_field' ).val()=='' ){
		$( '#search_field' ).val( $( '#search_field' ).attr('default') );
	}
	
	$( '#search_field' ).focus( function(ev){
	
		
		if( $( this ).val()==$( this ).attr('default') || $( this ).val()==oops){
			$( this ).val('');
		}
		
		
	}) ;///END CLICK TO SUBMIT SEARCH
	
	
	
	$( '#search_field' ).blur( function(ev){
	
		
		if( $( this ).val()==$( this ).attr('default') || $( this ).val()==oops ){
			$( this ).val('');
		}
		
		
	}) ;///END CLICK TO SUBMIT SEARCH
	

	

	
	
	
});




