// JavaScript Document

$(document).ready(function(){

	// Get the source directory of this script
	scriptSrc = $('script[@src$=jquery.js]').attr('src').match(/^((?:\.\.\/)*)/)[1];

	if(window.location.href.match('luna')){
		var dom = '/scripts/getimages.php';
	}else{
		dom = '/scripts/getimages.php';
	}

	// Function to reposition the images of the girls
	function PosGirl(){
		var widthPos = $('body').width() - 1235;
		if(widthPos < 0){
			$('#container').css('background-position','900px 153px');
		}else{
			$('#container').css('background-position','right 153px');
		}
	}

	// Put the image of the girls in the right spot on load
	PosGirl();

	// Put the girl in the right spot on resize
	$(window).resize( function() {
		PosGirl();
	} );

	// Show the dealerlogin on hover
	$('#dealerlogin').hover( function() {
			$('#dealerlogin #logform, #dealerlogin #logout').show();
		},function(){
			$('#dealerlogin #logform, #dealerlogin #logout').hide();
	});

	// Show the transparent border on hover
	$('.productselection a').hover( function() {
			$(this).attr('class', 'over');
		},function(){
			$(this).attr('class', 'out');
	});

	$('#dealerprods .prodsel').change(function(){
		$('#dealerprods').submit();
	});
	
	$('.productview .image').hover(function(){
			$('span', this).show();
		},function(){
			$('span', this).hide();
	});

	// menu hover function
	$('.hoverImage').hover(
		// Mouseover function
		function(){
			if(!this.src.match('_over.gif') && !this.src.match('_over.jpg') && !this.src.match('_active')){
				this.src = this.src.replace(/.gif/, "_over.gif");
				this.src = this.src.replace(/.jpg/, "_over.jpg");
			}
		},
		// Mouseout function
		function(){
			if(this.src.match('_over.gif') || this.src.match('_over.jpg')){
				this.src = this.src.replace(/_over.gif/, ".gif");
				this.src = this.src.replace(/_over.jpg/, ".jpg");
			}
		}
	);

	$('h1, h5, .search_btn a').flash(
	{
	    src: scriptSrc + '/js/metamedium.swf',
	    flashvars:{
		css: [
		    '* { color: #373836; }',
		    'a { color: #373836; text-decoration: none; }',
		    'a:hover { text-decoration: underline; }'
		].join(' ')
	    }
	},
	{ version: 7 },
	function(htmlOptions) {
	    htmlOptions.flashvars.txt = encodeURI(this.innerHTML);
	    this.innerHTML = '<div>'+this.innerHTML+'</div>';
	    var $alt = $(this.firstChild);
	    htmlOptions.height = $alt.height();
	    htmlOptions.width = $alt.width();
	    htmlOptions.wmode = 'transparent';
	    $alt.addClass('alt');
	    $(this)
		.addClass('flash-replaced')
		.prepend($.fn.flash.transform(htmlOptions));
	}

	);
	
	$('h2').flash(
	{
	    src: scriptSrc + '/js/metamedium.swf',
	    flashvars:{
		css: [
		    '* { color: #FE850C; }',
		    'a { color: #FE850C; text-decoration: none; }',
		    'a:hover { text-decoration: underline; }'
		].join(' ')
	    }
	},
	{ version: 7 },
	function(htmlOptions) {
	    htmlOptions.flashvars.txt = encodeURI(this.innerHTML);
	    this.innerHTML = '<div>'+this.innerHTML+'</div>';
	    var $alt = $(this.firstChild);
	    htmlOptions.height = $alt.height();
	    htmlOptions.width = $alt.width();
	    htmlOptions.wmode = 'transparent';
	    $alt.addClass('alt');
	    $(this)
		.addClass('flash-replaced')
		.prepend($.fn.flash.transform(htmlOptions));
	}

	);

	$('h4').flash(
	{
	    src: scriptSrc + '/js/metamedium.swf',
	    flashvars:{
		css: [
		    '* { color: #FFFFFF; }',
		    'a { color: #FFFFFF; text-decoration: none; }',
		    'a:hover { text-decoration: underline; }'
		].join(' ')
	    }
	},
	{ version: 7 },
	function(htmlOptions) {
	    htmlOptions.flashvars.txt = encodeURI(this.innerHTML);
	    this.innerHTML = '<div>'+this.innerHTML+'</div>';
	    var $alt = $(this.firstChild);
	    htmlOptions.height = $alt.height();
	    htmlOptions.width = $alt.width();
	    htmlOptions.wmode = 'transparent';
	    $alt.addClass('alt');
	    $(this)
		.addClass('flash-replaced')
		.prepend($.fn.flash.transform(htmlOptions));
	}

	);

	$('h6').flash(
	{
	    src: scriptSrc + '/js/metamedium.swf',
	    flashvars:{
		css: [
		    '* { color: #FE850C; }',
		    'a { color: #FE850C; text-decoration: none; }',
		    'a:hover { text-decoration: underline; }'
		].join(' ')
	    }
	},
	{ version: 7 },
	function(htmlOptions) {
	    htmlOptions.flashvars.txt = encodeURI(this.innerHTML);
	    this.innerHTML = '<div>'+this.innerHTML+'</div>';
	    var $alt = $(this.firstChild);
	    htmlOptions.height = $alt.height();
	    htmlOptions.width = $alt.width();
	    htmlOptions.wmode = 'transparent';
	    $alt.addClass('alt');
	    $(this)
		.addClass('flash-replaced')
		.prepend($.fn.flash.transform(htmlOptions));
	}

	);

	// Change product selectors when categories change
	$('#categorie').change(function() {
		$('#productselectors').children().hide();
		$('#productselectors ' + this.value).show();
	});
	
	/*$('.blackcard').flash({
		src: scriptSrc +'/swf/kaart.swf',
		width: 180,
		height: 200,
		wmode: 'transparent',
		flashvars: { testdirectory: 'false', achtergrondKleur: 'black', locationstring:String(window.location)}
	});*/

	/*$('.whitecard').flash({
		src: scriptSrc +'/swf/kaart.swf',
		width: 180,
		height: 200,
		wmode: 'transparent',
		flashvars: { testdirectory: 'false', achtergrondKleur: 'white', locationstring: String(window.location)}
	});*/

	$('#slideshow').flash({
		src: scriptSrc +'/swf/produkt_ani.swf',
		width: 309,
		height: 316,
		wmode: 'transparent',
		flashvars: {url: dom}
	});

	$('#header').flash({
		src: scriptSrc +'/swf/header.swf',
		width: 153,
		height: 74,
		wmode: 'transparent'
	});

	if($('.productview .image a').length){
		$(function() {
			$('.productview .image a').lightBox();
		});
	}

	if($('#mycarousel').length){
		jQuery('#mycarousel').jcarousel({
			orientation: "vertical",
			itemVisible: 3,
			itemScroll: 3,
			itemStart: $('#mycarousel').next('div').html(),
			wrap: false
		});
	}

	$('.productview img').bind("contextmenu",function(){
		return false;
	});

	
	$('#contactVerstuur').click(function(){
		sendContactForm();
		return false;
	});

	$('#loginButton').click(function(){
		loginDealer();
		return false;
	});

	$('#loginButtonForm').click(function(){
		loginDealerForm();
		return false;
	});

	$('#logOutButton').click(function(){
		logoutDealer();
	});
});

function loginDealer(){
    var username = $('#username').val();
    var password = $('#password').val();
    var obj = {};
    obj.username = username;
    obj.password = password;
	obj.location = window.location;
    $.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;
    
    $.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;
    $.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 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;
		$.ajax({
		   type: "POST",
		   url: "/ajax/sendContactFormAjax.php",
		   data: obj,
		   success: function(msg){
			 errorDiv.show();
		     errorDiv.html(msg);
		   }
		 });
		$('#contactForm')[0].reset();
	}else{
		errorDiv.show();
	}
}

// check the value of an input field and empty it if it's the original
function checkInput(item, check){
	if(item.value == check){
		item.value = '';
	}
}