/// JavaScript Document
/// by frizbee.be

	$(document).ready(function(){
		
		/// activate fancybox for pictures
			
			$("a[rel=lightbox]").fancybox({
				'type'				: 'image',
				'padding'			: 10,
				'autoScale'			: true,
				'cyclic'			: true,
				'overlayOpacity'	: 0.7,
				'overlayColor'		: '#000000',
				'transitionIn'		: 'fade',
				'transitionOut'		: 'fade',
				'titlePosition' 	: 'over',
				'titleShow'			: false,
				'resize'			: 'Auto'
			});

		/// validates the newsletter signup form on homepage and posts the form with ajax
			
			$('#frmhomenewsletter').validate({
				errorClass: 'invalid',
				submitHandler: function(form){
					
					$('#home-newsletter-signup-formcontainer').hide();
					$('#home-newsletter-signup-formcontainer').parent().append('<div id="newsletter-feedback">' + $('input#newsletter-sending').val() + '</div>');
					
					var name = $('input#newsletter-name').val();
					var email = $('input#newsletter-email').val();
					var language = get_language();
					var dataString = 'name='+ name + '&email=' + email + '&language=' + language;
					
					$.ajax({  
						
						type: "POST",  
						url: "../_pages/home/process-newsletter-signup.php",  
						data: dataString,  
						
						success: function(data) {  
							
							if(data == 'ok') $('#newsletter-feedback').html($('input#newsletter-feedback-success').val());
							else $('#newsletter-feedback').html($('input#newsletter-feedback-fail').val());
							
							$('#newsletter-feedback').hide();
							$('#newsletter-feedback').fadeIn(1500);
							 
						},
						
						error: function() {
							
							$('#newsletter-feedback').html($('input#newsletter-feedback-fail').val())
							.hide()
							.fadeIn(1500);	
						}
						
					});  
					
					return false;
					
				}
			});
			
		/// validates the contactform and posts the form with ajax
			
			$('#frmcontactform').validate({
				errorClass: 'invalid',
				submitHandler: function(form){
					
					$('#contact-formcontainer').hide();
					$('#contact-formcontainer').parent().append('<div id="contactform-feedback">' + $('input#contactform-sending').val() + '</div>');
					
					var name = $('input#contactform-name').val();
					var email = $('input#contactform-email').val();
					var telephone = $('input#contactform-telephone').val();
					var message = $('textarea#contactform-message').val();
					var language = get_language();
					var dataString = 'name='+ name + '&email=' + email + '&telephone=' + telephone + '&message=' + message + '&language=' + language;
					
					$.ajax({  
						
						type: "POST",  
						url: "../_pages/contact/process-contactform.php",  
						data: dataString,  
						
						success: function(data) {  
							
							if(data == 'ok') $('#contactform-feedback').html($('input#contactform-feedback-success').val());
							else $('#contactform-feedback').html($('input#contactform-feedback-fail').val());
							
							$('#contactform-feedback').hide()
							$('#contactform-feedback').fadeIn(1500);
							 
						},
						
						error: function() {
							
							$('#contactform-feedback').html($('input#contactform-feedback-fail').val())
							.hide()
							.fadeIn(1500);	
						}
						
					});  
					
					return false;
					
				}
			});
			
		/// activate textfields with a textholder and visually shows invalid fields
			
			$('.holding input, .holding textarea').each(function()
			{
				/// if not empty on laod, then hide textholder
					if($(this).val() != "") $(this).parent().children().last().hide();
			});
			
			$('.holding input, .holding textarea').keydown(function()
			{
				/// hide textholder when typing
					if($(this).val() == "") $(this).parent().children().last().hide();
			});
			
			$('.holding input, .holding textarea').blur(function()
			{
				/// when loosing focus, if textfield is invalid then make textholder also invalid and show textholder if textfield is empty
					if($(this).hasClass('invalid')) $(this).parent().children().last().addClass('invalid');
					if($(this).val() == "") $(this).parent().children().last().fadeIn();
			});
						
			$('.holding span').mouseup(function()
			{
				/// when clicking on the textholder, set focus to textfield
					$(this).parent().children().first().focus();
			});
			
			$('.siteform').submit(function()
			{
				/// when submitting the form, make all textholders of the required textfields invalid
				$(this).children('.holding').each(function(index){
					if($(this).children().first().hasClass('invalid')) $(this).children('.holder').first().addClass('invalid');
				});
			});
			
		/// products list rollover change image
			
			$('a.products-list-thumb').mouseover(function(){
				$(this).children('.thumb').hide();
				$(this).children('.thumb-rollover').css({'visibility' : 'visible'});
			});
			
			$('a.products-list-thumb').mouseout(function(){
				$(this).children('.thumb').show();
				$(this).children('.thumb-rollover').css({'visibility' : 'hidden'});
			});
			
		/// activate product detail image zoom
			
			$('a.zoom').jqzoom({
				zoomType: 'innerzoom'
			});
			
		/// make twitter links open in a new window
			
			$('#twitter_update_list li span a').attr('target', '_blank');
			$('#twitter_update_list li a').attr('target', '_blank');
			
		/// activate pngfix
			
			$(document).pngFix();
			
		/// activate sharethispage button
			$('#sharethispage-button').click(function()
			{
				var $icons = $('#sharethispage-container');
				if($icons.is(':visible')) $icons.slideUp('fast');
				else $icons.slideDown('fast');
			});
			
			$('#sharethispage-container #close').click(function()
			{
				$(this).parent().slideUp('fast');
			});
			
		/// activate tooltip
			
			var newTextBoxDiv = $(document.createElement('div')).attr("id", 'tooltip');
			$('body').append(newTextBoxDiv);
			$('#tooltip').html("");
			$('#tooltip').hide();
			
			$('.showhovertext').click(function(event)
			{
				if($(this).attr('href') == '') return false;
			});
			
			$('.showhovertext').mousemove(function(e)
			{
				if($(this).attr('alt'))
				{
					$("#tooltip").html($(this).attr('alt'));
					$("#tooltip").show();
					$("#tooltip").css({
					top: (e.pageY + 10) + "px",
					left: (e.pageX + 10) + "px"
					});
				}
			});
		
			$('.showhovertext').mouseout(function(e)
			{
				$("#tooltip").hide();
			});
			
		/// get the request prefix from the hidden input field in the footer
			
			function request_prefix()
			{
				return $('#request_prefix').val();
			}
						
		/// get the current language from the hidden input field in the footer
			
			function get_language()
			{
				return $('#language').val();
			}
						
	});

/// make an email link

	function make_email_link(user, host, subject)
	{
		document.write('<a href="mailto:' + user + '@' + host + '?subject=' + subject + '">' + user + '@' + host + '</a>');	
	}

