/**
 Pozovi frendove
*/

function sendFbRequests() {
    FB.ui({
        method: 'apprequests',
        message: 'Obavezno pogledaj',
    }, function(response) {
        /*
        if (response != null && response.request_ids && response.request_ids.length > 0) {
            
            var ids = [];
            for (var i = 0; i < response.request_ids.length; i++) {
                ids.push( response.request_ids[i] );
            }

            

        } else {
            //Nema pozivnica
        }
        */
    });
}

/**
* Ucitaj video player
*/
function PlayVideo(code)
{
	url = "http://www.youtube.com/v/"+code+"&autoplay=1&fs=1&enablejsapi=1&playerapiid=ytplayer";
    var el = $("#videoPreview");
    
    el.html("<div id='ytapiplayer'></div>");
        
    var params = { allowScriptAccess: "always" };
    var atts = { id: "ytplayer" };
    swfobject.embedSWF( url, "ytapiplayer", "973", "400", "8", null, null, params, atts);     
    
    $("#videoOptions").show();
    
    window.location.hash= code;
       
}

$(function(){
    
    //alert(window.location.hash);
    
    if (window.location.hash != "")
    {
        var code = window.location.hash;
        code = code.replace("#","");
        PlayVideo(code);
    }    
     
});

function onYouTubePlayerReady(playerId) {
  ytplayer = document.getElementById("ytplayer");
  ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
}

function play() {
  if (ytplayer) {
    ytplayer.playVideo();
  }
}

function onytplayerStateChange(newState){
  if (newState == 0)
  {
    if ( $("#randomTekst").is(":checked") )
    {
        randomTekst();
    }
  }
}

function randomTekst()
{
   document.location = baseUrl+'/tekst/random'; 
}
function playFirst(){

    if ($(".videListPanel").length == 0) return;
    
    var img = $(".videListPanel img:first");
    
    if (img.length >0)
    {
        img.click();
    }else{
        randomTekst();
    }
}

$(function(){
    if ( $("#randomTekst").is(":checked") )
    {
        playFirst();
    }    
});


function ShowImage(image)
{
	var img = "<img src='"+image+"' style='max-width:532px' >";
	
	$("#imagePreview").html(img);
}



var Moderate = {
    
    deleteComment:function(id) {
        var ans = confirm("Da li želite obrisati ovaj komentar?");
        if(!ans) return;
        var url = baseUrl +'/song/deleteComment/'+id;

        $.get(url, function(data){
            //window.location.reload();
            
            if (data.result)
            {
                $("#Comment_"+id).remove();
            }else
            {
                alert("Greška prilikom brisanja komentara.");
            }
            
        },"json");
    },
    
    deleteSong:function(id) {
        var ans = confirm("Da li želite obrisati ovaj tekst?");
        if(!ans) return;
        var url = baseUrl +'/song/deleteSong/'+id;

        $.get(url, function(data)
        {
            if (data.result)
            {
                alert("Tekst je uspiješno obrisan.");
            }else
            {
                alert("Greška prilikom brisanja komentara.");
            }
        },"json");
    }
}

var ArtistFilter = {
    
    timerId:null,
    
    filter:function()
    {
        clearTimeout(this.timerId);
        this.timerId  = setTimeout(this.load, 250);
    },
    
    load:function()
    {
        var url = baseUrl + "/artist?filter=" + $("#filterBox").val();
        
        var el = $("#artistListView");
        el.addClass("list-view-loading");

        $.get(url,function(data){
            
            var c = $(data).find("#artistListView");
            el.html(c.html());
            el.removeClass("list-view-loading");
        });
    }
}


var TagFilter = {
    timerId:null,
    filter:function()
    {
        clearTimeout(this.timerId);
        this.timerId  = setTimeout(this.load, 250);
    },
    
    load:function()
    {
        var url = baseUrl + "/tags?filter=" + $("#filterBox").val();
        
        var el = $("#tagListView");
        el.addClass("list-view-loading");

        $.get(url,function(data){
            
            var c = $(data).find("#tagListView");
            el.html(c.html());
            el.removeClass("list-view-loading");
        });
    }
}

var ActivityStream = {
    
    post:function()
    {
       var el = $(this).parents(".cf-frame").eq(0);
       var tb = el.find(".c-reply");

        
       var url =baseUrl + "/site/postComment";
        
       var set = {
            id : tb.attr("data-id"),
            aid: tb.attr("data-aid"),
            type:tb.attr("data-type"),
            text:tb.val(),
            xval:1
       };

       
       $.get(url, set, function(data){
            $("#actCl"+set.aid+" .cf-list").html(data);
            tb.val("");
       });
       
    }
    ,
    init:function()
    {
        $(".cf-frame .dugme").live("click", ActivityStream.post)
        
    }
}

/*Rating component*/
$(function(){
   
   ActivityStream.init(); 
    
   $(".star-rater .star").live(
        {
           "mouseover": function(){
                var p = $(this).parent();
                p.find(".star").removeClass("on");
                $(this).prevAll(".star").andSelf().addClass("on");
           },
           "mouseleave": function()
           {
                var p = $(this).parent();
                var v = p.attr("data-value");
                p.find(".star").removeClass("on");
                p.find(".star:lt("+v+")").addClass("on");
        
           },
           "click": function()
           {
                var p = $(this).parent();
                var v = p.find(".star").index(this)+1;
            
                p.attr("data-value",v );
                p.trigger("onrate", v );
           }
        }
   );

   $(".star-rater").bind("onrate", function(event,v){
        var el = $(this);
        
        var set = {
            id   : el.attr("data-id"),
            type : el.attr("data-type"),
            value: v,
        };
        
        var url = baseUrl+"/rate/rate";
        
        $.get(url,set, function(data){
            
            el.attr("title", data.title );
            el.find(".rater-result").text(data.average+" / "+data.count );
            
        },"json");
   });  
   
    controls = "bold italic underline strikethrough subscript superscript | font size " +
            "style | color highlight removeformat | bullets numbering | outdent " +
            "indent | alignleft center alignright justify | undo redo | " +
            "rule link unlink";
            
    controls = "bold italic underline strikethrough | font size " +
            "style | color highlight removeformat | bullets numbering | outdent " +
            "indent | alignleft center alignright justify | undo redo | " +
            "rule link unlink";
            
               
    $(".textEditor").each(function(){
       
       var h = $(this).height();
       var w = $(this).width();
       
       $(this).cleditor({width:w,height:h /*,controls:controls*/  }) 
        
    });
    
    $(".textEditorSmall").each(function(){
       
       var h = $(this).height();
       var w = $(this).width();
       
       $(this).cleditor({width:w,height:h ,controls:controls  }) 
        
    });
 });
