﻿$(function () {
    getNoticia($('#cnt li[class="selected"]').attr("idn"));
    $(".ntcContenedor a,#cnt a").click(function () { $.get("cNew.ashx?idN=" + $(this).attr("idn"), null); });
    $("#cnt li").each(function () {
        $(this).mouseover(changeStyle);
        $(this).mouseover(populate);
    });
});

(function() {
    $.fn.infiniteCarousel=function() {
        function repeat(str,n) {
            return new Array(n+1).join(str);
        }

        return this.each(function() {
            // magic!
            var $wrapper=$('> div',this).css('overflow','hidden'),
                $slider=$wrapper.find('> ul').width(2300),
                $items=$slider.find('> li'),
                $single=$items.filter(':first')

            singleWidth=$single.outerWidth(),
                visible=Math.ceil($wrapper.innerWidth()/singleWidth),
                currentPage=1,
                pages=Math.ceil($items.length/visible);

            $(".paginado span").html("1 de "+pages);

            /* TASKS */

            // 1. pad the pages with empty element if required
            if($items.length%visible!=0) {
                // pad
                $slider.append(repeat('<li class="empty" />',visible-($items.length%visible)));
                $items=$slider.find('> li');
            }

            // 2. create the carousel padding on left and right (cloned)

            $items.filter(':first').before($items.slice(-visible).clone());
            $items.filter(':last').after($items.slice(0,visible).clone());
            $items=$slider.find('> li');

            // 3. reset scroll
            $wrapper.scrollLeft(singleWidth*visible);

            // 4. paging function
            function gotoPage(page) {
                var dir=page<currentPage?-1:1,
                    n=Math.abs(currentPage-page),
                    left=singleWidth*dir*visible*n;

                if(page>pages) { $(".paginado span").html("1 de "+pages);; }
                else if(page==0) { $(".paginado span").html(pages+" de "+pages); }
                else { $(".paginado span").html(page+" de "+pages); }
                $wrapper.filter(':not(:animated)').animate({
                    scrollLeft: '+='+left
                },500,function() {
                    // if page == last page - then reset position
                    if(page>pages) {
                        $wrapper.scrollLeft(singleWidth*visible);
                        page=1;
                    } else if(page==0) {
                        page=pages;
                        $wrapper.scrollLeft(singleWidth*visible*pages);
                    }
                    currentPage=page;
                });
            }

            // 6. bind the back and forward links
            $('a.prev').click(function() {
                gotoPage(currentPage-1);
                return false;
            });

            $('a.next').click(function() {
                gotoPage(currentPage+1);
                return false;
            });

            $(this).bind('goto',function(event,page) {
                gotoPage(page);
            });

            $(this).bind('next',function() {
                gotoPage(currentPage+1);
            });
        });
    };
})(jQuery);

$(document).ready(function () {
    var autoscrolling = true;
    $('#cnt').infiniteCarousel();
    /*
    $('#cnt').infiniteCarousel().mouseover(function() {
    autoscrolling=false;
    }).mouseout(function() {
    autoscrolling=true;
    });

    /*setInterval(function () {
    if (autoscrolling) {
    $('#cnt').trigger('next');
    }
    }, 7500);
    */
});
function populate() {
    getNoticia($(this).attr("idN"));
}
function changeStyle() {
    $(this).parent().find("li").each(function() {
        $(this).attr("class","");
    });
    $(this).attr("class","selected");
}
function getNoticia(idN) {
    $.get("News.ashx?idN="+idN,function(d) {
        $(d).find("Noticia").each(function() {
            var n=$(this);
            $("#im").attr("src",n.find("Imagen").text());
            $("#hp2").html(n.find("Title").text());
            $(".lbody p span").html(n.find("Description").text());
            $("#hp1,#hp2,#hp3").each(function() {
                $(this).attr("href",n.find("Link").text());
                $(this).attr("idn",n.find("IdPublicacion").text());
            });
            $("#fbook").attr("href","http://www.facebook.com/sharer.php?u={0}&t={1}".replace("{0}",n.find("Link").text()).replace("{1}",n.find("Title").text()));
        });
    });
}
