$(document).ready(function() {
    
    var pageReloaded = true; 

    $.history.init(function(hash){

      if (hash == "" && pageReloaded) {
        pageReloaded = false;  
      }

      else {
        if (hash == "" || hash.match(/^#*/i)) {

       //   alert('hash is set');
        	//$('#result_handler').fadeTo('slow', 0.2);
        	var str = $("#search_input").serialize();
        	$.post('realestate/ajax_search/' + hash,str,function(data){
        		$('#result_handler').html(data);
        	});
        }
      }
    },
    { unescape: "/" });
 
	
	
	$(".realestatetabnav li a,.tabulation li a").click(function(){

		$(".tab-contained").hide();
	//	$("hr,br").hide();
		var anchor = $(this).attr('href').replace('#','');
		$('.' + anchor).show();
		return false;
//		var block = $('.' + anchor).html();
//		$(".tab-container").prepend(block);	
	});

	
  

    // search form features
	
	$('.quicksearchbutton').click(function(){
		$('#quicksearch').submit();
	});   
	$('.handler .tabs-nav a').click(function(){	
		var viewtype = $(this).text().toLowerCase();
		$('#quicksearch select option[value="'+viewtype+'"]').attr('selected', 'selected');
		$('#quicksearch').submit();
		return false;
	});  	
	
	
/*	$('.searchwrapper ul.itemlist li a').click(function(){	
		var stuff = $(this).attr("href").split('/');
		var type = stuff[stuff.length-3];
		var id = stuff[stuff.length-1];
		if(type == "neighborhood"){
			$.post(
					"realestate/searchResult/", 
					{ neigh: id, formsearch: 1 } 
					);
		}
		if(type == "community"){
			$.post(
					"realestate/searchResult/", 
					{ community: id, formsearch: 1 } 
					);
		}
		if(type == "type"){
			$.load(
					"realestate/searchResult/"
					);
		}
		return false;
	});  
	*/
	
	
 
    /* highlight navigation */
    var loc = window.location;
    /* exact match */
    $("#navigation li a[href=\""+loc+"\"]").addClass("selected");
    $("#editobox >	 a[href=\""+loc+"\"]").addClass("selected");
    
    
    /* highlight submenu */
    $(".itemlist li a[href=\""+loc+"\"]").addClass("selected");   



	// ALTERNATE ROW COLORS FOR TABLE #ALTERNATE_COLORS
	$("tr:even").css("background-color", "#FFF");
  	$("tr:odd").css("background-color", "#FFF");
	$("tr").hover(function(){
	   $(this).css("background","#EEE");
	 },function(){
	   	$("tr:even").css("background-color", "#FFF");
	   	$("tr:odd").css("background-color", "#FFF");
	 });
	

	// contact form submit 
	$("#contactformsubmit").click(function(){
		
		// check the form values
		var name = $('#contactform #name').val();
		var email = $('#contactform #email').val();
		var message = $('#contactform #message').val();
		if(name == ""){ alert('please enter a name'); $('#contactform #name').css('border','1px solid red');return false;}
		if(email == ""){ alert('please enter a valid email'); $('#contactform #email').css('border','1px solid red');return false;}

		// ajax
		var str = $('#contactform').serialize();
		//alert(str);
		$.post("homesite/sendContactEmail",str,function(data) {
			$('#contact-wrapper').html('<center><h1 style="padding-top:100px">Your message has been sent !</h1><p><center>We will get back to you very shortly.</center></p></center>');
	 	});
		// load result
		return false;
	});
	 $('#slideshow_container').cycle({
			fx: 'fade',
			speed:  2500 , 
			timeout: 4000, 
			autostop:4,
			before: onBefore1 ,
			after: onAfter1,
			end:onAfter2
	 	});

		function onAfter1(curr,next,opts) {
				var title = $(this).attr('alt');
				$('.goto'+ opts.currSlide).css("background-image","url('sites/coronadocoastalliving.com/css/pt_black_60.png')");
				$('.goto'+ opts.currSlide).append('<div class="slideshow_pager"></div>');
				$('.image_title').fadeIn("slow").html(title);	
	    	}
		function onAfter2(curr,next,opts) {
		//	$('.slideshow_action').animate({width:'toggle'},320);
	        
		}
		function onBefore1(curr,next,opts){
				$('.slideshow_pager').hide();
				$('.image_title').fadeOut();
				$('.goto'+ opts.currSlide).css("background-image","url('')");
				
				}
				
	 	function pagerFactory(idx, slide) {
			var s = idx > 2 ? ' style="display:none"' : '';
			return '<li'+s+'><a href="#">'+(idx+1)+'</a></li>';
		}; 
		
		$('.goto0').hover(function() {
			$('#slideshow_container').cycle(0).cycle('pause'); 
		    return false; 
		},function(){$('#slideshow_container').cycle('resume')});
		 
		$('.goto1').hover(function() { 
			$('#slideshow_container').cycle(1).cycle('pause'); 
		    return false; 
		},function(){$('#slideshow_container').cycle('resume')});
		 
		$('.goto2').hover(function() { 
			$('#slideshow_container').cycle(2).cycle('pause'); 
		    return false; 
		},function(){$('#slideshow_container').cycle('resume')}); 
		
		$('.goto3').hover(function() { 
		    $('#slideshow_container').cycle(3).cycle('pause'); 
		    return false; 
		},function(){$('#slideshow_container').cycle('resume')}); 
		

		$('.goto0').click(function() {
			window.location = 'http://www.coronadocoastalliving.com/'; 
		});
		$('.goto1').click(function() {
			window.location= 'http://www.coronadocoastalliving.com/realestate'; 
		});
		$('.goto2').click(function() {
			window.location = 'http://www.coronadocoastalliving.com/page/coronado_living'; 
		});
		$('.goto3').click(function() {
			window.location = 'http://www.coronadocoastalliving.com/page/about_us'; 
		});



		$('.slideshow_goto').hover(function(){
			$(this).css("background-image","http://www.coronadocoastalliving.com/sites/coronadocoastalliving.com/css/pt_black_60.png");
		});    
		
// expandable //
		//$('#expandable .itemlist li:not(.title):not([href=\''+loc+'\'])').hide();
		$('.sidebarsubnav').hide();

		$('#expandable .itemlist .title').toggle(function(){
			$(this).nextAll().not('.sidebarsubnav').each(function() {
			      if ($(this).hasClass('title')) {
			        return false; // stop execution
			      } else {
			    	  $(this).slideDown(); 
			      }
			});
			
		},function(){
			$(this).nextAll().each(function() {
			      if ($(this).hasClass('title')) {
			        return false; // stop execution
			      } else {
			    	  $(this).slideUp(); 
			      }
			});
			
		}).css('cursor','pointer');

		// show submenu
		
		$('#expandable .itemlist .submenuitem').toggle(function(){
			var foo = $(this).attr('id');
			var exploded = foo.split('_');
			var idneigh = exploded['1'];
			$(this).html('[-]');
			$('.menuitemcomplex_' + idneigh).slideDown();			
			return false;
		},function(){
			var foo = $(this).attr('id');
			var exploded = foo.split('_');
			var idneigh = exploded['1'];
			if($(this).next('li').hasClass('sidebarsubnav')){
				$(this).html('[+]');
			} 
			$('.menuitemcomplex_' + idneigh).slideUp();			
			return false;
		});
		
		var itemclass = $("#expandable .itemlist li a[href=\""+loc+"\"]").parent('li').attr("class");
		if(typeof itemclass == "undefined") {
			}
		else{
				if (itemclass != ''){
					$('[class=' + itemclass + ']').show();
				}
				if(itemclass.indexOf( "sidebarsubnav" ) !== -1 ){
					$('.menuitem_neigh').show();
					$(this).css('background','#F2F2F2').addClass("selected");
				}
				if(itemclass.indexOf( "menuitem_neigh" ) !== -1 ){
					$("#expandable .itemlist li a[href=\""+loc+"\"]").parent('li').nextAll().each(function() {
						if ($(this).hasClass('menuitem_neigh')) {
					        return false; // stop execution
					      } else {
					    	  $(this).show(); 
					      }
					});
				}				
				
				
			}
	// property detail page : show pictures
		$('.sccl li img').hover(function(){
			$('img.imgfirst').fadeIn().get(0).src = this.src;
		});
		
	// refine search
		$('#search_form_refine select').each().hide();
	
		
	// skinned-select
		// Custom <select>
		
	// pagination and search filtering
		$('#table_handler th a').click(function(){
			var sorting = $(this).attr('class');
			$('#search_input #sorting option[value='+sorting+']').attr("selected", "selected");
			var str = $("#search_input").serialize();
			$('#search_input').submit();
			return false;
		});
		
	// sorting 

	// quicksearch
		
	$('.advancedsearch').click(function() {
		  $('#advanced_search').toggle('slow', function() {
			  });
		});
	$('#quicksearch .quickgo').click(function() { 
		var value = $(this).prev('select').val();
		var type = $(this).prev('select').attr('name');
		var name = $('#'+type+' :selected').text(); 
		window.location = "/realestate/quicksearch_redirect/"+ type +"/"+ value+"/"+name;

	}); 
	
  $("#quicksearch #neigh").change(function(){
    $.get("/realestate/ajax_loadComplex/"+$(this).val(), function(j){
      $("#quicksearch select#community").html(j);
    });
  });
  $("#quicksearch #proptype").change(function(){
	    $.get("/realestate/ajax_loadComplexbytype/"+$(this).val(), function(j){
	      $("#quicksearch select#community").html(j);
	    });
	  });
  
	// hz_quicksearch
	$('#advancedsearch').click(function() {
		  $('#advanced_search').toggle('slow', function() {
			  });
		  return false;
		});
 
	$('#resetform').click(function() {
		$(':input','#hz_quicksearch')
		 .not(':button, :submit, :reset, :hidden')
		 .val('')
		 .removeAttr('checked')
		 .removeAttr('selected');
		return false;
		});

	$('#displayall').click(function() {
		$(':input','#hz_quicksearch')
		 .not(':button, :submit, :reset, :hidden')
		 .val('')
		 .removeAttr('checked')
		 .removeAttr('selected');
		$('#hz_quicksearch').submit();
		return false;
	});
	
	$('.hz_quicksearchbutton').click(function(){
		$('#hz_quicksearch').submit();
	});   
	$('.handler .tabs-nav a').click(function(){	
		var viewtype = $(this).text().toLowerCase();
		$('#hz_quicksearch input:radio[value="'+viewtype+'"]').attr('checked', true);
		$('#hz_quicksearch').submit();
		return false;
	}); 
	
	
$("#hz_quicksearch #neigh").change(function(){
  $.get("/realestate/ajax_loadComplex/"+$(this).val(), function(j){
    $("#hz_quicksearch select#community").html(j);
  });
});
$("#hz_quicksearch #proptype").change(function(){
	    $.get("/realestate/ajax_loadComplexbytype/"+$(this).val(), function(j){
	      $("#hz_quicksearch select#community").html(j);
	    });
	  }); 
  
  
  $(".homeboxes").hover(
		  function(){$(this).addClass('homeboxeshover');},
		  function(){$(this).removeClass('homeboxeshover');}			  
  );
  

	$('#propertyremark').each(function(){
		var propid = $(this).attr('class');
		$(this).load('/realestate/ajax_loadPropRemark/'+propid);
	});
	$('a.fancyenlarge, a.fancyboxitem').fancybox();
	$('a.fancyenlarge').live({
		        mouseenter:
		           function()
		           {
						$(this).next('.clicktoenlarge').show();
		           },
		        mouseleave:
		           function()
		           {
		        	   $(this).next('.clicktoenlarge').hide();
		           }
		       });
	
	$('#svl_slideshow').cycle({ 
		speed: 3000,
	    next:   '#svl_slideshow_nav_prev', 
	    prev:   '#svl_slideshow_nav_next'
	});
	/*
	$('#editobox').hover(function () {
		   $("#svl_slideshow_nav").fadeToggle('slow');
	});
	*/
	
	// dropdown 
	$('.dropdownform select').change(function(){
		$("form.dropdownform").submit();
	});
	
});  
	
