/**
* Show a preview of an image when hovering a small photo
* parent: div#content_result_container / table.tbl_houses / div.house_block / div.foto_container
**/
function imagePreview(){
	// these 2 variable determine popup's distance from the cursor
	var xOffset = 250;
	var yOffset = 30;

	$("a.preview").hover(function(e){
		this.t = this.title;
		this.title = "";
		var c = (this.t != "") ? "<br/>" + this.t : "";
		$("body").append("<p id='preview'><img src='"+ this.t +"' width='300' alt='Image preview' /></p>");
		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");
    },
	function(){
		this.title = this.t;
		$("#preview").remove();
    });
	$("a.preview").mousemove(function(e){
		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});
};
/*
* Empty Field on focus
* Only if equal to
**/
function clearFieldOnFocus( standSearcKey ){
	if( $('#key').val() == standSearcKey){
		$( '#key' ).css('color', '#999').css('font-size','11px').css('padding-top','3px'); // set the color of the font to a light grey
		$( '#key' ).focus( function() {
			$( '#key' ).val("").css('color', '#000').css('font-size','14px');
			$( '#pkey' ).val("1");
		} ); // lets empty the username field when focus
	}
}
/**
 *
 * @access public
 * @return void
 **/
function housTextHoverChangeBackground(){
	$("div.text_container").hover(function(){
		$("div.house_block").css("background", "someimage.gif"); },
		function(){});
}
/**
 * Subcontent Tab menu Display content handler
 *
 * @access public
 * @return void
 **/
function initTabs() {
	$('ul#subTabNav a').bind('click',function(e) {
		e.preventDefault();
		var thref = $(this).attr("href").replace(/#/, '');
		var colorCode = $(this).parent().attr("title");

		$(this).parent().parent().children('.selected').removeClass('selected');
		$(this).parent().addClass('selected');
		$('div#submenu_close').css("background", "#"+colorCode);

		$('#subcontent div.tcontent').removeClass('active');
		$('#'+thref).addClass('active');
	});
}
/**
 * Main Tab menu HOVER handler
 *
 * @access public
 * @return void
 **/
function hoverMainTabs( selectedClass ){

	var colorOutCode = $('li.'+selectedClass).attr("title");
	$('div#menu_close').css("background", "#"+colorOutCode);

	$('ul#tabNav a').hover(

		function() {
			var colorCode = $(this).parent().attr("title");
			$(this).parent().parent().children('.selected').removeClass('selected');
			$(this).parent().addClass('selected');
			$('div#menu_close').css("background", "#"+colorCode);

		return false;
		},
		function() {
			$(this).parent().parent().children('.selected').removeClass('selected');
			$('li.'+selectedClass).addClass('selected');
			$('div#menu_close').css("background", "#"+colorOutCode);

		return false;
		}
	);
}
/**
 * Subcontent Tab menu HOVER handler
 *
 * @access public
 * @return void
 **/
function hoverTabs(){

	$('ul#subTabNav a').hover(

		function() {
			var colorCode = $(this).parent().attr("title");

			$(this).parent().parent().children('.selected').removeClass('selected');
			$(this).parent().addClass('selected');
			$('div#submenu_close').css("background", "#"+colorCode);

			return false;
		},

		// DEPREC
		// TODO, SEE IF WE NEED THIS, OFF STATE, AT ALL?!
		function() {
			var colorOutCode = $('li.<?=$subSelLiClass?>').attr("title");

			$(this).parent().parent().children('.selected').removeClass('selected');
			$('li.<?=$subSelLiClass?>').addClass('selected');
			$('div#submenu_close').css("background", "#"+colorOutCode);

			return false;
		}
	);
}
/**
 * Load price ranges acording to user selected valuate type
 *
 * @access public
 * @return void
 **/
function loadPriceRanges(){
	// change select option on change
	$("select#dopdwn_valuta").change( function() {

		var sv = $("select#dopdwn_valuta").val(); //selected value
		var load_url = 'http://www.bonbiba.com/index.php/loader/load_price_options/';

		$("select#dopdwn_minimum").load(load_url, {ajax: true, vlta: sv, inverse:false }, function(){});
		$("select#dopdwn_maximum").load(load_url, {ajax: true, vlta: sv, inverse:true}, function(){});

	});
}
/**
 * Check min and max value, max value can not be lower than the minumum value
 *
 * @access public
 * @return void
 **/
function priceRangesCheck(){

	$("select#dopdwn_maximum").change( function() {

		var min = $("select#dopdwn_minimum").val(); //selected minimal value
		var max = $("select#dopdwn_maximum").val(); //selected maximum value

		if(max <= min){
			$("select#dopdwn_maximum").css({background: "red"});
		}else{
			$("select#dopdwn_maximum").css({background: "white"});
		}

	});
}
/**
 *
 *
 * @access public
 * @return void
 **/
function loadDetailObject( loadUrl, offset, offsetItem ){

	var base_url	= 'http://www.bonbiba.com/';
	// we want to know what values where posted and we have to send by ajax
	var offset  	= offset;
	var act     	= $('#act').attr('value');
	var pkey    	= $('#pkey').attr('value');
	var country 	= $('#lname').attr('value');
	var rentorsale 	= $("input[@name='rentorsale']:checked").val();
	var minimum 	= $('#dopdwn_minimum').attr('value');
	var maximum 	= $('#dopdwn_maximum').attr('value');
	var keyword 	= $('#key').attr('value');

	$('#offset').val( offset );
	$.ajax({
		type: "POST",
		url: base_url + "index.php/search/detail_pagination/",
		data: 'offset='+offsetItem+'&act='+act+'&pkey='+pkey+'&country='+country+'&rentorsale='+rentorsale+'&minimum='+minimum+'&maximum='+maximum+'&keyword='+keyword,
		beforeSend: function(){
			// need to find a clever and beautifull way to absolute center
			$('#object_content_container').html('<p align="center" style="width:726px; height:347px;"><img src="' + base_url + '/media/images/ajax-loader.gif" width="32" height="32" alt="" border="0" style="margin-top:150px;" /></p>');
		},
		success: function(html){

			$("#object_content_container").load( loadUrl+' #detail_large',{offset: offset}).show('slow', function(){
				$('#pagination').attr('innerHTML',html).show('slow');
			});
		}
	});
}
/**
 *
 *
 * @access public
 * @return void
 **/
function returnToList( offset ){

	// we want to know what values where posted and we have to send by ajax
	var offset  	= offset;
	var newOffset  	= Math.ceil(offset / 9)*9;
	var act     	= $('#act').attr('value');
	var pkey    	= $('#pkey').attr('value');
	var country 	= $('#lname').attr('value');
	var rentorsale 	= $("input[@name='rentorsale']:checked").val();
	var minimum 	= $('#dopdwn_minimum').attr('value');
	var maximum 	= $('#dopdwn_maximum').attr('value');
	var keyword 	= $('#key').attr('value');

	$.ajax({
		type: "POST",
		url: "http://localhost/www.bonbiba.com/index.php/search/ajax/" + act + "/",
		data: 'offset='+newOffset+'&act='+act+'&pkey='+pkey+'&country='+country+'&rentorsale='+rentorsale+'&minimum='+minimum+'&maximum='+maximum+'&keyword='+keyword,
		success: function(html){
			$('#content_result_container').attr('innerHTML',html);
			imagePreview();
		}
	});
}
/**
 *
 *
 * @access public
 * @return void
 **/
function loadFrontDetailObject( loadUrl, offset ){

	// we want to know what values where posted and we have to send by ajax
	var base_url	= 'http://www.bonbiba.com/';
	var offset  	= offset;
	var act     	= $('#act').attr('value');

	if(act == 'residential'){
		var actReturn = 'index';
	}else{
		var actReturn = act;
	}

	// we have to make sure when the back button is used we return to the page we cam from
	// The back-button on the detail page makes use of the form on the right hand so we have to set the action atribute of the form tag to the richt page
	$("#searchForm").attr( "action", base_url + "index.php/real_estate/" + actReturn );

	$.ajax({
		type: "POST",
		url: base_url + "index.php/real_estate/detail_pagination/" + act + "/" + offset + "/",
		data: 'offset=' + offset + '&act=' + act,
		beforeSend: function(){
			// need to find a clever and beautifull way to absolute center
			$("#object_content_container").hide('fast', function(){
				$('#cosm_image').html('<p align="center" style="width:726px;"><img src="' + base_url + 'media/images/ajax-loader.gif" width="32" height="32" alt="" border="0" style="margin-top:150px; /></p>');
			});
		},
		success: function(html){

			$("#object_content_container").load( loadUrl+' #detail_large',{offset: offset}).show('slow', function(){
				$('#cosm_image').hide('fast'); //.css('display', 'none');
				$('#pagination').attr('innerHTML',html).show('slow');
				$('#fmenu').show('slow'); //css('display', 'block');
			});
		}
	});
}