
jQuery.fn.accessNews = function (settings) {
    settings = jQuery.extend({
        headline: "Top Stories",
        speed: "normal",
        slideBy: 2
    }, settings);
    return this.each(function () {
        jQuery.fn.accessNews.run(jQuery(this), settings);
    });
};

jQuery.fn.accessNews.run = function ($this, settings) {
    jQuery(".javascript_css", $this).css("display", "none");
    var ul = jQuery("ul:eq(0)", $this);
    var li = ul.children();
	var index=1;
    if (li.length > settings.slideBy) {
		jQuery('#list').after(["<div class=\"view_all\"><span class=\"count\">"+index+"-"+settings.slideBy+" of "+li.length+ "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>برای بزرگ نمایی روی تصویر کلیک کنید</strong></span></div>"].join(""));
		jQuery(".back").appendTo('.view_all');
        jQuery(".next").appendTo('.view_all');
        var $next = jQuery(".next > a", $this);
        var $back = jQuery(".back > a", $this);
        var liWidth = jQuery(li[0]).width();
        var animating = false;
        ul.css("width", (li.length * liWidth));
		ul.css("background-color", "#ececec");
        $next.click(function () {
            if ( !animating ) {
				animating = true;
				offsetLeft = parseInt( ul.css( "left" ) ) - ( liWidth * settings.slideBy );
				if ( offsetLeft + ul.width() > 0 ) {
					index += settings.slideBy;//alert(index);
					$back.show();
					ul.animate({
						left: offsetLeft
					}, settings.speed, function() {
						if ( parseInt( ul.css( "left" ) ) + ul.width() <= liWidth * settings.slideBy ) {
						
						}
						animating = false;
					});
					$("#larrow").attr('src','images/left-arrow.png');
					if(((index+settings.slideBy)-1)==(li.length)){
						$("#rarrow").attr('src','images/right-arrow-stop.png');
					}
					jQuery('.count').html([index+"-"+((index+settings.slideBy)-1)+" of "+li.length+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>برای بزرگ نمایی روی تصویر کلیک کنید</strong>"].join(""));
				} else {
					animating = false;
				}
			}
			return false;
        });
        $back.click(function () {
            if (!animating) {
                animating = true;
                offsetRight = parseInt(ul.css("left")) + (liWidth * settings.slideBy);
                if (offsetRight + ul.width() <= ul.width()) {
					index -= settings.slideBy;//alert(index);
                    $next.show();
                    ul.animate({
                        left: offsetRight
                    }, settings.speed, function () {
                        if (parseInt(ul.css("left")) == 0) {

                        }
                        animating = false;
                    });
					$("#rarrow").attr('src','images/right-arrow.png');
					if(index==1){
						$("#larrow").attr('src','images/left-arrow-stop.png');
					}
					jQuery('.count').html([index+"-"+(settings.slideBy)+" of "+li.length+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>برای بزرگ نمایی روی تصویر کلیک کنید</strong>"].join(""));
                } else {
                    animating = false;
                }
            }
            return false;
        });

        $next.show();
        jQuery(".description").hide();
        var firstimg = jQuery("ul li:eq(0) img", $this);
        //var date = jQuery('<div></div>');
        //date.appendTo("#container").html("<p class='date'><strong>TODAY NEWS</strong>: 2009 december 28</p>");
        img = jQuery('<img></img>');
        img.appendTo("#container");
        img.attr('src', firstimg.attr('bigsrc'));
		img.attr('alt', firstimg.attr('alt'));
        para = jQuery('<div></div>');
        para.appendTo("#container");
        var firstli = jQuery("#newsslider ul li:eq(0)");
        para.html("<h1>" + jQuery('a.title', firstli).text() + "</h1>" + "<p id='paraText'>" + jQuery('p.description', firstli).html() + "</p>");
        firstli.addClass('selected');
        li.hover(function () {
			img.removeAttr('src');
			li.removeClass('selected')
			var current = jQuery(this);
			current.addClass('selected');
			img.attr('src', current.find('img').attr('bigsrc'));
			para.html("<h1>" + jQuery('.title', current).text() + "</h1>" + "<p id='paraText'>" + jQuery('.description', current).html() + "</p>");
		//jQuery('#container').attr('title', firstimg.attr('alt'));
		jQuery('#container').attr('href', current.find('a').attr('href'));
	},
	function () {
		current.parent().css('backgroundColor', 'transparent');
	});
}};

