$(document).ready(function() {
	$.Lightbox.construct({
		files: {
			// If you are doing a repack with packer (http://dean.edwards.name/packer/) then append ".packed" onto the js and css files before you pack it.
			js: {
				lightbox:	'js/jquery.lightbox.js'
			},
			css: {
				lightbox:	'css/lightbox.css'
			},
			images: {
				prev:		'images/lightbox/prev.gif',
				next:		'images/lightbox/next.gif',
				blank:		'images/lightbox/blank.gif',
				loading:	'images/lightbox/loading.gif'
			}
		},
		text: {
			about: {
				text: 	''
			}
		}
	});
	
	if($("#entry_country_id :selected").text() == 'Polska'){
    $("#entry_postcode").mask("99-999");
  	$("#entry_suburb").mask("9999999999");
	}
	
	$("#entry_country_id").change(function() {
		if($("#entry_country_id :selected").text() == 'Polska'){
      $("#entry_postcode").mask("99-999");
    	$("#entry_suburb").mask("9999999999");
	  }else{
	    $("#entry_postcode").unmask();
    	$("#entry_suburb").unmask();
    }
	});	
	
	$('#productSortForm :input[name="sort"]').change(function() {
		return $(this).parents('form').submit();
	});	
	
	$('.compareAll a, a.zagiel_popup').click(function() {
		window.open($(this).attr('href'),'comparator','width=800,height=600,centerscreen=yes,chrome=yes,menubar=no,toolbar=no,directories=no,resizable=yes,scrollbars=yes,dialog=yes,minimizable=yes');
		return false;
	});
	
	$('tr.attribute').mouseover(function() {
		$(this).children('td').addClass('active');
	});
	$('tr.attribute').mouseout(function() {
		$(this).children('td').removeClass('active');
	});
	
	$('input.cssButton').mouseover(function() {
		$(this).removeClass('cssButton');
		$(this).addClass('cssButtonHover');
		if ($(this).hasClass('button_zatwierdz')) {
			$(this).removeClass('button_zatwierdz');
			$(this).addClass('button_zatwierdzHover');
		}	
	});
	$('input.cssButton').mouseout(function() {
		$(this).removeClass('cssButtonHover');
		$(this).addClass('cssButton');
		if ($(this).hasClass('button_zatwierdzHover')) {
			$(this).removeClass('button_zatwierdzHover');
			$(this).addClass('button_zatwierdz');
		}	
	});

	
	var textWidth = $('#infoText').width();
	$('#infoText').css('margin-left',$('#infoBar').width()+'px');
	
	
	window.setInterval(function() {
		var margin = parseInt($('#infoText').css('margin-left'));
		$('#infoText').css('margin-left',(margin-1)+'px');
		if (parseInt($('#infoText').css('margin-left')) <= parseInt('-'+textWidth)) {
			$('#infoText').css('margin-left',$('#infoBar').width()+'px');
		}
	}, 60);
	
	
	$('form.customerDataForm input[class="receiver"]').each(function() {
		if ($(this).attr('checked')) {
			createAccountFormHandling($(this));
			createAccountFormHandling2($(this));
		}
	});
	$('form.customerDataForm input').click(function() {
		createAccountFormHandling($(this));
		createAccountFormHandling2($(this));
	});
	
	$('input[name="show_password_characters"]').click(function() {
		if ($(this).attr('checked')) {
			var inputPassword = $(this).parents('form').find('input[type="password"].inputPassword');
			var newInputPasswordType = 'text';
		} else {
			var inputPassword = $(this).parents('form').find('input[type="text"].inputPassword');
			var newInputPasswordType = 'password';
		}
		
		$(inputPassword).each(function() {
			var newInputPassword = '<input type="'+newInputPasswordType+'" name="'+$(this).attr('name')+'" id="'+$(this).attr('id')+'" class="'+$(this).attr('class')+'" value="'+$(this).val()+'"/>';
			$(this).after($(newInputPassword));
			$(this).remove();
		});
	});
	
	$(".copy_address").mouseover(function() {
			  $(".cp_address_span").css('text-decoration','underline');
	});
	$(".copy_address").mouseout(function() {
			  $(".cp_address_span").css('text-decoration','none');
	});
	$(".copy_address").click(function() {
		    if($("#receiver_p").attr('checked')){ 
            $("#receiver_delivery_p").attr('checked','checked');
            $('.receiver[value="p"]').click(); 
        }
			  if($("#receiver_c").attr('checked')){ 
            $("#receiver_delivery_c").attr('checked','checked');
            $('.receiver[value="c"]').click(); 
        }
			  $("#firstname_d").attr('value', $("#firstname").attr('value')) ;
			  $("#lastname_d").attr('value', $("#lastname").attr('value'));
			  $("#company_d").attr('value', $("#company").attr('value'));
			  $("#suburb_d").attr('value', $("#suburb").attr('value'));
			  $("#street_address_d").attr('value', $("#street-address").attr('value'));
			  $("#postcode_d").attr('value', $("#postcode").attr('value'));
			  $("#city_d").attr('value', $("#city").attr('value'));
			  $("#country_d").attr('value', $("#country").attr('value'));
	});
	
	function createAccountFormHandling(elem)
	{
		if ($(elem).hasClass('receiver')) {
			var inputCompany = $(elem).parents('fieldset:first').find('input.company');
			var inputSuburb = $(elem).parents('fieldset:first').find('input.suburb'); 
			if ($(elem).val() == 'p' ) {
				$(inputCompany).val('').hide().prev('label').hide();
				$(inputCompany).next('span.alert').hide().next('br').hide();
				$(inputSuburb).val('').hide().prev('label').hide();
				$(inputSuburb).next('span.alert').hide().next('br').hide();
			} else {
				$(inputCompany).show().prev('label').show();
				$(inputCompany).next('span.alert').show().next('br').show();
				$(inputSuburb).show().prev('label').show();
				$(inputSuburb).next('span.alert').show().next('br').show();
			}	
		} 
	}
	
	function createAccountFormHandling2(elem)
	{
		if ($(elem).hasClass('receiver')) {
			var inputCompany = $(elem).parents('fieldset:first').find('input.inputCompany');
			var inputSuburb = $(elem).parents('fieldset:first').find('input.inputSuburb'); 
			if ($(elem).val() == 'p' ) {
				$(inputCompany).val('').parents('tr:first').hide();
				$(inputSuburb).val('').parents('tr:first').hide();
			} else {
				$(inputCompany).parents('tr:first').show();
				$(inputSuburb).parents('tr:first').show();
			}	
		} 
	}
	
});






