$(function(){
		jQuery('#casosExito').jcarousel({
			vertical: false,
			scroll:1,
			auto:4,
			animation: "slow",
			wrap: 'last',
			easing:"easeOutExpo",
			buttonNextHTML:null,
			buttonPrevHTML:null,
			itemVisibleOutCallback: {onAfterAnimation: function(carousel, item, i, state, evt) { carousel.remove(i); }},
			itemVisibleInCallback: {onBeforeAnimation: function(carousel, item, i, state, evt) { /*debug(item,true)*/ }},
			itemLoadCallback: casosExito_itemLoadCallback
		});
})
//.animate({opacity:1},500,"swing");
var nPag = 0
var _topQuery = 2;
function casosExito_itemLoadCallback(carousel, state){
    // Lock until all items are loaded. That prevents jCarousel from
    // setup correctly and we have to do that in the ajax callback
    // function with carousel.setup().
    // We're doing that because we don't know the exact height of each
    // items until they are added to the list.
 
 // Check if the requested items already exist
    if (carousel.has(carousel.first, carousel.last)) {
        return;
    }
	nPag++
	carousel.lock()
    if ( typeof(_globalAmbito).toString() == "undefined" ) _globalAmbito = "port"
	var url = "aspimagina/noticias/mod/exitos/picker/returnAjaxPiker.asp"
    jQuery.getJSON(url,{iPag:nPag,topQuery:_topQuery,ambito:_globalAmbito},
        function(data, textStatus) {
			if(data.noticias[0].titulo.length==0) $("#casosExito").hide();
/*			debug("nNoticias: " + data.noticias.length,true)
			debug("nNoticias titulo : " + data.noticias[0].titulo.length,true)
			debug("data.iPag: " + data.iPag,true)
*/			nPag = data.iPag 
            casosExito_itemAddCallback(carousel, data);
			carousel.unlock()
        }
    );

};
function noticiaPublicitaria(){
	
	
}
function casosExito_itemAddCallback(carousel, data)
{
    // Simply add all items at once and set the size accordingly.
	var size = data.noticias.length;
	
	if( size < _topQuery){
		data.noticias.push({params:data.noticias[0].params,fecha:data.noticias[0].fecha,titulo:data.noticias[0].titulo,imagen:data.noticias[0].imagen,descripcion:data.noticias[0].descripcion});
		nPag = 0;
		size = _topQuery
	}
	
	$(data.noticias).each(function(i) {
        carousel.add(i + 1, casosExito_getItemHTML(this));
    });
    carousel.size(size);
   
};

/**
 * Item html creation helper.
 */
function casosExito_getItemHTML(e)
{
//	debug(e.imagen,true)
	var strBuilder = new String();
	strBuilder += "<div onclick=\"location.href='web.asp?" + e.params + "'\" style=\"cursor:pointer;cursor:hand;\" >"
	if (e.imagen.length > 0 )
		strBuilder += "<img class='imagPicker' src= \"" + e.imagen + "\" />"

	strBuilder += "<h5><a href=\"web.asp?" + e.params + "\">"
	strBuilder += e.titulo
	/*strBuilder += "<span class=\"fecha\">["
	strBuilder += e.fecha	
	strBuilder += "]</span>"*/
	strBuilder += "</a></h5>"
	strBuilder += "<p>"
	strBuilder += casosExito_truncate(e.descripcion,150)
	strBuilder += "</p>"
	strBuilder += "</div>"
	
    return strBuilder;
};

/**
 * Utility function for truncating a string without breaking words.
 */
function casosExito_truncate(str, length, suffix) {
    if (str.length <= length) {
        return str;
    }

    if (suffix == undefined) {
        suffix = '...';
    }

    return str.substr(0, length).replace(/\s+?(\S+)?$/g, '') + suffix;
};