$(document).ready(function(){
	// Detect JS
	$("#jstest").load("/ajax/detectjs.php"); 

	//Datepicker
	$('.datepicker').datepicker({numberOfMonths: 2});

	//Show javascript elements that are hidden for non-js users
	var elementsToShow = $('body').find('.script-only');
	for (i=0; i<elementsToShow.length; i++) {
		elementsToShow[i].style.display = 'block';
	}
	var elementsToShow = $('body').find('.script-only-i');
	for (i=0; i<elementsToShow.length; i++) {
		elementsToShow[i].style.display = 'inline';
	}
	//Hide elements that don't need to be displayed for js-users
	var elementsToHide = $('body').find('.script-hide');
	for (i=0; i<elementsToHide.length; i++) {
		elementsToHide[i].style.display = 'none';
	}
	//Tab functionality
	$('ul.jt-tabs').each(function(){
		$(this).tabs($(this).next('div.panes').children());
	});

	$('#jsEnabled').val('true');
	$('.jsEnabled').val('true');
	
	$('div#main-itinerary').find('div.collapse').hide();		

	//Fading notifications
	$('.auto-fade').each(function(){
		var obj = this;
		setTimeout(function(){
				$(obj).hide(1000);
			},
			2000
		);
	});

	$('.confirm').click(function() {		
		$msg = $(this).attr('title');
		if ($msg.length == 0) {			
			$msg = $(this).attr('name');
		}
		var response = confirm('Are you sure you want to '+$msg+'?');
		return response;
	});

	$('input.auto-select').focus(function() {
		$(this).select();
	});
	$('span.auto-select-inst').hide();
	
	//$('.time-show-list').tabs({ selected:3 });
	
	//Popup code
	$('a.new-window').bind('click',function(){ return false;});
	$('a.new-window').click(function(){		
		var URL = $(this).attr('href');
		var wPos = URL.indexOf('w=');
		var hPos = URL.indexOf('h=');
		var width;
		var height;
		if (wPos != -1) {        
		    width = URL.substr(wPos+2,4);        
		} else {
		    width = 600;
		}
		if (hPos != -1) {        
		    height = URL.substr(hPos+2,4);        
		} else {
		    height = 600;
		}
		
		var parameters = 'height='+height+', width='+width+', scrollbars=yes, location=no, menubar=no';
		
		window.open(URL, 'Popup', parameters); 
	});
	
	//Show/hide elements tied to checkbox statuses
	$('.toggle-box').click(function() {
		var name = $(this).attr('name');
		var partner = document.getElementById(name);
		if (this.checked == true) {
			$(partner).show();
		} else {
			$(partner).hide();
		}
	});
	
	$('#print-screen').bind("click", function() {return false;});
	$('#print-screen').click(function() {
		window.print();
	});	
	
	//Hide/Show Flash
	if (document.getElementById('main-slideshow')) {
		var flash = $('#main-slideshow');
		var hideFlash = $('.flash-toggle')[0];
		var showFlash = $('.flash-toggle')[1];
		
		$(hideFlash).bind("click", function() {return false;});
		$(hideFlash).click(function() {		
			$(flash).hide();
			$('#flash-shadow').show();
			$(hideFlash).hide();
			$(showFlash).show();
			var href = $(hideFlash).attr('href');
			var start = href.indexOf('#')+1;
			var key = href.substr(start);
			$.ajax({
				type: "GET",
				url: "/ajax/set-session.php?key="+key+"&val=hide"
			});
		});
		
		$(showFlash).bind("click", function() {return false;});	
		$(showFlash).click(function() {
			$('#flash-shadow').hide();
			$(flash).show();
			$(showFlash).hide();
			$(hideFlash).show();
			var href = $(showFlash).attr('href');
			var start = href.indexOf('#')+1;
			var key = href.substr(start);
			$.ajax({
				type: "GET",
				url: "/ajax/set-session.php?key="+key+"&val=show"
			});
		});
	}	
	
	var ajax = new Array();
	//Ajax search results keyboard navigation
	//$(document).bind("keydown", function(e) {if(e.keyCode == 40 || e.keyCode == 38) {return false;}});
	$('.ajax-input').keydown(function(e) {
		if (e.keyCode != 40 && e.keyCode != 38) {
			return;
		}
		var links = $(this).siblings('div').find('a');
		if (links.length == 0) {
			return;
		}
		var id = $(this).siblings('.ajax-results').attr('id');		
		if (e.keyCode == 40) {																		
			if (ajax[id+'-link'] < (links.length-1)) {
				$(links[ajax[id+'-link']]).removeClass('hover');
				ajax[id+'-link']++;
			} else {
				return;
			}			
			$(links[ajax[id+'-link']]).addClass('hover');			
		}
		if (e.keyCode == 38) {
			if (ajax[id+'-link'] > 0) {
				$(links[ajax[id+'-link']]).removeClass('hover');
				ajax[id+'-link']--;
			} else {
				return;
			}			
			$(links[ajax[id+'-link']]).addClass('hover');			
		}
		switch (id) {
			case 'header-ajax-results':
				var url = links[ajax[id+'-link']].href;
				$(this).siblings('.directLink').val(url);
				break;
			case 'directions-ajax-results':
				var url = links[ajax[id+'-link']].href;
				var pos = url.indexOf('saddr=',url);
				var loc = url.substr(pos+6);
				$(this).siblings('input[name="ajax-saddr"]').val(loc);								
		}		
	});		
	
	// Search Box AJAX
	//var headerSearchResults = $.ajax({type: "GET", url: "/search/ajax"});
	var headerSearchResults = null;
	var headerAjaxInput;
	if (document.getElementById('header-ajax-results')) {			
		headerAjaxInput = $('#header-ajax-results').siblings('input.ajax-input');
		$(headerAjaxInput).keyup(function(e) {
			if (e.keyCode == 38 || e.keyCode == 40) {
				return;
			}
			headerRunAjax();
			ajax['header-ajax-results-link'] = -1;			
		});
		
		var headerResultsFocus = false;
		$(headerAjaxInput).focus(function() {
			this.focused = this;
			headerResultsFocus = true;
			headerRunAjax();
			$('#header').css('z-index','3');
			$('.dateSelect').hide();
		});
		
		$(headerAjaxInput).blur(function() {
			resultsFocus = false;
			if (resultsHover == false) {
				$(headerAjaxInput).siblings('.directLink').val(false);
				headerHideResults();
			}
			$(headerAjaxInput).siblings('.directLink').val(false);
			setTimeout(headerHideResults,100);
		});		
		var resultsHover = false;
		$('#header-ajax-results').mouseover(function() {
			resultsHover = true;
		});
		$('#header-ajax-results').mouseout(function() {
			resultsHover = false;
			if (headerResultsFocus == false) {
				headerHideResults();
			}
		});
	}
	function headerHideResults() {
		$('#header-ajax-results').html('');
		$('#header').css('z-index','1');
	}
	function headerRunAjax() {
		if (headerSearchResults != null) {
			headerSearchResults.abort();
		}
		var str = $(headerAjaxInput).val();
		str = str.replace('#','%23');
		if (str == '') {
			$('#header-ajax-results').html('');
		} else {			
			headerSearchResults = $.ajax({
				type: "GET",
				url: "/search/ajax?search="+str,
				success: headerLoadAjax
			});
		}
	}	
	function headerLoadAjax(html) {		
		$('#header-ajax-results').html(html);
		$('#header-ajax-results').find('a').mousedown(function() {
			window.location.href = $(this).attr('href');
		});
	}
	
	//Driving directions AJAX	
	var directionsAjaxInput;
	var directionsAjaxForm;
	//var directionsSearchResults = $.ajax({type: "GET", url: "/itinerary/ajax/directions"});
	var directionsSearchResults = null;
	if (document.getElementById('directions-ajax-results')) {		
		directionsAjaxInput = $('#directions-ajax-results').siblings('input.ajax-input');
		directionsAjaxForm = $('#directions-ajax-results').parent();
		var directionsResultsFocus = false;
		$(directionsAjaxInput).focus(function() {			
			directionsResultsFocus = true;			
			directionsRunAjax();
			ajax['directions-ajax-results-link'] = -1;
		});		
		$(directionsAjaxInput).blur(function() {			
			resultsFocus = false;
			if (resultsHover == false) {
				$(directionsAjaxInput).siblings('.directLink').val(false);
				directionsHideResults();	
			}			
		});
		
		var resultsHover = false;
		$('#directions-ajax-results').mouseover(function() {
			resultsHover = true;
		});
		$('#directions-ajax-results').mouseout(function() {
			resultsHover = false;
			if (directionsResultsFocus == false) {
				directionsHideResults();
			}
		});							
	}
	function directionsHideResults() {
		$('#directions-ajax-results').html('');			
	}	
	function directionsRunAjax() {		
		var daddr = $(directionsAjaxInput).siblings('input[name="daddr"]').val();
		daddr = daddr.replace('#','%23');
		if (directionsSearchResults != null) {
			directionsSearchResults.abort();
		}
		directionsSearchResults = $.ajax({
			type: "GET",			
			url: "/itinerary/ajax/directions?daddr="+daddr,
			success: directionsLoadAjax
		});
	}	
	function directionsLoadAjax(html) {		
		$('#directions-ajax-results').html(html);
		/*$('#directions-ajax-results').find('a').mousedown(function() {
			window.location.href = $(this).attr('href');
		});*/
	}
	$('.qTips a[href][title]').qtip({ content: { text: false 	}, show: { delay: 50 }, position: {	corner: { tooltip: 'bottomMiddle', target: 'topMiddle' } }, style: { border: { width: 5, radius: 10 }, padding: 10, textAlign: 'center', tip: true,	name: 'cream' }});
	$('.qTips input[title]').qtip({ content: { text: false 	}, show: { delay: 50 }, position: {	corner: { tooltip: 'bottomMiddle', target: 'topMiddle' } }, style: { border: { width: 5, radius: 10 }, padding: 10, textAlign: 'center', tip: true,	name: 'cream' }});
	$('img.qTips[title]').qtip({ content: { text: false 	}, show: { delay: 50 }, position: {	corner: { tooltip: 'bottomMiddle', target: 'topMiddle' } }, style: { border: { width: 5, radius: 10 }, padding: 10, textAlign: 'center', tip: true,	name: 'cream' }});
	$(".props").change(function(){
		resultID = 'perfs_'+$(this).attr('id');
		//alert(resultID);
		//alert("<?php echo site_url('/controlpanel/shows/get_performances/'.$this->uri->segment(4));?>/"+$('#'+$(this).attr('id')+' :selected').val());
		$.getJSON("<?php echo site_url('/controlpanel/shows/get_performances/'.$this->uri->segment(4));?>/"+$('#'+$(this).attr('id')+' :selected').val(), '', function(j){
			var toptions = '<option value=""> </option>';
			for (var i = 0; i < j.length; i++) {
				toptions += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
			}
			//alert(toptions);
			$('#'+resultID).html(toptions);
		});
	});
});