$(document).ready(function() {

	//Set Default State of each portfolio piece
	$(".paging").show();
	$(".paging a:first").addClass("active");
		
	//Get size of images, how many there are, then determin the size of the image reel.
	var imageWidth = $(".window").width();
	var imageSum = $(".image_reel img").size();
	var imageReelWidth = imageWidth * imageSum;
	
	//Adjust the image reel to its new size
	$(".image_reel").css({'width' : imageReelWidth});
	
	//Paging + Slider Function
	rotate = function(){	
		var triggerID = $active.attr("rel") - 1; //Get number of times to slide
		var image_reelPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide

		$(".paging a").removeClass('active'); //Remove all active class
		$active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)
		
		//Slider Animation
		$(".image_reel").animate({ 
			left: -image_reelPosition
		}, 500 );
		
	}; 
	
	// MENU
	$('.dropdown').each(function () {
		$(this).parent().eq(0).hover(function () {
			$('.dropdown:eq(0)', this).show();
			$('a', this).addClass('active');
		}, function () {
			$('.dropdown:eq(0)', this).hide();
			$('a', this).removeClass('active');
		});
	});
			

	// SHOP - ORDER - TOGGLE
	$("#orderlk").click(function() {
		$('#orderform').animate({height: "toggle"}, 1000); 
		$(this).blur(); 
		return false;
	});
	
	//Rotation + Timing Event
	rotateSwitch = function(){		
		play = setInterval(function(){ //Set timer - this will repeat itself every 3 seconds
			$active = $('.paging a.active').next();
			if ( $active.length === 0) { //If paging reaches the end...
				$active = $('.paging a:first'); //go back to first
			}
			rotate(); //Trigger the paging and slider function
		}, 7000); //Timer speed in milliseconds (3 seconds)
	};
	
	rotateSwitch(); //Run function on launch
	
	//On Hover
	$(".image_reel a").hover(function() {
		clearInterval(play); //Stop the rotation
	}, function() {
		rotateSwitch(); //Resume rotation
	});	
	
	//On Click
	$(".paging a").click(function() {	
		$active = $(this); //Activate the clicked paging
		//Reset Timer
		clearInterval(play); //Stop the rotation
		rotate(); //Trigger rotation immediately
		rotateSwitch(); // Resume rotation
		this.blur();
		return false; //Prevent browser jump to link anchor
	});	
	
	
	// MORPH IMAGES 
	$(".thumbs").scrollable().click(function() {
		$(this).data("scrollable").next();	
	});

	$("#advanced input").click(function() {
		
		$('#advancedsearch').animate({height: 'toggle'}, 1000);
		
		this.blur();
		return false;
		
	});
	
		$("#reset input").click(function() {
	
				$('#advancedsearch').animate({height: 'toggle'}, 1000);
	
				$(':input','.morph-search')
				.not(':button, :submit, :reset, :hidden')
				.val('')
				.removeAttr('checked')
				.removeAttr('selected');

				// RESET SESSION
				
				var values = 'reset=yes';
				$.ajax({
						type: "GET",
						url: jspath + "ajax-files/quicksearch.php",
						data: values
				});

				quickSearch();
		
		});
	
	// CART
	$("#cart_add").click(function() {

	    var cart			= $('#cart');
	    var cartwrapper		= $('#cartwrapper');
		var amount	 		= $('#product_amount').val();
		var product_id 		= $('#product_id').val();
		var loader 			= $('#amountloader');
		var values			= '';
		
		if (is_int(amount) && amount != '0') {
			
			values += 'pid=' + product_id;
			values += '&amount=' + amount;
						
			// GET CONTENTs
			$.ajax({
				type: "GET",
				url: jspath + "ajax-files/shop.php",
				data: values,
				beforeSend: function(msg){
					loader.html('<img src="' + jspath + 'gfx/loader.gif" style="height: 15px;padding: 0;" alt="Loader..." />');
				},
				success: function(msg){
					loader.html('');
					cart.html(msg);
					cart.show();
					cartwrapper.show("fade");
				}
			});
			
		} 
		else {
			alert('You have to write the amount of products you want to purchase.');
				
		}
		
		$(this).blur();
		
		return false;
		
	});
	
	$("#orderform .delete").click(function() {
		
		var cart			= $('#cart');
		var cartwrapper		= $('#cartwrapper');
		var values			= '';
		var product_id		= $(this).attr('id');

		if (confirm("Are you sure you want to delete this product from your shopping cart?")) {
		
			values += 'pid=' + product_id;
			values += '&mode=delete';
			
			// GET CONTENTs
			$.ajax({
				type: "GET",
				url: jspath + "ajax-files/shop.php",
				data: values,
				success: function(msg){
					cart.html(msg);
					if (msg == '') {
						cartwrapper.hide('slow');
						cart.hide();
						$('#orderlist').hide('slow');
						$('#emptyorder').show();
						$('#formular').hide();
					}
					$('#row' + product_id).hide('slow');
				}
			});
		
		}
			
		$(this).blur();
		
		return false;
				
	}); 	
	
	$("#order").click(function() {
	
		var name 		= $('#ordername').val();
		var email 		= $('#orderemail').val();
		var phone 		= $('#orderphone').val();
		var address 	= $('#orderaddress').val();
		var comments 	= $('#ordercomments').val();
		var error		= $('#error');
		var loader		= $('#loader');
		var values		= '';
		
		values += 'mode=order';
		values += '&name=' + name;
		values += '&email=' + email;
		values += '&phone=' + phone;
		values += '&address=' + address;
		values += '&comments=' + comments;
		
		// GET CONTENTs
		$.ajax({
			type: "GET",
			url: jspath + "ajax-files/shop.php",
			data: values,
			beforeSend: function(msg){
				loader.html('<img src="' + jspath + 'gfx/loader.gif" style="height: 15px;margin: 3px 0 0 7px;" alt="Loader..." />');
			},
			success: function(msg){
				loader.html('');
				if (msg == '') {
					$('#orderlist').hide();
					$('#formular').hide();
					$('#orderdone').show('slow');
					$('#cart').hide();
				}
				else {
					error.html(msg);
					error.show();
				}
			}
		});
		
		$(this).blur();
		
		return false;
		
	});
	
	$('#ideas_search').keyup(function() {
		$.ajax({
			type: "GET",
			url: jspath + "ajax-files/ideas-search.php",
			data: 'search=' + $(this).val(),
			beforeSend: function(msg) {
				$('#ideas-list').css({'opacity' : '0.5'});		
			},
			success: function(data) {
				$('#ideas-list').html(data);
				$('#ideas-list').css({'opacity' : '1.0'});		
			}
		});
	})
	
	$('#create-idea').click(function() {
	
		var display = $('.add-idea').css('display');
		
		if (display == 'none') {
			$('.add-idea').show('slow');
		}
		else {
			$('.add-idea').hide("slow");
		}
		
		return false;
		
	});
	
	$('#addidea').submit(function() {
		
		var idea 	= $('#idea');
		var name 	= $('#name');
		var email	= $('#email');
		var text 	= $('#text');
		
		if (idea.val() == '') {
			alert('You have to choose a name for your idea.');
			idea.css({'border' : '1px solid red'});
			idea.focus();
			return false;
		}
		else {
			idea.css({'border' : '1px solid #AAA'});
		}
		
		if (name.val() == '') {
			alert('You have to write your name.');
			name.css({'border' : '1px solid red'});
			name.focus();
			return false;
		}
		else {
			name.css({'border' : '1px solid #AAA'});
		}
		
		if (email.val() != '') {
			if (!isValidEmailAddress(email.val())) {
				alert('Your email is not valid.');
				email.css({'border' : '1px solid red'});
				email.focus();
				return false;
			}
			else {
				email.css({'border' : '1px solid #AAA'});
			}
		}
		
		if (text.val() == '') {
			alert('You need to write some text.');
			text.css({'border' : '1px solid red'});
			text.focus();
			return false;
		}
		else {
			text.css({'border' : '1px solid #AAA'});
		}
		
		values = 'idea=' + idea.val();
		values += '&name=' + name.val();
		values += '&email=' + email.val();
		values += '&text=' + text.val();
		
		$.ajax({
			type: "GET",
			url: jspath + "ajax-files/ideas-add.php",
			data: values,
			beforeSend: function(msg) {
				$('.add-idea').css({'opacity' : '0.5'});		
			},
			success: function(data) {
				$('.add-idea').html(data);
				$('.add-idea').css({'opacity' : '1.0'});		
			}
		});
		
		return false;	
		
	});
	
	
		
	function is_int(value){ if((parseFloat(value) == parseInt(value)) && !isNaN(value)){ return true; } else { return false; }}
	
});

function isValidEmailAddress(emailAddress) {
    var pattern = new RegExp(/^(("[\w-+\s]+")|([\w-+]+(?:\.[\w-+]+)*)|("[\w-+\s]+")([\w-+]+(?:\.[\w-+]+)*))(@((?:[\w-+]+\.)*\w[\w-+]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][\d]\.|1[\d]{2}\.|[\d]{1,2}\.))((25[0-5]|2[0-4][\d]|1[\d]{2}|[\d]{1,2})\.){2}(25[0-5]|2[0-4][\d]|1[\d]{2}|[\d]{1,2})\]?$)/i);
    return pattern.test(emailAddress);
};

