var fadespeed = 'fast';
var slidespeed = 'slow';

$(function() {
	$('.ac-container').hide();
	$('.border-1').hide();
	$('.border-2').hide();
	$('.border-3').hide();
	
	$('.btn-close a').click(function(){
		closeAll();
		return false;
	});
	
	$('h1').click(function(){
		closeAll();
		return false;
	});
	
	//Contact
	$('#btn-contact').click(function(){
		if($('#contact .ac-container').is(':hidden') ) {
			closeAll('contact');
			$('#contact-form').show();
			$('.border-1').show();
			$(this).addClass('active');
			$('#contact .ac-container').slideDown(slidespeed);
		}
		return false;
	});
	
	$('#contact-form #form1').submit(function(){
		$('#contact-form #form1 input.submit').attr('disabled', 'true');
		$(this).ajaxSubmit({
			type: 'POST',
			url: 'app/mailer/sendmail.php',
			beforeSubmit: validateContact,
			success: function(res){
				$('#contact-form #form1').hide().after(res);
				$('#contact-form #form1 input.submit').removeAttr('disabled');
				$('#contact-form button.back').click(function(){
					$('#contact-form #form2').remove();
					$('#contact-form #form1').show();
					return false;
				});
				$('#contact-form #form2').submit(function(){
					$(this).ajaxSubmit({
						type: 'POST',
						url: 'app/mailer/sendmail.php',
						success: function(res){
							$('#contact-form #form2').after('<div id="form3">'+res+'</div>').remove();
							$('#contact-form #form1').clearForm();
						}
					});
					return false;
				});
			}
		});
		return false;
	});

	
	//News
	$('#news .trigger li').click(function(){
		var article = $(this).attr('title');
		if( $(this).parentsUntil('#news').next().is(':hidden') ) {
			closeAll('news');
			$('.border-2').show();
			$(this).addClass('active');
			$('#news-container').load('data/news.html #'+article, null, function(){
				$('#news .ac-container').slideDown(slidespeed);
			});
		} else {
			if( $(this).parent().hasClass('active') == false ) {
				$('#news .active').removeClass('active');
				$(this).addClass('active');
				$('#news .ac-container').fadeOut(fadespeed, function(){
					$('#news-container').load('data/news.html #'+article, null, function(){
						$('#news .ac-container').fadeIn(fadespeed);
					});
				});
			}
			
		}
		return false;
	});
	
	//Brand
	
	$('#brand').tabs({
		fx: { opacity: 'toggle', duration: fadespeed},
		select: function(event, ui){
			$('#brand .ac-container').fadeOut(fadespeed);
			closeAll();
		}
	});

	$('#brand .trigger a').click(function(){
		var brand_id = $(this).attr('title');
		
		if($(this).hasClass('active')) {
			return false;
		}
		else {
			brandTriggerBtnToggle(this);
		}
		
		if( $('#brand .ac-container').is(':hidden') ) {
			closeAll('brand')
			$('.border-3').show();
			$(this).addClass('active');
			$('#brand .ac-container').append('<a class="prev disabled">prev</a><a class="next disabled">next</a><div id="brand-container" class="block"></div>');
			$('#brand-container').load('data/brands.html #'+brand_id, null, function(){
					$("#brand-container").scrollable();
				$('#brand .ac-container').slideDown(slidespeed, function(){
					$('#brand .ac-container').css('position', 'static');
				});
			});
		} else {
			
			$('#brand .trigger .active').removeClass('active');
			$(this).addClass('active');
			$('#brand .ac-container').css('position', 'relative').fadeOut('fast', function(){
				$('#brand-container').remove();
				$('#brand .prev').remove();
				$('#brand .next').remove();
				$('#brand .ac-container').append('<a class="prev disabled">prev</a><a class="next disabled">next</a><div id="brand-container" class="block"></div>');
				$('#brand-container').load('data/brands.html #'+brand_id, null, function(){
					$('#brand .ac-container').fadeIn(fadespeed, function(){
						$('#brand .ac-container').css('position', 'static');
						$("#brand-container").scrollable();
					});
				});
			});
		}
		return false;
	});


	//Skimmer
	
	$(".skimmer").each(function(){
		$(this).css("background", "url('images/"+$(this).attr('title')+"/roll.jpg')");
	});
	
	$(".skimmer").mousemove(function(e) {
		if($(this).hasClass('active') == false) {
			$(this.firstChild).css('visibility', 'hidden');
	        var x = e.pageX;
	        var w = 10;
	        var offset = $(this).offset().left;
	        var image = Math.floor((x - offset) / w);
	        $(this).css("background-position", "0px " + (-80 * image) + "px");
        }
    }).mouseout(function() {
		$(this.firstChild).css('visibility', 'visible');
    });
    /*
	$("h1").mousemove(function(e) {
		if($(this).hasClass('active') == false) {
			$(this).css("background", "url('images/logo_roll.jpg')");
			$(this.firstChild).css('visibility', 'hidden');
	        var x = e.pageX;
	        var w = 10;
	        var offset = $(this).offset().left;
	        var image = Math.floor((x - offset) / w);
	        $(this).css("background-position", "0px " + (-120 * image) + "px");
        }
    }).mouseout(function() {
		$(this.firstChild).css('visibility', 'visible');
		$(this).css("background", "none");
    });
    */

});

function brandTriggerBtnToggle(t) {
	var active_bname = $("#brand .trigger .active").attr('title');

	$("#brand .trigger a.active img").attr('src', 'images/'+active_bname+'/'+active_bname+'_off.png');
	
	if(t) {
		var bname = $(t).attr('title');

		$(t.firstChild).attr('src', 'images/'+bname+'/'+bname+'_on.png');
	}
}

function closeAll(t) {
	if(t != 'contact') {
		$('#contact .ac-container').slideUp(slidespeed, function(){
			$('.border-1').hide();
			$('#contact-form #form2').remove();
			$('#contact-form #form3').remove();
			$('#contact-form #form1').show();
		});
		$('#btn-contact').removeClass('active');
	}
	if(t != 'news') {
		$('#news .trigger').next().slideUp(slidespeed, function(){
			$('.border-2').hide();
		});
		$('#news li.active').removeClass('active');
	}
	if(t != 'brand') {
		$('#brand .ac-container').css('position', 'relative').slideUp(slidespeed, function (){
			$('#brand .trigger a').removeClass('active');
			$('#brand .trigger .active').removeClass('active');
			$('#brand-container').remove();
			$('#brand .prev').remove();
			$('#brand .next').remove();
			$('.border-3').hide();
		});
	}
	brandTriggerBtnToggle();
}

function callSuccessFunction(){
	alert('suc');
}

function validateContact(formData, jqForm, options){
	var errorMessage = '';
	var form = jqForm[0];
	
	if(form.Name.value.length == 0){
		errorMessage += "Name is required\n";
		$('#contact-form #form1 #input-name').addClass('alert');
	}else{
		$('#contact-form #form1 #input-name').removeClass('alert');
	}
	if(form.Email.value.length == 0){
		errorMessage += "E-mail is required\n";
		$('#contact-form #form1 #input-email').addClass('alert');
	}else{
		$('#contact-form #form1 #input-email').removeClass('alert');
	}
	if(form.Subject.value.length == 0){
		errorMessage += "Subject is required\n";
		$('#contact-form #form1 #input-subject').addClass('alert');
	}else if(form.Subject.value.length > 20){
		errorMessage += "Do not enter more than 20 characters'\n";
		$('#contact-form #form1 #input-subject').addClass('alert');
	}else{
		$('#contact-form #form1 #input-subject').removeClass('alert');
	}
	if(form.Message.value.length == 0){
		errorMessage += "Message is required\n";
		$('#contact-form #form1 #input-message').addClass('alert');
	}else{
		$('#contact-form #form1 #input-message').removeClass('alert');
	}
	
	if(errorMessage.length > 0){
		alert(errorMessage);
		$('#contact-form #form1 input.submit').removeAttr('disabled');
		return false;
	}
}
