var wcCityChache = '';
var wcCityCount = 0;
var wcCityTerm = '';

// wywołanie miasta z flasha
function city(id)
{
	if(id==8059 || id==25207)
		iframe("pokaz-miasto-dzika-karta/" + id, "city-parent");	
	else
		iframe("pokaz-miasto/" + id, "city-parent");
}

// wywołanie miasta z flasha
function city_wild(id)
{
	iframe("pokaz-miasto-dzika-karta/" + id, "city-parent");
}

// wywołanie scrolla
function scroll()
{
	$('.jscroll').jScrollPane({showArrows:true, dragMinHeight: 20,dragMaxHeight: 20});
}

// przekierowanie
function parentLocation(url)
{
	parent.location.href=url;
}


function iframeClose()
{
	$('.box').fadeOut('slow').remove();
	$('#overmask').fadeOut('slow').remove();
}

// wywolanie okna
function iframe(url, c)
{

	var html = '<div id="overmask"></div>';

		html +='<div class="box ' + c + '">';
		html +='<div class="bg">';
		
		html +='<span class="close">ZAMKNIJ X</span>';
		html +='<div class="shadow">';
			html +='<div class="content">';

				html +='<iframe id="frame" src="' + base + url + '" scrolling="auto" frameborder="0"></iframe>';
				
				
				if(c=='login-parent' || c=='wild-card-parent' || c=='recommend-parent')
					html+='<div class="men"></div>';

			html +='</div>';
		html +='</div>';
		
		if(c=='city-parent')
			html+='<div class="city-men"></div>';
		
		html +='</div>';
		html +='</div>';

	$('body').prepend(html);
		
	jQuery('iframe').iframeAutoHeight();
	
	return false;

} 

// przesuwanie boksa
function boxMove()
{
	if($('.box').length)
	{
		if($.browser.msie) 
		{
			var wp = document.documentElement.scrollTop;
			var wh = document.documentElement.clientHeight;
		} 
		else 
		{			
			var wp = window.pageYOffset;
			var wh = window.innerHeight;
		}			
		
	
		var rp = wp + (wh/2);
		var ch = $('.box').outerHeight();

		
		if(wh<ch)
			rp = 0;
		else
			rp = rp - (ch / 2);
	
		$('.box').css({'top':rp});
	}
}
	

$(document).ready(function()
{

	$('#menu li').each(function(i){
		$(this).addClass('c'+(i+1));
	});

	// new window
	$('a[rel=external]').click(function()
	{
		window.open(this.href);
		return false;
	});
	
	
	// blokowanie pod ie
	$('a.block').click(function()
	{
		return false;
	});
	
	
	// ustawqienie okna
	boxMove();
	
	// dymki
	$('#content .i span').hover(function()
	{
		var pos = $(this).position();
		var h = pos.top-$(this).parent().parent().find('.infobox').height();
		$(this).parent().parent().find('.infobox').css({'top': h,  'left':pos.left-195}).fadeIn();

	},
	function()
	{
		$(this).parent().parent().find('.infobox').fadeOut();
	});
	
		
		
	// scroll
	if($('.jscroll').length)
		scroll();

		
	// opisy miast w karcie
	
	//$('#card1 .row .r2, #item1 .row .r2:not(.wild)').toggle(function()
	$('#card1 .row .r2, #item1 .row .r2').toggle(function()
	{
		$(this).parents('.list').find('.details:visible').find('.links a.up').click();
	
		$(this).parents('.row').addClass('clicked').css({'margin-top':'-1px'});
		$(this).parent().find('.details').slideDown(function(){
			scroll();
		});
		
		$(this).parents('.row').prev('li').css({'background':'none','border-bottom':'2px solid #C7C7C7'});

		
	
	},function()
	{
		$(this).parents('.row').prev('li').css({'background':'url(img/pic15.gif) 0px 100% repeat-x','border-bottom':'0px'});
		$(this).parents('.row').removeClass('clicked').css({'margin-top':'0px'});
		$(this).parent().find('.details').hide(function()
		{
			scroll();
		});
		

	});
	
	// zwijanie w gore szczegolow
	$('.details .up').click(function()
	{
		$(this).parents('.row').find('.r2').click();
		return false;
	});
	
	
	// karty - podmiana
	var c = 1;
	$('.card .other span span').click(function()
	{
			$('#item'+c).hide();
			
			if(c==1)
				c++;
			else
				c--;
			
			$('#item'+c).css({'visibility':'visible'}).show().find('.find counter').text($('#item'+c).find('ul li.row:visible').length);

		scroll();

		if (!$('#item2 .list ul').length)
		{
			$('#item2 .list').spinner();
			$.get(base+'ranking/get_wild_card_cities/', function(response){
				$('#item2 .list').html(response);
				scroll();
				var c = $('#item2 .list ul li.row').length;
				$('#item2 .counter').text(c);
			});
		}
	});

	
	// zamykanie okna
	$('.box span.close').live('click',function(){
		iframeClose();
	});
	
	
	// podmiana cheboxow
	if($("input[type='checkbox']").length)
		$("input[type='checkbox']").custCheckBox();

	
	// usuwanie domyslnych wartosci
		$('.box :text').bind('click focus',function()
	{
		if($(this).val() != '')
		{
			inputvalue = $(this).val();
			$(this).val('');
		}
	});

	$('.box :text').bind('blur', function()
	{
		if($(this).val() == '')
		{
			$(this).val(inputvalue);
		}
	});
	

	// przesuwanie shadowboxa
	$(window).resize(function () { 
		boxMove();
	});
	
	
	$(window).scroll(function () { 
		boxMove();
		
	});
	
	// wyszukiwarki
	$('input.searcher').focus(function(){
		if ($(this).val()=='wpisz miasto')
			$(this).val('');
	});

	$('#item1 form').submit(function(){
		if ($('#item1 input.searcher').val()=='wpisz miasto')
			return false;

		var t=$('#item1 input.searcher').val();

		var c=0;
		
		if (t=='')
		{
			$('#item1 .list ul li.row').show();
			c = $('#item1 .list ul li.row').length;
		}
		else
		{
			$('#item1 .list ul li.row .r2').each(function(){
				if ($(this).text().toLowerCase().substr(0, t.length)==t.toLowerCase())
				{
					$(this).parent().show();
					c++;
				}
				else
					$(this).parent().hide();
			});
		}

		scroll();
		$('#item1 .counter').text(c);
		return false;
	});

	wcCityCache=$('#item2 .list ul').html();
	wcCityCount=$('#item2 .list ul li').length;

	$('#item2 form').submit(function(){
		if ($('#item2 input.searcher').val()=='wpisz miasto')
			return false;

		var t=$('#item2 input.searcher').val();

		if (t==wcCityTerm)
			return false;

		wcCityTerm=t;
		var c=0;

		if (t=='')
		{
			$('#item2 .list ul').html(wcCityCache);
			c = wcCityCount;
		}
		else
		{
			$('#item2 .list').spinner();
			$.get(base+'ranking/get_wild_card_cities/'+t, function(response){
				$('#item2 .list').html(response);
				scroll();
				c = $('#item2 .list ul li.row').length;
				$('#item2 .counter').text(c);
			});
		}

		scroll();
		$('#item2 .counter').text(c);
		return false;
	});

	if ($("#WildCardCityName").length)
	{
		$("#WildCardCityName").autocomplete({
			source: base+'ranking/get_cities/'+$('#WildCardCityVoivodId').val()
		});
	}

});



