/**
 * @author Ronald
 */

// JavaScript Document
var hover = false;

$(document).ready(function(){

	// Show the dealerlogin on hover
	$('#dealerlogin').hover( function() {
			$('#dealerlogin #logform, #dealerlogin #logout').show();
		},function(){
			$('#dealerlogin #logform, #dealerlogin #logout').hide();
	});

	if ($("#home #imageCycle").length) {


		$("#home #imageCycle").cycle({
			fx: 'fade',
			timeout: 4000,
			speed: 1000,
			pause: 1
		});

		// Show forward and back buttons when hovering over image on homepage
		$("#home #imageCycle").hover(
			function(e){
				$("#btnBack,#btnForward").show();
				hover = true;
			},
			function(){
				$("#btnBack,#btnForward").hide();
				hover = false;
			}
		);
		$("#btnBack,#btnForward").hover(
			function(e){
				$("#btnBack,#btnForward").show();
				//Pause the slideshow
				$("#home #imageCycle").cycle('pause');
				hover = true;
			},
			function(){
//				$("#btnBack,#btnForward").show();
				$("#home #imageCycle").cycle('resume');
			}
		);
		$("#btnBack a").click(function(){
			$("#home #imageCycle").cycle('prev');
		});
		$("#btnForward a").click(function(){
			$("#home #imageCycle").cycle('next');
		});
		
	}
	
	$('#contactVerstuur').click(function(){
		sendContactForm();
		return false;
	});

	$('#loginButton').click(function(){
		loginDealer();
		return false;
	});

	$('#loginButtonForm').click(function(){
		loginDealerForm();
		return false;
	});

	$('#logOutButton').click(function(){
		logoutDealer();
	});

	// Change product selectors when categories change
	$('#categorie').change(function() {
		$('#productselectors').children().hide();
		$('#productselectors ' + this.value).show();
	});
	
	$('#dealerprods .prodsel').change(function(){
		retrieveDealerDownloads();
//		$('#dealerprods').submit();
	});
	
  if ($("#collectionProductGroup").length) {
		$(".productGroup ul li").hover(
		  function(){
				$(this).addClass("hover");
			},function(){
        $(this).removeClass("hover");
			});
  }	
	
	if ($("#collectionProductDetails").length) {
		$("#collectionProductDetails .image").hover(
			function(){
				$(".zoomglass").addClass("active");
			}
			,function(){
				$(".zoomglass").removeClass("active");
			}
		);
		
		$("#collectionProductDetails .thumbs img").click(function(){
			//Get the image_id
			var src = $(this).attr("src");
			var m = /^.*\/(\d+)\_.*\.(.+)$/.exec(src);
			//Hide the images 
			$("#collectionProductDetails a.fancyLink").hide();
			//Show clicked image
			$("#collectionProductDetails a#img_" + m[1]).show();
			/*
		 //Change the image
		 $("#productImage").attr('src', '/images/'+m[1]+'_mw290.'+m[2]);
		 //Change the fancybox link
		 $("#fancyLink").attr('href', '/images/'+m[1]+'_org.'+m[2]);
		 */
			//Highlighting of active thumb
			$("#collectionProductDetails .thumbs li").removeClass('active');
			$(this).parent().addClass('active');
		});
		
		//Show only active product image
		$("#collectionProductDetails a.fancyLink").not(".active").hide();
		
		$("#collectionProductDetails a.fancyLink").fancybox({
			transitionIn: 'elastic',
			transitionOut: 'elastic',
			titlePosition: 'over',
			type: 'image'
		});
	}
});

function loginDealer(){
    var username = $('#username').val();
    var password = $('#password').val();
    var obj = {};
    obj.username = username;
    obj.password = password;
	obj.location = window.location.href;
    $.ajax({
        type: "POST",
        url: "/ajax/loginDealer.php",
        data: obj,
		dataType: 'json',
        success: function(msg){
			if (msg.id == 1) {
				window.location.href = String(msg.link);
            }
            else 
                if (msg.id == 0) {
                    alert(msg.error);
                }
        }
    });
}


function loginDealerForm(){
    var username = $('#usernameForm').val();
    var password = $('#passwordForm').val();
    var obj = {};
    obj.username = username;
    obj.password = password;
	obj.location = window.location.href;

    $.ajax({
        type: "POST",
        url: "/ajax/loginDealer.php",
        data: obj,
		dataType: 'json',
        success: function(msg){
			if (msg.id == 1) {
				window.location.href = String(msg.link);
            }
            else if(msg.id == 0) {
                $('#loginErrorDivForm').html(msg.error);
            }
        }
    });
}

function logoutDealer(){
	var obj = {};
	obj.location = window.location.href;
    $.ajax({
        type: "POST",
        url: "/ajax/logoutDealer.php",
        data: obj,
		dataType: 'json',
        success: function(msg){
			window.location.href = String(msg.link);
		}
    });
}


function checkEmail(email){
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (filter.test(email)) {
		return true;
	}
	else {
		return false;
	}
}

function sendContactForm(){
	var c = true;
	var name = $('#nameValue').val();
	var email = $('#emailValue').val();
	var bericht = $('#berichtValue').val();
	var catalogue = $('#catalogueValue:checked').val() != undefined ? 1:0;
	var errorDiv = $('#contactError'); 
	if(name === "" || email === "" || !checkEmail(email)){
		c = false;
	}
	
	if(c){
		// ajax request	
		//errorDiv.html("");
		var obj = {};
		obj.name = name;
		obj.email = email;
		obj.bericht = bericht;
		obj.catalogue = catalogue;
		$.ajax({
		   type: "POST",
		   url: "/ajax/sendContactFormAjax.php",
		   data: obj,
		   success: function(msg){
			 errorDiv.show();
		     errorDiv.html(msg);
		   }
		 });
		$('#contactForm')[0].reset();
	}else{
		errorDiv.show();
	}
}


