$(document).ready(function(){
    
	$.ajaxSetup({ scriptCharset: "utf-8" , contentType: "application/json; charset=utf-8"});
	
    $('.vimeoLink').live("click",function(e) {

        var url=oEmbedUrl+'?url=' + encodeURIComponent($(this).attr("href"));
        $.getJSON(url+"&callback=?",function(data){
            $.fn.nyroModalManual({
              type: 'swf',
              content: unescape(data.html)
            });

        });
        e.preventDefault();
        return false;
    });
    
    
    $(".paginator > li > a").live("click",function(e){
        $("a.selected").removeClass("selected");
        $(this).addClass("selected");
        var offset=($(this).html()-1)*per_page;
        var limit=($(this).html()*per_page);
        showVideos(offset,limit);
        e.preventDefault();
        return false;
    });
    
    getAllVideos();


});
    

function getAllVideos(){
//	$.getScript(videosUrl+"?page="+page+"&callback=gt2");
	$.getJSON(videosUrl, function(data) {
		videos=data;
		videos.sort(function(a,b) { 
		    if(a.title > b.title ) {
		        return -1;
		    }
		    if(a.title == b.title ) {
		        return 0;
		    }
		    if(a.title < b.title ) {
		        return 1;
		    }
		} );
		showThumbs();
	});

}

function gt2(data){
    var video2;
    if (data.length!=0){
        if (videos===undefined){
            videos=data;
        }else{
            videos=videos.concat(data);
        }
        page++;
        $.getScript(videosUrl+"?page="+page+"&callback=gt2");
    }else{
        videos.sort(function(a,b) { 
            if(a.title > b.title ) {
                return -1;
            }
            if(a.title == b.title ) {
                return 0;
            }
            if(a.title < b.title ) {
                return 1;
            }
        } );
        showThumbs();
    }
}



    
function videoThumb(url,img,title){
    var content=$("<li/>").append(
        $("<a/>").addClass("vimeoLink").attr("href",url).append(
            $("<img/>").attr("src",img).attr("alt",title).attr("title",title)
        ).append(
            $("<p/>").text(title).css("margin-top","1px").css("width","200px")
        )
    );
    return content;
}

function showThumbs(){


    showVideos(0,per_page);
    showPaginator();

    $("#loading").hide();
    
    
}

function showVideos(offset,limit){
    var thumbs=$("#videoThumbs");
    thumbs.html("");
    for(var i=offset; (i < videos.length)&&(i<limit);  i++){
        thumbs.append(videoThumb(videos[i].url,videos[i].thumb,videos[i].title));
    }    
    fixImages();
}

function showPaginator(){
    var pagines=Math.ceil(videos.length/per_page);
    if (pagines<=1){return;}
    var paginator=$(".paginator");
    paginator.append("<li><a class=\"selected\" title=\"P&agrave;gina "+1+"\" href=\"#\">"+1+"</a></li>");
    for (var i=2;i<=pagines;i++){
        paginator.append("<li><a title=\"P&agrave;gina "+i+"\" href=\"#\">"+i+"</a></li>");
    }
}

function fixImages(){
    $("img").error(function () {
      $(this).unbind("error").attr("src", "/design/ezflow_site/images/error.png");
    });
    
    $("img[src*='default']").attr("src", "/design/ezflow_site/images/error.png");        
}

