attractive = {
	init: function() {
		this.font();
		this.carousel();	
		this.gallery();
		this.form();
		this.panel();
		this.imageReplace();
		this.scroll();
	},
	
	carousel: function() {
		$('.newness, .gallery').carousel({
			loop: true,
			effect: 'fade'
		});
		
		$('.newness').delegate('.carousel-previous', 'click' ,function(){
			$('.gallery .carousel-previous').click();
		});
		
		$('.newness').delegate('.carousel-next', 'click' ,function(){
			$('.gallery .carousel-next').click();
		});
	},
	
	gallery: function() {
		$('.gallery').delegate('.miniatures img', 'click', function() {
			var _srcreplace = $(this).attr('src').replace('small', 'big');
			$(this).parent().parent().find('.main img').attr('src', _srcreplace);
		});
	},
	
	form: function() {
		jQuery.extend(jQuery.validator.messages, {
			required: "wpisz brakujące słowa",
			email: "wpisz poprawny e-mail"
		});

		$("#contact-form").validate({
			errorContainer: "#message-box",
			errorPlacement: function(error, element) {
				error.appendTo(element.parent());
			},
			highlight: function(element, errorClass, validClass) {
				$(element).addClass(errorClass).removeClass(validClass);
				$(element.form).find("label[for=" + element.id + "]").addClass('error-label');
			},
			unhighlight: function(element, errorClass, validClass) {
				$(element).removeClass(errorClass).addClass(validClass);
				$(element.form).find("label[for=" + element.id + "]").removeClass('error-label');
			}
		});
	},
	
	panel: function() {
		var _hgroupLinks = $('.hgroup a'),
			_pgroupPanel = $('.pgroup .panel');
		
		_pgroupPanel.css({
			position: 'absolute',
			left: '-666em'
		});
		
		_hgroupLinks.bind('click', function(e) {
			var _hash = $(this).attr('href');
			_hgroupLinks.removeClass('active');
			$(this).addClass('active');
			_pgroupPanel.css({
				position: 'absolute',
				left: '-666em'
			});
			$(_hash).css({
				position: 'static'
			});
			
			attractive.font();
			
			e.preventDefault();
		}).first().click();
	},
	
	font: function() {
		Cufon.replace('.replace', {
			hover: true,
			separate: 'words'
		});
	},
	
	imageReplace: function() {
		var imageArray = [];
		
		$("img.image-replace")
			.mouseover(function() { 
				var src = $(this).attr("src").match(/[^\.]+/) + "_over.png";
				$(this).attr("src", src);
			})
			.mouseout(function() {
				var src = $(this).attr("src").replace("_over", "");
				$(this).attr("src", src);
			});
		
		$("img.image-replace").each(function(){
			imageArray.push($(this).attr("src").match(/[^\.]+/) + "_over.png");
		});
		
		$(imageArray).preload();
	},
	
	scroll: function() {
	    $('.wrapper:first').append('<img class="scroll-button top-scroll" src="img/layout/buttons/do-gory.png" width="66" height="34" alt="Do góry" />');
	    
	    $('#header .buttons a:first').bind('click', function(e){
	        $(window).scrollTo($('.contact-section'), 400);

	        e.preventDefault();
	    });
	    
	    $('.top-scroll').bind('click', function(e){
	        $(window).scrollTo($('.wrapper:first'), 400);

	        e.preventDefault();
	    });
	}
}

$.fn.preload = function() {
    this.each(function(){
        $('<img/>')[0].src = this;
    });
}

$(function(){
	attractive.init();
});
