$(document).ready(function() {

$("img.navButton").hover(
 function()
 {
  this.src = this.src.replace("_n","_r");
 },
 function()
 {
  this.src = this.src.replace("_r","_n");
 }
)
	
});


$(document).ready(function() {
	$(function() {
			//increase the height so the outer isn't bigger than the outer for big descriptions
			paddedCentered();
	});
	//hotel image slideshow
	if ( $('#hotelSlide').length ){
		$('#hotelSlide').cycle({
			fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
			timeout: 5000
		});
	}
	
	//no symbols for activity or attraction search..
	if($('.activitySearch').length){
		$('.activitySearch').alphanumeric({allow:" "});	
	}
	if($('.attractionSearch').length){
		$('.attractionSearch').alphanumeric({allow:" "});	
	}
	if($('.attractionName').length){
		$('.attractionName').alphanumeric({allow:" "});	
	}
	if($('.alphaNum').length){
		$('.alphaNum').alphanumeric({allow:" "});	
	}

	

 });
 
 //admin MCE
 $(document).ready(function() {
	 //test to see if it's actually there, was screwing up the rest of the code
	 if ( $('.mce').length ){
        $(".mce").cleditor({
			 width:     '700px', // width not including margins, borders or padding
          height:      '300px', // height not including margins, borders or padding
          controls:     // controls to add to the toolbar
                        "bold italic underline strikethrough subscript superscript | removeformat | bullets numbering | outdent " +
                        "indent | undo redo | " +
                        "link unlink | cut copy paste pastetext | print source"
			
		})
	 }
	 //add stuff
	 if ($('#addAttraction').length ){
		 
		 $('#addAttraction').click(function() {
			 $('#addAttraction').fadeOut();
			 $('#newAttraction').fadeIn();
			 paddedCentered();
		 });
		 
	 }
	 
	 if ($('#addDeal').length ){
		 
		 $('#addDeal').click(function() {
			 $('#addDeal').fadeOut();
			 $('#newDeal').fadeIn();
			 paddedCentered();
		 });
		 
	 }
	 //tooltips
	  if ( $('.help').length ){
		 $(".help").tooltip();
	  }
	 
});

function paddedCentered()
{
	//if the content is bigger than the parent, increase height by 128 pixels
				$('.paddedCentered').each(function() {
			if($(this).parent().height() <= $(this).height() + 14)
			{
				while($(this).parent().height() <= $(this).height() + 14)
				{
					$(this).parent().height($(this).parent().height() + 128);
				}
				
			}
			
		});
		//vertically center the text
		$('.paddedCentered').each(function() {
		this.style.position = 'absolute';
		this.style.top = $(this).parent().height()/2 + 'px';
		this.style.marginTop = -$(this).height()/2 + 'px';
		//this.style.left = ($(this).parent().width() - $(this).width())/2 + 'px';
		});
		
	if ($('.attractionsBox').length ){
		var atPos = $('.attractionsBox').parent().offset();
		var footPos = $('.lBox:last').offset();
		var eh = footPos.top + $('.lBox:last').height();
		//var newHeight = $('.attractionsBox').position().top;
		$('.attractionsBox').parent().height(eh - atPos.top);
		$('.attractionsBox').height($(document).height() - atPos.top - 6 - 167);
	}
}

 $(document).ready(function() {
//image cropping
function setCoords(c) {
    jQuery('#x').val(c.x);
    jQuery('#y').val(c.y);
    jQuery('#x2').val(c.x2);
    jQuery('#y2').val(c.y2);
    jQuery('#w').val(c.w);
    jQuery('#h').val(c.h);
}

if ($('#cropthis').length ){
	$('#cropthis').Jcrop({
		 onChange: setCoords,
		 onSelect: setCoords,
		 boxWidth: 900,
		 boxHeight:900,
		 aspectRatio: 1
	});
}
 });
 
 //datepicker
$(document).ready(function() {
if ($('.datePicker').length ){
	  $( ".datePicker" ).datepicker({dateFormat: 'yy-mm-dd'});

}
});
  
  //scrollable
$(document).ready(function() {
	if ($('.attractionsBox').length ){
		$(".attractionsBox").scrollable({ vertical: true, mousewheel: true });
	}
	if ($('.scrollable').length ){
		$(".scrollable").scrollable({circular:true}).autoscroll({
	interval: 6000		
})
	}
	  
});


//autocomplete
$(document).ready(function() {
	//style and remove default text
$('.sBox input[type="text"]').addClass("idleField");
$('.sBox input[type="text"]').focus(function() {
	$(this).removeClass("idleField").addClass("focusField");
	if (this.value == this.defaultValue){
		this.value = '';
	}
	if(this.value != this.defaultValue){
		this.select();
	}
});
$('.sBox input[type="text"]').blur(function() {
	if(this.value == this.defaultValue)
	{
		$(this).removeClass("focusField").addClass("idleField");
	}
});

$( ".locationSearch" ).autocomplete({
	source: function(request, response) {
		$.ajax({ url: "autocomplete/locations",
		data: { term: $(".locationSearch").val()},
		dataType: "json",
		type: "POST",
		success: function(data){
			response(data);
		}
	});
},
minLength: 1
});

$( ".activitySearch" ).autocomplete({
	source: function(request, response) {
		$.ajax({ url: "autocomplete/activities",
		data: { term: $(".activitySearch").val()},
		dataType: "json",
		type: "POST",
		success: function(data){
			response(data);
		}
	});
},
minLength: 1
});

$( ".activitySearch2" ).autocomplete({
	source: function(request, response) {
		$.ajax({ url: "autocomplete/activities",
		data: { term: $(".activitySearch2").val()},
		dataType: "json",
		type: "POST",
		success: function(data){
			response(data);
		}
	});
},
minLength: 1
});

$( ".activitySearch3" ).autocomplete({
	source: function(request, response) {
		$.ajax({ url: "autocomplete/activities",
		data: { term: $(".activitySearch3").val()},
		dataType: "json",
		type: "POST",
		success: function(data){
			response(data);
		}
	});
},
minLength: 1
});
	
$( ".attractionSearch" ).autocomplete({
	source: function(request, response) {
		$.ajax({ url: "autocomplete/attractions",
		data: { term: $(".attractionSearch").val()},
		dataType: "json",
		type: "POST",
		success: function(data){
			response(data);
		}
	});
},
minLength: 1
});



});



$(document).ready(function() {
	
		//loading gif
		$("#loading").bind("ajaxStart", function(){
			$(this).show();
		}).bind("ajaxStop", function(){
			$(this).hide();
		});
		
	$("#numOfResults").bind("ajaxStart", function(){
			$(this).hide();
		}).bind("ajaxStop", function(){
			$(this).show();
		});
	
	if($('#attsScroll').length){
	$('#attsScroll').jScrollPane({verticalGutter: 30});
	}
	
	$('#selectedState').val('none');
	$('#selectedActivity').val('none');
	$('#selectedAttraction').val('none');
	newAjax();
	

	 
	 $('.states span').click(function() { 
		 if($(this).data('selectable') == 'yes')
		 {
		 //if it's the same as the one in the box, clear the box
			if($('#selectedState').val() != $(this).attr('id'))
			{
				$('#selectedState').val($(this).attr('id'));
				$(this).css('font-weight', 'bold');
			}
			else
			{
				$('#selectedState').val('none');
				$(this).css('font-weight', 'normal');
			}
			 newAjax();
		 }
	 });
	 
	 $('.acts span').click(function() { 
	  if($(this).data('selectable') == 'yes')
		{
		 //if it's not in its box..
			if($('#selectedActivity').val().indexOf($(this).attr('id')) == -1)
			{
				//if 'none' is in the box, get rid of it
				if($('#selectedActivity').val() == 'none')
				{
					$('#selectedActivity').val("");	
				}
				//otherwise add the term and a comma to the box
				$('#selectedActivity').val($('#selectedActivity').val() + $(this).attr('id') + ",");
				$(this).css('font-weight', 'bold');
			}
			else
			{
				//otherwise remove it from the box
				var str = $('#selectedActivity').val();
				str = str.replace($(this).attr('id') + ',', "");
				//alert(str);
				$('#selectedActivity').val(str);
				
				//if($('#selectedActivity').val() 
				//$('#selectedActivity').val('none');
				$(this).css('font-weight', 'normal');
			}
			 newAjax();
		 }
	 });
	 
	 $('.atts span').click(function() { 
	 var text = $(this).text();
	  if($(this).data('selectable') == 'yes')
		 {
		 //if it's the same as the one in the box, clear the box
			if($('#selectedAttraction').val().indexOf(text) == -1)
			{
				if($('#selectedAttraction').val() == 'none')
				{
					$('#selectedAttraction').val("");	
				}
				$('#selectedAttraction').val($('#selectedAttraction').val() + text + ",");
				$(this).css('font-weight', 'bold');
			}
			else
			{
				var str = $('#selectedAttraction').val();
				str = str.replace(text + ',', "");
				//alert(str);
				$('#selectedAttraction').val(str);
				
				//if($('#selectedAttraction').val() 
				//$('#selectedAttraction').val('none');
				$(this).css('font-weight', 'normal');
			}
			 newAjax();
		 }
	 });
	 
});

function newAjax()
{
	var selectedState = $('#selectedState').val();
	var selectedActivity = $('#selectedActivity').val();
	var selectedAttraction = $('#selectedAttraction').val();
	
	$.post("browse/ajax", { 'selectedState': selectedState, 'selectedActivity': selectedActivity, 'selectedAttraction': selectedAttraction},
	function(data){
		//reset all of the fading for the states
		$('.states span').data('selectable', 'no');
		$('.acts span').data('selectable', 'no');
		$('.atts span').data('selectable', 'no');
		
		var numResults = new Array();
		//loop through the results
		for(var i = 0; i < data.length; i++)
		{
			//if there's a state that matches this, selectable
			$('.states span#'+data[i].state).data('selectable', 'yes');
			if(data[i].tag1)
				$('.acts span#'+data[i].tag1).data('selectable', 'yes');
			
			if(data[i].tag2)
				$('.acts span#'+data[i].tag2).data('selectable', 'yes');
				
			if(data[i].tag3)
				$('.acts span#'+data[i].tag3).data('selectable', 'yes');
				
			if(data[i].hotelTag1)
				$('.acts span#'+data[i].hotelTag1).data('selectable', 'yes');
				
			if(data[i].hotelTag2)
				$('.acts span#'+data[i].hotelTag2).data('selectable', 'yes');
				
			if(data[i].hotelTag3)
				$('.acts span#'+data[i].hotelTag3).data('selectable', 'yes');
			
			$('.atts span').each(function() {
				if($(this).text() == data[i].name)
				{
					$(this).data('selectable', 'yes');
				}	
			});
			
			if(jQuery.inArray(data[i].hotelID, numResults) == -1)
			{
				numResults.push(data[i].hotelID);
			}
		}
		
		$('.states span, .atts span, .acts span').each(function(index) {
				if($(this).data().selectable == 'no')
				{
					$(this).fadeTo("fast", .7);
					$(this).css( 'cursor', 'default' );
					
				}
				if($(this).data().selectable == 'yes')
				{
					$(this).fadeTo("fast", 1);
					$(this).css( 'cursor', 'pointer' );
				}
		});
		
		//how many results there are - put it in the box
		$('#numOfResults').text(numResults.length);
		$('#hotelsForm').val(numResults)

		
	}, "json");
	
	
	
}


