jQuery(document).ready(function($) {
	
	// Zakladki na glownej stronie
    $('#tabs').tabs({
        cookie: {
            expires: 1
        }
	}).addClass('ui-tabs-vertical ui-helper-clearfix');
    $('#tabs li').removeClass('ui-corner-top').addClass('ui-corner-left');
	
	
	// Slider
    $('#main_slideshow').cycle({
        fx: 'fade',
        next: '.next',
        prev: '.prev',
        timeout: 8000,
        pause: true,
        after: onAfter
    });
	
	function onAfter(curr, next, opts) {
		$('#main_slideshow div').each(function() {
            if( $(this).css('display') == 'block' ) {
				var active_class = $(this).attr('class');
				$('#boxes_menu li a').each(function () { $(this).attr('class', 'disable'); });
				$('#boxes_menu li.' + active_class + ' a').attr('class', 'active');
		    }
		});				
        return false;
    }    
    
	$('#boxes_menu .item-01 a').click(function() {
        $('#main_slideshow').cycle(0);
        return false;
    });
    
    $('#boxes_menu .item-02 a').click(function() {
        $('#main_slideshow').cycle(1);
        return false;
    });
	
    $('#boxes_menu .item-03 a').click(function() {
        $('#main_slideshow').cycle(2);
        return false;
    });
	
	$('#boxes_menu .item-04 a').click(function() {
        $('#main_slideshow').cycle(3);
        return false;
    });
	
	$('#boxes_menu .item-05 a').click(function() {
        $('#main_slideshow').cycle(4);
        return false;
    });
	
    $('.experts_slideshow').cycle({
        fx: 'none',
        timeout: 0,
        next: '.next-ex',
        prev: '.prev-ex' 
    });
	
	
	// Glowna wyszukiwarka
    var search_title;
    $('#s').focus(function() {
        search_title = $('#s').attr('value');
        $(this).attr('value','');
    });
    $('#s').blur(function() {
        if($(this).attr('value') == '')
        $(this).attr('value',search_title);
    });               
    
	
	// Wysokosc boksow pytanie-odpowiedz
    var q_height = $('.question').height();
    var r_height = $('.reply .reply-text').height();
    $('.question .bar').height(q_height);
    $('.reply .bar').height(r_height-20);      
    
	
	// Walidacja formularza 'zadaj pytanie'
    $('#questionform').validate({
        rules: {
            mail: { required:true, email:true },
            question: 'required',
            desc: 'required',
            agree: 'required'
        },
        messages: {
            mail: 'Niepoprawny adres email',
            question: 'To pole jest wymagane',
            desc: 'To pole jest wymagane',
            agree: 'Zaakceptuj regulamin'
        }    
    });
	
	
	// Warstwa z produktami            
    $('.recommend_prod a[rel]').overlay({
		mask: {
			color: '#000',
			loadSpeed: 200,
			opacity: 0.9
		},		
		fixed:false
	});
	
	
	// Warstwa z newsletterem
	$('#overlay_newsletter').overlay({
		top:150,
		closeOnClick:false,
		load:true
	});
	
	$emailFormNewsletter = $('#emailFormNewsletter');
	
	$('#emailFormNewsletter, #sidebar_emailFormNewsletter, #lp_emailFormNewsletter')
	.focus(function() {
		if($(this).attr('value') == 'Twój e-mail')
			$(this).attr('value', '');	
	}).blur(function() {
		if($(this).attr('value') == '')
			$(this).attr('value', 'Twój e-mail');
	});
	
	$('#submitFormNewsletter').click(function() {
		var email = $emailFormNewsletter.val();
		
		$.ajax({
			type: 'post',
			url: templateUrl + '/newsletter.php',
			data: 'email=' + email,
			success: function(results) {
				if( results.search("poprawnie") != -1 ) {
					$('#overlay_newsletter p').html(results);
					$('#overlay_newsletter .response, .newsletter-submit').css('display', 'none');
				}
				else {
					$('#overlay_newsletter .response').html(results).css('display', 'block');
				}				
			}
		});
		
		return false;		
	});
	
	$('#sidebar_submitFormNewsletter').click(function() {
		var email = $('#sidebar_emailFormNewsletter').val();
		
		$.ajax({
			type: 'post',
			url: templateUrl + '/newsletter.php',
			data: 'email=' + email,
			success: function(results) {
				if( results.search("poprawnie") != -1 ) {
					$('div.newsletter p').html(results);
					$('div.newsletter .response, div.newsletter .newsletter-submit').css('display', 'none');
				}
				else {
					$('div.newsletter .response').html(results).css('display', 'block');
				}				
			}
		});
		
		return false;		
	});
	
	$('#lp_submitFormNewsletter').click(function() {
		var email = $('#lp_emailFormNewsletter').val();
		
		$.ajax({
			type: 'post',
			url: templateUrl + '/newsletter.php',
			data: 'email=' + email,
			success: function(results) {
				if( results.search("poprawnie") != -1 ) {
					$('#lp-newsletter p').html(results);
					$('#lp-newsletter .response, #lp-newsletter .newsletter-submit').css('display', 'none');
				}
				else {
					$('#lp-newsletter .response').html(results).css('display', 'block');
				}				
			}
		});
		
		return false;		
	});		
	
	
	// Wydruk artykulu
	$('#print').css('display', 'block').live('click', function() {
        window.print();
        return false;
    });
	
	
	// Widok 'Eksperci' - wiecej/mniej informacji
	var expertHeight = [];
	
	// tablica przechowujaca pierwotne wysokosci boxow
	$('.eksperci .post').each(function() {
		expertHeight.push($(this).height());
		$(this).height( $(this).height() );
	});
	
	// rozwin - wiecej informacji
	$('.btn-expert-more').live('click', function() {	
		var leftBlockHeight, rightBlockHeight, currentHeight;
		
		$('.entry .expert-info', $(this).parents('.post')).css('display', 'none');
		$('.expert-articles, .left-info .expert-info', $(this).parents('.post')).fadeIn(500);
		
		leftBlockHeight = $('.left-info', $(this).parents('.post')).height();
		rightBlockHeight = $('.entry', $(this).parents('.post')).height();
		
		if( (leftBlockHeight - 40) > rightBlockHeight) {
			$('.entry', $(this).parents('.post')).height(leftBlockHeight - 40);
			currentHeight = leftBlockHeight;
		}
		else {
			$('.entry .expert-info', $(this).parents('.post')).height(rightBlockHeight);
			currentHeight = rightBlockHeight + 40;
		}
		
		$(this).parents('.post').animate({height:currentHeight + 2}, 500);
		return false;
	});
	
	// zwin - mniej informacji
	$('.btn-expert-less').live('click', function() {
		var currentHeight;
		
		$('.expert-articles, .left-info .expert-info', $(this).parents('.post')).css('display', 'none');
		$('.entry .expert-info', $(this).parents('.post')).fadeIn(500);
		
		if($(this).parents('.post').hasClass('item-01'))
			currentHeight = expertHeight[0];
		else if($(this).parents('.post').hasClass('item-02'))
			currentHeight = expertHeight[1];
		else if($(this).parents('.post').hasClass('item-03'))
			currentHeight = expertHeight[2];
		else if($(this).parents('.post').hasClass('item-04'))
			currentHeight = expertHeight[3];
		else if($(this).parents('.post').hasClass('item-05'))
			currentHeight = expertHeight[4];
		else if($(this).parents('.post').hasClass('item-06'))
			currentHeight = expertHeight[5];
		
		$(this).parents('.post').animate({height:currentHeight}, 500);		
		return false;		
	});
	
	
    // Tooltip
    /*	:: markup ::
     	<.* class="tooltip" href="#" title="">Tooltip
		<sup>Content<span class="tip">&nbsp;</span></sup>
	</.*>
    */
    $('.tooltip').hover(
	function() {
	    var tooltip_height = $('sup', this).height() + 2 * parseInt($('sup', this).css('padding-top'));
	    var offset = 5;
	    
	    var margin_top = tooltip_height + offset;
	    $('sup', this).fadeIn().css({
			'display' : 'block',
			'margin-top' : -margin_top
	    });
	},
	function() {
	    $('sup', this).css('display', 'none');
	}
    );
	
	
	// Wersja rodzicielska - tata
	/*
	$('#other_version').click(function() {
		$.cookie('version', 'tata', { path: '/' });
	});
	*/
	

});
