var selindex="";
var sectionindex="0";
var tabcol=new Array("#D5DBE6","#b9d8a9","#ddcf8f","#e9b5b5");
var sectiontype="news";

$(document).ready(function(){

	$(window).bind("load",function(){
		$('.datacontainer').corner("bottom tr");
		$('.nelist li a').corner("10px top");
		$('.datainner').corner("10px");
		$('.section_head').hide().eq(0).show();
		
		if(jQuery.browser.version!="8.0")
		{
			$('.section_head').corner("bottom 10px");
		}
		
		RefreshPosts();
	});
	
	$('.txtbox').val("");
	$('#pge_mask').FullWindow();
	$('#pge_mask').fadeTo(100,0.8);
	$('#comment_box').centreLI(); 
	$('#login_box').centreLogIn();

	$('.nelist li a').mouseover(function(){
		$(this).css("backgroundColor",tabcol[sectionindex]);
	});
	
	$('.nelist li a').mouseout(function(){
		$(this).css("backgroundColor","#F0F0F0");
	});

	$('.nelist li a').click(function(){
		if($(this).text()=="News")
		{
			RefreshPosts();	
		}
		else
		{
			RefreshEvents();	
		}
		return false;
	});
	
	
	
	$(window).resize(function(){
		$('#pge_mask').FullWindow();
		$('#comment_box').centreLI();
		$('#login_box').centreLogIn();
	});
	
	
	$('#ulo').click(function(){
		$.post("scripts/php/process.php",{func: "logout"},function(){
			window.location="default.php";				  
		});
	});
	
	$('.navlist li a').click(function(){
		sectionindex=$('.navlist li a').index(this);
		
		if($('.section_head').eq(sectionindex).css("display")=="none")
		{			
			$('.section_head').each(function(){
				if($(this).css("display")=="block")
				{
					$(this).css("display","none");
				}
			});
			$('.section_head').eq(sectionindex).css("display","block")
			RefreshPosts();
		}
	});
	
	
	$('#postinfo').submit(function(){
		
		if(CheckForVals($(this)))
		{
			if($('.buttoncorp').eq(0).val()=="Update Post")
			{
				//UPDATE
				f1=$('.txtbox').eq(0).val();
				f2=$('.txtbox').eq(1).val();
				f3=$('.selectbox').eq(2).val()+"-"+$('.selectbox').eq(1).val()+"-"+$('.selectbox').eq(0).val();
				
				if(sectiontype=="news")
					{
					$.post("scripts/php/process.php",{
					func: "update", recordid: selindex, f1: f1, f2: f2, newstable: sectionindex},function(data){
						if(data)
						{
							selindex="";
							$('.buttoncorp').eq(0).val("Submit Post");
							$('.txtbox').val("");
							$('#returnresult').text("Record updated successfully.");
							RefreshPosts();
						}
						else
						{
							$('#returnresult').text("Unable to update record.");
						}
					});
				}
				else if(sectiontype=="events")
				{
					$.post("scripts/php/process.php",{
					func: "updateevent", recordid: selindex, f1: f1, f2: f2, f3: f3, eventtable: sectionindex},function(data){
						if(data)
						{
							selindex="";
							$('.buttoncorp').eq(0).val("Submit Post");
							$('.txtbox').val("");
							$('#returnresult').text("Event updated successfully.");
							RefreshEvents();
						}
						else
						{
							$('#returnresult').text("Unable to update Event.");
						}
					});	
				}	
			}
			else if($('.buttoncorp').eq(0).val()=="Submit Post")
			{
				f1=$('.txtbox').eq(0).val();
				f2=$('.txtbox').eq(1).val();
				f3=$('.selectbox').eq(2).val()+"-"+$('.selectbox').eq(1).val()+"-"+$('.selectbox').eq(0).val();
				
				if(sectiontype=="news")
				{
					$.post("scripts/php/process.php",{
					func: "addto", recordid: selindex, f1: f1, f2: f2, newstable: sectionindex},function(data){
						if(data)
						{
							$('.txtbox').val("");
							$('#returnresult').text("Record added successfully.");
							RefreshPosts();
						}
						else
						{
							$('#returnresult').text("Unable to add record.");
						}
					});	
				}
				else if(sectiontype=="events")
				{
					$.post("scripts/php/process.php",{
					func: "addtoevent", recordid: selindex, f1: f1, f2: f2, f3: f3, eventtable: sectionindex},function(data){
						if(data)
						{
							$('.txtbox').val("");
							$('#returnresult').text("Event added successfully.");
							RefreshEvents();
						}
						else
						{
							$('#returnresult').text("Unable to add event.");
						}
					});
				}	
			}
		}
		else
		{
			$('#returnresult').text("Please ensure all fields are filled in correctly.");	
		}
		return false;
	});
	
	
	
	
	$('.buttoncorp').click(function(){
		if($(this).val()=="No")
		{
			$('#pge_wrap').css("display","none");
		}
		else if($(this).val()=="Yes")
		{
			$.post("scripts/php/process.php",{
			func: "removerec", recordid: selindex, newstable: sectionindex, delfrom: sectiontype},function(data){
				$('#pge_wrap').css("display","none");
				if(sectiontype=="news")
				{
					RefreshPosts();
				}
				else if(sectiontype=="events")
				{
					RefreshEvents();
				}
								  
			});
		}
	});
	
	
	
	$('.buttoncorp').eq(1).click(function(){
		$('.buttoncorp').eq(0).val("Submit Post");									
	});
	
	$('#deletepost').submit(function(){
		return false;
	});
	
	$('li.ico_remove').click(function(){
		selindex=$(this).children().attr("rel");
		$('#pge_wrap').css("display","block");
		$('#comment_box').centreLI(); 
	});
	
	$('li.ico_edit').click(function(){
		selindex=$(this).children().attr("rel");		
		$.post("scripts/php/process.php",{func: "editrec", recordid: selindex, selelement: "title"},function(data){
			$('.txtbox').eq(0).val(data);					  
		});
		$.post("scripts/php/process.php",{func: "editrec", recordid: selindex, selelement: "content"},function(data){
			$('.txtbox').eq(1).val(data);
		});
		
		$('.buttoncorp').eq(0).val("Update Post");
	});
	

	$('#li_prompt').submit(function(){
		$('#returnresult').text("Processing...");
		if(CheckForVals($(this)) && CheckEmail($('.txtbox').eq(0).val()))
		{
			
			i1=$('.txtbox').eq(0).val();
			i2=$('.txtbox').eq(1).val();
			
			$.post("scripts/php/chkli.php",{func: "chklog", f1: i1, f2: i2},function(data){
				if(data=="true")
				{
					$('#returnresult').text("Redirecting....");
					window.location="home.php";
				}
				else
				{
					$('#returnresult').text("Please ensure all fields are filled in correctly.");
				}
			});
		}
		else
		{
			$('#returnresult').text("Please ensure all fields are filled in correctly.");
		}
		return false;
	});
	

});

//Jquery functions
(function($) {
	$.fn.centreLI = function(){
		w_height=$(window).height()/2;
		w_width=$(window).width()/2;
		
		li_height=$(this).height()/2;
		li_width=$(this).width()/2;
		
		li_top=w_height-li_height;
		li_left=w_width-li_width;
		
		$(this).css({
			"top": li_top+"px",
			"left": li_left+"px"
		});
	};
	
	$.fn.centreLogIn = function(){
		w_height=$(window).height()/2;
		li_height=$(this).height()/2;
		li_top=w_height-li_height;
		
		$(this).css("top",li_top+"px");
	};
	
	$.fn.FullWindow = function(){
		w_height=$(window).height();
		w_width=$(window).width();
		$(this).css({
			"width": w_width+"px",
			"height": w_height+"px"
		});
	};
	
})(jQuery);

//javascript functions

function CheckEmail(inputemail) {
	AtPos = inputemail.indexOf("@");
	StopPos = inputemail.lastIndexOf(".");

	if (AtPos == -1 || StopPos == -1)
	{
		return false;
	}
	else
	{
		return true;	
	}

}

function CheckForVals(frmname)
{
	j=0;
	frmname.children('p').children('.txtbox').each(function(i){
		if($(this).val()!="")
		{
			j++;	
		}
	});
	
	if(j==frmname.children('p').children('.txtbox').size())
	{
		return true;
	}
	else
	{
		return false;	
	}
}

function RefreshPosts()
{
		sectiontype="news";
		$('#postinfoselectdate').hide();
		$('#postsheading').text("News");
		$('#bloginfo').html("<p>Refreshing</p>");
		$('#bloginfo').css("background-position","center center");
		$('.txtbox').val("");
		$('.buttoncorp').eq(0).val("Submit Post");
		
		
		$.post("posts.php",{secindex:sectionindex, posttype:"news"},function(data){
			$('#bloginfo').html(data);	
			
			$('#bloginfo').css("background-position","-250px center");
			$('#returnresult').text("");
			
			$('li.ico_remove').click(function(){
				selindex=$(this).children().attr("rel");
				$('#pge_wrap').css("display","block");
				$('#comment_box').centreLI(); 
			});
			
			$('li.ico_edit').click(function(){
				selindex=$(this).children().attr("rel");

				$.post("scripts/php/process.php",{
				func: "editrec", recordid: selindex, selelement: "title", newstable: sectionindex, formtype: "news"},function(data){
					$('.txtbox').eq(0).val(data);					  
				});
				
				$.post("scripts/php/process.php",{
				func: "editrec", recordid: selindex, selelement: "content", newstable: sectionindex, formtype: "news"},function(data){
					$('.txtbox').eq(1).val(data);					  
				});
				
				$('.buttoncorp').eq(0).val("Update Post");
			});	
		});
}

function RefreshEvents()
{
		sectiontype="events";
		$('#postinfoselectdate').show();
		$('#postsheading').text("Events");
		$('#bloginfo').html("<p>Refreshing</p>");
		$('#bloginfo').css("background-position","center center");
		$('.txtbox').val("");
		$('.buttoncorp').eq(0).val("Submit Post");
		
		
		$.post("posts.php",{secindex:sectionindex, posttype:"events"},function(data){
			$('#bloginfo').html(data);	
			
			$('#bloginfo').css("background-position","-250px center");
			$('#returnresult').text("");
			
			$('li.ico_remove').click(function(){
				selindex=$(this).children().attr("rel");
				$('#pge_wrap').css("display","block");
				$('#comment_box').centreLI(); 
			});
			
			$('li.ico_edit').click(function(){
				selindex=$(this).children().attr("rel");
				
				
				$.post("scripts/php/process.php",{
				func: "editrec", recordid: selindex, selelement: "title", newstable: sectionindex, formtype: "events"},function(data){
					$('.txtbox').eq(0).val(data);					  
				});
				
				$.post("scripts/php/process.php",{
				func: "editrec", recordid: selindex, selelement: "content", newstable: sectionindex, formtype: "events"},function(data){
					$('.txtbox').eq(1).val(data);					  
				});
				
				//selectbox D M Y
				$.post("scripts/php/process.php",{
				func: "editrecreturndate", recordid: selindex, selelement: "eventdate", newstable: sectionindex},function(data){
					
					reDate=data.split("-",-1);
					SelectFromSelctBox(0,reDate[0]);
					SelectFromSelctBox(1,reDate[1]);
					SelectFromSelctBox(2,reDate[2]);
				});
				
				
				$('.buttoncorp').eq(0).val("Update Post");
			});	
		});
}

function SelectFromSelctBox(elementId, InputNumber)
{
	$('.selectbox').eq(elementId).children("option").each(function(){
		if($(this).val()==InputNumber)
		{
			$(this).attr("selected","selected");	
		}
	});
}
