﻿var index = 0;
var created = 0;

function scroller_init() {


    if(typeof(categoryId)!="undefined")
        scroller_next();
}

function scroller_prev(){

  
    $("#scroller_" + index).animate({left:710},1000);
    $("#scroller_" + (index-1)).animate({left:0},1000);
    
    index--;
}

function scroller_next(){

    if(index==created)
        scroller_create();
    
    //alert(index);
     
    $("#scroller_" + index).animate({left:-710},1000);
    $("#scroller_" + (index+1)).animate({left:0},1000);
    
    index++;
    
    //alert(index);
}

function scroller_move(pixels){

    var children = $("#scroller_container").children("div");
    
    for(var i = index;i<children.length;i++)
    {
        var childIndex = parseInt(children[i].id.substring(9));
        alert(childIndex + " " + index);
        if(childIndex==index || (pixels<0 && childIndex==index-1) || (pixels>0 && childIndex==index+1))
        {
            var oldLeft = parseInt(children[i].style.left.substring(0,children[i].style.left.length-2));
            oldLeft+=pixels;

            $(children[i]).animate({left:oldLeft+"px"},1000);
        }
    }
}

function scroller_create() {

    created++;

    var div = $("<div></div>").attr("class","scroller").attr("id","scroller_" + created);
    div.hasMore = false;
    
    
    if(index>0)
    {
        var a = $("<a></a>").click(function(){scroller_prev();}).attr("class","scroller_prev");
        div.append(a);
        
        var span = $("<span></span>").html("TIDIGARE").css("left",50);
        div.append(span);
        
        scroller_appendcovers(div,4);
    }
    else
    {

        var line = $("<div></div>").attr("class","scroller_line");
        div.append(line);
        
        var span2 = $("<span></span>").html("TIDIGARE").css("left",370);
        div.append(span2);
        
        scroller_appendcovers(div,1);
        
        scroller_appendcovers(div,2);
    }

    $("#scroller_container").append(div);

}

function scroller_appendcovers(container,number) {
    
    
    
    var postData = "action=getcovers";
    postData += "&featured=" + (number==1);
    postData += "&page=" + index;
    postData += "&categoryId=" + categoryId;
    

    
    jQuery.post("Ajax/Articles.aspx",postData,function(data){
       
        
        if(number==1 && data.articles[0]!=null)
        {
            var id = data.articles[0].Id;
        
            var featured = $("<div></div>").attr("class","scroller_featured");
            featured.attr("id","scroller_article_" + id);
            featured.click(function(){location.href = "Index.aspx?article=" + this.id.substring(17);});
            container.append(featured);
            
            var img = $("<img></img>").attr("src","../../Servers/Image.aspx?type=Articles&file="+id+".jpg");
            featured.append(img);
            
            var title = "<b>" + data.articles[0].Title+"</b>";
            if(data.articles[0].Title2!="")
                title+= " - " + data.articles[0].Title2;
                    
            
            var text = $("<div></div>").html(title);
            featured.append(text);
            
            var span = $("<span></span>").html(data.articles[0].Category.Title);
            container.append(span);
        }
        
        else
        {
        
            
        
            for(var i = 0;i<data.articles.length-1;i++)
            {
                var offset = 40;
                if(number==2)
                    offset += 325;
                    
                var id = data.articles[i].Id;

                var div = $("<div></div>").attr("class","scroller_cover").css("left",offset + i*162);
                div.attr("id","scroller_article_" + id);
                div.click(function(){location.href = "Index.aspx?article=" + this.id.substring(17);});
                container.append(div);



                var img = $("<img></img>").attr("src", "../../Servers/Image.aspx?type=Articles&file=" + id + ".jpg");
                div.append(img);
                
                var title = "<b>" + data.articles[i].Title+"</b>";
                if(data.articles[i].Title2!="")
                    title+= " - " + data.articles[i].Title2;
                    
                var text = $("<div></div>").html(title);
                div.append(text);
            }
            
            if(data.articles.length-1==number)
            {

                var a = $("<a></a>").click(function(){scroller_next();}).attr("class","scroller_next");
                container.append(a);
                
                var a2 = $("<a></a>").click(function(){scroller_next();}).attr("class","scroller_next_text").html("Mer...");
                container.append(a2);
            }
        }
    
    },"json");
    
}

$(document).ready(function(){
    scroller_init();
});
