var form='';
var loaded=0;
$(document).ready(function() {
	
	/* photo_line */
	if ($('.photo_line').size() >0 ) {
		photo_line_fill();
	}
	/* /photo_line */

	/* zoomable image */
	$('.trans_zoomable').bind('click', function() {
		show_plan_big('popup_kvart', $(this).attr('rel'), $(this).attr('title'), $('.trans_zoomable').size(), $(this).index());
	});
	$('.inner .gal .gal_item').live('click', function() {
		show_plan_big('popup_kvart', $(this).attr('rel'), $(this).attr('title'), $('.gal_item').size(), $(this).index());
	});
	/* /zoomable image */
	
	/* plan_index */
	if ($('.plan_index').size() > 0) {
		$('.plan_index1').addClass('plan_a');
		plan_index_anim();
	}
	/* /plan_index */
	
	/* gallery */
	if ($('.gal').size() > 0) {
		gal.init();
	}
	/* /gallery */

	/* infra map */
	if ($('.infra_map').size() > 0) {
		$('.infra_lnk').bind('click', function() {
			if (!$(this).hasClass('button2')) {
				infra_switch_tab($(this));
			}
		});
		$('.infra_map .points div[txt]').live('mouseover', function() {
			infra_show_info($(this));
		});
		$('.infra_map .points div[txt]').live('mouseleave', function() {
			infra_hide_info();
		});
	}
	/* /infra map */

	/* inputs */
	if ($('input[type=text]').size() > 0) {
		$('input[type=text]').bind('focus', function() {
			if ($(this).attr('rel')) {
				if($(this).val() == $(this).attr('rel')) {
					$(this).val('');
					$(this).addClass('focused');
				}
			}
		});
		$('input[type=text]').bind('blur', function() {
			if ($(this).attr('rel')) {
				if($(this).val() == '') {
					$(this).val($(this).attr('rel'));
					$(this).removeClass('focused');
				}
				else {
					$(this).addClass('focused');
				}
			}
		});
	}
	/* /inputs */
	
	/* genplan */
	if ($('.genplan').size() > 0) {
		//genplan.init();
		var total = 8;
		var img=new Array();
		for(var i=0; i<total; i++) {
			img[i] = new Image();
			if (i < (total-2)/2) {
				img[i].src='/img/gp_' + i + '.gif';
			}
			else if (i >= (total-2)/2 && i < total-2) {
				img[i].src='/img/gpb_' + (i-(total-2)/2) + '.gif';
			}
			else if (i == total-2) {
				img[i].src='img/genplan_map.png';
			}
			else if (i == total-1) {
				img[i].src='/img/genplan_map_b.png';
			}
			img[i].onload = function(){
				clearTimeout(window.load_timer);
				window.loaded++;
				$('.genplan_loader span').text(Math.floor(window.loaded/total*100));
				if(window.loaded == total) {
					clearTimeout(window.load_timer);
					genplan.init();
					$('.genplan_loader').hide();
					$('.genplan_map').show();
					$('.genplan .zoom').bind('click', function() {
						if ($('.popup_genplan').size() == 0) {
							popup.p_create('popup_genplan');
							$('.popup_genplan .pop_content').append($('.for_genplan_b').html());
							$('.for_genplan_b').remove();
						}
						popup.p_show('popup_genplan');
					});
				};
				window.load_timer = setTimeout(function(){
					genplan.init();
					$('.genplan_loader').hide();
					$('.genplan_map').show();
					$('.genplan .zoom').bind('click', function() {
						if ($('.popup_genplan').size() == 0) {
							popup.p_create('popup_genplan');
							$('.popup_genplan .pop_content').append($('.for_genplan_b').html());
							$('.for_genplan_b').remove();
						}
						popup.p_show('popup_genplan');
					});
				}, 3000);
			}
		}
	}
	/* /genplan */
	
	/* order */
	if ($('.order_lnk').size() > 0) {
		$('.order_lnk a').bind('click', function() { order_show(); return false; });
		$('.popup_bg, .form .form_close').bind('click', function() { order_hide(); });
	}
	/* /order */

});

function photo_line_fill() {
	var img_l = $('.photo_line_l .ph_l_img');
	var img_r = $('.photo_line_r .ph_l_img');
	var max_num = Math.max(img_l.size(), img_r.size());
	
	if (max_num > 0) {
		for (var i=0; i<max_num; i++) {
			var src_l = img_l.eq(i).attr('src');
			if (src_l != '' && typeof(src_l) != 'undefined') {
				$('.photo_line_l').append('<img src="' + src_l + '" />');
			}
			var src_r = img_r.eq(i).attr('src');
			if (src_r != '' && typeof(src_r) != 'undefined') {
				$('.photo_line_r').append('<img src="' + src_r + '" />');
			}
		}
		$('.photo_line_l div').remove();
		$('.photo_line_r div').remove();
	}
}

function plan_index_anim() {
	var img_a = $('.plan_index img.plan_a');
	var img = $('.plan_index img:not(.plan_a)');
	
	img_a.animate({'opacity': 0}, 1500, function() {
		img.css({'zIndex': 10}).addClass('plan_a');
		img_a.css({'zIndex': 1, 'opacity': 1}).removeClass('plan_a');
		plan_index_anim();
	});
}

function embed_flash () {
	var flash_width = $('#flash_width').val();
	var flash_height = $('#flash_height').val();
	var flash_src = $('#flash_src').val();
	var flash_name = 'menu_kvart';
	
	// если флэш включен
	if (GetSwfVer() != -1) {
		var flash_obj = AC_FL_RunContent(
			'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0',
			'width', flash_width,
			'height', flash_height,
			'src', flash_src,
			'quality', 'high',
			'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
			'align', 'middle',
			'play', 'true',
			'loop', 'true',
			'scale', 'showall',
			'devicefont', 'false',
			'id', flash_name,
			'name', flash_name,
			'menu', 'true',
			'allowFullScreen', 'false',
			'allowScriptAccess','sameDomain',
			'movie', flash_src,
			'salign', '',
			'wmode', 'transparent'
		);
		
		$('.menu_kvart .flash').append(flash_obj).css('zIndex', 10);
		
		wait_until_loaded();
	}
}

function wait_until_loaded() {
	var movie = (navigator.appName.indexOf("Microsoft") != -1) ? document.all.menu_kvart : document.menu_kvart;

	if (movie.IsPlaying()) {
		$('.flash .no_flash').hide();
		return;
	}
	else {
		setTimeout(function() { wait_until_loaded(); }, 10);
	}
}

function infra_switch_tab(lnk) {
	$('.infra_lnk').removeClass('button2');
	lnk.addClass('button2');

	$('.infra_map').hide();
	$('.infra_map' + lnk.attr('rel')).show();

	$('.infra_block').hide();
	$('.infra_block' + lnk.attr('rel')).show();
}

function infra_show_info(pnt) {
	infra_hide_info();

	var map = pnt.parents('.infra_map');

	if (typeof(pnt.attr('img')) != 'undefined' && pnt.attr('img') != '') {
		map.find('.info tr').append('<td class="info_img"><img src="' + pnt.attr('img') + '" /></td>');
	}
	if (typeof(pnt.attr('txt')) != 'undefined' && pnt.attr('txt') != '') {
		map.find('.info tr').append('<td class="info_txt">' + pnt.attr('txt') + '</td>');
	}
	pnt.css('zIndex', 5);

	if (map.hasClass('infra_map1')) {
		map.find('.info').css({
			'left': (pnt.position().left >= 235) ? (pnt.position().left - 240) : (pnt.position().left + 29) + 'px',
			'top': pnt.position().top - 13 + 'px'
		});
	}
	else if (map.hasClass('infra_map2')) {
		map.find('.info').css({
			'left': (pnt.position().left <= 309) ? (pnt.position().left + 22) : (pnt.position().left - 250) + 'px',
			'top': (pnt.position().top <= 308) ? (pnt.position().top - 12) : (284) + 'px'
		});
	}
	map.find('.info').show();
}

function infra_hide_info() {
	$('.infra_map .points div[txt]').css('zIndex', 1);

	$('.infra_map .info tr td').remove();
	$('.infra_map .info').hide();
}

var popup = {
	p_create: function(name) {
		$('.body').append('<div class="popup ' + name + '"><div class="pop_sh iePNG"></div><div class="pop_close">закрыть<i class="iePNG"></i></div><div class="pop_content"></div>');
		$('.popup .pop_close, .popup_bg').bind('click', function() { popup.p_hide(); });
	},
	p_destroy: function() {
		//$('.popup_bg').remove();
		$('.popup').remove();
		$('.popup .p_close, .popup_bg').unbind('click');
	},
	p_clear: function(name) {
		$('.' + name + ' .pop_content').html('');
	},
	p_show: function(name) {
		$('.popup_bg').height($(document).height()).show();
		$('.' + name).css({
			'top': ($(document).height()/2 - $('.popup').height()/2)  + 'px',
			'left': ($(window).width() - $('.popup').width())/2 + 'px'
		}).show();
		$(document).scrollTop(($(document).height()/2 - $('.popup').height()/2-50));
	},
	p_hide: function() {
		$('.popup_bg').hide();
		$('.popup').hide();
	}
};

function show_plan_big(name, img, title, count, index) {
	var image = document.createElement('img');
	image.src = img;
	if (image.width != 0) {
		img_on_load(name, img, title, count, index);
	}
	else {
		//$('<img />').attr('src', img).load(function() {
		$(image).load(function() {
			img_on_load(name, img, title, count, index);
		});
	}
}

function img_on_load(name, img, title, count, index) {
	if ($('.' + name).size() == 0) {
		popup.p_create(name);
		popup.p_clear(name);
	}
	
	$('.' + name + ' .pop_content').html('<table cellpadding="0" cellcpacing="0" border="0" style="background: #fff;"><tr><td><img src="' + img + '" /></td></tr></table>');
	if (typeof(title) != 'undefined' && title != '') {
		$('.' + name + ' .pop_content table').prepend('<tr><td style="font-family: Georgia, \'Times New Roman\', Times, serif; font-size: 30px;line-height: 32px;color: #8E253D;text-align: center;">' + title + '</td></tr>');
	}
	if(count>=1 && $('.' + name + ' .gal_prev').size() == 0){
		$('.' + name).prepend('<div class="gal_prev iePNG"></div><div class="gal_next iePNG"></div>');
	}
	popup.p_show(name);
	var width=$('.' + name + ' .pop_content img').width();
	var height=$('.' + name + ' .pop_content img').height();
	if (typeof(title) != 'undefined' && title != '') {
		height=height+32;
	}
	if($('.' + name + ' .pop_content img').height()>530&&typeof(title) != 'undefined' && title != ''){
		$('.' + name + ' .pop_content img').height(530);
	}
	$('.' + name + ' .pop_content table').width(width);
	$('.' + name + ' .pop_content table').height(height);
	$('.' + name + ' .pop_content').width(width);
	$('.' + name + ' .pop_content').height(height);
	$('.' + name + ' .pop_sh').width(width+45);
	$('.' + name + ' .pop_sh').height(height+45);
	$('.' + name + ' .pop_sh').css('background-size', (width+45)+'px '+(height+45)+'px');
	$('.' + name + '').width(width);
	$('.' + name + '').height(height);
	$('.popup .gal_prev, .popup .gal_next').css('top', height/2);
	$('.popup .gal_next').css('left', width+45);
	popup.p_hide(name);
	popup.p_show(name);
	
	var prev=index-1;
	var next=index+1;
	if(index==0){
		prev=count-1;
	}
	if(index==(count-1)){
		next=0;
	}
	
	$('.' + name + ' .gal_prev').unbind('click');
	$('.' + name + ' .gal_next').unbind('click');
	
	$('.' + name + ' .gal_prev').click(function(){
		$('.' + name + ' .pop_content').html('<table cellpadding="0" cellcpacing="0" border="0" style="background: #fff;"><tr><td valign="middle" align="center"><img src="/img/ajax-loader.gif" /></td></tr></table>');
		if($('.gal_item').size()>0){
			//$.get($('.gal_item').eq(prev).attr('rel'), function(){
				//popup.p_destroy();
				show_plan_big('popup_kvart', $('.gal_item').eq(prev).attr('rel'), $('.gal_item').eq(prev).attr('title'), $('.gal_item').size(), prev);
			//});
		}
		else{
			//$.get($('.trans_zoomable').eq(prev).attr('rel'), function(){
				//popup.p_destroy();
				show_plan_big('popup_kvart', $('.trans_zoomable').eq(prev).attr('rel'), $('.trans_zoomable').eq(prev).attr('title'), $('.trans_zoomable').size(), prev);
			//});
		}
	});
	
	$('.' + name + ' .gal_next').click(function(){
		$('.' + name + ' .pop_content').html('<table cellpadding="0" cellcpacing="0" border="0" style="background: #fff;"><tr><td valign="middle" align="center"><img src="/img/ajax-loader.gif" /></td></tr></table>');
		if($('.gal_item').size()>0){
			//$.get($('.gal_item').eq(next).attr('rel'), function(){
				//popup.p_destroy();
				show_plan_big('popup_kvart', $('.gal_item').eq(next).attr('rel'), $('.gal_item').eq(next).attr('title'), $('.gal_item').size(), next);
			//});
		}
		else{
			//$.get($('.trans_zoomable').eq(next).attr('rel'), function(){
				//popup.p_destroy();
				show_plan_big('popup_kvart', $('.trans_zoomable').eq(next).attr('rel'), $('.trans_zoomable').eq(next).attr('title'), $('.trans_zoomable').size(), next);
			//});
		}
	});
}

var gal = {
	num: 0,
	block: '',
	is_anim: false,
	timer: '',
	speed: 500,
	init: function() {
		gal.block = $('.gal');
		gal.num = gal.block.find('.gal_item').size();
		gal.block.find('.gal_line').width(gal.block.find('.gal_item').eq(0).outerWidth() * gal.num);

		if(gal.num>3){
			gal.block.find('.gal_prev').bind('click', function() { gal.item_prev(); });
			gal.block.find('.gal_next').bind('click', function() { gal.item_next(); });
		}
		else{
			gal.block.find('.gal_prev').remove();
			gal.block.find('.gal_next').remove();
		}
	},
	item_prev: function() {
		if (!gal.is_anim) {
			gal.is_anim = true;

			var item_last = gal.block.find('.gal_item').eq(gal.num - 1);
			item_last.remove();
			gal.block.find('.gal_line').prepend(item_last);
			gal.block.find('.gal_line').css({'left': - gal.block.find('.gal_item').eq(0).outerWidth() + 'px'});

			gal.block.find('.gal_line').animate({'left': 0}, gal.speed, function() {
				gal.is_anim = false;
			});
		}
	},
	item_next: function() {
		if (!gal.is_anim) {
			gal.is_anim = true;

			gal.block.find('.gal_line').animate({'left': - gal.block.find('.gal_item').eq(0).outerWidth() + 'px'}, gal.speed, function() {
				gal.is_anim = false;
				var item_first = gal.block.find('.gal_item').eq(0);
				item_first.remove();
				gal.block.find('.gal_line').append(item_first);
				gal.block.find('.gal_line').css({'left': 0});
			});
		}
	}
};
function infra_switch_tab(lnk) {
	$('.infra_lnk').removeClass('button2');
	lnk.addClass('button2');
	
	$('.infra_map').hide();
	$('.infra_map' + lnk.attr('rel')).show();

	$('.infra_block').hide();
	$('.infra_block' + lnk.attr('rel')).show();
}

function infra_show_info(pnt) {
	infra_hide_info();
	
	var map = pnt.parents('.infra_map');
	
	if (typeof(pnt.attr('img')) != 'undefined' && pnt.attr('img') != '') {
		map.find('.info tr').append('<td class="info_img"><img src="' + pnt.attr('img') + '" /></td>');
	}
	if (typeof(pnt.attr('txt')) != 'undefined' && pnt.attr('txt') != '') {
		map.find('.info tr').append('<td class="info_txt">' + pnt.attr('txt') + '</td>');
	}
	pnt.css('zIndex', 5);
	
	if (map.hasClass('infra_map1')) {
		map.find('.info').css({
			'left': (pnt.position().left >= 235) ? (pnt.position().left - 240) : (pnt.position().left + 29) + 'px',
			'top': pnt.position().top - 13 + 'px'
		});
	}
	else if (map.hasClass('infra_map2')) {
		map.find('.info').css({
			'left': (pnt.position().left <= 309) ? (pnt.position().left + 22) : (pnt.position().left - 250) + 'px',
			'top': (pnt.position().top <= 308) ? (pnt.position().top - 12) : (284) + 'px'
		});
	}
	map.find('.info').show();
}

function infra_hide_info() {
	$('.infra_map .points div[txt]').css('zIndex', 1);

	$('.infra_map .info tr td').remove();
	$('.infra_map .info').hide();
}

var genplan = {
	block: '',
	info_data: '',
	init: function() {
		genplan.info_data = info_arr;
		genplan.info_data[0]=[];
		var num = 0;
		var status = '';
		for(var i in genplan.info_data) {
			num=i;
			status = genplan.info_data[i].status;
			$('.genplan').each(function() {
				if ($(this).parents('.for_genplan_b').size() > 0) {
					$(this).find('.genplan_map').append('<div class="point p' + num + '" pid="' + i + '" status="' + status + '"><img src="/img/gpb_' + status + '.gif" /></div>');
				}
				else {
					$(this).find('.genplan_map').append('<div class="point p' + num + '" pid="' + i + '" status="' + status + '"><img src="/img/gp_' + status + '.gif" /></div>');
				}
			});
		}
		
		$('.genplan map area').live('mouseover', function() {
			genplan.show_info($(this));
		});
		$('.genplan map area').live('mouseleave', function() {
			genplan.hide_info();
		});
		$('.genplan map area').live('click', function() {
			if(genplan.info_data[$(this).attr('rel')].src)
				location.href=genplan.info_data[$(this).attr('rel')].src;
			return false;
		});

		$('.genplan input[type=checkbox]').each(function() {
			genplan.toggle_houses($(this));
		});

		$('.genplan input[type=checkbox]').live('click', function() {
			genplan.toggle_houses($(this));
		});

	},
	hide_info: function() {
		$('.genplan .info').html('').hide();
	},
	show_info: function(area) {
		var block = area.parents('.genplan');
		if(area.attr('rel') != 0){
			var data = genplan.info_data[block.find('.p' + area.attr('rel')).attr('pid')];
			block.find('.info').append('<div class="info_title">Участок: <span>№' + data.num + '</span></div><table cellpadding="0" cellspacing="0" width="100%"><tr><td>' + ((data.img != '' && typeof(data.img)!='undefined')?('<img src="' + data.img + '" />'):('<img src="/img/logo-mw-sm.jpg" />')) + '</td><td></td></tr></table>');
			
			var td_txt = block.find('.info table td').eq(1);
			var units = '';
			var status = '';
			for(var i in data) {
				if (i != 'num' && i != 'status' && i != 'img' && i != 'src') {
					if (i.match('Площадь')) {
						units = 'м<sup>2</sup>';
					}
					td_txt.append(i + ': <span>' + data[i] + ((units != '')?('&nbsp;' + units):('')) + '</span><br />');
				}
				if (i == 'src'&&data[i]!=='undefined') {
					block.find('.info *').click(function(){if(data[i]) location.href=data[i];});
				}
				if (i == 'status') {
				
					switch (data[i]) {
						case '0':
							status = 'Свободен';
							break;
						case '1':
							status = 'Продан';
							break;
						case '2':
							status = 'Забронирован';
							break;
						defualt:
							status = 'Свободен';
							break;
					}
					td_txt.append('Статус: <span>' + status + '</span>');
				}
			}
		}
		else{
			block.find('.info').append('<div class="info_title" align="center">Офис продаж</span></div><table cellpadding="0" cellspacing="0" width="100%"><tr><td align="center"><img src="/img/logo-mw-sm.jpg" style="margin: 0 90px;"></td></tr></table>');
			
		}
		var pnt = block.find('.p' + area.attr('rel'));
		var pnt_left = pnt.position().left;
		var pnt_top = pnt.position().top;
		var left = Math.max(pnt_left - 170, 0);
		var top = pnt_top - 175;
		block.find('.info').css({'left': left + 'px', 'top': top + 'px'});
		block.find('.info').show();
	},
	toggle_houses: function(check) {
		var status = check.attr('rel');
		var block = check.parents('.genplan');
		var pnt = block.find('.point[status=' + status + ']');
		if (check.attr('checked')) {
			pnt.find('img').show();
		}
		else {
			pnt.find('img').hide();
		}
	}
};

function order_show() {
	window.form=$('.form_content').html();
	$('.popup_bg').height($(document).height()).show();
	$('.form').css({
		'top': $(window).height()/2 - 263 + $(document).scrollTop() + 'px'
	}).show();
	$('#error').html('');
}
function order_hide() {
	$('.popup_bg').hide();
	$('.form_content').html(window.form);
	$('.form').hide();
}

function show_form(content){
	window.form=$('.form_content').html();
	$('.form_content').html('<form><table><tr><td width="625" height="450" align="center"><h2>'+content+'</h2></td></tr></table></form>');
	$('.popup_bg').height($(document).height()).show();
	$('.form').css({
		'top': $(window).height()/2 - 263 + $(document).scrollTop() + 'px'
	}).show();
}

function swicth_quart(num){
	switch(num){
		case 1:
			document.location.href="/quarters/sunny.html";
		break;
		case 2:
			document.location.href="/quarters/rainbow.html";
		break;
		case 3:
			document.location.href="/quarters/park.html";
		break;
		case 4:
			document.location.href="/quarters/floral.html";
		break;
		case 5:
			document.location.href="/quarters/forest.html";
		break;
	}
}
