function toggleSearch(){
	
	$(".searchToggle").click( 
		function()
		{
	    	if ( $("form#detailed_prop_search").is(":hidden") ){
	    		$("form#detailed_prop_search").slideDown(500);
	    	} else {
	        	$("form#detailed_prop_search").slideUp(500);
	      	}
	    }
	);

}

function checkDates(){
	
	var date_filter = /^\d{1,2}(\/)\d{1,2}\1\d{4}$/;
	var email_filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	// DATE FORMAT MM/DD/YYYY
	$("input.isDate").focus(
		function()
		{
			if( $(this).attr("value") == "mm-dd-yyyy"){
				$(this).attr("value", "");
			}
		}
	);
	$("input.isDate").blur(
		function()
		{
			if( $(this).attr("value") == null){
				$(this).attr("value", "mm-dd-yyyy");
			}
		}
	);
	
	// EMAIL ADDRESS
	$("input.isEmail").focus(
		function()
		{
			if( $(this).attr("value") == "enter your valid email address"){
				$(this).attr("value", "");
			}
		}
	);
	$("input.isEmail").blur(
		function()
		{
			if( $(this).attr("value") == null){
				$(this).attr("value", "enter your valid email address");
			}
		}
	);
	/*
	$("input.isDate").blur(
		function()
		{
			//alert( "YOU HAVE JUST LEFT THE INPUT " + $(this).attr("id") );
			var currentInput_ID = $(this).attr("id");
		//	alert(currentInput_ID);
			var str = $(this).attr("value");
		//	alert(str);
		//	alert( $(this) );
			if (!filter.test(str)){
				//alert('Please enter a vaild date, MM/DD/YYYY');
				return false;
				$("#" + currentInput_ID).focus();
			//	alert( $("#" + currentInput_ID) );
				//alert($(this));
			}
		}
	);
	*/
	
}

function toggleSpecials(){
	
	$("ul#specialsNav li.num a").click( 
		function()
		{
			$("ul#specialsNav li.num a").attr("class", ""); //remove the selected special styling
			
			$(this).attr("class" ,"selected"); //add the selected special styling to the newly viewed special
			
			var whichID = $(this).attr("id").replace(/a_/, ""); //derive the ID of the newly viewed special from the corresponding anchors ID
			
			$("div.specials_huddle.current").removeClass("current"); //hide all specials
			
			$("#" + whichID).addClass("current").fadeIn("slow"); //show the new special
		
			return false; //disable the anchor from refreshing the page
		}
	);

	$("ul#specialsNav li.previous a").click( 
		
		function()
		{
			var prefix = "specials_00";
			var anch_prefix = "specials_a_00";
			// remove the current class, derive the numerical suffix from the current special
			var currentSpecial = $("div.specials_huddle.current").removeClass("current").attr("id").replace(prefix, "");
			// destermin the next special to toggle
			var nextSpecial = parseInt(currentSpecial) - 1;
			// if at the first special, set next special to the last special
			if(nextSpecial < 1){
				//determine total specials
				var nums = $("div.specials_huddle").length;
				nextSpecial = nums;
			}
			// toggle off all specials
			$("div.specials_huddle").css("display", "none");
			//
			$("ul#specialsNav li.num a").attr("class", ""); //remove the selected special styling
			$("ul#specialsNav li.num a#" + anch_prefix + nextSpecial).attr("class", "selected");
			
			
			$( "ul#specialsNav li.num a#" + prefix.replace("/_/", "_a_") + nextSpecial).attr("class" ,"selected");
			// toggle on the new special
			$("#" + prefix + nextSpecial).attr("class", "specials_huddle current").fadeIn("slow");
			// set next special
			nextSpecial --;
			
			return false; //disable the anchor from refreshing the page
		}
	);
	
	$("ul#specialsNav li.next a").click(
		
		function()
		{
			var prefix = "specials_00";
			var anch_prefix = "specials_a_00";
			var nums = $("div.specials_huddle").length;
			// remove the current class, derive the numerical suffix from the current special
			var currentSpecial = $("div.specials_huddle.current").removeClass("current").attr("id").replace(prefix, "");
			// destermin the next special to toggle
			var nextSpecial = parseInt(currentSpecial) + 1;
			// if at the first special, set next special to the last special
			if(nextSpecial > nums){
				//determine total specials
				nextSpecial = 1;
			}
			// toggle off all specials
			$("div.specials_huddle").css("display", "none");
			//
			$("ul#specialsNav li.num a").attr("class", ""); //remove the selected special styling
			$("ul#specialsNav li.num a#" + anch_prefix + nextSpecial).attr("class", "selected");
			
			
			$( "ul#specialsNav li.num a#" + prefix.replace("/_/", "_a_") + nextSpecial).attr("class" ,"selected");
			// toggle on the new special
			$("#" + prefix + nextSpecial).attr("class", "specials_huddle current").fadeIn("slow");
			// set next special
			nextSpecial ++;
			
			return false; //disable the anchor from refreshing the page
		}
	);
	
}

function resultsFilter(){
		
	$("form#resultsFilter select").change(
		function(){
			
			geturl = $("form#resultsFilter input#geturl").attr("value");
			bedroomsVal = $("form#resultsFilter select#numbeds").attr("value");
			viewVal = $("form#resultsFilter select#viewtype").attr("value");
			/*
			if(bedroomsVal < 1){
				return false;
			}
			if(viewVal < 1){
				return false;
			}
			*/
			if( bedroomsVal != 0 || viewVal != 0){
				replyQuery(bedroomsVal, viewVal, geturl);
			}
			
		});
}

function movepropphotos()
{
	if($('#photolist').length)
	{
		$('#photolist').sortable({
								stop: saveOrder
							});
	}
							
	$('#photolist li a').mouseover(
		function(){
			num = $(this).text();
			$(this).text("X");
			$(this).addClass("delete");
		});
	$('#photolist li a').mouseout(
		function(){
			$(this).text(num);
			$(this).removeClass("delete");
		});
	$('#photolist li a').click(
		function(){
			slug = $("input#propertyslug").attr("value");
			list = $("#photolist li");
			prop_id = $("#photolist").attr("class").replace(/propid_/, '').replace(/ ui-sortable/, '');
			new_order = new Array();
			for (var i = 0; i < list.length; i++)
			{
				id = list[i].getAttribute("id").replace(/image_order_/, '');
				new_order.push(id);
			}
			$(this).parent().fadeOut("easeInExpo");
			
			new_order = JSON.stringify(new_order);
			$.post("/js/deletephoto.php", { image_order: new_order, deleteme: num, propertyID: prop_id, slug: slug }, function(data){
				$('#photolistwrap').html(data);
			});
			return false;
		});
}

function saveOrder()
{
	slug = $("input#propertyslug").attr("value");
	list = $("#photolist li");
	prop_id = $("#photolist").attr("class").replace(/propid_/, '').replace(/ ui-sortable/, '');
	new_order2 = new Array();
	for (var i = 0; i < list.length; i++)
	{
		id = list[i].getAttribute("id").replace(/image_order_/, '');
		new_order2.push(id);
	}
	new_order2 = JSON.stringify(new_order2);
	$.post("/js/photos.php", { image_order: new_order2, propertyID: prop_id, slug: slug }, function(data){
		$('#photolistwrap').html(data);
	});
	
}

function getE(){
       	var url_string = String(document.location);
       	var url_array1 = url_string.split('?');
		if(url_array1[1]){
			var e_array = url_array1[1].split('&');
			for(i=0;i<=(e_array.length);i++){
				if(e_array[i]){
					var useVal = e_array[i].split('=');
				}
			} 
		}
		return useVal[1];
}


function toot(){
	$('img.ico').mouseover( function(){
			cursrc = $(this).attr("src");
			newsrc = cursrc.replace(/.gif/, "_on.gif");
			$(this).attr("src", newsrc);
		});
	$('img.ico').mouseout( function(){
			cursrc = $(this).attr("src");
			newsrc = cursrc.replace(/_on.gif/, ".gif");
			$(this).attr("src", newsrc);
		});
}

function toggle_forgot(){
	$("p.toggle_forgot a").click( function(){
		$("form#forgot").slideToggle(500);
		return false;
	})
}
function mycarousel_initCallback(carousel) {

    $('div.prev_btn p a').click( function() {
        carousel.prev();
        return false;
    });

	$('div.next_btn p a').click( function(){
		carousel.next();
        return false;
	});
	
	$('ul#cabin_gallery_list li.item a img').click( function(){
		$(this).parent().parent().parent().children().children().removeClass("current");
		$(this).parent().addClass("current");
		newsrc = $(this).attr("src").replace(/_th/, "_lg");
		$('div#mainImg_wrap img').attr("src", newsrc);
		return false;
	});

	/*
    jQuery('a#prevProperty').bind('click', function() {
        carousel.prev();
        return false;
    });
    jQuery('a#nextProperty').bind('click', function() {
        carousel.next();
        return false;
    });
	*/

};
function cabin_gallery()
{
	if($("ul#cabin_gallery_list").length)
	{
		jQuery("ul#cabin_gallery_list").jcarousel({
			scroll: 4,
			easing: 'easeOutElastic',
			animation: 500,
			wrap: "both",
			initCallback: mycarousel_initCallback,
			// This tells jCarousel NOT to autobuild prev/next buttons
			buttonNextHTML: null,
			buttonPrevHTML: null
		});
	}
}

$(document).ready(function(){

	cabin_gallery();
	toggleSearch();
	toggleSpecials();
	checkDates();
	resultsFilter();
	//toot();
	toggle_forgot();
	
	movepropphotos();
	
});

// Ride the carousel...

jQuery(document).ready(function() {
});
