var t;

$(document).ready(function() {
	$.ajaxSetup({cache: false});
	$("a.box").fancybox();
	$("a.logout").click(function() {
		
		event.preventDefault(); 
		logout();
	});

	$("a.listV").click(function() {
		$("div.fill").fadeOut(function() { $("div#list").fadeIn(); });
	});

	$("a.articleV").click(function() {
		$("div#list").fadeOut(function() { $("div.fill").fadeIn(); });
	});

	$("#em").click(function() {
		$("#lif").empty();
	});
	
	$("#pw").click(function() {
		$("#lif").empty();
	});

	$("#em").keydown(function() {
		$("#lif").empty();
	});
	
	$("#pw").keydown(function() {
		$("#lif").empty();
	});
	
	resize();
	
});

$(window).resize(function() {
	resize();
});

function resize() {
	var pageWidth = $(window).width();
	pageWidth = pageWidth * 0.98;
	var h1width = pageWidth - 300;
	var loadwidth = pageWidth - 50;
	loadwidth = loadwidth/2;
	h1width = h1width+"px";
	loadwidth = loadwidth+"px";
	pageWidth = pageWidth+"px";
	
	var pageHeight = $(window).height();
	pageHeight2 = pageHeight - 130;
	pageHeight = pageHeight2+"px";
	
	//if(pageHeight2<=620) { $(".fill h1").css("margin-top", "30px") }
	
	//else { $(".fill h1").css("margin-top", "100px") }
	
	$(".fill").css('width', pageWidth);
	//$(".fill").textfill({ maxFontPixels: 5000 });
	$("#list").css('width', pageWidth);
	$("#loader").css('left', loadwidth);
	$("#list .item h1").css('width', h1width);
	$(".feedBox .item h1").css('width', h1width);
	$(".fill h1").css('width', pageWidth);
}

function doSignSubmit(e) {
	e.preventDefault();
	$("img#signUpLoad").fadeIn();	
	$("#su").find("input[type='submit']").attr("disabled", true);
	var $form = $("#su"),
		term = $form.find( "input[name='emS']" ).val(),
		term2 = $form.find( "input[name='pwS']" ).val(),
		term3 = $form.find( "input[name='news']" ).is(':checked'),
		term4 = $form.find( "input[name='business']" ).is(':checked'),
		term5 = $form.find( "input[name='science']" ).is(':checked'),
		term6 = $form.find( "input[name='tech']" ).is(':checked'),
		term7 = $form.find( "input[name='entertainment']" ).is(':checked'),
		term8 = $form.find( "input[name='sport']" ).is(':checked'),
		url = $form.attr("action");

			$.post(url, { emS: term, pwS: term2, news: term3, business: term4, science: term5, tech: term6, entertainment: term7, sport: term8 },
			function(data) {
				$("img#signUpLoad").fadeOut();
				if(data=="OK") { $("a#fancybox-close").trigger("click"); $.facebox('<h3>Thanks for registering.</h3> <p>Please check your email for a message from us. Click on the link within that email to activate your account.</p><button class="closeButton" onclick=\'$("a.close").trigger("click")\'>OK</button>'); }
				else if(data=="FAIL") { $.facebox('<h3>There\'s been a problem.</h3> <p>Please wait a second and try again.</p><p>If the problem persists, please <a href="mailto:contact@hcetech.com">contact us</a>.</p><button class="closeButton" onclick=\'$("a.close").trigger("click")\'>OK</button>'); }
				else if(data=="Signed") {  $("a#fancybox-close").trigger("click"); $.facebox('<h3>You\'ve already signed up for ChewsNews.</h3><p>Please log in.</p><button class="closeButton" onclick=\'$("a.close").trigger("click")\'>OK</button>'); }
				else { $.facebox('<h3>There\'s been a problem.</h3> <p>Please wait a second and try again.</p><p>If the problem persists, please <a href="mailto:contact@hcetech.com">contact us</a>.</p><button class="closeButton" onclick=\'$("a.close").trigger("click")\'>OK</button>'); }
			  }
			);
}

function doSettingsChange(e) {
	e.preventDefault();
	$("img#signUpLoad").fadeIn();	
	$("#setF").find("input[type='submit']").attr("disabled", true);
	var $form = $("#setF"),
		term = $form.find( "input[name='npS']" ).val(),
		term1 = $form.find( "input.blackTheme" ).is(':checked'),
		term2 = $form.find( "input.whiteTheme" ).is(':checked'),
		term3 = $form.find( "input[name='news']" ).is(':checked'),
		term4 = $form.find( "input[name='business']" ).is(':checked'),
		term5 = $form.find( "input[name='science']" ).is(':checked'),
		term6 = $form.find( "input[name='tech']" ).is(':checked'),
		term7 = $form.find( "input[name='entertainment']" ).is(':checked'),
		term8 = $form.find( "input[name='sport']" ).is(':checked'),
		url = $form.attr("action");

			$.post(url, { npS: term, blackTheme: term1, whiteTheme: term2, news: term3, business: term4, science: term5, tech: term6, entertainment: term7, sport: term8 },
			function(data) {
				$("img#signUpLoad").fadeOut();
				/*if(data=="OK") { */$("a#fancybox-close").trigger("click"); $.facebox('<h3>Thanks for updating your settings.</h3><p>Click OK to log out. Once you\'ve logged back in, your new settings will come into effect.</p><button class="closeButton" onclick=\'$("a.close").trigger("click"); logout();\'>OK</button>');/* }*/
				//else { $.facebox('<h3>There\'s been a problem.</h3> <p>Please wait a second and try again.</p><p>If the problem persists, please <a href="mailto:contact@hcetech.com">contact us</a>.</p><button class="closeButton" onclick=\'$("a.close").trigger("click")\'>OK</button>'); }
			  }
			);
}

function doLogin(e) {
	/* stop form from submitting normally */
	e.preventDefault(); 
	$("img#signUpLoad").fadeIn();
	/* get some values from elements on the page: */
	var $form = $("form#l"),
		term = $form.find( "input[name='em']" ).val(),
		term2 = $form.find( "input[name='pw']" ).val(),
		url = "login.php";
	
	/* Send the data using post and put the results in a div */
	$.post(url, { user: term, pass: term2 },
	function(data) {
		$("img#signUpLoad").fadeOut();
		if(data=="OK") { top.location = "home.php"; }
		else { $("#lif").empty().append(data); }
	  }
	);
}


function loadFeed() {
	$("#loader").fadeIn();
	$.ajax( 
	{ 
		url: "feed.php", 
		cache: false,
		success: function(html) 
		{ 
			//$("#rightText").fadeIn();
			$("#list").empty().append(html);
			resize();
			var latest = $("#list").find("div.item:first");
			$("div.fill").animate({opacity: 0}, 500, function () {
				$("div.fill").empty().append(latest.html()).animate({opacity: 1}, 500, function () {
					resize();
					$("#loader").fadeOut();
										
					$("div.fill span h1 a, #list .item h1 a, a.readMe").click(function(event) {
						event.preventDefault();
						$.fancybox({
							'width': '90%',
							'height': '90%',
							'autoScale': true,
							'transitionIn': 'fade',
							'transitionOut': 'fade',
							'type': 'iframe',
							'href': 'read.php?url='+encodeURI($(this).attr("href"))
						});
					});
					$("a.share").click(function(event) {
						event.preventDefault();
						$.fancybox({
							'width': 400,
							'height': 400,
							'autoScale': true,
							'transitionIn': 'fade',
							'transitionOut': 'fade',
							'type': 'iframe',
							'href': encodeURI($(this).attr("href"))
						});
					});
					$("a.archiveMe").click(function(event) {
						event.preventDefault();
						$("#loader").fadeIn();
						$.ajax( 
							{ 
								url: "archive.php?url="+encodeURI($(this).attr("href")),
								cache: false,
								success: function(html) 
								{ 
									if(html=="OK") { $("#loader").fadeOut(); $.facebox('<h3>News story archived.</h3><button class="closeButton" onclick=\'$("a.close").trigger("click")\'>OK</button>'); }
									else { $.facebox('<h3>There\'s been a problem.</h3> <p>Please wait a second and try again.</p><p>If the problem persists, please <a href="mailto:contact@hcetech.com">contact us</a>.</p><button class="closeButton" onclick=\'$("a.close").trigger("click")\'>OK</button>'); }
							} 
						});

					});
				});
			});
		} 
	});
	t = setTimeout("loadFeed()", 120000);
}

function logout() {
		$.ajax({
			url: 'logout.php',
			success: function(data) {
				if(data=="OK") { top.location = "index.php" }
				else { $.facebox('<p>There was a problem logging out. We apologise for the inconvenience.</p> <p>Please wait a moment then try again.</p><button class="closeButton" onclick=\'$("a.close").trigger("click")\'>OK</button>'); }
			}
		});
}

function showNews() {
	if($("div.feedSelector a.showNews").hasClass("chosen")) { return false; }
	else {
		$("#loader").fadeIn();
		$.ajax( 
		{ 
			url: "category.php?t=news", 
			cache: false,
			success: function(html) 
			{ 
				$("div.feedSelector a.chosen").removeClass("chosen");
				$("div.feedSelector a.showNews").addClass("chosen");
				$("div#list, div.feedBox, div.fill").hide(0, function() {
					$("div#news").empty().append(html).show(0, function() {
						$("#loader").fadeOut();
						$("#news .item h1 a").click(function(event) {
							event.preventDefault();
							$.fancybox({
								'width': '90%',
								'height': '90%',
								'autoScale': true,
								'transitionIn': 'fade',
								'transitionOut': 'fade',
								'type': 'iframe',
								'href': 'read.php?url='+encodeURI($(this).attr("href"))
							});
						});
						$("#news .item a.readMe").click(function(event) {
							event.preventDefault();
							$.fancybox({
								'width': '90%',
								'height': '90%',
								'autoScale': true,
								'transitionIn': 'fade',
								'transitionOut': 'fade',
								'type': 'iframe',
								'href': 'read.php?url='+encodeURI($(this).attr("href"))
							});
						});
						$("#news .item a.share").click(function(event) {
							event.preventDefault();
							$.fancybox({
								'width': 400,
								'height': 400,
								'autoScale': true,
								'transitionIn': 'fade',
								'transitionOut': 'fade',
								'type': 'iframe',
								'href': encodeURI($(this).attr("href"))
							});
						});
						$("#news .item a.archiveMe").click(function(event) {
							event.preventDefault();
							$("#loader").fadeIn();
							$.ajax( 
								{ 
									url: "archive.php?url="+encodeURI($(this).attr("href")),
									cache: false,
									success: function(html) 
									{ 
										if(html=="OK") { $("#loader").fadeOut(); $.facebox('<h3>News story archived.</h3><button class="closeButton" onclick=\'$("a.close").trigger("click")\'>OK</button>'); }
										else { $.facebox('<h3>There\'s been a problem.</h3> <p>Please wait a second and try again.</p><p>If the problem persists, please <a href="mailto:contact@hcetech.com">contact us</a>.</p><button class="closeButton" onclick=\'$("a.close").trigger("click")\'>OK</button>'); }
								} 
							});
	
						});
					
					});
				});
			} 
		});
	}
}

function showBusiness() {
	if($("div.feedSelector a.showBusiness").hasClass("chosen")) { return false; }
	else {
		$("#loader").fadeIn();
		$.ajax( 
		{ 
			url: "category.php?t=business", 
			cache: false,
			success: function(html) 
			{ 
				$("div.feedSelector a.chosen").removeClass("chosen");
				$("div.feedSelector a.showBusiness").addClass("chosen");
				$("div#list, div.feedBox, div.fill").hide(0, function() {
					$("div#business").empty().append(html).show(0, function() {
						$("#loader").fadeOut();
						$("#business .item h1 a").click(function(event) {
							event.preventDefault();
							$.fancybox({
								'width': '90%',
								'height': '90%',
								'autoScale': true,
								'transitionIn': 'fade',
								'transitionOut': 'fade',
								'type': 'iframe',
								'href': 'read.php?url='+encodeURI($(this).attr("href"))
							});
						});
						$("#business .item a.readMe").click(function(event) {
							event.preventDefault();
							$.fancybox({
								'width': '90%',
								'height': '90%',
								'autoScale': true,
								'transitionIn': 'fade',
								'transitionOut': 'fade',
								'type': 'iframe',
								'href': 'read.php?url='+encodeURI($(this).attr("href"))
							});
						});
						$("#business .item a.share").click(function(event) {
							event.preventDefault();
							$.fancybox({
								'width': 400,
								'height': 400,
								'autoScale': true,
								'transitionIn': 'fade',
								'transitionOut': 'fade',
								'type': 'iframe',
								'href': encodeURI($(this).attr("href"))
							});
						});
						$("#business .item a.archiveMe").click(function(event) {
							event.preventDefault();
							$("#loader").fadeIn();
							$.ajax( 
								{ 
									url: "archive.php?url="+encodeURI($(this).attr("href")),
									cache: false,
									success: function(html) 
									{ 
										if(html=="OK") { $("#loader").fadeOut(); $.facebox('<h3>News story archived.</h3><button class="closeButton" onclick=\'$("a.close").trigger("click")\'>OK</button>'); }
										else { $.facebox('<h3>There\'s been a problem.</h3> <p>Please wait a second and try again.</p><p>If the problem persists, please <a href="mailto:contact@hcetech.com">contact us</a>.</p><button class="closeButton" onclick=\'$("a.close").trigger("click")\'>OK</button>'); }
								} 
							});
	
						});

					
					});
				});
			} 
		});
	}
}

function showScience() {
	if($("div.feedSelector a.showScience").hasClass("chosen")) { return false; }
	else {
		$("#loader").fadeIn();
		$.ajax( 
		{ 
			url: "category.php?t=science", 
			cache: false,
			success: function(html) 
			{ 
				$("div.feedSelector a.chosen").removeClass("chosen");
				$("div.feedSelector a.showScience").addClass("chosen");
				$("div#list, div.feedBox, div.fill").hide(0, function() {
					$("div#science").empty().append(html).show(0, function() {
						$("#loader").fadeOut();
						$("#science .item h1 a").click(function(event) {
							event.preventDefault();
							$.fancybox({
								'width': '90%',
								'height': '90%',
								'autoScale': true,
								'transitionIn': 'fade',
								'transitionOut': 'fade',
								'type': 'iframe',
								'href': 'read.php?url='+encodeURI($(this).attr("href"))
							});
						});
						$("#science .item a.readMe").click(function(event) {
							event.preventDefault();
							$.fancybox({
								'width': '90%',
								'height': '90%',
								'autoScale': true,
								'transitionIn': 'fade',
								'transitionOut': 'fade',
								'type': 'iframe',
								'href': 'read.php?url='+encodeURI($(this).attr("href"))
							});
						});
						$("#science .item a.share").click(function(event) {
							event.preventDefault();
							$.fancybox({
								'width': 400,
								'height': 400,
								'autoScale': true,
								'transitionIn': 'fade',
								'transitionOut': 'fade',
								'type': 'iframe',
								'href': encodeURI($(this).attr("href"))
							});
						});
						$("#science .item a.archiveMe").click(function(event) {
							event.preventDefault();
							$("#loader").fadeIn();
							$.ajax( 
								{ 
									url: "archive.php?url="+encodeURI($(this).attr("href")),
									cache: false,
									success: function(html) 
									{ 
										if(html=="OK") { $("#loader").fadeOut(); $.facebox('<h3>News story archived.</h3><button class="closeButton" onclick=\'$("a.close").trigger("click")\'>OK</button>'); }
										else { $.facebox('<h3>There\'s been a problem.</h3> <p>Please wait a second and try again.</p><p>If the problem persists, please <a href="mailto:contact@hcetech.com">contact us</a>.</p><button class="closeButton" onclick=\'$("a.close").trigger("click")\'>OK</button>'); }
								} 
							});
	
						});

					
					});
				});
			} 
		});
	}
}

function showTech() {
	if($("div.feedSelector a.showTech").hasClass("chosen")) { return false; }
	else {
		$("#loader").fadeIn();
		$.ajax( 
		{ 
			url: "category.php?t=tech", 
			cache: false,
			success: function(html) 
			{ 
				$("div.feedSelector a.chosen").removeClass("chosen");
				$("div.feedSelector a.showTech").addClass("chosen");
				$("div#list, div.feedBox, div.fill").hide(0, function() {
					$("div#tech").empty().append(html).show(0, function() {
						$("#loader").fadeOut();
						$("#tech .item h1 a").click(function(event) {
							event.preventDefault();
							$.fancybox({
								'width': '90%',
								'height': '90%',
								'autoScale': true,
								'transitionIn': 'fade',
								'transitionOut': 'fade',
								'type': 'iframe',
								'href': 'read.php?url='+encodeURI($(this).attr("href"))
							});
						});
						$("#tech .item a.readMe").click(function(event) {
							event.preventDefault();
							$.fancybox({
								'width': '90%',
								'height': '90%',
								'autoScale': true,
								'transitionIn': 'fade',
								'transitionOut': 'fade',
								'type': 'iframe',
								'href': 'read.php?url='+encodeURI($(this).attr("href"))
							});
						});
						$("#tech .item a.share").click(function(event) {
							event.preventDefault();
							$.fancybox({
								'width': 400,
								'height': 400,
								'autoScale': true,
								'transitionIn': 'fade',
								'transitionOut': 'fade',
								'type': 'iframe',
								'href': encodeURI($(this).attr("href"))
							});
						});
						$("#tech .item a.archiveMe").click(function(event) {
							event.preventDefault();
							$("#loader").fadeIn();
							$.ajax( 
								{ 
									url: "archive.php?url="+encodeURI($(this).attr("href")),
									cache: false,
									success: function(html) 
									{ 
										if(html=="OK") { $("#loader").fadeOut(); $.facebox('<h3>News story archived.</h3><button class="closeButton" onclick=\'$("a.close").trigger("click")\'>OK</button>'); }
										else { $.facebox('<h3>There\'s been a problem.</h3> <p>Please wait a second and try again.</p><p>If the problem persists, please <a href="mailto:contact@hcetech.com">contact us</a>.</p><button class="closeButton" onclick=\'$("a.close").trigger("click")\'>OK</button>'); }
								} 
							});
	
						});

					
					});
				});
			} 
		});
	}
}

function showEntertainment() {
	if($("div.feedSelector a.showEntertainment").hasClass("chosen")) { return false; }
	else {
		$("#loader").fadeIn();
		$.ajax( 
		{ 
			url: "category.php?t=entertainment", 
			cache: false,
			success: function(html) 
			{ 
				$("div.feedSelector a.chosen").removeClass("chosen");
				$("div.feedSelector a.showEntertainment").addClass("chosen");
				$("div#list, div.feedBox, div.fill").hide(0, function() {
					$("div#entertainment").empty().append(html).show(0, function() {
						$("#loader").fadeOut();
						$("#entertainment .item h1 a").click(function(event) {
							event.preventDefault();
							$.fancybox({
								'width': '90%',
								'height': '90%',
								'autoScale': true,
								'transitionIn': 'fade',
								'transitionOut': 'fade',
								'type': 'iframe',
								'href': 'read.php?url='+encodeURI($(this).attr("href"))
							});
						});
						$("#entertainment .item a.readMe").click(function(event) {
							event.preventDefault();
							$.fancybox({
								'width': '90%',
								'height': '90%',
								'autoScale': true,
								'transitionIn': 'fade',
								'transitionOut': 'fade',
								'type': 'iframe',
								'href': 'read.php?url='+encodeURI($(this).attr("href"))
							});
						});
						$("#entertainment .item a.share").click(function(event) {
							event.preventDefault();
							$.fancybox({
								'width': 400,
								'height': 400,
								'autoScale': true,
								'transitionIn': 'fade',
								'transitionOut': 'fade',
								'type': 'iframe',
								'href': encodeURI($(this).attr("href"))
							});
						});
						$("#entertainment .item a.archiveMe").click(function(event) {
							event.preventDefault();
							$("#loader").fadeIn();
							$.ajax( 
								{ 
									url: "archive.php?url="+encodeURI($(this).attr("href")),
									cache: false,
									success: function(html) 
									{ 
										if(html=="OK") { $("#loader").fadeOut(); $.facebox('<h3>News story archived.</h3><button class="closeButton" onclick=\'$("a.close").trigger("click")\'>OK</button>'); }
										else { $.facebox('<h3>There\'s been a problem.</h3> <p>Please wait a second and try again.</p><p>If the problem persists, please <a href="mailto:contact@hcetech.com">contact us</a>.</p><button class="closeButton" onclick=\'$("a.close").trigger("click")\'>OK</button>'); }
								} 
							});
	
						});

					
					});
				});
			} 
		});
	}
}

function showSport() {
	if($("div.feedSelector a.showSport").hasClass("chosen")) { return false; }
	else {
		$("#loader").fadeIn();
		$.ajax( 
		{ 
			url: "category.php?t=sport", 
			cache: false,
			success: function(html) 
			{ 
				$("div.feedSelector a.chosen").removeClass("chosen");
				$("div.feedSelector a.showSport").addClass("chosen");
				$("div#list, div.feedBox, div.fill").hide(0, function() {
					$("div#sport").empty().append(html).show(0, function() {
						$("#loader").fadeOut();
						$("#sport .item h1 a").click(function(event) {
							event.preventDefault();
							$.fancybox({
								'width': '90%',
								'height': '90%',
								'autoScale': true,
								'transitionIn': 'fade',
								'transitionOut': 'fade',
								'type': 'iframe',
								'href': 'read.php?url='+encodeURI($(this).attr("href"))
							});
						});
						$("#sport .item a.readMe").click(function(event) {
							event.preventDefault();
							$.fancybox({
								'width': '90%',
								'height': '90%',
								'autoScale': true,
								'transitionIn': 'fade',
								'transitionOut': 'fade',
								'type': 'iframe',
								'href': 'read.php?url='+encodeURI($(this).attr("href"))
							});
						});
						$("#sport .item a.share").click(function(event) {
							event.preventDefault();
							$.fancybox({
								'width': 400,
								'height': 400,
								'autoScale': true,
								'transitionIn': 'fade',
								'transitionOut': 'fade',
								'type': 'iframe',
								'href': encodeURI($(this).attr("href"))
							});
						});
						$("#sport .item a.archiveMe").click(function(event) {
							event.preventDefault();
							$("#loader").fadeIn();
							$.ajax( 
								{ 
									url: "archive.php?url="+encodeURI($(this).attr("href")),
									cache: false,
									success: function(html) 
									{ 
										if(html=="OK") { $("#loader").fadeOut(); $.facebox('<h3>News story archived.</h3><button class="closeButton" onclick=\'$("a.close").trigger("click")\'>OK</button>'); }
										else { $.facebox('<h3>There\'s been a problem.</h3> <p>Please wait a second and try again.</p><p>If the problem persists, please <a href="mailto:contact@hcetech.com">contact us</a>.</p><button class="closeButton" onclick=\'$("a.close").trigger("click")\'>OK</button>'); }
								} 
							});
	
						});

					
					});
				});
			} 
		});
	}
}

function showArchives() {
	if($("div.feedSelector a.showArchives").hasClass("chosen")) { return false; }
	else {
		$("#loader").fadeIn();
		$.ajax( 
		{ 
			url: "arcs.php", 
			cache: false,
			success: function(html) 
			{ 
				$("div.feedSelector a.chosen").removeClass("chosen");
				$("div.feedSelector a.showArchives").addClass("chosen");
				$("div#list, div.feedBox, div.fill").hide(0, function() {
					$("div#archives").empty().append(html).show(0, function() {
						$("#loader").fadeOut();
						$("#archives .item a").click(function(event) {
							event.preventDefault();
							$.fancybox({
								'width': '90%',
								'height': '90%',
								'autoScale': true,
								'transitionIn': 'fade',
								'transitionOut': 'fade',
								'type': 'iframe',
								'href': 'read.php?url='+encodeURI($(this).attr("href"))
							});
						});

					
					});
				});
			} 
		});
	}
}

function latest() {
	if($("div.feedSelector a.showLatest").hasClass("chosen")) { return false; }
	else {
		$("div#list, div.feedBox, div.fill").hide(0, function() {
			$("div.fill").show(0);
			$("div.feedSelector a.chosen").removeClass("chosen");
			$("div.feedSelector a.showLatest").addClass("chosen");
		});
	}
}

function allFeeds() {
	if($("div.feedSelector a.showAll").hasClass("chosen")) { return false; }
	else {
		$("div#list, div.feedBox, div.fill").hide(0, function() {
			$("div#list").show(0);
			$("div.feedSelector a.chosen").removeClass("chosen");
			$("div.feedSelector a.showAll").addClass("chosen");
		});
	}
}
