document.createElement('header');
document.createElement('nav');
document.createElement('section');
document.createElement('article');
document.createElement('aside');
document.createElement('footer');

$(function() {
	$window = $(window);
	var w = $window.scrollLeft();
	$('body').css("background-position","-"+w+"px 0");
	$window.scroll(function() {
		var w = $window.scrollLeft();
		$('body').css("background-position","-"+w+"px 0");
		$('#topNav').css("left","-"+w+"px");
		if (w > 0) {
			$('#topNav').css("width","950px");
		}
	});
});

$(document).ready(function() {
	initCommentDelete();
	initBanUser();
	initCommentAdd();
	initFancyBox();
	initCLEditor();
	initPopup();
	initForm();
	theRotator();
	//resizer();
	initLogin();
});

///////////////// BEGIN STANDARD FANCUE FUNCTIONS /////////////////

function siteMsg(msg,type) {
	$('#FanCueMessage').css('opacity','0');
	$('#FanCueMessage').css('display','table');
	$('#FanCueMessage').removeClass();
	$('#FanCueMessage').addClass(type);
	$('#FanCueMessage').html(msg);
	$('#FanCueMessage').stop().animate({
		opacity: 1
	}, 500);
	setTimeout(hideMsg, 3000);
}
function hideMsg() {
	$('#FanCueMessage').stop().animate({
		opacity: 0
	}, 500, function() {
		$('#FanCueMessage').css('display','none');
	});
}

function initCommentDelete() {
	if ($('.commentDeleteForm').length > 0) {
		$('.commentDeleteForm').submit(function() {
			var myQS = $(this).serialize();
			var commentID = $(this).children('[name=comment_id]').val();
			var ajaxDeleteComment = $.ajax({
				url: '/_scripts/deleteComment.php?comment_id='+commentID,
				success: function(data) {
					
					if(data == 'success') {
						if ($.isFunction($().slideUp)) {
							$('#comment'+commentID).slideUp('normal');
						} else {
							$('#comment'+commentID).stop().animate({
								opacity: 0
							},300,
							function(){
								$('#comment'+commentID).css('display','none');
							});
						}
						siteMsg('The comment has been deleted','FanCueSuccess');
					} else {
						siteMsg(data,'FanCueError');
					}
					
				}
			});
			return false;
		});
	}
}

function initBanUser() {
	if ($('.userBanForm').length > 0) {
		$('.userBanForm').submit(function() {
			var myQS = $(this).serialize();
			var userID = $(this).children('[name=user_id]').val();
			var ajaxDeleteComment = $.ajax({
				url: '/_scripts/banUser.php?user_id='+userID,
				success: function(data) {
					
					if(data == 'success') {
						siteMsg('The user has been banned','FanCueSuccess');
					} else {
						siteMsg(data,'FanCueError');
					}
					
				}
			});
			return false;
	});
	}
}

function initCommentAdd() {
	if ($('.commentAddForm').length > 0) {
		$('.commentAddForm').submit(function() {
			var $form = $(this);
			$form.children('.submitBtn').attr('disabled', 'disabled');
			$form.children('.submitBtn').css('opacity', '0.5');
			var myQS = $(this).serialize();
			var ajaxAddComment = $.ajax({
				url: '/_scripts/addComment.php?'+myQS,
				success: function(data) {
					
					if(data == 'success') {
						parent.reloadComments();
					} else {
						siteMsg(data,'FanCueError');
						$form.children('.submitBtn').removeAttr("disabled");
						$form.children('.submitBtn').css('opacity', '1');
					}
					
				}
			});
			return false;
		});
	}
}

function reloadComments() {
	var ajaxCommentReload = $.ajax({
		url: '/_scripts/reloadComments.php',
		success: function(data) {
			siteMsg('Your comment has been added','FanCueSuccess');
			$.fancybox.close();
			$('#comments').html(data);
			initCommentDelete();
			initCommentAdd();
			initFancyBox();
			initPopup();
		}
	});	
}

// set recaptcha style
var RecaptchaOptions = { theme : 'clean' };

// wysiwyg for comments
function initCLEditor() {
	if ($('.cleditor').length > 0) {
		$(".cleditor").cleditor({
			height:		120, // height not including margins, borders or padding
			controls:     // controls to add to the toolbar
						"bold italic underline strikethrough | size " +
						"color removeformat " +
						"undo redo | " +
						"icon cut copy paste pastetext "
		});
	}
}

// fancybox for comments
function initFancyBox() {
	if ($('.boxlink').length > 0) {
		$(".boxlink").fancybox({
			'width'				: 600,
			'height'			: 440,
			'padding'			: '5',
			'autoDimensions'	: true,
			'transitionIn'		: 'elastic',
			'transitionOut'		: 'elastic',
			'type'				: 'iframe'
		});
	}
}

function confirmSubmit() {
	var agree=confirm("Are you sure you wish to continue?");
	if (agree)
		return true ;
	else
		return false ;
}

function initPopup() {
	if ($('.popupLink').length > 0) {
		$('.popupLink').click(function() {
			var href = $(this).attr('href');
			var w = 985;
			var h = 565;
			var left = (screen.width-w)/2;
			var top = (screen.height-h)/2;
			var newwin = window.open(href,'','location=1,status=0,scrollbars=0,width='+w+',height='+h+',left='+left+',top='+top+'');
			//newwin.moveTo(left, top);
			return false;
		});
	}
}

// login and registration functions

function initForm() {
	var myDate = new Date();
	var startYear = myDate.getFullYear() - 90;
	var endYear = myDate.getFullYear() - 10;
	if ($(".datefield").length > 0) {
		$(".datefield").datepicker({showOn: 'both', buttonImage: '/images/spacer.gif',  buttonText: '', buttonImageOnly: true, dateFormat: 'dd-MM-yy',changeMonth: true,changeYear: true, yearRange: ''+startYear+':'+endYear+''});
	}
	if ($('.form').length > 0) {
		$('.form').submit(function() {
			var stopper = 0;
			$('.form .required').each(function(i) {
				if ( ($(this).val() == '') ) {
					$(this).addClass('error');
					stopper=1;
				} else {
					$(this).removeClass('error');
				}
			});
			if (stopper == 1) {
				alert('Please fill out all required fields');
				return false;
			}
		});
	}
	if ($('.FanCueForm').length > 0) {
		$('.FanCueForm').submit(function() {
			var stopper = 0;
			$('.FanCueForm .required').each(function(i) {
				if ( ($(this).val() == '') ) {
					$(this).addClass('error');
					stopper=1;
				} else {
					$(this).removeClass('error');
				}
			});
			if (stopper == 1) {
				alert('Please fill out all required fields');
				return false;
			}
		});
	}
}

function show(id) {
	document.getElementById(id).style.display='block';
}

function hide(id) {
	document.getElementById(id).style.display='none';
}

///////////////// END STANDARD FANCUE FUNCTIONS /////////////////

function initLogin() {
	$('#loginBtn').click(function() {
		if ($('#loginContainer').is(':hidden')) {
			$('#loginContainer').css('display','block');
		} else {
			$('#loginContainer').css('display','none');
		}
		return false;
	});
}

// rotator
function theRotator() {
	//Set the opacity of all images to 0
	$('div#rotator ul li').css({opacity: 0.0});
	
	//Get the first image and display it (gets set to full opacity)
	$('div#rotator ul li:first').css({opacity: 1.0});
	
	var len = $('div#rotator ul li').length;
		
	//Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
	if (len > 1) {
		setInterval('rotate()',8000);
	}
	
}

function rotate() {	
	//Get the first image
	var current = ($('div#rotator ul li.show')?  $('div#rotator ul li.show') : $('div#rotator ul li:first'));

	//Get next image, when it reaches the end, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div#rotator ul li:first') :current.next()) : $('div#rotator ul li:first'));	
	
	
	//Set the fade in effect for the next image, the show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);

	//Hide the current image
	current.animate({opacity: 0.0}, 2000)
	.removeClass('show');
	
};

