function LayoutFixes() {
	/* IE: zaznaczanie checkbox'ów .tripcats poprzez ikonki */
	$(".tripcats:not(.tc_disabled) li img").toggle(
		function() { $("input[@type=checkbox]", $(this).parent()).attr("checked", "checked") },
		function() { $("input[@type=checkbox]", $(this).parent()).attr("checked", "") }
	)			
	/* IE: dziwny margines dla fieldset>legend. */
	$('fieldset > :first-child').css("margin-top", 0)
	// 
	$(".formHolder fieldset:last-child").css("border-bottom", 'none')	
}

function BoxActions() {
	
	/* rozsuwany poradnik */
	$("dl.shortguide > dd").hide();
	$("dl.shortguide > dt").click(function() {
		$(this).addClass('chosen')
		$(this).next('dd').slideToggle();
		$(this).siblings('dt').removeClass('chosen')
		$(this).siblings('dt').not('.chosen').next().slideUp();
	})
	
	/* 
		rozwijane drzewko kontynentów i krajów
		jeśli w html chcemy 'na sztywno' otworzyć gałąź, dodajemy klasę "open" dla <dt>
	*/
	$("dl.countriestree > dt.open + dd").show()
	$("dl.countriestree > dt:not(.open) + dd").hide()
	$("dl.countriestree > dt").click(function() {
		$(this).next('dd').slideToggle();
		$(this).toggleClass('open')
		$(this).siblings('dt').removeClass('open')
		$(this).siblings('dt').not('.open').next().slideUp();
	});
			
	/* 
		obsługa miniatur div.photoviewer 
		TODO: skończyć loader obrazków
	*/
	(function(){
		var p = 'div.photoviewer'
		var next = $(p+" div.thumbs .btn_next")
		var prev = $(p+" div.thumbs .btn_prev")
		var viewport = $(p+" div.thumbs-viewport")
		var slider = $(p+" div.thumbs-viewport ul")
		var thumbLinks = $("> li a", slider)
		var pos = 0
		var items = slider.children().size()
		var sliderWidth = items*slider.children().width()		
		var step = slider.children().width()*2
		slider.width(sliderWidth)
		
		next.click(function() {
			if (pos < sliderWidth-viewport.width()) {
				slider.animate( { left: -(pos+step) }, 200,
					function() {
						pos += step
					}
				)
			}
		})	
		prev.click(function() {
			if (pos > 0) {
				slider.animate( { left: -(pos-step) }, 200,
					function() {
						pos -= step
					}
				)
			}
		})
				
		slider.children("li").each(function(i) {
			$(this)[0].index = i; 
		})
						
		var SetTitle = function(i){
			t = $("> img",thumbLinks.eq(i)).attr("alt");		
			$(p+" div.single h3").html(				
				( (t != undefined)?("<strong>"+t+"</strong>"):("") ) +"<small>"+(i+1)+" z "+thumbLinks.length+"</small>"
			)
		}		
	
		thumbLinks.click(function(e){
			$(p+" div.single a img")
				.attr({"src": $(this).attr("id"), "alt": $("> img", this).attr("alt") })
				.parent("a").attr({"href": $(this).attr("href"), "title": $("> img", this).attr("alt")})								
				.end()
			$(this).parent('li').addClass('active')
			$(this).parent().siblings('li').removeClass('active')		
			SetTitle($(this).parent()[0].index)
			e.preventDefault()
		})
				
		SetTitle(0)
		slider.children().eq(0).addClass("active") 		
	})()


	jQuery.fn.activateCollapsing = function() { 
		return this.each( function () {
			var $box = $(this)
			var $expander = $(" .box-head .expander", this)
			if ($box.hasClass('box_collapsed')) $(' .box-body',$box).hide();
			$expander.click( function() { 
				$(" .box-body", $box).toggle()
				$box.toggleClass("box_expanded").toggleClass("box_collapsed")
				if ($box.hasClass("box_expanded")) 
					{$expander.html("zwiń").attr('title', "zwiń")} 
				else 
					{$expander.html("rozwiń").attr('title', "rozwiń")}
			})
		}) // each
	}

	$(".box1").activateCollapsing();
	
	
	var $currentNavItem = $('ul#nav-root > li.current')
	$currentNavItem.siblings().hover(function() {
		$currentNavItem.removeClass('current')
	}, function() {
		$currentNavItem.addClass('current')
	})	
					
	/* "poleć znajomemu" */
	var recommendform = $("#recommendform")
	$("#act-recommend").click( function() {
		recommendform.toggle()
		window.location = ('#recommendform')	 
	})
	$("#recommendform-close").click( function() {
		recommendform.hide()
		return false
	})
	/* "ask" */
	var askform = $("#askform")
	$("#act-ask").click( function() {		
		askform.toggle()
		window.location = ('#askform')	 
	})
	$("#askform-close").click( function() {
		askform.hide()
		return false
	})
} // BoxActions()

/* zaznaczanie input[radio] wewnątrz <tr> */
function markRowRadio(el) {
	$("input[@type=radio]", el).attr("checked", "checked")
}

$(document).ready(function(){
	LayoutFixes();
	BoxActions();
	$('.datepicker input').date_input();
})