var search_all_deftext_pl = 'Wyszukaj w serwisie';
var search_all_deftext_en = 'Search the site';
var fpopup_timer = null;

$().ready(function () {
	$('#menu #info').corner("6px");

	if ($('#bg').size() > 0)
	{
		$('#bg').media({
			width: '100%',
			height: 421,
//			src: cp + 'frontend/f/polnord' + (jlang && jlang != 22 ? '-en-zyczenia' : '-zyczenia') + '.swf',
			src: cp + 'frontend/f/polnord' + (jlang && jlang != 22 ? '-en' : '') + '.swf',
			params: {
				quality: 'high',
				// bgcolor: '#ffffff',
				menu: 'false',
				wmode: 'transparent',
				scale: 'noscale'
			},
			flashversion: '8'
		});
	}
	
	if ($('div.headerimg').size() > 0)
	{
		var flf = null;
		if ($('div.header').filter('.h0').size() == 1)
			flf = 'h0';
		else if ($('div.header').filter('.h1').size() == 1)
			flf = 'h1';
		else if ($('div.header').filter('.h2').size() == 1)
			flf = 'h2';
		else if ($('div.header').filter('.h3').size() == 1)
			flf = 'h3';
		else if ($('div.header').filter('.h4').size() == 1)
			flf = 'h4';
		else if ($('div.header').filter('.h5').size() == 1)
			flf = 'h5';
		
		if (flf !== null)
		{
			$('div.headerimg').media({
				width: '100%',
				height: 185,
				src: cp + 'frontend/f/header_' + flf + '.swf',
				params: {
					quality: 'high',
					bgcolor: '#ffffff',
					menu: 'false',
					wmode: 'transparent',
					scale: 'noscale'
				},
				flashversion: '8'
			});
		}
	}
	
	if ($('a[rel=lightbox]').size() > 0)
		$('a[rel=lightbox]').lightBox();
	
	if ($('a[rel=qtpano], a[rel=qtpano1]').size() > 0)
		init_qtpano();
	
	if ($('div.ajaxload').size() > 0)
		$('div.ajaxload').ajaxStart(function () { $(this).fadeIn(); }).ajaxStop(function () { $(this).fadeOut(); });
	
	if ($('#apartment').size() > 0)
		$('#apartment').find('tr:gt(0)').each(function () { $(this).hover(function () { $(this).addClass('hv'); }, function () { $(this).removeClass('hv'); }); var href = $(this).find('td:first a').attr('href'); $(this).find('td:first').text($(this).find('td:first a').text()); $(this).find('td:not(:last)').addClass('clik').attr('title', 'Zobacz szczegółowe informacje o lokalu').click(function () { location.href = cp + href; }); });
	
	if ($('#flashbnrc').size() > 0 && typeof fbvars != 'undefined')
	{
		$('#flashbnrc').media({
			width: fbvars.width,
			height: fbvars.height,
			src: cp + 'frontend/f/' + fbvars.name + '.swf',
			params: {
				quality: 'high',
				bgcolor: '#ffffff',
				menu: 'false',
				wmode: 'transparent',
				scale: 'noscale'
			},
			flashversion: '8'
		});
	}
	
	if ($('#search-all-div').size() > 0)
		searchAllInit(jlang);
	
	if ($('#fpopup').size() > 0)
		showFPopup();
	
	if ($('div.biurosp').size() > 0)
		prepareBSp();

	/*
	 * Obsługa box'a sprzedaz mieszkan
	 */
	$('.redBox').find('h2').click(function () {
		$('.redBox').find('div').slideUp('50').parent('li').filter('.active').removeClass('active');
		if ($(this).next('div').is(':hidden')) {
			$(this).next('div').slideDown('50').parent('li').addClass('active');
		} else {
			$(this).next('div').slideUp('50');
		}
	});
});

var loadMap = function (lang, invmap, temp)
{
	$('#mapa').media({
		width: 650,
		height: 600,
		src: cp + 'frontend/f/mapa' + (invmap == true ? 'i' : '') + '-' + lang + '.swf?t=' + temp,
		params: {
			quality: 'high',
			bgcolor: '#ffffff',
			menu: 'false',
			wmode: 'transparent',
			scale: 'noscale'
		},
		flashversion: '8'
	});
};

var loadMapName = function (w, h, mapname, temp)
{
	if (w == 'auto')
		w = 650;
	$('#mapa').media({
			width: w,
			height: h,
			src: cp + 'frontend/f/mapam.swf?t=' + temp,
			flashvars: {
				'map': cp + 'frontend/f/mapa-' + mapname + '.gif',
				'xml': cp + 'frontend/f/x/points_' + mapname + '.xml'
			},
			params: {
				quality: 'high',
				bgcolor: '#ffffff',
				menu: 'false',
				wmode: 'transparent',
				scale: 'noscale'
			},
			flashversion: '8'
	});
};

var loadMap2Name = function (w, h, mapname, temp)
{
	if (w == 'auto')
		w = 680;
	$('#mapa').media({
			width: w,
			height: h,
			src: cp + 'frontend/f/mapa2-' + mapname + '.swf?t=' + temp,
			params: {
				quality: 'high',
				bgcolor: '#ffffff',
				menu: 'false',
				wmode: 'transparent',
				scale: 'noscale'
			},
			flashversion: '8'
	});
};

var searchSimple = function ()
{
	$('#searchadv').animate({ 'height': 'toggle' }, 'normal');
	$('#searchsimple').animate({ 'height': 'toggle' }, 'normal');
	document.getElementById('search_mode').value = 0;
	setStatus(0);
	return false;
};

var searchAdv = function ()
{
	$('#searchsimple').animate({ 'height': 'toggle' }, 'normal');
	$('#searchadv').animate({ 'height': 'toggle' }, 'normal');
	document.getElementById('search_mode').value = 1;
	setStatus(1);
	return false;
};

var setStatus = function(status){
    $.post(cp + 'wyszukiwarka/setMode/', { 'mode': status}, function (ret) {
    	;
    });
    return false;;
};

var setQueryString = function (){
	var city_s = $('#city_s').val();
	var rooms_s = $('#rooms_s').val();
	
	var city = $('#city').val();
	var district = $('#district').val();
    var rooms = $('#rooms').val();
    var floor = $('#floor').val();
    var pricemin = $('#pricemin').val();
    var pricemax = $('#pricemax').val();
    var surfacemin = $('#surfacemin').val();
    var surfacemax = $('#surfacemax').val();
    var realization = $('#realization').val();
	var inv_type = $('#inv_type').val();
    
    $.post(cp + 'wyszukiwarka/setParams/', { 'city_s':city_s,
                                                                           'rooms_s':rooms_s,
                                                                           'city':city,
                                                                           'district':district,
                                                                           'rooms':rooms,
                                                                           'floor':floor,
                                                                           'pricemin':pricemin,
                                                                           'pricemax':pricemax,
                                                                           'surfacemin':surfacemin,
                                                                           'surfacemax':surfacemax,
                                                                           'realization':realization,
																		   'inv_type':inv_type}, function (ret) {
        if(ret=='OK'){
        	document.location.href = cp + 'wyszukiwarka/';
        };
    });
    return false;
    
};

var searchAll = function()
{
	alert($('#search-form').html());
	// $('#search-form')[0].submit();
	// return false;
};

var loadJs = function (url)
{
	var s = document.createElement('script');
	s.src = url;
	document.getElementsByTagName('head')[0].appendChild(s);
};

var readMore = function (el, id, lang)
{
	if(lang == '22')
		$(el).html('wczytywanie...');
	else
		$(el).html('reading...');
		
	$.get(cp + 'sprzedaz/pelnyOpis/' + lang + '/' + id, function (ret) {
		if (ret == '-ERR')
		{
			if(lang == '22') {
				alert('Podana inwestycja nie istnieje.');
				$(el).html('więcej...');
			} else {
				alert('Selected project does not exist.');
				$(el).html('more...');
			}
		}
		else
		{
			$(el).remove();
			$('#propdesc-d').append('<div id="propfulldesc" style="display: none; margin-top: 10px"></div>');
			$('#propfulldesc').html(ret).animate({ opacity: 'toggle', height: 'toggle' }, 'normal');
		}
	});
	
	return false;
};

var scroll_top = function ()
{
	window.scrollTo(0, 0);
	return false;
};

var start_print = function (el)
{
	pwnd = window.open(el.href, 'POLNORD_PrintWnd', 'width=10,height=10,innerwidth=10,innerheight=10,menubar=no,location=no,resizable=yes,scrollbars=no');
	return false;
};


var updatePropertyList = function (){
	//$('#searchadv').fadeTo('fast',0.1);
//	$('#searchadv').fadeTo('fast',0.4);
	$('select,input').attr('disabled',true);
	$('div.slabel').css('color','#cbcbcb'); 
	$('#sz').hide();
    $("#loader").show();
	$.post(cp + 'wyszukiwarka/getUPL/', { 'address_city_url':$('#city').val()}, function (ret) {
        $('#district').html(ret);
		$('#district').val('-1');
		$('#searchadv').fadeTo('fast',1);
		$('select,input').attr('disabled',false);
		$('div.slabel').css('color','#000000');
		$('#sz').show();
        $("#loader").hide();
    });
};

var searchAllInit = function (lang)
{
	$('#search-all').parents('form').submit(function () { if ($('#search-all').val() == (lang=='22' ? search_all_deftext_pl : search_all_deftext_en) || $('#search-all').val() == '') { $('#search-all').val(''); return true; } else return true; });
	$('#search-all').focus(function () { if ($(this).val() == (lang=='22' ? search_all_deftext_pl : search_all_deftext_en)) { $(this).val(''); } }).blur(function () { if ($(this).val() == '') { $(this).val((lang=='22' ? search_all_deftext_pl : search_all_deftext_en)); } }).autocomplete(search_data);
};

var showFPopup = function ()
{
	/*
	$('#fpopup').media({
			width: 450,
			height: 250,
			src: cp + 'frontend/f/fpop.swf',
			params: {
				quality: 'high',
				menu: 'false',
				wmode: 'transparent',
				scale: 'noscale'
			},
			flashversion: '8'
	});
	*/
	$('#fpopup').fadeIn('normal', function () {
		fpopup_timer = setTimeout(function () { $('#fpopup').fadeOut('normal', function () { $('#fpopup').remove(); }); }, 25000);
	});
};

var closepop = function ()
{
	$('#fpopup').remove();
};

var closeFPopup = function ()
{
	clearTimeout(fpopup_timer);
	$('#fpopup').fadeOut('normal', function () { $('#fpopup').remove(); });
	return false;
};

var doFullSearch = function ()
{
	var sstring = $("#search-all").val();
	//alert(sstring);
	$("#search-form").submit();
	return false;
};

var prepareBSp = function ()
{
	$('div.biurosp').find('h4').each(function () {
		var parent = $(this).parents('div.biurosp:eq(0)');
		$(this).click(function () {
			$('div.biurosp').find('div.biurospc:visible').animate({ height: 'hide', opacity: 0 }, 'normal', function () { });
			$(parent).find('div.biurospc:not(:visible)').animate({ height: 'show', opacity: 1 }, 'normal', function () { if ($.browser.msie) { $(this)[0].runtimeStyle.zoom = 1; } });
		});
	});
};

var init_qtpano = function ()
{
	$('a[rel=qtpano]').click(function () {
		var _wn_width = 600;
		var _wn_height = 515;
		var _wn_left = (window.screen.width / 2) - (_wn_width / 2);
		var _wn_top = (window.screen.height / 2) - (_wn_height / 2);
		var qtpano = window.open(cp + 'prezentacja?f=' + $(this).attr('href'), 'qtpano', 'menubar=no, location=no, resizable=no, scrollbars=no, status=yes, left=' + _wn_left + ', top=' + _wn_top + ', width=' + _wn_width + ', height=' + _wn_height + ', innerWidth=' + _wn_width + ', innerHeight=' + _wn_height);
		return false;
	});
	$('a[rel=qtpano1]').click(function () {
		var _wn_width = 700;
		var _wn_height = 450;
		var _wn_left = (window.screen.width / 2) - (_wn_width / 2);
		var _wn_top = (window.screen.height / 2) - (_wn_height / 2);
		var qtpano = window.open(cp + 'prezentacja?f=' + $(this).attr('href'), 'qtpano', 'menubar=no, location=no, resizable=no, scrollbars=no, status=yes, left=' + _wn_left + ', top=' + _wn_top + ', width=' + _wn_width + ', height=' + _wn_height + ', innerWidth=' + _wn_width + ', innerHeight=' + _wn_height);
		return false;
	});
};

var show_buttonad = function (divid, hrefid)
{
	var toppos = $("#"+hrefid).position().top;
	var leftpos = $("#"+hrefid).position().left;
	var offset = $("#"+hrefid).offset();
	var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
	
	$("#"+divid).css("clear", "both");
	if(IE6 == true)
		$("#"+divid).css("left", leftpos + 40).css("top", toppos - 60);
	else
		$("#"+divid).css("left", leftpos + 40).css("top", toppos - 60);
	
	$("#"+divid).addClass("locked");
	$("#"+divid).show();
	
	if($("#"+hrefid).position().top != toppos)
		$("#"+hrefid).css("margin-top", (toppos- $("#"+hrefid).position().top)+"px"); 
};

var delayed_hide = function (divid, hrefid)
{
	if(!$("#"+divid).hasClass("locked"))
	{
		$("#"+divid).css("top", -10000).css("left", -10000);
		//$("#"+divid).hide();
		$("#"+divid).css("display", "none");
		$("#"+hrefid).css("margin-top", "0px");
	}
};

var hide_buttonad = function (divid, hrefid)
{
	$("#"+divid).removeClass("locked");
	setTimeout(function() {delayed_hide(divid, hrefid);}, 500);
};

var email_add = function (email)
{
	$('#newsletterinfo').html('<div id="emailloader" style="text-align: center;"><img id="emailloader" src="frontend/i/ajax-loader.gif"></div>');
	$.get(cp + 'newsletter/add/' + email, function (ret) {
		if (ret != '-ERR')
		{
			$('#newsletterinfo').html(ret);
			$('#emailinput').val('');
		}
		else
		{
			$('#emailloader').remove();
		}
	});
	
};

var email_remove = function (email)
{
	$('#newsletterinfo').html('<div id="emailloader" style="text-align: center;"><img src="frontend/i/ajax-loader.gif"></div>');
	$.get(cp + 'newsletter/remove/' + email, function (ret) {
		if (ret != '-ERR')
		{
			$('#newsletterinfo').html(ret);
			$('#emailinput').val('');
		}
		else
		{
			$('#emailloader').hide();
		}
	});
};
