//begin global variabele

//einde globale variabele

//begin window load

$(window).load(function(){
	//mobilepic op homepagina centreren
	var width 		= 	parseInt($('.contentSix img').width());
	var height 		= 	parseInt($('.contentSix img').height());
	var LeftOffset 	= 	(210 - width) / 2;
	var TopOffset	= 	(250 - height) / 2;
	
	$('.contentSix img').css({
		'left' 	: 	LeftOffset + 'px',
		'top' 	: 	TopOffset + 'px'
	})
});

//einde window load

//begin document ready
$(document).ready(function(){


	Init();
	
	InitClick();
	
	PostGuestbook();
	

});
//einde document ready

//functies

function Init(){
	//fancybox
	$('a#fancybox').fancybox();
	
	MobileOverlay();
	GetMobilePic();
	BlurFocusContact();
	MailContactForm();
	RateArticle();
	RateComment();
}

function GetMobilePic(){
	if($('.contentSix').length){
	
		$.ajax({
		url		:		"mobile.php",
		dataType	:	'json',
		success:	function(data){
			
			var Obj	=	$.parseJSON(data);
			var	ImgInfo	=	Obj.img_info;
			$('.mobileOverlay').text(ImgInfo);
			$('.mobilePic').attr({'title'	:	ImgInfo});
				
			}
		});
			
	}
}

function InitClick(){

	//show menu
	$('.loginFormDiv').css('display', 'block');
	
	//slide down menu
	$('#login').click(function(e){
		e.preventDefault();
		var top = parseInt($('.loginFormDiv').css('top'));
		
		if(top < 0){
			$('.loginFormDiv').animate({
				'top'	: 0
			}, 'slow')
		} else {
			$('.loginFormDiv').animate({
				'top'	: '-70px'
			}, 'slow')
		}
		
	});
}

function MobileOverlay(){
var _this			=	$('.mobileOverlay');

	$('.contentSix').mouseenter(function(){		
		_this.stop(true, true).animate({
			height	:	'60px'
		});
	});
	
	$('.contentSix').mouseleave(function(){
		_this.stop(true, true).animate({
			height	:	'0px'
		});
	});

	
}

function PostGuestbook() {
	if($('.gastenboek').length) {
		
		GetGuestbook();
	
		$('#postMessage').click(function(){
		
			var PostInfo 	=	$('.gboek_form').serialize();
			var PostUrl		=	"/gbookpost.php?"+PostInfo;
			
			var	Naam		= $('#schrijfNaam').attr('value');
			var Bericht	=	$('#schrijfBericht').attr('value');
			
			//als alles ingevuld via ajax opslaan
			if(Naam != "" && Bericht != "") {
			
				$.ajax({
				url			:	PostUrl,
				success	:	function(){
										//formuliervelden resetten
										$('#schrijfNaam').attr('value', "");
										$('#schrijfBericht').attr('value', "");
										
										//gastenboekcontent uitfaden. dan infaden met nieuwe content.
										var GboekContent	=	$('.gastenboek_content');
										GboekContent.fadeOut();
										GboekContent.load('/gboek.php');
										GboekContent.fadeIn();
									}
				});
				
				
			
			}
			//niet alles ingevuld foutmelding geven
			else if(Naam == "" && Bericht == "" || Naam == null && Bericht == null) {
				alert("Alleen op toevoegen klikken zonder iets in te vullen gaat natuurlijk niet he...");
			} else if(Naam != "" && Bericht == "" || Naam != null && Bericht == null) {
				alert("Je hebt alleen je naam ingevuld. DAT MAG NIE JONGUH!");
			} else if(Naam == "" && Bericht != "" || Naam == null && Bericht != null) {
				alert("Anoniem posten is voor loserts. VUL JE NAAM IN MIETJE!");
			}
			
		});
	}
}

function GetGuestbook() {

	if($('.gastenboek').length) {
		$('.gastenboek_content').load('/gboek.php');
	}
	
}

function BlurFocusContact() {
	if($('.contactHeader').length > 0) {
	
		$('#contactnaam').focus(function(){
			var	_this	=	$(this);
			
			if(_this.attr('value') == "Naam") {
				_this.attr({'value'	: ''});
			}
		});
		
		$('#contactnaam').blur(function(){
			var _this	= $(this);
			if(_this.attr('value') == "") {
				_this.attr({'value'	: 'Naam'});
			}	
		});
		
		$('#contactmail').focus(function(){
			var _this	=	$(this);
			
			if(_this.attr('value') == 'E-mailadres') {
				_this.attr({'value'	: ''});
			}
		});
		
		$('#contactmail').blur(function(){
			var _this	=	$(this);
			if(_this.attr('value') == "") {
				_this.attr({'value'	: 'E-mailadres'});
			}
		});
		
	}
}

function MailContactForm() {
	if($('.contactHeader').length > 0) {
		$('#contactbutton').click(function () {
			var Naam 		= $('#contactnaam').attr('value');
			var	Email		= $('#contactmail').attr('value');
			var Bericht	= $('#contactbericht').attr('value');
			
			var Mail		=	$('#contactform').serialize();
			var MailUrl	=	"/mail.php?"+Mail;
			
			$.ajax({
				url				:	MailUrl,
				dataType	: 'json',
				success		:	function(data) {
					var Obj	=	$.parseJSON(data);
					if(Obj.success == "true") {
						$('.sendmail').fadeIn();
						$('.sendmail').delay(2000).fadeOut();
						$('#contactnaam').attr({'value'	:	'Naam'});
						$('#contactmail').attr({'value'	:	'E-mailadres'});
						$('#contactbericht').attr({'value'	:	''});
					} else {
						$('.failmail').fadeIn();
						$('.failmail').delay(2000).fadeOut();						
					}						
				}
			});			
		});
	}
}

function RateArticle() {

	$('#minarticle').click(function(e) {
		e.preventDefault();
		
		var	rating		=	"min";
		var ArticleId	=	$('#minarticle').attr('rel');
		
		$.ajax({
			url				:	"/rateMessageJs.php",
			dataType	:	"json",
			data			: {
				rating	:	rating,
				article	:	ArticleId
			},
			success		:	function(data) {
				var Obj	=	$.parseJSON(data);
				if(Obj.success == "true") {
					$('.rating').empty().text(Obj.rating);
				}
			}
		});
	});
	
	$('#plusarticle').click(function(e) {
		e.preventDefault();
		
		var	rating		=	"plus";
		var ArticleId	=	$('#plusarticle').attr('rel');
		
		$.ajax({
			url				:	"/rateMessageJs.php",
			dataType	:	"json",
			data			: {
				rating	:	rating,
				article	:	ArticleId
			},
			success		:	function(data) {
				var Obj	=	$.parseJSON(data);
				if(Obj.success == "true") {
					$('.rating').empty().text(Obj.rating);
				}
			}
		});
	});
	
}

function RateComment() {

	$('.mincomment').click(function(e) {
		e.preventDefault();
		var _this			=	$(this)
		var	rating		=	"min";
		var CommentId	=	_this.attr('rel');
		
		$.ajax({
			url				:	"/rateCommentJs.php",
			dataType	:	"json",
			data			: {
				rating	:	rating,
				comment	:	CommentId
			},
			success		:	function(data) {
				var Obj	=	$.parseJSON(data);
				if(Obj.success == "true") {
					var Class	=	".comment"+CommentId;
					$(Class).empty().text(Obj.rating);
				}
			}
		});
	});
	
	
	$('.pluscomment').click(function(e) {
		e.preventDefault();
		var _this			=	$(this)
		var	rating		=	"plus";
		var CommentId	=	_this.attr('rel');
		
		$.ajax({
			url				:	"/rateCommentJs.php",
			dataType	:	"json",
			data			: {
				rating	:	rating,
				comment	:	CommentId
			},
			success		:	function(data) {
				var Obj	=	$.parseJSON(data);
				if(Obj.success == "true") {
					var Class	=	".comment"+CommentId;
					$(Class).empty().text(Obj.rating);
				}
			}
		});
	});
	
}

//einde functies


