var tooltip_html, tooltip_title;

var wisetooltip_html = function(element, data_array){
	var color = "#55A20A";
	if($(element).attr('class')=='sold'){
		color = "#FF0000";
	}
	if($(element).attr('class')=='reserved'){
		color = "#FF9900";
	}
	
	tooltip_html = $('<div id="tooltip" style="background:'+ color +';"><h3></h3><div class="body"></div></div>').appendTo(document.body).hide();
	$('h3', tooltip_html).html(data_array.shift());
	$('.body', tooltip_html).html(data_array.join(' | '));
}

var showWiseTooltip = function(element) {
	
	wisetooltip_html(element, $(element).attr('tab').split('#'));
	
	var image_coords = $('#map').offset();
	tooltip_html.css({top:image_coords.top + 'px', left:image_coords.left + 'px', width:$('#map').width()-14 + 'px'}).show();

}

;(function($) {
	$.wisetooltip = {};
	
	$.fn.extend({
		wisetooltip: function() {
		return this.each(function() {
			jQuery.data(this, 'title_txt', $(this).attr("tab"));
			$(this).removeAttr("title");
			this.alt = "";
			}).mouseover(show).click(hide);
		}
	});
	
	function create_tooltip_html(element){
		if(jQuery.data(element, 'title_txt')) wisetooltip_html(element, jQuery.data(element, 'title_txt').split('#'));
	}
	
	function show() {
		
		if(tooltip_html) {
			hide(this);
		}
		
		create_tooltip_html(this);
		
		var image_coords = $('#map').offset();
		if(tooltip_html) tooltip_html.css({top:image_coords.top + 'px', left:image_coords.left + 'px', width:$('#map').width()-14 + 'px'}).show();
		
	}
	
	function hide(element){
		$(element).attr({tab:jQuery.data(element, 'title_txt')});
		tooltip_html.hide();
		tooltip_html = null;	
	}
	
})(jQuery);


var set_active_house = function(number){
	if(!number) return;
	var coords = $('#'+number).attr('coords').split(','); 
	
	
	var x_coord = 0;
	var y_coord = 0;
	
	for(var i = 0; i < coords.length; i++){
	  
	  if((i % 2) != 0){
	    y_coord += parseInt(coords[i]);
	  }else{
	    x_coord += parseInt(coords[i]);
	  }
	}
	
	x_coord = x_coord / (coords.length/2);
	y_coord = y_coord / (coords.length/2);
	
	
	var image_coords = $('#map').offset();
	
	$('#reo_pointer').css({top:(y_coord + image_coords.top - 20) + 'px', left:(x_coord + image_coords.left + 10) + 'px'}).show();
}	
	
$(function(){
	

	$('.equal').css('height', ($('.equal').height() < 373) ? 373 : $('.equal').height());
	
	if($('#contanct_form form').length){
	  $('.equal_form').css('height', $('.equal_form').height());
	}
	
	$("map#liepu_gatve area, map#ziedu_gatve area").wisetooltip();
		
	$('map#liepu_gatve area, map#ziedu_gatve area').mouseover(function(){
		set_active_house($(this).attr('id'));
	});
	
	if ($('#reo_selected_id').html()) {
		if ($('#reo_selected_id').html().length) {
			set_active_house($('#reo_selected_id').html());
			showWiseTooltip($('#'+$('#reo_selected_id').html()));
		}
	}
	
	$('.sold').click(function(){
		return false;
	}
	);
	
	$('.lightbox').lightBox({fixedNavigation:true});
	
	//png fix
	$(document).pngFix();
	
	var time = 0;
	
	//submenu
	var show_submenu = function(){
		clearTimeout(time);
		$('#house_menu').css({top:$('#house_menu_link').offset().top + 32, left:$('#house_menu_link').offset().left}).show();
	}
	
	
	var hide_submenu = function(){
		time = setTimeout(function(){
			$('#house_menu').hide();
		}, 500);
	
	}
	
	$('#house_menu_link').mouseover(show_submenu).mouseout(hide_submenu);
	$('#house_menu a').mouseover(show_submenu).mouseout(hide_submenu);
	
	//some css fixes
	$('.re_plans td').css({border:0});
	$('.re_plans table td').css({padding:0, margin:0});
	$('.re_plans table table').height(200);
	$('.re_plans a').attr({target:'_blank'});
	
});