/*
----------------------------------------------------------------------------------------------------
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.
*/

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();
if ( li.length > settings.slideBy ) {
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 ) );
$next.click(function() {
if ( !animating ) {
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() {
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();
jQuery(".description").hide();
jQuery(".h1Title").hide();
jQuery('#list').after( [ "<div class=\"view_all\"><span class=\"count\"><a class=readHome href='/browse_by/category/announcements/'>More news</a></span></div>" ].join( "" ) );
jQuery( ".back").appendTo('.view_all');
jQuery( ".next").appendTo('.view_all');
var date = jQuery('<div></div>');
var firstimg = jQuery( "ul li:eq(0) img", $this );
//date.appendTo("#container").html("<p class='date'><strong>TODAY NEWS</strong>: 2009 december 28</p>");
para = jQuery('<div class=boxCont></div>');
para.appendTo("#containerHome");
var firstli = jQuery( "#newsslider ul li:eq(0)");
//para.html("<h1>" + jQuery('a.title', firstli ).text() + "</h1>"  + "<div class=images></div><div class=abstract><div id='paraText'>" + jQuery('.description', firstli).html() + "</div></div>");
para.html("<div class=mainTitle><h1>"+ jQuery('.h1Title', firstli ).text()+"</h1></div><div class=images></div><div class=abstract><div id='paraText'>" + jQuery('.description', firstli).html() + "</div></div>");

// ********************** PRIMO

// primo caricamento della pagina
	firstimg = $("#list li:eq(0) img");
	var myimgmax = new Image();
	myimgmax.onload= function(){
		mywidth= myimgmax.width;
		myheight= myimgmax.height;
		if (mywidth == 0) {
					var img = jQuery('<img class="bigImage" style="height: 0px; width: 0px;"/>');
				//	jQuery('<style>.images {border:0;width:0px; height:180px; margin-left:0px;}</style>').appendTo("#containerHome");
					$(function() {
			    $(".images").css( 'border','0');
			    $(".images").css( 'width','0');
			    $(".images").css( 'height','180px');
			    $(".images").css( 'float','left');
			    $(".images").css( 'margin-left','5px');
			    $(".images").css( 'margin-right','10px');
				//$('<style>.images {border:solid;width:0px; height:180px; margin-left:0px;}</style>').appendTo(document.body);
			});
		}
		if (mywidth<=180 && mywidth>0)
		{
//			alert(1);
			var img = jQuery('<img class="bigImage" style="float:left; height:180px;"/>')
		//	jQuery('<style>.images {border:0;  width:auto; overflow:hidden;float:left;margin-right:8px; margin-left:5px;}</style>').appendTo("#containerHome");
			$(function() {
			    $(".images").css( 'border','0');
			    $(".images").css( 'width','auto');
			    $(".images").css( 'overflow','hidden');
			    $(".images").css( 'float','left');
			    $(".images").css( 'margin-right','10px');
			    //$(".images").css( 'height','180px');
			    $(".images").css( 'margin-left','5px');
				//$('<style>.images {border:solid;width:0px; height:180px; margin-left:0px;}</style>').appendTo(document.body);
			});
		}
		if (mywidth>180 && mywidth>0 && (mywidth < myheight))
		{
//			alert(11);
			var img = jQuery('<img class="bigImage" style="float:left; width:320px; height:auto;"/>')
		//	jQuery('<style>.images {border:0; width:320px;overflow:hidden;float:left;margin-right:8px; margin-left:5px;}</style>').appendTo("#containerHome");
			$(function() {
			    $(".images").css( 'border','0');
			    $(".images").css( 'width','320px');
			    $(".images").css( 'overflow','hidden');
			    $(".images").css( 'float','left');
			    $(".images").css( 'margin-right','10px');
			    //$(".images").css( 'height','180px');
			    $(".images").css( 'margin-left','5px');
				//$('<style>.images {border:solid;width:0px; height:180px; margin-left:0px;}</style>').appendTo(document.body);
			});
		}
		if (mywidth > myheight && mywidth>0 && myheight>0)
		{
//			alert(111);
		//	jQuery('<style>.images {border:0; width:320px; height:180px; overflow:hidden;float:left;margin-right:8px; margin-left:5px;}</style>').appendTo("#containerHome");
			var img = jQuery('<img class="bigImage" style="float:left; height:auto;width:320px;"/>');
				$(function() {
			    $(".images").css( 'border','0');
			    $(".images").css( 'width','320px');
			    $(".images").css( 'overflow','hidden');
			    $(".images").css( 'float','left');
			    $(".images").css( 'margin-right','10px');
			    $(".images").css( 'height','180px');
			    $(".images").css( 'margin-left','5px');
				//$('<style>.images {border:solid;width:0px; height:180px; margin-left:0px;}</style>').appendTo(document.body);
			});			
		}
		img.appendTo("#containerHome");
		img.attr('src', firstimg.attr('src'));
		img.appendTo('.images');
		firstli.addClass('selected');
	
		} // end onload
		myimgmax.src = 'http://'+window.location.hostname + firstimg.attr('src');

//alert(1);
/*
if ((mywidth==0 || myheight==0)) {
	// first time the script is loaded an no image is in memory
	img = jQuery('<img class="bigImage" style="float:left; height: 180px; padding-left: 4px; padding-right:5px; "></img>');
	mywidth = -1;
	myheight = -1;
				firstimg = $("#list li:eq(0) img");
			var myimgmax = new Image();
			myimgmax.onload= function(){
				mywidth= myimgmax.width;
				myheight= myimgmax.height;
				alert(myheight);
				}
				myimgmax.src = 'http://'+window.location.hostname + firstimg.attr('src');
	//jQuery('<style>.images {border:0;  width:auto; overflow:hidden;float:left;margin-right:8px; margin-left:5px;}</style>').appendTo("#containerHome");
}	
*/	

// ********************** FINE PRIMO

}

li.hover(
function () {
li.removeClass('selected');
var current = jQuery(this);
current.addClass('selected');
//para.html("<h1>" +  jQuery('.title', current).text() + "</h1>" + "<div class=images></div><div class=abstract><div id='paraText'>" + jQuery('.description', current).html() + "</div></div>");
para.html("<div class=mainTitle><h1>"+ jQuery('.h1Title', current ).text()+"</h1></div><div class=images></div><div class=abstract><div id='paraText'>" + jQuery('.description', current).html() + "</div></div>");

// ********************** SECONDO
var imgmax = new Image();
mywidth =0;
imgmax.src = 'http://'+window.location.hostname + current.find('img').attr('src'); //  // 'http://www.bioversityinternational.org/uploads/media/Bananas.jpg'; //firstimg.attr('src');
var mywidth= imgmax.width+0;
var myheight= imgmax.height+0;
//alert(current.find('img').attr('src'));
imgmax = '';
//alert(2);

if (mywidth<=180 && mywidth>0)
{
//	alert(current.find('img').attr('src'));

	img = jQuery('<img class="bigImage" style="float:left; width:auto; height:180px;"/>')
	//jQuery('<style>.images {border:0; width:auto; height:auto; overflow:hidden;float:left;margin-right:8px; margin-left:5px;}</style>').appendTo("#containerHome");
		$(function() {
			    $(".images").css( 'border','0');
			    $(".images").css( 'width','auto');
			    $(".images").css( 'overflow','hidden');
			    $(".images").css( 'float','left');
			    $(".images").css( 'margin-right','10px');
			    $(".images").css( 'height','auto');
			    $(".images").css( 'margin-left','5px');
				//$('<style>.images {border:solid;width:0px; height:180px; margin-left:0px;}</style>').appendTo(document.body);
			});	
}
if (mywidth<=180 && mywidth<=30)
{
	//alert(mywidth);
		img = jQuery('<img class="bigImage" style="display:none"/>')
	//jQuery('<style>.images {border:0; width:auto; height:auto; overflow:hidden;float:left;margin-right:8px; margin-left:5px;}</style>').appendTo("#containerHome");
		$(function() {
			    $(".images").css( 'border','0');
			    $(".images").css( 'width','auto');
			    $(".images").css( 'overflow','hidden');
			    $(".images").css( 'float','left');
			    $(".images").css( 'margin-right','10px');
			    $(".images").css( 'height','auto');
			    $(".images").css( 'margin-left','5px');
				//$('<style>.images {border:solid;width:0px; height:180px; margin-left:0px;}</style>').appendTo(document.body);
			});	
}
if (mywidth>180 && mywidth>0)
{
	img = jQuery('<img class="bigImage" style="float:left; width:320px; height:auto;"/>')
	//jQuery('<style>.images {border:0; width:320px;height:auto; overflow:hidden;float:left;margin-right:8px; margin-left:5px;}</style>').appendTo("#containerHome");
		$(function() {
			    $(".images").css( 'border','0');
			    $(".images").css( 'width','320px');
			    $(".images").css( 'overflow','hidden');
			    $(".images").css( 'float','left');
			    $(".images").css( 'margin-right','10px');
			    $(".images").css( 'height','auto');
			    $(".images").css( 'margin-left','5px');
				//$('<style>.images {border:solid;width:0px; height:180px; margin-left:0px;}</style>').appendTo(document.body);
			});
}

if (mywidth > myheight && mywidth>0 && myheight>0)
{
	//jQuery('<style>.images {border:0; width:320px; height:180px; overflow:hidden;float:left;margin-right:8px; margin-left:5px;}</style>').appendTo("#containerHome");
	img = jQuery('<img class="bigImage" style="float:left; width:320px; height:auto;"/>')
		$(function() {
			    $(".images").css( 'border','0');
			    $(".images").css( 'width','320px');
			    $(".images").css( 'overflow','hidden');
			    $(".images").css( 'float','left');
			    $(".images").css( 'margin-right','10px');
			    $(".images").css( 'height','180px');
			    $(".images").css( 'margin-left','5px');
				//$('<style>.images {border:solid;width:0px; height:180px; margin-left:0px;}</style>').appendTo(document.body);
			});
}

else {
	if (mywidth < myheight ) {
		//img = jQuery('<img class="bigImage" style="float:left; height: 180px; width:auto;padding-left: 4px; padding-right:5px;"></img>');	
        //	alert(current.find('img').attr('src'));
	} 
	else 
		if (mywidth > -1) 
		{
		    
			// mywidth=0 OR myheight=0 (no image at all! UNDEFINED IMAGE)
			img = jQuery('<img class="bigImage" style="height: 0px; width: 0px; display:none;"/>');
	//		jQuery('<style>.images {border:0;width:0px; height:180px; margin-left:0px;}</style>').appendTo("#containerHome");
			$(function() {
			    $(".images").css( 'border','0');
			    $(".images").css( 'width','0px');
			    $(".images").css( 'height','180px');
			    $(".images").css( 'margin-left','0px');
			    $(".images").css( 'float','left');
   			    $(".images").css( 'margin-right','10px');
   			    $(".images").css( 'margin-left','5px');			    
//				var debug = new jQuery.debug();
//				debug.dump($(".images").css());

			    
			});
		}
	
}

// **********************
//img = jQuery('<img class="bigImage" style="float:left; height: 180px; padding-left: 4px; padding-right:5px; "></img>')
//alert('secondo:'+mywidth+','+myheight+current.find('img').attr('src'));
img.attr('src', current.find('img').attr('src'));
img.appendTo('.images');
},
function () {
//current.parent().css('backgroundColor', 'transparent');
}


);
//$('#list li:eq(0)').trigger('mouseover');
 
};

