function changeItemTitle(NewTitle)
{
	$('#detail_title').html(NewTitle);
}
	function forum_frame_change()
	{
		if ($('#forum_frame').exists())
			if (frames['forum_frame'].document)
				$('#Listings #ListingsExpanded #grid_title').html('Forums • '+frames['forum_frame'].document.title);
	}
	function forum_frame_reload()
	{
		if ($('#forum_frame').exists())
			frames['forum_frame'].location.href = frames['forum_frame'].location.href; 
	}

var pageAJAX; // used for aborting old ones..
var SeriesMaxPages = 1; // used for series pagination....
var account_name = ''; //Account name - returned from ajax
var CurItem;
var CurSeries;
var LoggedIn = false;
var UserFaves = Array();

$(document).ready(function()
{

$('#slideshow').serialScroll({
				items:'li',
				prev:'#screen2 a.prev',
				next:'#screen2 a.next',
				axis:'x',
				offset:0, //when scrolling to photo, stop 230 before reaching it (from the left)
				start:0, //as we are centering it, start at the 2nd
				duration:0,
				force:true,
				//interval: 3000,
				constant: false,
				step:2,
				stop:true,  
				lock:false,
				cycle:false, //don't pull back once you reach the end                   
				jump: false //click on the images to scroll to them
			});

	/* Login */        
	$('.toggle_login').click(function(e)
	{
		$this = $('#TopHeader a.toggle_login');
		if ($('#LoginHeader').css('display') == 'block')
			$('#LoginHeader').slideUp(1000,function(){
				$this.removeClass('closeTab');
				if (LoggedIn)
					$this.addClass('logoutTab');
				else
					$this.addClass('login');
			});
		else
			$('#LoginHeader').show(1000,function(){
				if (LoggedIn)
					$this.removeClass('logoutTab');
				else
					$this.removeClass('login');
				
				$this.addClass('closeTab');
				});
		return preventDefault(e);
	});    
					
	/* Subscribe */		
	
	$('#subscription_form').submit(function(e)
	{
		$('.mailing_list_text').html('Checking <img src="./_ui/media/loading_animation.gif" alt="Loading" />');
		
		$.get("./AJAX_subscription.php", 	{ 	action: 'subscribe',
																				value: $("input[name$='subscribe']:checked").val(),
																				email: $('#subscribe_field').val()
																			}, function(data){
																					$('.mailing_list_text').html(data);  
																				}
		); 

		return false;
	});    
				
	
		//$('#displayFooter div a').hide();
	$('#SeriesList').serialScroll(
	{
		items:'li', 	duration:700, 	axis:'y', 		lazy: true, step: 4,
		cycle: true,	stop: true,			jump: true,		exclude: 3,
		onBefore:function( e, elem, $pane, $items, pos )
		{
			/**
			* 'this' is the triggered element, e is the event object, elem is the element we'll be scrolling to, 
			* $pane is the element being scrolled,	$items is the items collection at this moment
			* pos is the position of elem in the collection, if it returns false, the event will be ignored
			* those arguments with a $ are jqueryfied, elem isn't.
			**/
			e.preventDefault();
			page = (pos +4) / 4;
			$('#page_current').text(page).css("display","none").fadeIn("fast");
			if (page == 1) {
			 $('#series_pag_first, #series_pag_prev').addClass('disabled');
			 $('#series_pag_last, #series_pag_next').removeClass('disabled');
			}
			else
			 if (page == SeriesMaxPages)
			 {
				 $('#series_pag_first, #series_pag_prev').removeClass('disabled');
				 $('#series_pag_last, #series_pag_next').addClass('disabled');
			 }
			 else
				 $('#series_pag_first, #series_pag_prev, #series_pag_next, #series_pag_last').removeClass('disabled');
	 }
	});
	$('#series_pag_next').click(function()
	{
		if (this.className != 'disabled')
			$('#SeriesList').trigger( 'next' );
			
		return false;
	});
	$('#series_pag_last').click(function()
	{
		if (this.className != 'disabled')
			$('#SeriesList').trigger( 'goto', [ ($('#SeriesList li').length -1) ]);
			
		return false;
	});
	$('#series_pag_first').click(function()
	{
		if (this.className != 'disabled')
			$('#SeriesList').trigger( 'goto', [ 0 ]);
			
		return false;
	});
	$('#series_pag_prev').click(function()
	{
		if (this.className != 'disabled')
			$('#SeriesList').trigger( 'prev' );
			
		return false;
	});

	/*$.preLoadAllImages(['./_ui/media/ajax-loader.gif']);*/
	
	$('#DetailDisplayDialog').jqm(
	{
		target: '#DetailsContent',
		overlay: 75,
		onShow: function(h)
		{
				h.w.css('opacity',0.92).slideFadeToggle(function() {
					$('#DetailsContent').attr("src",$(h.w).attr('rel'));
				});
				rowload = true;
		},
		onHide: function(h)
		{
			/* callback executed on window hide. Hide notice, overlay. */
			h.w.slideFadeToggle("slow",function() {
																		if(h.o)
																				h.o.remove();
																		
																	$('#DetailsContent').attr("src",'./noitem.html');
															});
			rowload = false;
			h.w.jqm = undefined;
		}
	});

	$('#reg_info form').submit(function(e)
	{
		preventDefault(e);
		$('#reg_message').html('');
		$('#reg_data').slideDown('fast');
		$('#reg_info').slideUp('fast');
		return false;
	});
	$('#login_info form').submit(function(e)
	{
		preventDefault(e);
		$('#login_message').html('');
		$('#login_data').slideDown('fast');
		$('#login_info').slideUp('fast');
		return false
	});
	$('#logout_data form').submit(function(e)
	{
		preventDefault(e);
		$('#logout_data').slideUp('fast');
		$('#login_info').slideDown('fast');
		$('#login_message').html('Please Wait...');
		$('#login_loading').removeClass('DisplayNone');
		$.post("user.php",{"action":"logout"},function(data)
			{
				$('#login_loading').addClass('DisplayNone');
				if (data.logout) {
					//$('#LoginHeader').removeClass('logOutHeader').removeClass('logInHeader');
					$('.toggle_account').fadeOut('1500');
					$('#login_data').slideDown('fast');
					$('#login_info').slideUp('fast');
					$.get(data.phpBB,{},function(phpBB_data){forum_frame_reload();});
					LoggedIn = false;
					$('#TopHeader a.login_toggle').removeClass('logoutTab').addClass('login');
					
					$('#reg_info form').submit();
					window.setTimeout("$('#TopHeader a.toggle_login').click()",1500);
					//$('#reg_message_ok').click();
				}
			},'json');
		return false;
	});
	$('form.frmLogin').submit(function(e)
	{
		preventDefault(e);
		$('#login_message').html('');
		$('#login_data').slideUp('fast');
		$('#login_info').slideDown('fast');
		$('#login_loading,#login_message_ok').removeClass('DisplayNone');
		$ToSend = new Object();
		$regErrors = new Array();
		if ($('#login_username').val() == '')
			$regErrors.push("Please enter a Username");
		
		if ($('#login_password').val() == '') 
			$regErrors.push("Please enter a Password");
		
		if ($regErrors.length > 0) {
			$('#login_loading').addClass('DisplayNone');
			html = '<h3>You have the following errors:</h3><br />';
			$.each($regErrors,function(i,item){
				html = html + '<strong>'+ item + '</strong><br />';
			});
			$('#login_message_ok').text('Edit Details');
			$('#login_message').html(html);
		}
		else {
			$ToSend['username'] = $('#login_username').val();
			$ToSend['password'] = $('#login_password').val();
			$ToSend['action'] = 'login';
			$.post("user.php",$ToSend,function(data)
				{
					$('#login_loading').addClass('DisplayNone');
					if (!data.login || data.error)
					{
						html = '<h3>You have the following errors:</h3><br /><strong>'+data.error+'</strong>';
						html = html + '';
						$('#login_message_ok').text('Edit Details');
						$('#login_message').html(html);
					}
					else
					{
						$('#LoginHeader').removeClass('logInHeader').removeClass('logOutHeader');
						if ($('#login_info').css("display") == 'block')
							$('#login_info').slideUp('fast');
	
						$('#cur_user_name').text(', ' + data.login.name);
						account_name = data.login.name;
						LoggedIn = true;
						$('#TopHeader a.toggle_login').addClass('logoutTab').removeClass('login');
						if (data.user)
							$.each(data.user,function(key,value){
								if ($('#acc_'+key).length > 0)
									$('#acc_'+key).val(value);
							});
						$('#logout_data').slideDown('fast');
						$('#login_message_ok').addClass('DisplayNone');
						$('.toggle_account').fadeIn('1500');
						if (data.favourites)
						{
							html = '';
							UserFaves = new Array();
							$('#right_grid .rightNav').html(html).hide();
							$.each(data.favourites,function()
							{
								html = html + this.li;
								UserFaves.push(this.id);
							});
							$('#right_grid .rightNav').html(html).fadeIn();
						}
						if (data.phpBB)
						{
							data.phpBB.username = $ToSend['username'];
							data.phpBB.password = $ToSend['password'];
							$.post("./forum/ucp.php?mode=login",data.phpBB,function(phpBB_data){forum_frame_reload();});
						}
						window.setTimeout("$('#TopHeader a.toggle_login').click()",1500);
					}
				},'json');
		}
		return false;
	});
	$("form.frmRegister").submit(function(e)
	{
		preventDefault(e);
		$('#reg_message').html('');
		$('#reg_data').slideUp('fast');
		$('#reg_info').slideDown('fast');
		$('#reg_loading').removeClass('DisplayNone');
		$('#reg_message_ok').removeClass('DisplayNone');
		$ToSend = new Object();
		$regErrors = new Array();
		$(this).find("input:text,input:password").each(function(i,inField) {
			$inField = $(inField);
			if ($inField.val() == '')
			{
				$regErrors.push($("label[for='"+$inField.attr('id')+"']").attr('rel'));
			}
			else {
				$ToSend[$inField.attr('name')] = $inField.val();
			}
		});
		if ($('#reg_pass_confirm').val() != $('#reg_pass').val()) {
			$regErrors.push("The Passwords do not match");	
		}
		if ($regErrors.length > 0) {
			$('#reg_loading').addClass('DisplayNone');
			html = '<h3>You have the following errors:</h3><br />';
			$.each($regErrors,function(i,item){
				html = html + '<strong>'+ item + '</strong><br />';
			});
			$('#reg_message_ok').text('Edit Details');
			$('#reg_message').html(html);
		}
		else {
			$ToSend['action'] = 'register';
			$.post("user.php",$ToSend,function(data){
				$('#reg_loading').addClass('DisplayNone');
				if (data.error) {
					html = '<h3>You have the following errors:</h3><br /><strong>'+data.error+'</strong>';
					html = html + '';
					$('#reg_message_ok').text('Edit Details');
					$('#reg_message_ok').removeClass("DisplayNone");
				}
				else {
					$('#reg_message_ok').addClass("DisplayNone");
					html = data.message;
					if (data.login)
					{
						LoggedIn=true;
						$('#TopHeader a.login_toggle').addClass('logoutTab').removeClass('login');
						$('#LoginHeader').removeClass('logInHeader').removeClass('logOutHeader');
						$('.toggle_account').fadeIn('1500');
						$('#cur_user_name').text(data.login.name);
						account_name = data.login.name;
						if (data.user) {
							$.each(data.user,function(key,value){
								if ($('#acc_'+key).length > 0)
								{
									$('#acc_'+key).val(value);
								}
							});
						}
						
						if (data.phpBB)
						{
							data.phpBB.username = $ToSend['username'];
							data.phpBB.password = $ToSend['password'];
							$.post("./forum/ucp.php?mode=login",data.phpBB,function(phpBB_data){forum_frame_reload();});
						}
						
						if ($('#login_data').css("display") == 'block')
							$('#login_data').slideUp('fast');
						
						if ($('#login_info').css("display") == 'block')
							$('#login_info').slideUp('fast');
						
						$('#logout_data').slideDown('fast');
					}
				}
				$('#reg_message').html(html);
			},'json');
		}
	});
	$('.toggle_account').click(function(e)
	{
		preventDefault(e);
		$("#SeriesBottom #Content #loading").fadeIn('fast'); 
		if ($('#SeriesExpanded').css('display') == 'block')
			$('#SeriesExpanded').slideUp(1000);

		if ($('#account_panel').hasClass("DisplayNone"))
			$('#Listings #ListingsExpanded').slideUp(1000,function()
			{
				show_content_panel('#account_panel','#right_account');
				$('#Listings #ListingsExpanded #grid_title').html('Account Settings for: '+account_name);
				$('#Listings #ListingsExpanded').slideDown(1000);
			});
		else 
			$('#Listings #ListingsExpanded').slideUp(1000,function() { hide_all_content_panels(); });

		$("#SeriesBottom #Content #loading").fadeOut();    
	});
	
	
	$('.acc_update_password').click(function(e)
	{
		preventDefault(e);
		$this = $(this);
		if ($('#acc_password_data').css("display") != 'none')
		{
			//data form there, so process the data to send
			$('#acc_password_data').slideUp('fast');
			$('#acc_password_info').slideDown('fast',function()
				{
					$('#acc_password_loading').removeClass('DisplayNone');
					$errorList = new Array();
					$ToSend = new Object();
					$ToSend['action'] = "updatePassword";
					if ($('#acc_old_password').val() == '')
						$errorList.push("You need to enter your old password");
					else
						$ToSend['old_password'] = $('#acc_old_password').val();	
					
					if ($('#acc_password').val() == '')
						$errorList.push("You need to enter your new password");
					else 
						$ToSend['password'] = $('#acc_password').val();
					
					if ($('#acc_password_confirm').val() == '')
						$errorList.push("You need to confirm your new password");
					else if ($('#acc_password').val() != $('#acc_password_confirm').val())
						$errorList.push("You're new Password doesn't match the confirmation");
					
					
					html = '';
					$('#acc_password_message').html(html);
					if ($errorList.length > 0) {
							$('#acc_password_loading').addClass('DisplayNone');
							html = '<h3>You have the following errors:</h3><br />';
							$.each($errorList,function(i,item){
								html = html + item +'<br />';
							});
							//$('#login_message_ok').text('Edit Details');
							$('#acc_password_message').html(html);
					}
					else {
						$('#acc_password_info .acc_update_password').addClass("DisplayNone");
						$('#acc_password_message').html('Updating your Password<br />Please Wait....');
						$.post("user.php",$ToSend,function(data){
							if (data.error)
							{
								$('#acc_password_loading').addClass('DisplayNone');
								$('#acc_password_message').html('<h3>You have the following errors:</h3><br />'+data.error);
								$('#acc_password_info .acc_update_password').removeClass("DisplayNone");
							}
							else {
								$('#acc_password_loading').addClass('DisplayNone');
								$('#acc_password_message').html('<h3>Your Password has been updated</h3>');
							}
						},'json');
					}
				});  
		}
		else {
			$('#acc_password_info').slideUp('fast');
			$('#acc_password_data').slideDown('fast');
		}
	});
	$('.acc_update_personal').click(function(e)
	{
		preventDefault(e);
		$this = $(this);
		$this.attr("disabled","disabled");
		if ($('#acc_personal_data').css("display") != 'none')
		{
			$ToSend = new Object();
			$errorList = new Array();
			$ToSend['action'] = "UpdatePersonal";
			$('#acc_personal_info .acc_update_personal').addClass("DisplayNone");
			$('#acc_personal_data input:text').each(function(i,inField)
				{
					$inField = $(inField);
					if ($inField.val() != '')
						$ToSend[$inField.attr('name')] = $inField.val();

				}
			);
			$.post("user.php",$ToSend,function(data)
				{
					if (data.error)
					{
						html = "<h3>You have the following errors</h3>"+data.error;
						$('#acc_personal_info .acc_update_personal').removeClass("DisplayNone");
					}
					else 
						html = "<h3>"+data.message+"</h3>";
						
					$('#acc_personal_loading').addClass('DisplayNone');
					$('#acc_personal_message').html(html);
				},'json');
			$('#acc_personal_data').slideUp('fast');
			$('#acc_personal_info').slideDown('fast',function()
				{
					$this.removeAttr("disabled");
				});
		}
		else {
			$('#acc_personal_info').slideUp('fast');
			$('#acc_personal_data').slideDown('fast',function()
				{
					$this.removeAttr("disabled");
				});
		}  
	});
	
	$('#right_acc_btn').click(function(e)
	{
		preventDefault(e);
		if ($('#account_panel').css("display") == 'none')
			$('#manage_faves_panel').slideFadeToggle('slow',function()
			{
				$('#Listings #ListingsExpanded #grid_title').html('Account Settings for: '+account_name);
				$(this).addClass("DisplayNone").removeAttr("style");
				$('#account_panel').slideFadeToggle(function(){
					$(this).removeClass("DisplayNone").removeAttr("style");
					});
			});
	});
	
	$('#right_fave_btn').click(function(e)
	{
		preventDefault(e);
		if ($('#manage_faves_panel').css("display") == 'none')
			$('#account_panel').slideFadeToggle(function()
			{
				$('#Listings #ListingsExpanded #grid_title').html('Favourites for: '+account_name);
				$(this).addClass("DisplayNone").removeAttr("style");
				$('#manage_faves_data').html("<br />Loading your favourites, Please Wait....<br /><br /><br /><br /><br /><br />");
				$("#SeriesBottom #Content #loading").show();
				$('#manage_faves_panel').slideFadeToggle(function(){
					$.post("user.php",{action:'showfaves'},function(data)
					{
						$('#manage_faves_data').fadeOut(function(){
							$('#manage_faves_data').html(data.page).fadeIn();
						});
						$("#SeriesBottom #Content #loading").hide();
	
					},'json');
					$(this).removeClass("DisplayNone").removeAttr("style");
				});
			});
	});
	
	$(".addtoFave").click(function(e)
	{
		preventDefault(e);
		$this = $(this);
		$ToSend = new Object();
		$ToSend['action'] = "addfave";
		$ToSend['item_id'] = CurItem;
		$.post("user.php",$ToSend,function(data){
			if (data.added)
				$this.fadeOut('fast',function(){
					$this.addClass("DisplayNone");
					html = '';
					UserFaves = new Array();
					$('#right_grid .rightNav').html(html).hide();
					if (data.favourites)
					{
						$.each(data.favourites,function()
						{
							html = html + this.li;
							UserFaves.push(this.id);
						});
						$('#right_grid .rightNav').html(html).fadeIn();
					}
						
					$('#faveAdded').fadeIn('fast',function(){
						window.setTimeout(function(){$('#faveAdded').fadeOut();},1000);
					});
				});
		},'json');
	});
});

function AbortAjax()
{
	//any previous ajax calls being processed - abort them
	if (typeof pageAJAX != 'undefined') {
		pageAJAX.abort();
		pageAjax = undefined;
	}
	if (typeof gridLoad != 'undefined') {
		gridLoad.abort();
		gridLoad = undefined;
	}
}
function hide_all_content_panels()
{
	$("#ListBox div[rel='content_panel']").each(function(){
		$this = $(this);
		if (!$this.hasClass("DisplayNone"))
			$this.addClass("DisplayNone");
	});
	
	$('div#grid_spot').html('');
	$('div#cms').html('');
}
function show_content_panel(panel,rightpanel)
{
	hide_all_content_panels();
	show_right_panel(rightpanel);
	$(panel).removeClass('DisplayNone');     
}
function hide_right_panels() {
	show_right_panel();
}
function show_right_panel(Panel)
{
	$("div[rel='right']").addClass("DisplayNone");
	if (typeof Panel != 'undefined')
	{
		$(Panel).removeClass("DisplayNone");
	}
}

