$(document).ready(function(){
	var box1 = $(".first .update").height();
	var box2 = $(".second .form-2").height();

 /*
	*	   clear  text in search input
	*/	

	var fixe = document.getElementById('fixing');
	fixe.value="Type home name or number here.";
	fixe.onfocus = function(){
		if(this.value == "Type home name or number here."){
			this.value =  "";
		}
	};

	fixe.onblur = function(){
		if(this.value == ""){
			this.value =  "Type home name or number here.";
		}
	};

//menu
function mainmenu(){
$(" #manu ul ").css({display: "none"}); // Opera Fix
$(" #menu li").hover(function(){
		$(this).find('ul:first').css({visibility: "visible",display: "none"}).show(200);
		},function(){
		$(this).find('ul:first').css({visibility: "hidden"});
		});
}
  
 $(document).ready(function(){					
	mainmenu();
});


	$("div.form-2").css("display","none");
	$(".first .links a:first").hover( 							//toggle color for link on  slide menu
    function () {
      $(this).css("color","#19446D");
    }, 
    function () {
      $(this).css("color","#665E4D");
   });

	$(".second .links a:first").hover(
		function () {
		  $(this).css("color","#19446D");
		}, 
		function () {
		  $(this).css("color","#665E4D");
	});

 /*
	*      slideing right menu
	*/		       

		$(".first .links a#beachbag").toggle(          //top menu window
      function () { //close
				$(".first .links a#beachbag em").removeClass("close");
				$(".first .links a#beachbag em").addClass("open");
				$(".first .links").animate({height: box1+53}, { duration: 200,complete:function(){$(".first .links").css("height","auto");} });
				$(".first .update").animate({height: box1},{ duration: 200,complete:function(){$(".first .update").css("height","auto");} });
				$(".first .links a:first").css("color","#19446D");
				$("div.block").css("display","block");
      },

      function () {//open
				$(".first .links a#beachbag em").removeClass("open");
				$(".first .links a#beachbag em").addClass("close");
				$(".first .update").animate({height: "0px"},{ duration: 200,complete:function(){$(".first .update").css("display","none");} });
				$(".first .links").animate({height: "55px"},{ duration: 200,complete:function(){$("div.block").css("display","none");} });
				$(".first .links a:first").css("color","#665E4D");
      }
    );

    	$(".second .links a").toggle(			//bottom menu window
      function () {  //closed
				$(".second .links  em").removeClass("close");
				$(".second .links  em").addClass("open");
				$(".second .links").animate({height: box2+105}, { duration: 200,complete:function(){$(".second .links").css("height","auto");} });
				$(".second .link-3").animate({height: box2+104}, { duration: 200,complete:function(){$(".second .link-3").css("height","auto");} });
				$(".second .form-2").animate({height: box2}, 200 );
				$(".second .links a:first").css("color","#19446D");
				$(".second .links p:first").css("paddingTop","0");
				$(this).css("marginBottom","0");
				$("strong.open span").hide();

      },

      function () {//open
				$(".second .links a em").removeClass("open");
				$(".second .links a em").addClass("close");
				$(".second .form-2").animate({height: "0px"}, 200 );
				$(".second .links").animate({height: "55px"}, 200 );
				$(".second .link-3").animate({height: "51px"}, { duration: 200,complete:function(){$(".second .form-2").css("display","none");} });
				$(".second .links a:first").css("color","#665E4D");
				$(".second .links p:first").css("paddingTop","10px");
				$(this).css("marginBottom","15px");
				$("strong.open span").show();
      }
    );

/*
    *    drop down for custom  selects
    */    
  // ----- /// ---
		initSelectorsSidebar();
		//init popup
		// desribe actions in popups
		popupActions();

		$('a.adv-search').click(function(){					
			$('#refine_search').slideDown(300);	
			$('#refine_search_title_1').css('display','none');
			$('#flesh1').css('margin-top',0);
			$('#flesh2').css('display','none');		
			$(this).before('<p id="mach_search">101 homes match your search.</p>');
	    	$(this).hide();					
			var pos_left = $('div.sidebar').offset().left;
			var pos_top = $('div.sidebar').offset().top;
			var form_copy = $('div#form_wrapper').html();			
			var form_h = $('div.form').height();			
			$('div.form').empty();
			$('div.form').css('height',form_h + 180);			
			var out_form = '<div id="out_form"></div>';			
			$('#fader').after(out_form);
			$('#out_form').css('position','absolute');
			$('#out_form').css('left',pos_left);
			$('#out_form').css('top',pos_top);
			$('#out_form').css('z-index',400);
			$('#out_form').append(form_copy);
			$('#refine_search').css('height',180);
			$('#refine_search').css('overflow','visible');
			$('div.form').css('left',0);
			$('div.form').css('top',0); 			

			

			//
			initSelectorsSidebar();

			//
			$('div.form a.go2').click(function(){
				initSearchResults();
				return false;
			});	

			$(window).resize(function(){
				outFormPosition();
			});		
			return false;
		});	

		$('div.sidebar a.go2').click(function(){
			initSearchResults();
			return false;
		});		
});

var click_a = null;
var over_a = null;
var active_icon = null;

function outFormPosition(){
	var pos_left = $('div.sidebar').offset().left;
	var pos_top = $('div.sidebar').offset().top;
	$('#out_form').css('left',pos_left);
	$('#out_form').css('top',pos_top);
}

/**
 *	Open and position popups  
 *	
 */  	

function popupPosition(){
		if($('#refine_search').css('display')=="none")			
			var	popup = $('#popup_search');		
		else			
			var popup = $('#search_results');			

		var fader = $('#fader');
		var position_elem = $('div.image');
		var position_top = position_elem.offset().top + position_elem.height();
		var position_left = position_elem.offset().left;		
		var cont_h = popup.height() + position_elem.height(); 
		$('#content').css('height', cont_h + 75);		
		var win_w = $(document.body).width();
		if ($('#main').width() > win_w) win_w = $('#main').width() + 50;		 
	
		popup.css('top', position_top);
		popup.css('left', position_left);
		popup.css('z-index', 200);
		popup.css('display', 'block');		
	
		fader.css('position', 'absolute');
		fader.css('display', 'block');
		fader.css('z-index', 100);		
		var win_h = $(document).height();
		fader.css('height', win_h);
		fader.css('width', win_w);
		fader.css('top', 0);
		fader.css('left', 0);
		fader.css('opacity', 0.6); 

		fader.click(function(e){		
			if($('#refine_search').css('display')=="none")			
				var	popup = $('#popup_search');		
			else			
				var popup = $('#search_results');			
			e.preventDefault();
			$(this).hide();
			popup.hide();
					
			//popupActions();
		});
}

function popupActions(){
		// click in "search_results" popup on "on special" button (action: toggle small popup)	
	$('div.popup_homes_link a').click(function(){
		if($(this).next().css('display') == 'block')
			$(this).next().hide();
		else
			$(this).next().show();			
		return false;
	});	

	// clicks in "feature_column" menu, show arrow (one clk -> down, two clk -> up)
/*
	$('div.feature_column a').click(function(){			
		var className = $(this).attr('class');
		if(className.indexOf("active")!=-1){
			if(click_a != null){
				click_a.removeClass("click");		
			}		
			$(this).addClass("click");
			$(this).removeClass("active");			
		}
		else{
			if(click_a != null){
				click_a.removeClass("active");
				click_a.removeClass("click");		
			}
			$(this).addClass("active");
			$(this).removeClass("click");						
		}		
		click_a = $(this);
		return false;
	});	
*/

	// show/hide small popups in "popup_search", block "Search By Amenity"
	$('#search_amenity a').mouseover(function(){
		if(active_icon!=null)
			active_icon.hide();			
		$(this).next().show();
		active_icon = $(this).next();
	});	

	$('#search_amenity a').mouseout(function(){
		if(active_icon!=null)
			active_icon.hide();		
		active_icon = null;
	}); 

	var middle_i = $('#search_category li.middle input');
	var right_i = $('#search_category li.right input');
	var left_i = $('#search_category li.left input');	

	$('#search_category li.left input').click(function(){
		right_i.each(function(){
			this.checked = false;
		});

		middle_i.each(function(){
			this.checked = false;
		});
	});

	$('#search_category li.middle input').click(function(){
		right_i.each(function(){
			this.checked = false;
		});
		left_i.each(function(){
			this.checked = false;
		});
	});

	$('#search_category li.right input').click(function(){
		left_i.each(function(){
			this.checked = false;
		});
		middle_i.each(function(){
			this.checked = false;
		});
	});	

	var active_input = null;
	// set to all checkboxes false 
	/*$('div.vacation_form div.right input').each(function(){
		this.checked = false;
	});*/

	// clear all content in #text
	$('#text p').each(function(){
		$(this).css('display', 'none');
	});

	$('div.vacation_form div.right input').click(function(){
		var next_class = $(this).next().attr('class');
		//if next element has class "underlined"				
		if(next_class.indexOf("underlined")!=-1){
			// remove changes from last active element
			if(active_input!=null){	
				if(active_input.tagName == "INPUT")
					active_input = $(active_input).next();	
				else
					active_input = $(active_input);								
				var idnumb = active_input.attr('id');
				idnumb = idnumb.split('descrip_')[1];			
				var thisid = '#desc_text_' + idnumb;
				$(thisid).hide();
				active_input.css('border-bottom', 'none');
				active_input.css('background', 'url(/images/bg-underlined-label.gif) repeat-x scroll 0pt 100%');
			}	

			var idnumb = $(this).next().attr('id');
			idnumb = idnumb.split('descrip_')[1];			
			var thisid = '#desc_text_' + idnumb;
			$(thisid).show();
			$(this).next().css('background','none');
			$(this).next().css('border-bottom',"1px solid");
			active_input = this;
		}
	});

	// for two links: "smoking" and "Dog friendly"
	$('div.label_div label').click(function(){	
			// remove changes from last active element	
		if(active_input!=null){
			if(active_input.tagName == "INPUT")
				active_input = $(active_input).next();
			else
				active_input = $(active_input);
			var idnumb = active_input.attr('id');
			idnumb = idnumb.split('descrip_')[1];			
			var thisid = '#desc_text_' + idnumb;
			$(thisid).hide();
			active_input.css('border-bottom', 'none');
			active_input.css('background', 'url(/images/bg-underlined-label.gif) repeat-x scroll 0pt 100%');
		}

		var idnumb = $(this).attr('id');		
		idnumb = idnumb.split('descrip_')[1];			
		var thisid = '#desc_text_' + idnumb;		
		$(thisid).show();
		$(this).css('background','none');
		$(this).css('border-bottom',"1px solid");
		active_input = this;		

	});

}

/**
 *	Describe all actions in popups and open\close popup
 */ 

function initSearchResults(){	
	// open											
	popupPosition();

	//popupActions();
	// resize window -> new position of popup
	$(window).resize(function(){
		popupPosition();		
	}); 		
}

function initSelectorsSidebar(){
	  var select_open = false;
    var selected,top_id;
    $("div.select").click(    //toggle  open - closed
			function () {			
				 top_id = $(this);
			 	$("div.select ul li").click(function(){
					selected = $(this).children("a").text();
					top_id.children("a").text(selected);
			 		top_id.children("input").val(selected);	
				});

				if(select_open){
					$(this).children("ul").css("display","none");
					select_open = false;
					return false;
				}
				else{
					$("div.select ul").css("display","none");
					$(this).children("ul").css("display","block");
					select_open = true;
					return false;
				}
			});

	  $("div.select ul li").hover(   // hover on drop down menu 
			function(){
				$(this).addClass(" hovered")
			},	
			function(){
				$(this).removeClass(" hovered")
			}
		);
}