function preloadImages() {
	  var d=document; if(d.images){ if(!d.p) d.p=new Array();
	    var i,j=d.p.length,a=preloadImages.arguments; for(i=0; i<a.length; i++)
	    if (a[i].indexOf("#")!=0){ d.p[j]=new Image; d.p[j++].src=a[i];}}
}
function slideSwitch() {
	$('.inactive').show();
	$('#slideshow img').removeClass('inactive');
	var $active = $('#slideshow .active');
   
    if ( $active.length == 0 ) $active = $('#slideshow img:last');

    // use this to pull the divs in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow img:first');

    $active.addClass('last-active');
    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1500, function() {
            $active.removeClass('active last-active');
        });
};

$(function() {
	preloadImages(
			  "images/backgroundblack.png" 
			 );
	setInterval( "slideSwitch()", 8000 );
	/*Feature Prop*/

	var homeSliderTimer=null;
	
	$('.content_prop a[href^="#left"]').live('mousedown',function (event) {
		
		if (homeSliderTimer)clearInterval(homeSliderTimer);
		homeSliderTimer= setInterval(function(){
			$('.allproperties').scrollTo( '-=10px', 0, { axis:'x' });
		},50);
	});
	$('#allprop a[href^="#right"]').live('mousedown',function (event) {

		if (homeSliderTimer)clearInterval(homeSliderTimer);
		
		homeSliderTimer = setInterval(function(){
			$('.allproperties').scrollTo( '+=10px', 0, { axis:'x' });
		},50);
	});

	$('.content_prop a[href^="#right"]').live("dragstart", function() {
	     return false;
	});
	$('.content_prop a[href^="#left"]').live("dragstart", function() {
	     return false;
	});

	$('.content_prop a[href^="#left"]').live('mouseup',function () {
		if (homeSliderTimer)clearInterval(homeSliderTimer);
		return false;
	});
	$('.content_prop a[href^="#right"]').live('mouseup',function () {
		if (homeSliderTimer)clearInterval(homeSliderTimer);
		return false;
	});
	$('.content_prop a[href^="#left"]').live('mouseleave',function () {
		if (homeSliderTimer)clearInterval(homeSliderTimer);
		return false;
	});
	$('.content_prop a[href^="#right"]').live('mouseleave',function () {
		if (homeSliderTimer)clearInterval(homeSliderTimer);
		return false;
	});
	$('.content_prop a[href^="#right"]').live('click',function () {
		return false;
	})
	$('.content_prop a[href^="#left"]').live('click',function () {
		return false;
	});
	/* */
	
	if ($('#sale_tab_content .allproperties').children().size() < 1)
	{
		$('#sale_tab_content .allproperties').append('<div id="tab-loader"></div>');
		$.post("index/ajax-sale-prop",{}, function (data) {
			$('#sale_tab_content .allproperties').html(data);
			updateArrowVisibility('#sale_tab_content');
		},"text");
	}
	$('.title a[href^="#"]').live('click',function () {
		$('.left_butt').hide();
		$('.right_butt').hide();
		$('.allproperties').removeClass('space');
		var $this = $(this);
		var url = $(this).attr("href").split('#')[1];
		
		if ($('#'+$this.attr('id')+'_content .allproperties').children().size() < 1 )
		{
			$('#'+$this.attr('id')+'_content .allproperties').append('<div id="tab-loader"></div>');
			$.post(url,{}, function (data) {
				$('#'+$this.attr('id')+'_content .allproperties').html(data);
				
				updateArrowVisibility('#'+$this.attr('id')+'_content');
			},"text");
		}
		else 
		{
			updateArrowVisibility('#'+$this.attr('id')+'_content');
		}
	})
	
	function updateArrowVisibility(id)
	{
		var nb = $(id+' .properties').length;
		if (nb>3) 
		{
			$('.left_butt').show();
			$('.right_butt').show();
			$('.allproperties').removeClass('space');
		} 
		else{
			$('.left_butt').hide();
			$('.right_butt').hide();
			$('.allproperties').addClass('space');
		}
	}
	
	$('.title a[href^="#index/ajax-sale-prop"]').live('click',function() {
		$("#sale_tab_select").css ('display','block');
		$("#listed_tab_select").css ('display','none');
		$("#sold_tab_select").css ('display','none');
		$(".sale_prop").removeClass("sel");
		$(".list_prop").removeClass("sel");
		$(".sold_prop").removeClass("sel");
		$(".best_prop").removeClass("sel");
		$(".sale_prop").addClass("sel");
		$('.sale_prop').show();
		$('.sold_prop').hide();
		$('.list_prop').hide();
		$('.best_prop').hide();
		return false;
	});
	
	
	$('.title a[href^="#index/ajax-just-listed"]').live('click',function() {
		$("#sale_tab_select").css ('display','none');
		$("#listed_tab_select").css ('display','block');
		$("#sold_tab_select").css ('display','none');
		$(".sale_prop").removeClass("sel");
		$(".list_prop").removeClass("sel");
		$(".sold_prop").removeClass("sel");
		$(".best_prop").removeClass("sel");
		$(".list_prop").addClass("sel");
		$('.list_prop').show();
		$('.sale_prop').hide();
		$('.sold_prop').hide();
		$('.best_prop').hide();
		return false;
	});
	
	$('.title a[href^="#index/ajax-forclosure-prop"]').live('click',function() {
		$("#sale_tab_select").css ('display','none');
		$("#listed_tab_select").css ('display','none');
		$("#sold_tab_select").css ('display','block');
		$(".sale_prop").removeClass("sel");
		$(".list_prop").removeClass("sel");
		$(".sold_prop").removeClass("sel");
		$(".best_prop").removeClass("sel");
		$(".sold_prop").addClass("sel");
		$('.sold_prop').show();
		$('.sale_prop').hide();
		$('.list_prop').hide();
		$('.best_prop').hide();
		return false;
	});

	/**/
	
	var keywodsText;
	$("input.before-focus").live("focus", function(){
		keywordsText = $(this).val();
		$(this).val("");
		$(this).removeClass('before-focus');
	});
	$("textarea.before-focus").live("focus", function(){
		keywordsText = $(this).val();
		$(this).html("");
		$(this).removeClass('before-focus');
	});
	
	$(".nr").keydown(function(event) {
		if ( event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 37 || event.keyCode == 39) {
		} else {
			if (event.keyCode < 95) {
				if (event.keyCode < 48 || event.keyCode > 57 ) {
					event.preventDefault();
				}
			} else {
				if (event.keyCode < 96 || event.keyCode > 105 ) {
					event.preventDefault();
				}
			}
		
		}
	});
	$("#send_contact_email").live('click', function(){
		A25Core.__validate('.contact', 'index/json-validate-email', function(resp){
			if (resp.success){                                        
				$.post("index/contact-me", {from: $('#contact_from').val(), subject: $('#contact_subject').val(), comments: $('#contact_content').val()}, 
        				function (data) {
		            		if (data.success){
		            			$('input[name=from]').val('');
								$('input[name=subject]').val('');
								$('textarea[name=content]').val('');                                              
		            			$.fancybox("Message Sent",{});
	                        } 
		            			else { alert('Try sending the mail later!')}
				}, "json");
			} else {
                $.each(resp.errors,function(k,value){
                    $("input[name='" +k +"']").parent().parent().find('em').html("*");
                    $("select[name='"+ k + "']").parent().parent().find('em').html("*");                                        
                });
            }
		});
		return false;
	});
	
});
