/*-------------------------------------------------------+
| Ajax News Panel
| Copyright (C) 2009 bartek124
| http://www.bartek124.net/
+--------------------------------------------------------+
| Filename: jquery.ajax_news.php
| Author: bartek124
+--------------------------------------------------------+
| This program is released as free software under the
| Affero GPL license. You can redistribute it and/or
| modify it under the terms of this license which you
| can read by viewing the included agpl.txt or online
| at www.gnu.org/licenses/agpl.html. Removal of this
| copyright header is strictly prohibited without
| written permission from the original author(s).
+--------------------------------------------------------*/

(function($) {
	$.fn.jAjaxNews = function(settings) {
		settings = $.extend({
			prevLink: '.anp-prev-news',
			nextLink: '.anp-next-news',
			newsNum: "input[name='news_num']",
			newsRows: "input[name='news_rows']",
			newsScript: 'news.php?news=',
			editIcon: 'images/edit.gif',
			printIcon: 'images/print.gif'
		}, settings);
		settings.newsNum = $(settings.newsNum).val();
		$.fn.jAjaxNews.links(this, settings);
		
	}
	
	$.fn.jAjaxNews.links = function(element, settings) {
		$(settings.prevLink).click(function() {
			$.fn.jAjaxNews.response(element, settings, 'prev');
			return false;
		});
		
		$(settings.nextLink).click(function() {
			$.fn.jAjaxNews.response(element, settings, 'next');
			return false;
		});
	}
	
	
	$.fn.jAjaxNews.response = function(element, settings, direction) {
		var rows = $(settings.newsRows).val();
		$(element).fadeTo("fast", 0.01, function() {
			if(direction == 'next') {
				settings.newsNum++;
				$(element).load(settings.newsScript+(settings.newsNum)+"&rows="+rows, function() {
					
					$("img.edit-icon").attr("src", settings.editIcon);
					$("img.print-icon").attr("src", settings.printIcon);
					$.fn.jAjaxNews.links(element, settings);
					$(element).fadeTo("fast", 1.0);
				});	
			} else if(direction == 'prev') {
				settings.newsNum--;
				$(element).load(settings.newsScript+(settings.newsNum)+"&rows="+rows, function() {
					$("img.edit-icon").attr("src", settings.editIcon);
					$("img.print-icon").attr("src", settings.printIcon);
					$.fn.jAjaxNews.links(element, settings);
					$(element).fadeTo("fast", 1.0);
				});	
			}
		});
	}
})(jQuery);