$(document).ready(function(){
	var expand = $('div.item div.close').html();
	var collapse = $('div.item div.open').html();

	var popupContent = $("div#popup").html();
	if(popupContent != '') {
		$.fancybox(
		    	popupContent, {
		    	'autoDimensions' : true,
		    	'autoScale': true,
				'centerOnScroll': true,
		    	'hideOnContentClick': true,
		    	'margin': 0,
		    	'overlayOpacity': 0,
		    	'padding:': 0,
		    	'scrolling' : 'no',
	        	'height': 'auto',
	        	'width' : 'auto',
		    	'transitionIn' : 'none',
		    	'transitionOut' : 'none',
		    	'showCloseButton' : false,
		    	'onComplete' : function(){
		    			//setTimeout( "$.fancybox.close();", 5000);
		    	}
		    }
		);
	}
	
	$('div#poland-product-big').css('cursor', 'pointer');
	$('div#poland-product-big').click(function(){
		window.document.location.href="/pl/produkt-polski"; 
	});
	
	
	$('div[rel="btn"]').click(function() {

		if($(this).attr('class') == 'close'){
			var old = $(".open");
			old.parent().next().find('.img_hover').attr('style','display:none;');
			old.parent().next().slideToggle('2000');
			
			old.parent().attr('class','no-active');
			old.html(expand);
			old.attr('class','close');

			$(this).attr('class','open');
			$(this).html(collapse);
			$(this).parent().attr('class','active');
			
			$(this).parent().next().slideToggle('2000', function() {
				$(this).parent().find('.img_hover').show();
				if( $.browser.msie ){
					$("#offer_bar .item .list ul").css("z-index","1");
					$("#offer_bar .item .list ul").css("z-index","3");
				}
			});
			Cufon.refresh();
		}else{
			$(this).attr('class','close');
			$(this).parent().next().find('.img_hover').attr('style','display:none;');
			$(this).html(expand);
			$(this).parent().attr('class','no-active');
			
			$(this).parent().next().slideToggle('2000', function(){
					if( $.browser.msie ){
						$("#offer_bar .item .list ul").css("z-index","1");
						$("#offer_bar .item .list ul").css("z-index","3");
					}
				});
			Cufon.refresh();
		}
		
	});

	var ie_ver = jQuery.browser.version;

	$('a#showAll').click(function() {
		
		$('div.item').each(function(index) {
			
			var obj = $(this).children().next();
			
			if(obj.attr('style') == 'display: none;' || obj.attr('style') == 'DISPLAY: none'){
				var objNext = $(this).children().children().next();
				if(objNext.attr('class') == 'close'){
					
					obj.parent().find('.close').html(collapse);
					obj.parent().find('.close').attr('class','open');
					obj.parent().find('.no-active').attr('class','active');
					
					//blad na ie 7
					if ($.browser.msie && ie_ver == 7) {
						obj.parent().find('.list ul').css('margin-top','-40px');
						obj.parent().find('.list ul').css('padding-bottom','40px');
					}
				}
				$(this).children().next().slideToggle('2000', function() {
					$(this).find('.image').parent().show();
					$(this).attr('style','display: block;');
				});
			}
		});
		$('a#showAll').css('display','none');
		$('a#hideAll').css('display','inline-block');
		Cufon.refresh();
	});
	
	$("#hideAll").click("click", function(){
		$('div.item').each(function(index) {
			var obj = $(this).children().next();
			
			if(obj.attr('style') == 'display: block;' || obj.attr('style') == 'DISPLAY: block'){
				obj.parent().find('.open').html(expand);
				obj.parent().find('.open').attr('class','close');
				obj.parent().find('.active').attr('class','no-active');
				
				$(this).find('.image').parent().hide();
				$(this).children().next().slideToggle('2000', function() {	
					$(this).attr('style','display: none;');
				});
			}
		});
		$('a#showAll').css('display','inline-block');
		$('a#hideAll').css('display','none');
		Cufon.refresh();
	});
	
	$("input#calculator").click("click", function(){
		$.ajax({
	        url: '/ajax/pl/calculator/index/value/'+$('#value').val()+'/unit1/'+$('#unit1').val()+'/unit2/'+$('#unit2').val(),
	        type: 'GET',
	        success: function (html) {
				$('#result').html(html);
	        }
	    });
	});
	
	$("input#bmi-submit").click("click", function(){
		$.ajax({
	        url: '/ajax/pl/calculator/bmi/sex/'+$('input[name="sex"]').val()+'/weight/'+$('input[name="weight"]').val()+'/height/'+$('input[name="height"]').val(),
	        type: 'GET',
	        success: function (html) {
				$('#result').html(html);
	        }
	    });
	});
	
	$('#print').click("click", function(){
		var o = $("#content");
		o.jqprint();
	});
	
	$('div.belt').click(function() {
		if($(this).parent().attr('class') == 'closed'){
			var old = $(".opened").find('.active');
			if(old.html() != null){
				old.attr('class','deactive belt cufon');
				old.parent().find('.hover').slideToggle('2000', function() {
					old.parent().attr('class','closed');
					old.parent().find('.hover').css('display','none');
				});
			}
			$(this).attr('class','active belt cufon');
			$(this).parent().find('.hover').slideToggle('2000', function() {
				$(this).parent().attr('class','opened');
				$(this).parent().find('.hover').css('display','block');
			});
		}else{
			$(this).attr('class','deactive belt cufon');
			$(this).parent().find('.hover').slideToggle('2000', function() {
				$(this).parent().attr('class','closed');
				$(this).parent().find('.hover').css('display','none');
			});
		}
		Cufon.refresh();
	});
	


})
