$(function(){

/* TABS */		
	var active2target = $('#menu li.active').find('a').attr('class');
	$('.submenu').hide();
	$('#sub-' + active2target).show();


	$('#menu li').mouseover(function(){
		var target = $(this).find('a').attr('class');
		
		if ( !$(this).hasClass('active') ) {
		
			// remove 'active' class form all LI
			$('#menu li').removeClass('active');
			
			// add 'active' class to selected LI
			$(this).addClass('active');
			
			// hide all elements (LI) form submenu
			$('.submenu').hide();
			
			// show target element
			$('#sub-' + target).fadeIn('fast');	
		}
	});

	
/* 
		FANCY SELECT 
*/
	$.fn.initSelect = function(){
		return this.each(function(){
			$(this).parents('div:eq(1)').width($(this).parents('div:eq(1)').width());
			$(this).click(function(){
				var eId = '#'+$(this).parents('div:eq(1)').attr('id');
				var eWidth = $(this).parents('div:eq(0)').width();
				
				$(eId+' .select-options li').width(eWidth-6);
				var select = $(eId).find('select');
				
				$(eId +' .select-value').attr('readonly', true);
				if($(eId+' .select-options').is(':hidden')) {								
					$(eId+' .select-options').show();
					
					$(eId+' .select-options > li')
						.click(function(){
							
							// only select a subelement
							if( $(this).hasClass('title') ) {
								return false;
							} else {

								var thisIndex = $(eId+' .select-options > li').index(this);
								select.attr("selectedIndex", thisIndex)
								
								var thisValue = $(this).find('a').attr('href');
								//console.log(thisValue);
								select.attr('value', thisValue);
								
								$(eId+' .select-value').val($(this).text());
								$(eId+' .select-options > li').filter('.selected').attr('class','');
								$(this).addClass('selected');										
								$(eId+' .select-options').hide();
								//return false;	
							
							}	
						})
						.hover(function(){
							// hover only on a subelement
							if( $(this).hasClass('title') ) {
								return false;
							} else {								
								$(this).addClass('hover');
							}
						}, function(){
							$(this).removeClass('hover');
						});				
				} else {
					$(eId+' .select-options').hide();
				}
				$(eId+' .select-options').mouseleave(function(){
					$(eId+' .select-options').hide();							
				});													
			});
	});
	}
		
	$('#select-type .select-value').initSelect();
	$('#select-type .btn-select').initSelect();	
	$('#select-type .btn-select').click(function(){
		$('#select-type .select-value').focus();
	});	
	// lang
	if( $('.lang-en').length != 0 ) {
		$('#select-lang').find('.select-value').attr('value','english');
	} else {
		$('#select-lang').find('.select-value').attr('value','polski');
	}
	
	$('#select-lang .select-value').initSelect();
	$('#select-lang .btn-select').initSelect();	

/* NEWSLETTER ACTIVATION */

	function showNewsletter(target){
		var api = $('#newsletter-msg').overlay({
		api:true,
	    expose: { 
	        color: '#000', 
	        loadSpeed: 200, 
	        opacity: 0.6 
	    }			
		});
		api.load();
		api = $('#newsletter-msg').overlay();
		
		$('.'+target).show();
	}
	
	var url = window.location.href;
	var url_arr = url.split('#');
	
	if(url_arr[1] == 'activation-ok') {
		showNewsletter('msg-ok');
	} else if(url_arr[1] == 'activation-faild' ) {
		showNewsletter('msg-err');
	}
	

/* TOPLAYER */
    
$("a.toplayerload").overlay({ 
 
    expose: {
        color: '#000',
        loadSpeed: 200,
        opacity: 0.5 
    },
 
	onBeforeLoad: function() { 
		var wrap = this.getContent().find(".contentWrap"); 
		wrap.load(this.getTrigger().attr("href")); 
	} 
});
	
		
/* szerokosc dla non-label */
	var labelWidth = $('.filter').find('.label').width();
	$('.filter').find('.non-label').width(labelWidth);
	
// wysokosc promoboxa
	var tallest = 0;
		$('#promo-slider li').each(function(){
			thisHeight = $(this).height();
			tallest = (thisHeight > tallest) ? thisHeight : tallest;
		});	

	$('#carousel').css('height',tallest);
		
	// wyrownanie elementow 
	$.fn.equalHeight = function() {
		var tallest = 0;
		this.each(function(){
			thisHeight = $(this).height();
			tallest = (thisHeight > tallest) ? thisHeight : tallest;
		});
		$(this).find('.distance').css({'padding-top' : -tallest/4});
		return this.each(function(){
			$(this).css({
				'height' : tallest
			});
		});
	}
// sasiednie caption
	$('#category1').find('.caption').equalHeight();
	$('#category2').find('.caption').equalHeight();
	$('#category3').find('.caption').equalHeight();
	$('#category4').find('.caption').equalHeight();
	$('#category5').find('.caption').equalHeight();					

// promo box
	$('#carousel')
	.scrollable({
		size : 1,
		items : '.slider-item',
		clickable : false,
		loop : true 
	})
	.circular()
	.navigator({
		navi : '.carousel-ctrl',
		naviItem : 'li',
		activeClass : 'active',
		idPrefix : 'navi'
	})
	.autoscroll({
		steps : 1,
		autopause : true,
		interval : 5000
	});
	
	
// pagepeel
	$('#pagepeel').click(function(){
		$('#pagepeel img , .msg_block').stop()
			.animate({
				width: '307px',
				height: '309px'
		}, 300, function(){ 
							$('#newsletter-form, #pagepeel .close').fadeIn('fast'); 
							$('#newsletter-form').find('input.input_text').focus();
						});
	});
	$('#pagepeel .close').click(function(){	
			$('#newsletter-form, #pagepeel .close').hide();
			$('#pagepeel img').stop()
				.animate({
					width: '80px',
					height: '80px'
				}, 200);
		$('.msg_block').stop()
			.animate({
				width: '80px',
				height: '80px'
			}, 200);
			return false;
	});
		
// contact form hidding inputs
	$('#startplace').parent().hide();
	$('#transform').change(function(){
		if( $(this).attr('selectedIndex') != 0  ){
			$('#startplace').parent().show();
		} else {
			$('#startplace').parent().hide();
		}	
	});
	if($.browser.msie && $.browser.version.substr(0,1) < 8){
		var zIndexNumber = 1000;
		$('#content div').each(function() {
			$(this).css('zIndex', zIndexNumber);
			zIndexNumber -= 10;
		});
	}

	
}); // domready end


