/** 
* Skrypty obsługujące stronę główną
*
*/

function smartColumns() {
	$("ul.boxes").css({ 'width' : "100%"});
	
	var colWrap = $("ul.boxes").width();
	var colNum = Math.floor(colWrap / 232);
	var colFixed = Math.floor(colWrap / colNum);
	
	
	$("ul.boxes").css({ 'width' : colWrap});
	$("ul.boxes li.box").css({ 'width' : colFixed});
	
	
	
	$("ul.categoryBoxes").css({ 'width' : "100%" });
	
	var colWrap = $("ul.categoryBoxes").width();
	var colNum = Math.floor(colWrap / 237);
	var colFixed = Math.floor(colWrap / colNum);
	
	
	$("ul.categoryBoxes").css({ 'width' : colWrap});
	$("ul.categoryBoxes li.catBox").css({ 'width' : colFixed});
	
}	

function getHash() {
  var hash = window.location.hash;
  return hash.substring(1); // remove #
}

$(window).load(function(){
		smartColumns();
});
	
$().ready(function(){
	// Fluid boxes
	
	$(window).resize(function () {
		smartColumns();
	}); 	
	
	// Font sizer
	
	$(".resetFont").click(function(){
			$(".changeFontSize").removeAttr('style');
			$.cookie('fontsize',null,{ path: '/', expires: 1 });
			return false;
	});
	
	 // Zwiekszenie o 2
	 $(".increaseFont").click(function(){
			 $(".changeFontSize").each(function () {
			 		$(this).removeAttr('style');
				 	$(this).css('font-size', parseFloat($(this).css('font-size'), 10)+2);
				 	$.cookie('fontsize',2,{ path: '/', expires: 1 });
			});
	 		return false;
	});  
	
	// Zwiekszenie o 4
	 $(".increaseMoreFont").click(function(){
			 $(".changeFontSize").each(function () {
			 		$(this).removeAttr('style');
					$(this).css('font-size', parseFloat($(this).css('font-size'), 10)+4);
					$.cookie('fontsize',4,{ path: '/', expires: 1 });
			});
	 		return false;
	}); 
	
	var fontsize=parseInt($.cookie('fontsize'),10);
	if(fontsize>0){
			$(".changeFontSize").each(function () {
			 		$(this).removeAttr('style');
					$(this).css('font-size', parseFloat($(this).css('font-size'), 10)+fontsize);
			});
	}
		
	// Box hover
	
		$('.boxes div.block').hover(function() {
		  $(this).addClass('imgHover');
		}, function() {
		  $(this).removeClass('imgHover');
		});
	
	// FAQ
	
	$(".faq div").hide();
	$(".faq a.faqa").toggle(function(){
		$(this).addClass("open"); 
		}, function () {
		$(this).removeClass("open");
	});
	$(".faq a.faqa").click(function(){
		$(this).next(".faq div").slideToggle("slow");
	});

	var hash_id = getHash();
	
	if (hash_id != false)
	{
		$(".faq a[name='"+hash_id+"'].faqa").addClass("open").next(".faq div").show();
	}
	
	// Wyszukiwarka
	var searchtext=$('#searchtext').text();
	$("#searchMain").focus(function(){ if($(this).val()==searchtext) $(this).val(''); }).blur(function(){ if($(this).val()=="") $(this).val(searchtext); }).val(searchtext).autocomplete(base_url+lang+"/ajax/products/find.html", {width: 288, minChars: ac_minc, max:ac_maxl, selectFirst: false, cacheLength: 1});
	$("#SearcherForm").submit(function(){ if($("#searchMain").val().length>=3) return true; else { alert('Wpisz minimum 3 znaki'); return false; } });

	// Newsletter
	$("#newsletterTextfield").val('Dopisz swój adres e-mail');
	var newslettertext = $("#newsletterTextfield").val();
	
	$("#newsletterTextfield").focus(function(){ if($(this).val()==newslettertext) $(this).val(''); }).blur(function(){ if($(this).val()=="") $(this).val(newslettertext); }).val(newslettertext);

	$(".blockOverlay").live( 'click', function() {
		$.unblockUI({ fadeOut: 200 }); 
	});
	$(".blockUI").live( 'click', function() {
		$.unblockUI({ fadeOut: 200 }); 
	});
	
	// Zamówienie
	$("#makeorder").click(function(){
		play();
	});
	
	return false;
});

function openForm() {
	if($('#newsletterTextfield').val() != 'Dopisz swój adres e-mail' && $('#newsletterTextfield').val() != '') {
		$.blockUI({ 
			message: '<div style="margin: 0pt auto; width: 540px; position: relative; top: 38px; height: 32px; z-index: 1000"><img style="float: right; z-index: 10000;" src="public/images/fancy_close.png" alt="Zamknij" /></div><iframe width="520px" height="360" frameborder="0" scrolling="no" style="position: relative; border: 3px solid #0090d3; background: #000; margin: 0 auto; padding-right: 20px; display: block;" src="'+base_url+'/'+lang+'/newsletter/index/'+$('#newsletterTextfield').val()+'.html"></iframe>', 
            fadeIn: 700, 
            fadeOut: 700, 
            centerY: true,
            centerX: true,
            css: { 
            	textAlign:  'center',
                border: 'none', 
                opacity: .9,
	            overflow: 'hidden',
	            width: '100%',
	            top: '20%',
	            left: '0',
	            background: 'none',
	            cursor: 'pointer'
	            
            },
            overlayCSS:  { 
				backgroundColor: '#000', 
				opacity: 0.6,
				cursor: 'pointer'
			}
        });
	}
	return false;

};

function openForm5() {
	if($('#newsletterEmail').val() != '') {
		$.blockUI({ 
			message: '<div style="margin: 0pt auto; width: 540px; position: relative; top: 38px; height: 32px; z-index: 1000"><img style="float: right; z-index: 10000;" src="public/images/fancy_close.png" alt="Zamknij" /></div><iframe width="520px" height="360" frameborder="0" scrolling="no" style="border: 3px solid #0090d3; background: #000; margin: 0 auto; padding-right: 20px; display: block;" src="'+base_url+'/'+lang+'/newsletter/index/'+$('#newsletterEmail').val()+'.html"></iframe>', 
            fadeIn: 700, 
            fadeOut: 700, 
            centerY: true,
            centerX: true,
            css: { 
            	textAlign:  'center',
                border: 'none', 
                opacity: .9,
	            overflow: 'hidden',
	            width: '100%',
	            top: '20%',
	            left: '0',
	            background: 'none',
	            cursor: 'pointer'
            },
            overlayCSS:  { 
				backgroundColor: '#000', 
				opacity: 0.6,
				cursor: 'pointer'
			}
        });
	}
	return false;
};

function play(){
      getMovieName("CatalogFlash").playMusic("Ching"); 
}
function getMovieName(movieName){  
      return document.getElementById(movieName);  
}

