(function($){

var handle_tracker = function() {
	$("a[href^='http://'], a[href^='https://']").not('[href^="' + cms_systempath + '"]')
		.addClass("offSite")
		.attr('target', '_blank')
		.bind('click keypress', function(ev) {
			var code=ev.charCode || ev.keyCode;

			if((!code || (code && code == 13)) && pageTracker !== undefined) {
				pageTracker._trackEvent('Outbound', $(this).context.hostname, $(this).attr('href'));
			}

			return false;
		});
}

var handle_facebook = function() {
	if (!$('#fb-root').length)
		return;

	window.fbAsyncInit = function() {
		FB.init({
			appId: '223752184320128',
			status: true,
			cookie: true,
			xfbml: true
		});
	};

	var e = document.createElement('script'); e.async = true;
	e.src = document.location.protocol + '//connect.facebook.net/pl_PL/all.js';
	document.getElementById('fb-root').appendChild(e);
};

var handle_find_pleace = function(){
	$('#find-pleace select').change(function() {
		window.location = $(this).val();
	});
	$('#find-pleace select option').each(function(){
		if($(this).attr('id') == location.hash){
			$(this).attr('selected', 'selected');
		}
	});
	
}

var handle_imgs = function() {
    $('.rot .rotate-boxes').hide();
	$('.rot .rotate-boxes').eq(0).show();
	
	
	
    if(jQuery.browser.msie){
        $('.rot img').each(function() {
            this.style.filter = "progid:DXImageTransform.Microsoft." +
                "AlphaImageLoader(src=" + this.src + ",sizingMethod='scale')";	
		
        });
    }

	var toggle = function(idx) {
    	var imgs = $('.rot .rotate-boxes');
    	var next = (idx + 1) % imgs.length;
    	imgs.eq(idx).fadeOut(2000);
    	imgs.eq(next).fadeIn(2000);
		setTimeout(function() { toggle(next) }, 6000);
	}
	setTimeout(function() { toggle(0); }, 6000);
}


var handle_dzial_rot = function(){
	var imgs = $('.img-box img');

	if(imgs.length > 1){
		imgs.css('opacity',0);
		imgs.eq(0).css('opacity',1);	
		
		var change = function(idx){
			var next = (idx + 1) % imgs.length;
			imgs.eq(idx).animate({'opacity': 0},{duration: 1200});
			imgs.eq(next).animate({'opacity': 1},{duration: 1200, queue: false});
			setTimeout(function() { change(next) }, 6000);
		}
		setTimeout(function() { change(0); }, 6000);
	}
}

var handle_kols_height = function(){
	var kols = $('.kols-height');
	if(kols.length != 0){
		var max = 0;
		kols.each(function(){
			if($(this).height() > max) max = $(this).height();
		});
		kols.css('height',max);
	}
}

$(function() {
	handle_tracker();
	handle_facebook();
	handle_find_pleace();
	handle_imgs();
	handle_dzial_rot();
	handle_kols_height();
	
	$('.back-js').click(function(ev){
		ev.preventDefault();
        parent.history.back();
        return false;
    });
	
});

})(jQuery);

