var status_submenu = 0;
var menu_out;
var id_to;


jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}


function init(page)
{
	if (page == 'main') {
		$('#slideshow').slideshow({width: 1024, height: 411}).playSlide();
		$('#slideshow_text').slideshow({width: 1024, height: 15}).playSlide();
		$('#slideshow').show();
		$('#slideshow_text').show();
	}
	
	init_menu();
	
	var h = window.innerHeight ? window.innerHeight : (document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.offsetHeight);
	if ($(document).height() == h) {
	  var d =  $(document).height() - $("#footer")[0].offsetTop - getEl('footer').style.height.split('px')[0] - 30;
	  if (d) styleEl('footer').marginTop = d+'px';
	}
}

function init_menu()
{
	$("#menu_1").mouseover(function(){
		if (menu_out != '1') hideAllSubmenu();
		if (status_submenu == 0) {
			$("#submenu")[0].style.left = ($("#menu")[0].offsetLeft + 8) + 'px';
			$("#submenu")[0].style.top = ($("#menu")[0].offsetTop + 18) + 'px';
			$("#submenu").show("blind",{},200);
		}
		status_submenu = 1;
		if (id_to) clearTimeout(id_to);
	});
	$("#menu_1").mouseout(function(){ status_submenu = 2; menu_out = 1; id_to = setTimeout("hideSubmenu('submenu')", 400); });	
	$("#submenu").mouseover(function(){ status_submenu = 1; if (id_to) clearTimeout(id_to); });
	$("#submenu").mouseout(function(){ status_submenu = 2; menu_out = 1; id_to = setTimeout("hideSubmenu('submenu')", 400); });
	
	$("#menu2").mouseover(function(){
		if (menu_out != '2') hideAllSubmenu();
		if (status_submenu == 0) {
			$("#submenu2")[0].style.left = ($("#banner")[0].offsetLeft + $("#menu2")[0].offsetLeft) + 'px';
			$("#submenu2")[0].style.top = ($("#banner")[0].offsetTop + $("#menu2")[0].offsetTop + 30) + 'px';
			$("#submenu2").show("blind",{},200);
		}
		status_submenu = 1;
		if (id_to) clearTimeout(id_to);
	});
	$("#menu2").mouseout(function(){ status_submenu = 2; menu_out = 2; id_to = setTimeout("hideSubmenu('submenu2')", 400); });	
	$("#submenu2").mouseover(function(){ status_submenu = 1; if (id_to) clearTimeout(id_to); });
	$("#submenu2").mouseout(function(){ status_submenu = 2; menu_out = 2; id_to = setTimeout("hideSubmenu('submenu2')", 400); });
	
	$("#menu3").mouseover(function(){
		if (menu_out != '3') hideAllSubmenu();
		if (status_submenu == 0) {
			$("#submenu3")[0].style.left = ($("#banner")[0].offsetLeft + $("#menu3")[0].offsetLeft) + 'px';
			$("#submenu3")[0].style.top = ($("#banner")[0].offsetTop + $("#menu3")[0].offsetTop + 30) + 'px';
			$("#submenu3").show("blind",{},200);
		}
		status_submenu = 1;
		if (id_to) clearTimeout(id_to);
	});
	$("#menu3").mouseout(function(){ status_submenu = 2; menu_out = 3; id_to = setTimeout("hideSubmenu('submenu3')", 400); });	
	$("#submenu3").mouseover(function(){ status_submenu = 1; if (id_to) clearTimeout(id_to); });
	$("#submenu3").mouseout(function(){ status_submenu = 2; menu_out = 3; id_to = setTimeout("hideSubmenu('submenu3')", 400); });
}

function hideSubmenu(id)
{
	if (status_submenu == 2) {
		$("#"+id).fadeOut(100);
		status_submenu = 0;
	}
}

function hideAllSubmenu()
{
	var submenus = ['submenu','submenu2','submenu3'];
	var i = 0;
	for (i in submenus) {
	  $("#"+submenus[i]).fadeOut(1);
	}
	status_submenu = 0;
}

function init_map() {
	document.body.style.overflow='hidden';
	hideEl('page_content');
	tune_map();
	showEl('mapdiv');
	var hold = 0;
	var x_start;
	var y_start;
	var old_map = getEl('bigmap');

	old_map.onmousedown = function(e) {
		hold = 1;
		var evt = e || event;
		x_start = getPositionOnMap(evt).x;
		y_start = getPositionOnMap(evt).y;
		hideEl('controls');
		old_map.style.cursor = "url(/i/grabbing.cur), -moz-grabbing";
		old_map.style.cursor = "url(/i/grabbing.cur), -webkit-grabbing";
	}
	old_map.onmouseup = function() {
		hold = 0;
		showEl('controls');
		old_map.style.cursor = "url(/i/grab.cur), -moz-grab";
		old_map.style.cursor = "url(/i/grab.cur), -webkit-grab";
	}
	old_map.onmousemove = function(e) {
	    if (hold == 1) {	
		var evt = e || event;
		var x_end = getPositionOnMap(evt).x;
		var y_end = getPositionOnMap(evt).y;
	      
		var left = old_map.style.left.split('px')[0]/1 + x_end - x_start;
		var top = old_map.style.top.split('px')[0]/1 + y_end - y_start;
	
		if (left <= 0 && left >= mapWidth) old_map.style.left = left + 'px';
		if (top <= 0 && top >= mapHeight) old_map.style.top = top + 'px';		
	    }
	}	
}

function getPositionOnMap(evt) {
 		
	var ob = getEl('bigmap');
	var left = ob.style.left.split('px')[0]/1 + 2;
	var top = ob.style.top.split('px')[0]/1 + 2;
	
	var pos_x = evt.offsetX ? (event.offsetX - 3) : evt.pageX - 3 - getEl('mapdiv').offsetParent.offsetLeft - left;
	var pos_y = evt.offsetY ? (event.offsetY - 3) : evt.pageY - getEl('mapdiv').offsetParent.offsetTop - top - 35 + getEl('mapdiv').offsetParent.scrollTop;

        return {x: pos_x, y: pos_y};
}

function resize_map() {
	tune_map();
	var old_map = getEl('bigmap');
	old_map.style.top = '-400px';
	old_map.style.left = '-400px';
}

function tune_map() {
	var h = window.innerHeight ? window.innerHeight : (document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.offsetHeight); 
	var w = window.innerWidth ? window.innerWidth : (document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.offsetWidth);
	var st = document.body.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop;
	var sl = document.body.scrollLeft ? document.body.scrollLeft : document.documentElement.scrollLeft;
	
	mapWidth = w - 2146;
	mapHeight = h - 2126;
	
	var old_map = getEl('mapdiv');
	old_map.style.height = h + 'px';
	old_map.style.top = st + 'px';
	old_map.style.width = w + 'px';
	old_map.style.left = sl + 'px';	
}

function hide_map() {
	document.body.style.overflow='auto';
	showEl('page_content');
  	hideEl('mapdiv');
	hide_shadow();
}



function set_image(cid, prefix)
{
	var poster = getEl('poster');
	var link = getEl('poster-link')
	poster.src = '/estates/'+cid+'/'+prefix+'_poster.jpg';

	link.href='javascript:next_image(\''+prefix+'\')';
}

function favourites(id) {
	var str = getCookie('mybest') || '';
	str = unescape(str);
	var m = str.split(',');
	var add = 1;
	for (var i in m) {
	  if (m[i] == id) {
	    showHint({content:'Объект находится в избранном',fade:1, height:'100px'});
	    add = 0;
	    break;
	  }
	}
	if (add) {
	  str = id + (str ? ',' + str : '');
	  setCookie('mybest', str);
	  showHint({content:'Объект добавлен в избранное',fade:1, height:'100px'});
	}
}


function pressButton(name) {  			
	document.data[name].value = 1 - document.data[name].value;
	if (name == 'allnew') document.data.period.value = '';
	document.data.submit();
}

function pressGroupButton(name,type) {
	document.data[type].value = name;
	document.data.submit();
}

function startChangePic(event) {
	var el = document.all ? event.srcElement : event.target;
	if (el.tagName == 'IMG') {
		window.tm = setInterval('changePic(\''+el.id+'\',\''+photos[el.id]+'\')',1500);
	}
}
	
function stopChangePic(event) {
	var el = document.all ? event.srcElement : event.target;
	if (el.tagName == 'IMG') {
		clearInterval(window.tm);
		ind = 0;
	}
}
	

function changePic (id,photos) {
	var p = photos.split(/,/);
	id = getEl(id);
	id.src = id.src.replace(/\w+\.\w+$/, p[ind]);
	ind++;
	if (ind == p.length) ind = 0;
}
