var start_x = 0;
var start_y = 0;
var drag = false;

$(document).ready(function(){

/* 	$("#tab2 area").each(function(){
		var coords = $(this).attr("coords").split(",");
		var dx = 0;
		var dy = 0;
		var new_coords = new Array();
		$.each( coords, function(i,val){
			if ( (i % 2) == 1){
				new_coords.push( +val + dy );
			} else {
				new_coords.push( +val + dx );
			}
		});
		$(this).attr("coords", new_coords.join(",") );
		
	}); */
	
	/* заполняем информацией всплывающие окна */
	$("#plan area").not('#tab1 area').each(function(){
		var temp = $(this).attr("rel").split("|");
		if (temp[3] == undefined){temp[3] = 1;}
		
		$(this).data({img_x:temp[1],img_y:temp[2],img_width:temp[3],description: $(this).attr("title"), s:temp[0]});	
		$("#popup_windows").append('<div class="popup_window" id="' + $(this).attr("href") +'"><div class="kv_title">' + $(this).attr("alt") + ' - общей площадью ' + $(this).data("s") + ' кв.м.</div><div class="kv_img"><img style="left:' + -$(this).data("img_x") + 'px;top:' + -$(this).data("img_y") + 'px; ' + ( (+$(this).data("img_width") > 1)?'width:' + $(this).data("img_width") + 'px':'') + '" src="img/plan/l' + $(this).parent().attr("name").substr(4) + '.png" alt=""/></div><div class="kv_description">' + $(this).attr("title") + '</div><div class="kv_s">Общая площадь помещения ' + $(this).data("s") + ' кв.м.</div><div class="kv_advance"><br/><a href="img/plan/l' + $(this).parent().attr("name").substr(4) + '.png" target="_blank">Скачать планировку этажа</a><br/><a href="#print" class="print">Распечатать</a></div><a href="#close" class="popup_close"></a><a href="#close" class="popup_close_text">Закрыть</a></div>');
		$(this).attr("title", '');
	});
	
	if ( !$.browser.opera && !$.browser.msie) {	
		$(".popup_window").corner();
		$("#tips").corner();
	}
	
	$(".popup_close, #mask, .popup_close_text").click(function(){
		$(".popup_window:visible").fadeOut("fast");
		$("#mask").fadeOut("normal");
		return false;
	});
	
	//всплывающая подсказка
	//на страницах с планировкой
	$(".tab area").not("#tab1 area").hover(function(obj){
		//$(this).data("description")
		$("#tips").html( $(this).attr("alt") + '. Площадью ' + $(this).data("s") + ' кв.м. ' ).css({left:obj.pageX + 10, top: obj.pageY}).stop().fadeTo("fast",1);
	},function(){
		$("#tips").stop().fadeOut("fast");
	});
	
	//на главной
	$("#tab1 area").hover(function(obj){		
		$("#tips").html( $(this).attr("alt") ).css({left:obj.pageX + 200, top: obj.pageY }).stop().fadeTo("fast",1);
	},function(){
		$("#tips").stop().fadeOut("fast");
	});	
	
	//показываем всплывающее окно
	$(".tab area").not("#tab1 area").click(function(){
		$("#mask").fadeTo("slow",1);
		if ($.browser.msie && +$.browser.version < 7){
			var obj = $("#" + $(this).attr("href") );
			$(obj).css({left:"200px",top:"-850px"}); 
		}
		$("#" + $(this).attr("href") ).fadeTo("slow",1);
		
		return false;
	});
	
	$("#tab1 area, #plan .bread a").click(function(){
		var left = $( $(this).attr("href") ).position().left;
		$("#tabs").animate({left:-left},"normal");
		$("#plan .bread a").removeClass("current");
		$('#plan .bread a[href="' + $(this).attr("href") + '"]').addClass("current");
		
		location.hash = '#' + $(this).attr("href");
		return false;
	});
	
	$('.print').click(function(){
		 window.print();
		 return false;
	});
	
	//если хэш есть то переходим к нужному табу
	if (location.hash.length > 2){
		$('.bread a[href="' + location.hash.replace("##","#") + '"]').click();
	}
	
	if ( !$.browser.msie ){
		var listener = new listenHash(function(){
			$('.bread a[href="' + location.hash.replace("##","#") + '"]').click();
		});
	}
	
/* 	
	// отладка
	$('.kv_img').mousedown(function(e){
		start_x = e.pageX;
		start_y = e.pageY;
		drag = true;
	});
	$('.kv_img').mouseout(function(e) {
		if (!drag){ return false; } else {drag = false;}
		
		var left = (start_x - e.pageX) + +$(this).find("img").position().left;
		var top =  (start_y - e.pageY) + +$(this).find("img").position().top;
		console.clear();
		console.log( left + ' ' + top );
		$(this).find("img").css({left:left, top:top});
	});
  */
  
});

function listenHash( f ){
	this.lastHash = location.hash;
	this.func = f;
	
	var interval = setInterval( function( obj ){
		obj.change();
	}, 500, this );
	
 	this.change = function(){
		if (this.lastHash != location.hash ){
			this.lastHash = location.hash;
			this.func();
		}
	}
}
