/* --- GameHonk.nl JavaScript --- */
	$(document).ready(function(){
		$('.openpage').live('click', function(){
			window.location.href = $(this).attr('url');
		});
		
		$('#breadcrumbs a').hover(
			function () {
				$(this).attr('style', 'color:#343434');
				Cufon.refresh();
			}, 
			function () {
				$(this).attr('style', '');
				Cufon.refresh();
			}
		);
		
		$('.oddeven li, .tableRow, .openpage').hover(
			function () {
				if($(this).hasClass('tableRow')){
					$(this).addClass('tableRowHover');
				}
				
				$(this).addClass('linkCursor');
			},function () {
				if($(this).hasClass('tableRow')){
					$(this).removeClass('tableRowHover');
				}
				
				$(this).removeClass('linkCursor');
			}
		);
	
		$('.magical-input').each(function(){
			var text = $(this).attr('alt');
			
			if($(this).val() == '' || $(this).val() == text){
				$(this).val( text );
				$(this).addClass('waitingField');
			} else {
				$(this).removeClass('waitingField');
			}

			$(this).keypress(function() {
				if($(this).hasClass('waitingField') == true){
					$(this).val('');
					$(this).removeClass('waitingField');
				}
			});

			$(this).blur(function() {
				if($(this).hasClass('waitingField') == false && $(this).val() == ''){
					$(this).addClass('waitingField');
					$(this).val( text );
				}
			});
			
			$(this).hover(
				function () {
					if($(this).hasClass('waitingField') == true){
						$(this).removeClass('waitingField');
						$(this).val('');
						$(this).focus();
					}
				},function () {
				
				}
			);
		});
		
		$('.submit-newsletter').live('click', function(){
			$('#results-newsletter-submit span:first').hide();
			$('#results-newsletter-submit').hide();
			
			var hash = $(this).attr('href');
			hash = hash.substr(1);
			
			if($('#subscribe-newsletter').attr('alt') != $('#subscribe-newsletter').val()){
				$('#load-newsletter-submit').fadeIn(400, function(){
					var email = $('#subscribe-newsletter').val();
					$.post(BASE_URL_HTTP + 'ajax/newsletter.php?action=' + hash, { email : email }, function(data){
						var results = data.split('|');
						var status = results[0];
						var text = results[1];
						
						$('#load-newsletter-submit').hide();
						
						if(status == 'success' || status == 'warning'){
							if(status == 'success'){
								var refreshitem = $('#subscribe-newsletter').attr('alt');
								$('#subscribe-newsletter').val(refreshitem);
								$('#subscribe-newsletter').addClass('waitingField');
							}
							
							$('#results-newsletter-submit span:first').html(text);
							$('#results-newsletter-submit span:first').show();
							$('#results-newsletter-submit').show();
							
							setTimeout(function(){
								$('#results-newsletter-submit span:first').fadeOut(700, function(){
									$('#results-newsletter-submit').slideUp(700);
								})
							},2000);
						} else if(status == 'error'){
							alert(text);
						}
					})
					.error(function() { 
							$('#load-newsletter-submit').hide();
							
							$('#results-newsletter-submit span:first').html('<span style="color:red;font-weight:bold;">Please try again later</span>');
							$('#results-newsletter-submit span:first').show();
							$('#results-newsletter-submit').show();
							
							setTimeout(function(){
								$('#results-newsletter-submit span:first').fadeOut(700, function(){
									$('#results-newsletter-submit').slideUp(700);
								})
							},2000);
					});
				});
			} else {
				alert('Er is geen e-mailadres ingevuld!');
			}
			
			return false;
		});

		$('#submitSearch').click(function(){
			if($('#searchGames').attr('alt') == $('#searchGames').val()){
				alert('Er zijn geen zoekwoorden ingevuld!');
				return false;
			}
		});
		
		$('#box-game').show();
		setTimeout(function(){
			$('#box-game-overlay').hide();
		},15000);
		
		$('.show-random-games').show();
		$("#slider").easySlider({
			controlsBefore:	'<p id="controls">',
			controlsAfter:	'</p>',
			auto: false,
			controlsShow: true,
			controlsFade: false,
			vertical: false,
			speed: 2000,
			continuous: true
		});
		
		$('.show-small-random-games').show();
		$("#small-slider").easySlider({
			controlsBefore:	'<p id="small-controls">',
			controlsAfter:	'</p>',
			prevId: 		'prevBtn2',
			nextId: 		'nextBtn2',
			auto: false,
			controlsShow: true,
			controlsFade: false,
			vertical: true,
			speed: 2000,
			continuous: true
		});
		
		$('.game-tab').click(function(){
			var tabSelect = $(this).attr('id');
			tabSelect = tabSelect.substr(7);
			
			if(tabSelect == 'tab-save'){
				// Do nothing
			} else if(tabSelect == 'tab-comments'){
				var tabSwitch = true;
				var game_id = $(this).attr('game');
				
				$.post(BASE_URL_HTTP + 'ajax/comments.php?task=show&_gid=' + game_id, function(data){
					var json = $.parseJSON(data);
					var comment = '';
					
					if(json.sandbox == true){
						comment += '<div class="textCenter">Momenteel in onderhoud.</div>';
					} else {
						if(json.info.results == 0){
							comment += '<div class="textCenter box-no-comments">Ben jij de eerste die bij deze game een opmerking plaatst?</div>';
						} else {
							$.each(json.comments, function(key, value) {
								comment += '<div id="comment-' + key + '" class="box-comment">';
									
									comment += '<div class="box-comment-icon">';
										comment += '<img src="' + BASE_URL_HTTP + 'images/profiles/avatars/' + value.profile.img + '" />';
									comment += '</div>';
									
									comment += '<div class="box-comment-data">';
										comment += '<div class="box-comment-head">' + value.subject + ' <span style="font-size:10px;color:#888;font-weight:default;font-style:italic;"> - ' + value.profile.name + '</span></div>';
										comment += '<div class="box-comment-actions"><a id="report-comment-' + key + '" class="report-comment" href="#report"><img src="' + BASE_URL_HTTP + 'images/icons/alert.png" /></a></div>';
										comment += '<div class="clear"></div>';
										comment += '<div class="box-comment-text">' + value.text + '</div>';
									comment += '</div>';
									
									comment += '<div class="clear"></div>';
									comment += '<div class="box-comment-datetime">' + value.datetime + '</div>';
									
								comment += '</div>';
							});
						}
						comment += '<div id="make-comment" game="' + game_id + '"><a class="button" style="font-size:10px" href="#dialog" name="modal">Opmerking plaatsen</a></div>';
					}
						
					$('#'+tabSelect).html(comment);
				});
				
			} else {
				var tabSwitch = true;
			}

			if(tabSwitch == true){
				$('.game-tab').removeClass('active-game-tab');
				$(this).addClass('active-game-tab');
				$('.game-tab-box').hide();
				$('#'+tabSelect).show();
			}
		});
		
		$('#post-comment').live('click', function(){
			var game_id = $(this).attr('game');
			
			var name = $('input[name="name"]');
			var email = $('input[name="email"]');
			var subject = $('input[name="subject"]');
			var comment = $('textarea[name="comment"]');
			var anonymous = $('input[name="anonymous"]:checked');
			var newsletter = $('input[name="newsletter"]:checked');
			
			var disallowposting = false;
			if(name.val() == name.attr('alt')){
				name.attr('style', 'border:1px solid red;');
				disallowposting = true;
			} else {
				name.attr('style', 'border:1px solid #ccc;');
			}
			if(email.val() == email.attr('alt')){
				email.attr('style', 'border:1px solid red;');
				disallowposting = true;
			} else {
				email.attr('style', 'border:1px solid #ccc;');
			}
			if(subject.val() == subject.attr('alt')){
				subject.attr('style', 'border:1px solid red;');
				disallowposting = true;
			} else {
				subject.attr('style', 'border:1px solid #ccc;');
			}
			if(comment.val() == comment.attr('alt')){
				comment.attr('style', 'border:1px solid red;');
				disallowposting = true;
			} else {
				comment.attr('style', 'border:1px solid #ccc;');
			}
			
			if(disallowposting == false){
				$.post(BASE_URL_HTTP + 'ajax/comments.php?task=post&_gid=' + game_id, { name : name.val(), email : email.val(), subject : subject.val(), comment : comment.val(), anonymous : anonymous.val(), newsletter : newsletter.val() }, function(data){
					var json = $.parseJSON(data);
					
					if(json.status == 'success'){
						// Clear selected fields
						subject.val(subject.attr('alt'));
						subject.addClass('waitingField');
						comment.val(comment.attr('alt'));
						comment.addClass('waitingField');
						
						// Hide the popup
						$('#mask').hide();
						$('.popup-window').hide();
						
						// Added for showing swf object
						$('#box-game-play').attr('style','visibility: display');
						
						// If user wants to recieve the newsletter subscribe him/her
						if(newsletter.val() == 'on'){
							$.post(BASE_URL_HTTP + 'ajax/newsletter.php?action=subscribe', { email : email.val() });
						}
					} else {
						// Checks after request
						if(json.errors.name == true){
							name.attr('style', 'border:1px solid red;');
						}
						if(json.errors.email == true){
							email.attr('style', 'border:1px solid red;');
						}
						if(json.errors.subject == true){
							subject.attr('style', 'border:1px solid red;');
						}
						if(json.errors.comment == true){
							comment.attr('style', 'border:1px solid red;');
						}
					}
				});
			}
			
			return false;
		});
		
		/* --- Start Of InScreen Popup --- */
			$('a[name=modal]').live('click', function(e) {
				// Cancel the link behavior
				e.preventDefault();
				
				// Scroll to top
				$('html, body').animate({scrollTop:0}, 'slow');
				
				// Get the A tag
				var id = $(this).attr('href');
			
				// Get the screen height and width
				var maskHeight = $(document).height();
				var maskWidth = $(window).width();
			
				// Set heigth and width to mask to fill up the whole screen
				$('#mask').css({'width':maskWidth,'height':maskHeight});
				
				// transition effect		
				$('#mask').fadeIn(1000);	
				$('#mask').fadeTo("slow",0.8);	
			
				// Get the window height and width
				var winH = $(window).height();
				var winW = $(window).width();
					  
				// Set the popup window to center
				$(id).css('top',  winH/2-$(id).height()/2);
				$(id).css('left', winW/2-$(id).width()/2);
				
				// Added for hiding swf object
				$('#box-game-play').attr('style','visibility: hidden');
				
				// transition effect
				$(id).fadeIn(2000); 
			});
			
			// if close button is clicked
			$('.popup-window .popup-close').click(function (e) {
				//Cancel the link behavior
				e.preventDefault();
				
				$('#mask').hide();
				$('.popup-window').hide();
				
				// Added for showing swf object
				$('#box-game-play').attr('style','visibility: display');
			});		
			
			// if mask is clicked
			$('#mask').click(function () {
				$(this).hide();
				$('.popup-window').hide();
				
				// Added for showing swf object
				$('#box-game-play').attr('style','visibility: display');
			});
		/* --- End Of InScreen Popup --- */
			
		var shuffleHoverLock = false;
		$('div#small-fav-games').hover(
			function(){
				if(shuffleHoverLock == false){
					shuffleHoverLock = 1;
					$('div.small-fav-games').slideDown(1000, function(){
						if(shuffleHoverLock == 2){
							$('div.small-fav-games').slideUp(750, function(){ $('div.small-fav-games-title').attr('style', 'border-top:1px solid #ccc'); shuffleHoverLock = false; });
						} else {
							$('div.small-fav-games-title').attr('style', 'border-top:1px solid #ccc');
							shuffleHoverLock = false;
						}
					});
				}
			}, function(){
				if(shuffleHoverLock == 1){
					shuffleHoverLock = 2;
				} else if(shuffleHoverLock == false){
					$('div.small-fav-games').slideUp(1000, function(){
						$('div.small-fav-games-title').attr('style', 'border-top:none');
						shuffleHoverLock = false;
					});
				}
			}
		);
		
		$('.my-games-action').live('click', function(){
			var save = $(this).attr('task');
			var game = $(this).attr('game');
			$.post(BASE_URL_HTTP + 'ajax/my-games.php?save=' + save, { game : game }, function(data){
				var results = data.split('|');
				var status = results[0];
				var text = results[1];
				
				if(status == 'success'){
					if(save == 'undo'){
						var totalGames= $('#counter-fav-games').html();
						totalGames--;
						$('#counter-fav-games').html(totalGames);
						
						$('#game-hold-for-' + game).remove();
						$('#small-game-' + game).remove();
						
						var countGames = $('#small-my-games-list li').size();
						if(countGames <= 0){
							$('#no-fav-games').show();
							$('#small-my-games-list').hide();
						}
					} else {
						var urlGen = window.location.pathname;
						urlGen = urlGen.substr(1);
						urlGen = BASE_URL_HTTP + urlGen;
						
						$('#small-my-games-list').prepend('<li id="small-game-' + game + '" class="openpage" url="' + urlGen + '"><a href="' + urlGen + '">&#187; ' + $('#game-title').html() + '</a></li>');
						
						var countGames = $('#small-my-games-list li').size();
						if(countGames >= 0){
							$('#no-fav-games').hide();
							$('#small-my-games-list').show();
						}
					}
					
					$('#game-save-results').html('');
					$('#game-save-results').html(text);
					$('#game-save-results').show();
					
					setTimeout(function(){
						$('#game-save-results').fadeOut(700)
					},2000);
				}
			});
			
			if(save == 'undo'){
				var newSave = 'do';
				var newText = 'Opslaan';
			} else {
				var newSave = 'undo';
				var newText = 'Verwijderen';
			}
			
			$(this).html(newText);
			$(this).attr('task', newSave);
		});
		
		$('.list-preview-image').lazyload({
			placeholder : "images/loading-preview.png",
			effect : "fadeIn"
		});
		
		$('a.a-nice-title').mousetip({
			position: {
                left: 10,
                top: 20,
				vertical: 'bottom'
			}
		});
		$('a.magical-preview-text').mousetip({ type: 'text' });
		$('a.magical-preview-image').mousetip({ type: 'image' });
	});
