/*
----------------------------------------------------------------------------------------------------
Accessible News Slider
----------------------------------------------------------------------------------------------------
Author:
Brian Reindel, modified and adapted by Andrea Ferracani

Author URL:
http://blog.reindel.com, http://www.micc.unifi.it/ferracani

License:
Unrestricted. This script is free for both personal and commercial use.
*/

$.fn.accessNews = function( settings ) {
settings = $.extend({
headline : "Top Stories",
speed : "normal",
slideBy : 2
}, settings);
return this.each(function() {
$.fn.accessNews.run( $( this ), settings );
});
};

$.fn.accessNews.run = function( $this, settings ) {
$( ".javascript_css", $this ).css( "display", "none" );
var ul = $( "ul:eq(0)", $this );
var li = ul.children();
if ( li.length > settings.slideBy ) {
var $next = $( ".next > a", $this );
var $back = $( ".back > a", $this );
var liWidth = $( li[0] ).width();
var animating = false;
ul.css( "width", ( li.length * liWidth ) +100 );
$next.click(function() {
if ( !animating ) {
vv=$('#countflag').val();
var countflag=parseInt(vv)+1;
if(countflag<=4){
    $('#flagpaging').html(countflag+' of 4');
    $('#countflag').val(countflag);
}

    
animating = true;
offsetLeft = parseInt( ul.css( "left" ) ) - ( liWidth * settings.slideBy );
if ( offsetLeft + ul.width() > 0 ) {
$back.show();
ul.animate({
left: offsetLeft
}, settings.speed, function() {
if ( parseInt( ul.css( "left" ) ) + ul.width() <= liWidth * settings.slideBy ) {
}
animating = false;
});
} else {
animating = false;
}
}
return false;
});
$back.click(function() {
vv=$('#countflag').val();
var countflag=parseInt(vv)-1; 
if(countflag>0){
$('#flagpaging').html(countflag+' of 4');
$('#countflag').val(countflag);     
}


if ( !animating ) {
animating = true;
offsetRight = parseInt( ul.css( "left" ) ) + ( liWidth * settings.slideBy );
if ( offsetRight + ul.width() <= ul.width() ) {
$next.show();
ul.animate({
left: offsetRight
}, settings.speed, function() {
if ( parseInt( ul.css( "left" ) ) == 0 ) {

}
animating = false;
});
} else {
animating = false;
}
}
return false;
});

$next.show();
$(".description").hide();
$('#list').after( [ "<div class=\"view_all\"><span class=\"count1\">Featured Articles</span></div>" ].join( "" ) );
//$( ".back").appendTo('.view_all');
//$( ".next").appendTo('.view_all');
var date = $('<div></div>');
var firstimg = $( "ul li:eq(0) img", $this );
//date.appendTo("#container").html("<p class='date'><strong>TODAY NEWS</strong>: 2009 december 28</p>");
//img = $('<img width="370" height="246"></img>')
//img.appendTo("#container");
str=firstimg.attr('src');

str=str.replace('w=70','w=370');
str=str.replace('h=46','h=246');

//img.attr('src',str );

var firstli = $( "#newsslider ul li:eq(0)");
txturl=$('.txturl', firstli).text();
img2='<a href="'+txturl+'"><img src="'+str+'"></a>';
$('#container').append(img2);
para = $('<div class="fixheight"></div>');
para.appendTo("#container");

para.html('<h1><a href="'+txturl+'">' +  $('a.title', firstli ).text() + "<a/></h1>" + "<p id='paraText'>" + $('p.description', firstli).html() + "</p>");
firstli.addClass('selected');
li.hover(
function () {
    li.removeClass('selected')
    var current = $(this);
    current.addClass('selected');
    str=current.find('img').attr('src');
    str=str.replace('w=70','w=370');
    str=str.replace('h=46','h=246');
    txturl=$('.txturl', current).text();
    img2='<a href="'+txturl+'"><img src="'+str+'"></a>';
    $('#container a:first').replaceWith(img2);
    para.html('<h1><a href="'+txturl+'">' +  $('.title', current).text() + "</a></h1>" + "<p id='paraText'>" + $('.description', current).html() + "</p>");
    },function () {
        var current = $(this);
        current.parent().css('backgroundColor', 'transparent');
    }
);
}
};

