// JavaScript Document
var setHeight = function() {
	var windowHeight = $(window).height();
	var bodyHeight = $('body').height();
	var lastSectionPosition = $('#hirek').offset();
	if ( lastSectionPosition ) {
		if ( bodyHeight < windowHeight ) {
			var newHeight = windowHeight+lastSectionPosition.top-143;
			$('body').height(newHeight); 
		} else {
			var windowBodyDifference = bodyHeight-windowHeight;
			var newHeight = windowHeight+lastSectionPosition.top-143;
			$('body').height(newHeight);
		}
		return windowBodyDifference;
	}
	if ( bodyHeight < windowHeight ) {
			//$('#footer').addClass('fixed');
	}
}

$(function(){
	/*var windowBodyDifference = setHeight();
	$(window).resize(function() {
		windowBodyDifference = setHeight();
	});

	$(window).scroll(function() {
		if ( $(window).scrollTop() > (windowBodyDifference + 80) ) {
			$('#footer').addClass('fixed');
		} else {
			$('#footer').removeClass('fixed');
		}
		if ( $('#filters form').size() > 0 ) {
			$.cookie('scroll', $(window).scrollTop() );
			document.location.hash = $(window).scrollTop();
		}
	});*/
    
	Cufon.replace('#events-container h2, #brands-container h2, #services-container h2, #contact-container h2, .collections h2, h1, #education-container h2, #brand-details-container h2, #product-details-container h2, #event-details-container h2, .related-list h2, #services');	
    
	/* Contact/Newsletter form */
    $('#contact-form input:text, #newsletter-form input:text, #footer-newsletter-form input:text').each(function(index) {
        if($(this).val() != '') {
            $(this).prev().hide();
        }
    });
    $('#contact-form label span, #newsletter-form label span, #footer-newsletter-form label span')
        .click(function() {
            $(this).fadeOut(500);
            $(this).next().focus();
        });
    $('#contact-form label input:text, #newsletter-form label input:text, #footer-newsletter-form input:text')
        .blur(function() {
            if ( $(this).val() == '' ) {
                $(this).prev().fadeIn(500);
            }
        });
    //Validate form
    /*$('#contact-form').parent().validate({
        debug: true,
        /*errorLabelContainer: $("#contact-form .validation-summary"),
		wrapper: 'li',* /
        errorElement: 'b',
        errorClass: "validation-error",
		rules: {
			'message-name' : "required",
			'message-email' : {
                required: true,
                email: true
            },
			'message-subject': "required"
		},
        messages: {
			'message-name' : "",
			'message-email' : {
                required: "",
                email: ""
            },
			'message-subject': ""
		}
    });*/
	
	$('#subscribe-form').ajaxForm({success: showFeedback});
	function showFeedback(responseText, statusText, xhr, $form)  { 
 	    alert(responseText);
		return false;
	} 
	
	$('#myform').ajaxForm({beforeSubmit: validate});

	function checkMail(email) {
	   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	   var address = email;
	   if(reg.test(address) == false) {
		  return false;
	   }
	   return true;
	}
	
	function validate(formData, jqForm, options) {
		var message = $('textarea[name=message-text]').fieldValue();
		var name = $('input[name=message-name]').fieldValue();
		var email = $('input[name=message-email]').fieldValue();
		var subject = $('input[name=message-subject]').fieldValue();
 
 		if ( message == '' || name == '' || email == '' || subject == '' ) {
			alert('Kérjük, töltsd ki az össes mezőt!');
			return false;
		}
		if ( !checkMail( email ) ) {
			alert('Kérjük, ellenőrizze a megadott e-mail címet!');
			return false;
		}
		alert('Köszönjük érdeklődését. Üzenete sikerrel beérkezett hozzánk, hamarosan felkeressük az Ön által megadott elérhetőségen.');
		return true;
	}
	
	$('#search-header a')
	.hide()
	.click(function(){
		$('#filters form').stop().animate({marginTop: 0, marginBottom: 0}, 600, 'easeOutQuart', function(){
			$('#search-header a').fadeOut();
		});
		return false;
	});
	$('#filters form').ajaxForm({
		dataType: 'json',
		success: displaySearchResults
	});
	function displaySearchResults( data ) {
		if ( $('#filters input:checked').length == 0 ) {
			if ( data.lang == 'en' ) {
				alert('For search result please mark one or more characteristic!');
			} else {
				alert('A kereséshez kérjük, adj meg legalább egy feltételt!');
			}
			return false;
		}
		window.location.hash = $("#filters form").serialize();			
								
		$('.product-list').empty();
		$('.product-list').append($('<ul />'));
		$.each(data.products, function(i, product){
			$('.product-list ul').append('<li id="' + product.id + '"' + ( !(i%4)?' class="row"':'' ) + '><a href="?q=' + data.path + '&amp;id=' + product.id + '"><img src="' + product.path_small + '" alt="' + product.name + '" /><strong>' + product.name + '</strong><span>' + product.details + '</span></a></li>');
			if ( (i+1) == data.number ) {
				if ( $('#filters input:checked').length > 0 ) {
					//alert( 'aaa' );
					$.scrollTo( $.cookie('scroll'), 0 );
				}
			}
		});
		init_pager(10000);
		//alert( $('#filters form>ul').height() );
		$('#filters form').animate({marginTop: '-' + ($('#filters form>ul').height() ) +'px', marginBottom: -70}, 600, 'easeOutQuart', function() {
			$('#search-header a').fadeIn();
			$('#search-submit').addClass('filter');		
																								
		});	
	}

	if ( $('#filters input:checked').length > 0 ) {
		$('#filters form').submit();
	}
	
	
	 $('#filters form input:checkbox').click(function(){
		if ( $('#filters input:checked').length == 0 ) {
				$('#filters input:checkbox')
				.removeAttr('disabled')
				.parent().css({color: '#6a6360'});

				$('#number-of-results-number').text(0);
				return true;
		}
     	$.post("includes/search.php", $("#filters form").serialize(), function(data){
				$('#number-of-results-number').text(data.number);
				$('#filter-applications label, #filter-types label, #filter-brands label, #filter-materials label, #filter-styles label').css({color: '#cbc7c2'});
				$('#filter-applications input, #filter-types input, #filter-brands input, #filter-materials input, #filter-styles input').attr('disabled', true);
				$.each(data.options.applications, function(i, application){
				$('#filter-applications input[value=' + application.application_id + ']')
					.removeAttr('disabled')
					.parent().css({color: '#6a6360'});
				})
				$.each(data.options.types, function(i, type){
					$('#filter-types input[value=' + type.type_id + ']')
					.removeAttr('disabled')
					.parent().css({color: '#6a6360'});
				})
				$.each(data.options.brands, function(i, brand){
					$('#filter-brands input[value=' + brand.b_and_d_id + ']')
					.removeAttr('disabled')
					.parent().css({color: '#6a6360'});
				})
				$.each(data.options.materials, function(i, material){
					$('#filter-materials input[value=' + material.material_id + ']')
					.removeAttr('disabled')
					.parent().css({color: '#6a6360'});
				})
				$.each(data.options.styles, function(i, style){
					$('#filter-styles input[value=' + style.style_id + ']')
					.removeAttr('disabled')
					.parent().css({color: '#6a6360'});
				})

			}
		, 'json');
	 });
	 
	$('#filter-reset').click(function(){
		$('#filters :checkbox').removeAttr('checked');
		$('#filters :checkbox').change();
		return false;
	});
	$('#search-submit').click(function(){
		$('#filters form').submit();
		$.cookie('scroll', '0');
		return false;
	});
	 
	//collection
	$('#brand-poster li').css({opacity: 0});

	if ( !$('.current-brand').length ) {
		$('#brand-poster li:lt(1)').css({opacity: 1});
	} else {
		$('#brand-poster li.current-brand').css({opacity: 1});
	}
	/*$('#collection_brands a').mouseenter(function(){
			var $current_image = $('#poster_no_' + $(this).attr('rel'));
			$('#brand-poster li').stop(true, true).hide();
			$current_image.fadeIn(1500);
			//$('#brand-poster').stop().scrollTo( $current_image, 900, {offset: -3, easing:'easeOutQuart'});
	});*/
	$('#collection_brands a')
	.mouseenter(function(){
		//alert('a');
		var $current_image = $('#poster_no_' + $(this).attr('rel'));
		$(this).animate({width: '210px'}, 300, 'linear', function(){
			$current_image.css({'z-index':3});
			$current_image.animate({opacity:1}, 600, function(){
				$('#brand-poster li').css({opacity: 0});
				$current_image.css({opacity: 1, 'z-index': 2});
			});
		})
	})
	.mouseleave(function(){
		var $current_image = $('#poster_no_' + $(this).attr('rel'));
		$(this).stop().css({width: '215px'});
	});
});

$(window).load(function(){  
	$.localScroll.hash({duration: 1200, offset: -143, hash: true, easing: 'easeOutQuart'});
	$.localScroll({duration: 1200, offset: -143, hash: true, easing: 'easeOutQuart'});
		$('#index-gallery').nivoSlider({
			effect: 'sliceDown',
			slices: 1,
			animSpeed: 1500,
			pauseTime: 6000,
			controlNav: false,
			pauseOnHover: true
		});
});
