// JavaScript Document

jQuery(document).ready(function()
{
	// fade cicle on main page
	jQuery("#fade-cycle").cycle({
								fn : 'fade',
								speed : 1000,
								slideExpr : 'div.slide',
								timeout : 8000
								});
	

	var advanced_search_visible = false;
	var contacts_visible = false;	
	var country_guide_visible = false;	
	
	// hides all menu items without element
	function hideMenuItmes(element)
	{
		jQuery(element).removeClass("top-unhover");	
		
		jQuery("#main-nav a").each(function(){
			if(this != jQuery(element).get(0))
				jQuery(this).addClass("top-unhover");
		});
	}
	// shows all menu items
	function showMenuItems()
	{
		jQuery("#main-nav a").removeClass("top-unhover");	
	}	
	
	jQuery("a[rel=advanced-property-search]").click(function()
	{	
		// if contacts bar is visible hide it
		if(contacts_visible)
		{
			jQuery(".top-board.contacts").slideToggle(500);
			jQuery("#main-nav a[rel=contacts]").toggleClass("current");
			contacts_visible = false;			
		}	
		if(country_guide_visible)
		{
			jQuery(".top-board.country-guide").slideToggle(500);
			jQuery("#main-nav a[rel=country-guide]").toggleClass("current");
			country_guide_visible = false;			
		}	
		
		// if it is a top menu link then show or hide 
		// search board else only show		
		if(jQuery(this).hasClass("top"))
		{
			jQuery(".top-board.advanced-search").slideToggle(500);	
			jQuery("#main-nav a[rel=advanced-property-search]").toggleClass("current");
			
			if(advanced_search_visible)
			{	
				
				advanced_search_visible = false;
				// show all itmes without current
				showMenuItems();
			}
			else
			{
				// hide all itmes without current
				hideMenuItmes(this);
				advanced_search_visible = true;	
			}
		}
		else
		{
			if(jQuery(this).hasClass("new-search"))
			{
				jQuery(".search-form").clearForm();
			}
			// scroll to top of the page
			jQuery('html, body').animate({scrollTop:0}, 'slow');
			
			jQuery(".top-board.advanced-search").slideDown(500);	
			jQuery("#main-nav a[rel=advanced-property-search]").addClass("current");
			advanced_search_visible = true;
						// hide all itmes without current
			hideMenuItmes("#main-nav a[rel=advanced-property-search]");			
		}			
		return false;
	});
	
	jQuery("a[rel=contacts]").click(function()
	{			
		// if advanced search bar is visible hide it
		if(advanced_search_visible)
		{
			jQuery(".top-board.advanced-search").slideToggle(500);
			jQuery("#main-nav a[rel=advanced-property-search]").toggleClass("current");
			advanced_search_visible = false;
		}
		
		if(country_guide_visible)
		{
			jQuery(".top-board.country-guide").slideToggle(500);
			jQuery("#main-nav a[rel=country-guide]").toggleClass("current");
			country_guide_visible = false;			
		}	

		// if it is a top menu link then show or hide 
		// contacts board else only show
		if(jQuery(this).hasClass("top"))
		{
			jQuery(".top-board.contacts").slideToggle(500);				
			jQuery("a[rel=contacts]").toggleClass("current");		
			if(contacts_visible)
			{				
				contacts_visible = false;
				// show all itmes without current
				showMenuItems();
			}
			else
			{				
				// hide all itmes without current
				hideMenuItmes(this);
				contacts_visible = true;	
			}
		}
		else
		{
			// scroll to top of the page
			jQuery('html, body').animate({scrollTop:0}, 'slow');
			
			jQuery(".top-board.contacts").slideDown(500);				
			jQuery("#main-nav a[rel=contacts]").addClass("current");		
			contacts_visible = true;
			// hide all itmes without current
			hideMenuItmes("#main-nav a[rel=contacts]");
		}
		
		return false;
	});
	
	jQuery("a[rel=country-guide]").click(function()
	{			
		// if advanced search bar is visible hide it
		if(advanced_search_visible)
		{
			jQuery(".top-board.advanced-search").slideToggle(500);
			jQuery("#main-nav a[rel=advanced-property-search]").toggleClass("current");
			advanced_search_visible = false;
		}
		if(contacts_visible)
		{
			jQuery(".top-board.contacts").slideToggle(500);
			jQuery("#main-nav a[rel=contacts]").toggleClass("current");
			contacts_visible = false;			
		}	

		// if it is a top menu link then show or hide 
		// contacts board else only show
		if(jQuery(this).hasClass("top"))
		{
			jQuery(".top-board.country-guide").slideToggle(500);				
			jQuery("a[rel=country-guide]").toggleClass("current");		
			
			if(country_guide_visible)
			{				
				country_guide_visible = false;
				// show all itmes without current
				showMenuItems();
			}
			else
			{				
				// hide all itmes without current
				hideMenuItmes(this);
				country_guide_visible = true;	
			}
		}
		else
		{
			// scroll to top of the page
			jQuery('html, body').animate({scrollTop:0}, 'slow');
			
			jQuery(".top-board.ountry-guide").slideDown(500);				
			jQuery("#main-nav a[rel=country-guide]").addClass("current");		
			country_guide_visible = true;
			// hide all itmes without current
			hideMenuItmes("#main-nav a[rel=country-guide]");
		}
		
		return false;
	});
	

		// action for showing tooltips box
		// activated by click on checkbox with class "dropdown"
		jQuery("input.checkbox.dropdown").click(function()
		{
			if(jQuery(this).attr('checked') == true)
			{
				jQuery(this).parent('div').find('.tooltips').fadeIn(300);
			}
			else
			{
				jQuery(this).parent('div').find('.tooltips').find(".checkbox").attr("checked", false);
				jQuery(this).parent("div").find('.tooltips').fadeOut(300);
			}				
		});		 
		
		// action for closing tooltips box
		jQuery("a.tooltips-close").add(".popup-block .close").click(function()
		{
			
			jQuery(this).parent("div").fadeOut(300);
			
			nov_my_account_popup_visible = false;
			nov_language_popup_visible = false;
			nov_send_to_friend_popup_visible = false;
			return false;
		});
		
		// displaing popup blocks 
		jQuery(".details.popup a").click(function()
		{
			jQuery(this).parent().find(".popup-block").fadeIn(400);
		});
		
		
		var nov_language_popup_visible = false; 
		jQuery("a.language").click(function()
		{		
			if(nov_language_popup_visible)
				jQuery(this).parent().find(".popup-block.language").fadeOut(400);
			else
				jQuery(this).parent().find(".popup-block.language").fadeIn(400);
				
			nov_language_popup_visible = nov_language_popup_visible ? false : true;
		});
		
		var nov_my_account_popup_visible = false; 
		jQuery("a.my-account").click(function()
		{	
	
			if(nov_my_account_popup_visible)
				jQuery(this).parent().find(".popup-block.my-account").fadeOut(400);
			else
				jQuery(this).parent().find(".popup-block.my-account").fadeIn(400);
				
			nov_my_account_popup_visible = nov_my_account_popup_visible ? false : true;
			
			return false;
		});
		
		var nov_send_to_friend_popup_visible = false;
		jQuery("a.send-to-friend").click(function()
		{	
			if(nov_send_to_friend_popup_visible)
			{
				jQuery(this).parent().find(".popup-block.send-to-friend").fadeOut(400);
			}
			else
			{
				jQuery(".ajax-send-friend").show();
				jQuery(this).parent().find(".popup-block.send-to-friend").find(".response-block").hide();
				jQuery(this).parent().find(".popup-block.send-to-friend").fadeIn(400);
				
			}	
			nov_send_to_friend_visible = nov_send_to_friend_popup_visible ? false : true;
			
			return false;
		});
		
		
		// function clear text if it is not default text 
		// in search input and text input fields which
		// has class "comment-input" 
		// when input focus in
		jQuery("input[name=s]").add('input.comment-input').focus(function()
		{ 
			var value = jQuery(this).attr('value');
			var text = jQuery(this).attr('rel');
			jQuery(this).removeClass("light-gray");

			if(value == text)	
			{				
				jQuery(this).attr('value','');
			}					
		});
		
		// if searh and input fields is empty when input focus out
		// return default text
		jQuery("input[name=s]").add('input.comment-input').blur(function()
		{ 
			var value = jQuery(this).attr('value');			
			var text = jQuery(this).attr('rel');
			
			if(value == '')
			{
				jQuery(this).attr('value',text);
				jQuery(this).addClass("light-gray");
			}
		});
		
		// adding default text to search blocks
		jQuery("input[name=s]").add('input.comment-input').each(function()
		{
			var value = jQuery(this).attr('value');
			var text  = jQuery(this).attr('rel');			
			if(value == '')
			{
				jQuery(this).attr('value',text);
				jQuery(this).addClass("light-gray");
			}
		});
		
		// clearing seach query if "s" contains
		// default text
		jQuery(".search-form").submit(function()
		{
			var search_text = jQuery(this).find("input[name=s]").attr("value");
			var default_text = jQuery(this).find("input[name=s]").attr("rel");
			
			if(search_text == default_text)
				jQuery(this).find("input[name=s]").attr("value",'');
		});
		
		// Ajax submit forms which has class "ajax-form"
		// it send data to novrealty.ajaxurl
		jQuery('.ajax-form').submit(function()
		{
			// get objects of form, request block
			// and get form data
			var form = this;
			var response_block = jQuery(this).find('.response-block');
			var params = jQuery(this).serialize();
			
			// post form data and display response 
			// in response block
			jQuery.post(novrealty.ajaxurl, params, function(response)
			{
				jQuery(response_block).removeClass('error');
				
				if(response.status == "OK")
				{
					jQuery(response_block).html(response.message).fadeIn(300);
				}
				else
				{
					jQuery(response_block).addClass('error').html(response.message).fadeIn(300);
				}				
			}, 'json');
			
			return false;
		});
		
		jQuery(".ajax-send-friend").submit(function()
		{	
			// get objects of form, request block
			// and get form data
			var form = this;
			var response_block = jQuery(this).parent().find(".response-block");
			var params = jQuery(this).serialize();
			
		//	var form-to-friend = response_block.html();
			waiting = '<img class="loading-icon" src="'+ novrealty.path_to_theme +'/images/ajax-loader-dark.gif" width="16" height="16"/>' + novrealty.sending_email;
			jQuery(form).hide();			
			jQuery(response_block).show();
			response_block.html(waiting);
			// post form data and display response 
			// in response block
			jQuery.post(novrealty.ajaxurl, params, function(response)
			{
			//	jQuery(response_block).removeClass('error');
				if(response.status == "OK")
				{
					response_block.html(response.message);
				}
				else
				{
					response_block.html(response.message);
				}				
			}, 'json');
			
			return false;		
		});
		
		jQuery(".print").click(function() 
		{
			var show = this.getAttribute("rel");
			jQuery('link[rel*=style][title]').each(function(i)
			{
				this.disabled = true;
				if (this.getAttribute('title') == show) this.disabled = false;
			});
		});
		
		
		// jQuery handlers for filter form
		
		// it's for submit
		jQuery(".filter-form").submit(function()
		{
			// get action address and parameters 
			action_path = jQuery(this).attr("action");
			action_params = jQuery(this).find("input[name=search-params]").attr("value"); 	
			
			
			if(action_params != "") action_params = "?" + action_params;
			else
				action_params = "?";
			
			path_add = '';
			// different handlers for news filter 
			// and property filter 			
			if(jQuery(this).hasClass("news"))
			{
				// if select country address will be : ../category/	
				if(jQuery("select[name=category_name]").val() != "")
					path_add += jQuery("select[name=category_name]").val() + "/";	
					
				// if select country address will be : ../all/country/
				if(jQuery("select[name=location]").val() != "all")
					path_add += jQuery("select[name=location]").val() + "/";
			}
			else 
			if(jQuery(this).hasClass("property"))
			{
				if(jQuery(this).find("select[name=pp_price_range]").val() != "all")
				{
					if(action_params != "?") action_params += '&';
					action_params += jQuery(this).find("select[name=pp_price_range]").serialize();
				}
				
				if(jQuery(this).find("select[name=pp_area_range]").val() != "all")
				{
					if(action_params != "?") action_params += '&';
					action_params += jQuery(this).find("select[name=pp_area_range]").serialize();
				}
			}		
			window.location = action_path + path_add + action_params;
			return false;
		});	

	// function adds to property to user favorites
	// by ajax query 
	jQuery(".favorites-action").live("click",function()
	{
		if(novrealty.user_id < 1) return false;
		
		action = jQuery(this).attr("rel");
		params = {
			'action' : 'novrealty_favorites',
			'favorites_action' : action,
			'user_id' : novrealty.user_id,
			'property_id' : novrealty.property_id,
		}			
		
		waitng = '<div class="favorite-icon main loading"><img src="'+ novrealty.path_to_theme +'/images/ajax-loader.gif" width="16" height="16"/></div><span>' + novrealty.one_second  + '</span>';
		if(action == 'add')
			ready = '<div class="favorite-icon">'+
					'<img src="http://novrealty.com/wp-content/themes/novrealty/images/favorites-icon-added.gif" title="' 
					+ novrealty.in_your_favorites + 
					'" alt="' + novrealty.in_your_favorites + '" height="30" width="31"></div>'+
					'<span>' + novrealty.in_your +
					' <a href="http://novrealty.com/my-account/">' + novrealty.favorites + 
					'</a> [<a class="favorites-action" rel="remove" href="#" >' + novrealty.remove + '</a>]</span>';	
					
		else
			ready = '<div class="favorite-icon"><img src="http://novrealty.com/wp-content/themes/novrealty/images/favorites-icon.gif" title="' 
			+ novrealty.add_listing + 
			'" alt="' + novrealty.add_listing + '" height="30" width="31"></div><span>' + 
			novrealty.add_listing_1 + ' <a class="favorites-action" rel="add" href="#" >' + novrealty.favorites + '</a></span>';
	
			
		jQuery(".favorite-block").html(waitng);
		
		jQuery.post(novrealty.ajaxurl, params, function(response){
			
			jQuery(".favorite-block").html(ready);
		});
		
		return false;
	});

	
	
	// function adds to property to user favorites
	// by ajax query
	jQuery(".search-save-action").click(function()
	{
		if(novrealty.user_id < 1) return false;
		
		// get parameter
		search_param = jQuery(this).attr("rel");
		
		// if link has class 'delete'
		// then action is remove
		// else add
		if(jQuery(this).hasClass("delete")) 
		{
			action = 'remove';
			search_id = search_param;
		}
		else
		{
			action = 'add';	
			search_id = '';
		}
		
		// set ajax request parameters
		params = {
			'action' : 'novrealty_saved_search',
			'search_action' : action,
			'user_id' : novrealty.user_id,
			'search' : search_param
		};
		
		jQuery('.search_'+ search_id + " .save-search").html(novrealty.search_waiting);
		jQuery.post(novrealty.ajaxurl, params, function(response){
			
			
			if(action == 'remove')
			{
				jQuery('.search_'+ search_id).html('<div class="deleted">' + novrealty.deleted + '</div>');				
			}
			else
			{
				jQuery(" .save-search").html(novrealty.search_ready);
			}
			
		});	
			
		return false;
	});
});