﻿jQuery(document).ready(function() {

    var itemCount = $("#myScroller>li").length;			
    while((itemCount%4)!=0)
    {
	    $("#myScroller").append("<li></li>");		
	    itemCount+=1;
    }	
    
    jQuery('#myScroller').jcarousel({
        start: 0,
        initCallback: initBullets,
        itemFirstInCallback: rebuildBullets
    });
});

function rebuildBullets(carousel, a, i1)
{
    $(".pageBuletHolder").find("div").removeClass("pageBullet");
    $(".pageBuletHolder").find("div").removeClass("pageBulletActive");
    $(".pageBuletHolder").find("div").addClass("pageBullet");
    if (parseInt(i1) > 1)
    {
        var currentPage = 1;
        if(i1 % carousel.options.scroll==0)
        {
            currentPage = Math.round(i1/4);
        }
        else
        {               
            currentPage = Math.round(i1/4) + 1;
        }        
        currentPage-=1;        
        $(".pageBuletHolder").find("div[pageNo="+currentPage+"]").addClass("pageBulletActive");
    }
    else
    {
        $(".pageBuletHolder").find("div:first").addClass("pageBulletActive");
    }
}

function initBullets(carousel)
{
    var totalPages = carousel.size() / carousel.options.scroll;    
    var bullets = "<div class=\"pageBuletHolder\">";
    for (var i = 0; i < totalPages; i++)
    {
        if (i == 0)
        {
            bullets += "<div id=\"pageBullet_" + i + "\" pageNo=\"" + i + "\" class=\"pageBulletActive\"></div>";
        }
        else
        {
            bullets += "<div id=\"pageBullet_" + i + "\" pageNo=\"" + i + "\" class=\"pageBullet\"></div>";
        }
    };
    bullets += "</div>";
    $(".paging").parent().append(bullets);
    $(".pageBuletHolder").find("div").each(
    function() {
        $(this).bind("click",
        function()
        {   
            carousel.scroll((carousel.options.scroll * $(this).attr("pageNo"))+1);
        })
    });
};
