$(document).ready(function(){
	//get a reference to the search box
	var sb = $('#searchbox');
	var st = $('#searchto');
	var sf = $('#searchfrom');
	//set the special focus/blur functions for the search box
	sb.focus(function(){fcs($(this));});
	sb.blur(function(){blr($(this));}).blur();
	//set the special focus/blur functions for the search dates	
	st.focus(function(){fcsdate($(this));});
	st.blur(function(){blrdate($(this));}).blur();
	sf.focus(function(){fcsdate($(this));});
	sf.blur(function(){blrdate($(this));}).blur();
	
	//add the date picker	
	var min = new Date();
    $('#searchfrom,#searchto').datepicker({  
        duration: '',
        onSelect: function(){blrdate($(this));},
        dateFormat: 'yy-mm-dd',
        showTime: false,
        constrainInput: true,
        changeMonth: true,
        changeYear:true,
        minDate: '0',
        maxDate: '1y'
     });
     
	$('#searchform').submit(function(e){
		//if search text was not entered, don't submit form
		if ($.trim(sb.val()) == '' || $.trim(sb.val()) == sb.prev().text()){
			sb.focus();
			e.preventDefault();
		}
	});
	
	$('#banner.panel.greatseats').addClass('hasJS');
	$('#banner.panel.greatseats.hasJS').nivoSlider();

if($('#main table tr').length > 6 && $('#vmap').length){

	//move the map to the side
	$('#vmap').appendTo('#side').addClass('panel');
	$('#vmap h2').after('<p>Click the map to expand or hide it.</p>');
	
	$('p.map','#vmap').html('<a href="#" class="vmapexpand">' + $('p.map','#vmap').html() + '</a>');
	$('p.map img','#vmap').css('width','100%');
	
	var offsettop = $('#vmap').offset().top;
	var margintop = parseInt($('#vmap').css('marginTop'));
	
	$('#vmap').appendTo('#side').css('zIndex',10);
	$('#vmap').css('position','relative');
	$('#vmap').css('top',0);
	$('#vmap').css('left',0);
	$('#vmap').css('width',$('#side').css('width'));

	//what to do when they click on the map
	$('a.vmapexpand').live('click',function(e){
		e.preventDefault();
		
		if($('#vmap').hasClass('newwindow')){
			window.open($('img',this).attr('src'), '_blank');
		}else{
			$('#vmap').animate({"width":'560px'});
			$('a.vmapexpand').removeClass('vmapexpand').addClass('vmapshrink');
		}
	});
	//what to do when they click on the map (to hide it)
	$('a.vmapshrink').live('click',function(e){
		e.preventDefault();
		$('#vmap').animate({'width':$('#side').css('width')});
		$('a.vmapshrink').removeClass('vmapshrink').addClass('vmapexpand');
	});
	$('a[href=#vmap]').click(function(e){
		e.preventDefault();
		
		if($('a.vmapshrink').length == 1) $('a.vmapshrink').click();
		else $('a.vmapexpand').click();
	});
	

	$(window).scroll(function(){
		var pos = $(window).scrollTop() + margintop;
		pos =  pos < offsettop ? margintop : pos - offsettop + margintop;

		//if the side is smaller than the main panel, then shrink the map		
		if($('#main').innerHeight() > $('#side').innerHeight() && $('#vmap').innerHeight() < $(window).height()){
			if($('a.vmapshrink').length == 1){
				$('a.vmapshrink').click();
			}
			$("#vmap").css({"width":$('#side').css('width')});
		}

		var sidebottompos = ($('#main').offset().top + $('#main').innerHeight() + margintop - 2) - $('#vmap').innerHeight() - offsettop;

		if(pos > sidebottompos) pos = sidebottompos;
		if(pos < margintop) pos = margintop;

		//slide the map into the scroll focus area
		$("#vmap").stop().animate({"marginTop": (pos) + "px"}, "fast");
	});		
}

/*
	if($('#main table tr').length > 5){
		$('#vmap').appendTo('#wrap').css('zIndex',100).click(function(e){
			$('#vmap').fadeOut('fast');
		}).hide();
		
		$('a[href="#vmap"]').click(function(e){
			e.preventDefault();
			
			var lnk = $(e.target);
			var main = $('#main');
			
			if($('#vmap').css('display') == 'none')
			{
				$('#vmap').css('position','absolute');
				$('#vmap').css('top',$('#content').position().top + lnk.parents('tr').height() + lnk.parents('div').position().top + lnk.position().top-10);
				$('#vmap').css('left',$('#content').position().left + main.position().left + 20);
				$('#vmap').fadeIn();
			}
			else $('#vmap').fadeOut('fast');
		});
	}
*/	
	
	//if there are more than 10 rows...
	if($('tbody tr').length > 10)
	{
		$('div.pricefilter').addClass('panel');
		$('div#price-range-label').css('paddingBottom','1em');
	
		//get the maximum/minumum range value based on the current table
		var maxrange = 0;
		$('tbody tr').each(function(){
			var price = parseInt($(this).find('td:eq(2)').text().replace('$','').replace('each','')) || 0;
			if(price > maxrange) maxrange = price;
		});
		var minrange = maxrange;
		$('tbody tr').each(function(){
			var price = parseInt($(this).find('td:eq(2)').text().replace('$','').replace('each','')) || 0;
			if(price < minrange) minrange = price;
		});
		
		//create the slider
		$('#price-range').slider({
			range: true,
			min: minrange,
			max: maxrange,
			values: [ minrange, maxrange ],
			slide: function( event, ui ) {
				$('#price-range-label').text("Price Range : $" +ui.values[0]+ " - $" +ui.values[1]);
				$('#filter_from').val(ui.values[0]);
				$('#filter_to').val(ui.values[1]).keyup();
			}
		});
		//set the starting price range based on the current table
		$("#price-range-label").text('Price Range : $'+minrange+' - $'+maxrange);
		
		//default each row to visible  
		$('tbody tr').addClass('visible');
		zebraRows('tr.visible:odd', 'odd');
		
		$('#filter_from,#filter_to').keyup(function(event) {
			//if esc is pressed or nothing is entered  
			if (event.keyCode == 27 || $('#filter_from').val() == '') {  
				//if esc is pressed we want to clear the value of search box  
				$('#filter_from,#filter_to').val('');
				//we want each row to be visible because if nothing is entered then all rows are matched.  
				$('tbody tr').removeClass('visible').show().addClass('visible');  
			//if there is text, lets filter
			}else{
				filter('tbody tr', $('#filter_from').val(),$('#filter_to').val());  
			}
			//reapply zebra rows  
			$('tr.visible').removeClass('odd');  
			zebraRows('tr.visible:odd', 'odd'); 	
		});
	  
		//used to apply alternating row styles  
		function zebraRows(selector, className)  
		{  
		  $(selector).removeClass(className).addClass(className);  
		}
		function filter(selector, queryfrom,queryto) {  
			queryfrom = parseInt($.trim(queryfrom)) || 0 ; //trim white space  
			queryto = queryto < queryfrom ? 0 : (parseInt($.trim(queryto)) || 0); //trim white space  
		  
			$(selector).each(function(){
				var price = parseInt($(this).find('td:eq(2)').text().replace('$','').replace('each','')) || 0;
				
				if(queryto)
					if(price >= queryfrom && price <= queryto)
						$(this).show().addClass('visible');
					else
						$(this).hide().removeClass('visible');
				else
					if(price >= queryfrom)
						$(this).show().addClass('visible');
					else
						$(this).hide().removeClass('visible');
			})
		}
	}
});
function fcs(obj){
	obj.removeClass('blr');
	if($.trim(obj.val()) == obj.prev().text()) obj.val('');
}
function blr(obj){
	if ($.trim(obj.val()) == '' || $.trim(obj.val()) == obj.prev().text()){
		obj.addClass('blr');
		obj.val(obj.prev().text());
	}
}
function fcsdate(obj){
	obj.removeClass('blr');	
	if($.trim(obj.val()) == 'Any time') obj.val('');
}
function blrdate(obj){
	obj.removeClass('blr');
	if ($.trim(obj.val()) == '' || $.trim(obj.val()) == 'Any time'){
		obj.addClass('blr');
		obj.val('Any time');
	}
}


