/* 
 * external js file for video gallery IULM
 * by Speakage
 */

$(document).ready(function() {
    // Video Mod
    $(".video-play").colorbox({
        iframe: true,
        loop: false,
        opacity: 0.61,
        close: "chiudi",
        height: "500px",
        width: "650px",
        scrolling: false
    });
});

 $(function() {

    var page = 1;
    var html = '';
    var contestParam = 0;

    function getContentByVideoList(data)
    {
        if (contest) {
            html += '<div class="header"></div><ul>';
            contestParam=1;
        } else {
            html += '<ul>';
        }
            var i = 0;
            for (var key in data.videoList) {
                if (typeof(data.videoList[key]) != 'function') {
                    i++;

                    html += '<li>'
                        + '<div class="prewiev"> <a class="video-play-gallery" title="' + data.videoList[key].title + '" href="' + domain + '/embed/video/' + data.videoList[key].slug + '/ecode/1/contest/' + contestParam + '">'
                        + '<img width = 200;  src="http://videogalleryiulm.static.speakagefarm.info/thumb/' + data.videoList[key].file_name.replace('flv', 'jpg') + '" alt="' + data.videoList[key].title + '"/> </a> </div>';


                    if (data.videoList[key].title.length > 0) {
                        html += '<div class="videotitle">' + data.videoList[key].title + '</div>'
                    }

                    if (typeof(contest) != 'undefind') {
                        if (contest) {
                            if (typeof(data.videoList[key].videoFirstTag) != 'undefind') {
                                if (data.videoList[key].videoFirstTag != null) {
                                    html += '<div class="flag"><img src="http://videogalleryiulm.speakage.com/static/images/default/contest/' + data.videoList[key].videoFirstTag + '.png" title=' + data.videoList[key].videoFirstTag + ' /></div>';
                                }
                            }
                        } else {
                            html += '<div class="views">' + data.videoList[key].countView + ' views</div>';
                        }
                    } else {
                        html += '<div class="views">' + data.videoList[key].countView + ' views</div>';
                    }

                        
                    if (contest) {
                        if (data.videoList[key].is_vote * 1 > 0) {
                            html += '<div class="vote voted" alt="' + data.videoList[key].id + '"></div>'
                        } else {
                            html += '<div class="vote" alt="' + data.videoList[key].id + '"></div>'
                        }
                    }
                    
                    if(contest){
                        html += '<div class="fb-like-container"><iframe src="http://www.facebook.com/plugins/like.php?app_id=207021286005648&amp;href=' + domain + '/video/' + data.videoList[key].slug + '/contest/1&amp;locale=en_US&amp;send=false&amp;layout=button_count&amp;width=48&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:48px; height:21px;" allowTransparency="true"></iframe></div>';
                    } else {
                        html += '<div class="fb-like-container"><iframe src="http://www.facebook.com/plugins/like.php?app_id=207021286005648&amp;href=' + domain + '/video/' + data.videoList[key].slug + '/&amp;locale=en_US&amp;send=false&amp;layout=button_count&amp;width=48&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:48px; height:21px;" allowTransparency="true"></iframe></div>';
                    }
                       
                     
                    if (i % cols == 0 && i > 0) {
                       if(i == cols * rows) {
                           html += '</ul>';
                       } else {
                           html += '</ul><ul>';
                       }
                    }
                }
            }

            if (i % cols != 0 || i == 0) {
               if(i == 0) {
                   html += '</ul>';
               } else {
                   html += '</ul><ul>';
               }
            }

html+= '<script src="http://connect.facebook.net/en_US/all.js#appId=' + data.appId + '&amp;xfbml=1"></script>';

        return html
    }


    function getPaginator(data) {


        if (data.paging.first == data.paging.current) {
            var html = '<div class="paginator"><span class = "itemPage">&lt;&lt; First</span><span class = "itemPage">&lt; Previous</span>';
        } else {
            var html = '<div class="paginator"><span class = "itemPage"><a href="javascript:void(0)" alt = "' + data.paging.first + '" class="getPage">&lt;&lt; First</a></span><span class = "itemPage"><a href="javascript:void(0)" alt = "' + data.paging.previous + '" class="getPage">&lt; Previous</a></span>';
        }


        for (var i=data.paging.first; i<=data.paging.pageCount; i++) {

            if (i == data.paging.current) {
                html += '<span class = "itemPage">' + i + '</span>'
            } else {
                html += '<span class = "itemPage"><a href="javascript:void(0)" class="getPage">' + i + '</a></span>'
            }
        }

        if (data.paging.last == data.paging.current) {
            html += '<span class = "itemPage">Next &gt;</span><span class = "itemPage">Last &gt;&gt;</span></div>';
        } else {
            html += '<span class = "itemPage"><a href="javascript:void(0)" alt = "' + data.paging.next + '" class="getPage">Next &gt;</a></span><span class = "itemPage"><a href="javascript:void(0)" alt = "' + data.paging.last + '" class="getPage">Last &gt;&gt;</a></span></div>';
        }

        return html;
    }


    function getPageByUrl(url) {
        $.getJSON(url,
            function(data) {
                var videoList = getContentByVideoList(data);
                var paging = getPaginator(data);

                $('#gallery').html(videoList + paging);
                //getHover();
            }
        );
    }

    function getPage(slug, page) {
        var url = domain + '/rest/get-video-list/slug/' + slug + '/limit/' + cols*rows + '/page/' + page + '?jsoncallback=?';
        getPageByUrl(url);
    }



    $('.getPage').live('click', function(el) {

        $('#gallery').html('<div class= "ajaxLoader"></div>');

        if ($(this).attr('alt')) {
            var page = $(this).attr('alt');
        } else {
            var page = $(this).html();
        }

        getPage(slug, page);

    });

    if (typeof(slug) == 'string') {
        getPage(slug, page);
    }

    $('.galleryName').change(function() {
        slug = $(this).val();
        getPage(slug, 1);
    });
    //

    $(".video-play-gallery").live('click', function() {

        var url = $(this).attr('href');

        $.fn.colorbox({
            href: url,
            iframe: true,
            loop: false,
            opacity: 0.61,
            close: "chiudi",
            height: "550px",
            width: "500px",
            scrolling: false
        });



        return false;
        /*
        $.ajax({
            type: "GET",
            url: url,
            success: function(data) {
                $('#popup-block').html(data);
                $('#popup-block').show();
                $('#popup-block').dialog({ width: 500, resizable: false, modal: true }).show();
            }
        });*/
    });




    $('#share').live('click', function() {
        $("#embedCode").toggle('slow').focus().select();
	});

    $("#embedCode").live('click', function() {
        $("#embedCode").focus().select();
    });

    $('#gallery .vote, #popup .vote').live('mousedown', function() {

        var classes = $(this).attr('class').split(' ');
        var videoId = $(this).attr('alt');

        if (typeof(classes[1]) == 'string' && classes[1] == 'voted') {
            /*$.ajax({
                type: "POST",
                url: '/profile/del-vote/videoid/' + videoId,
                success: function(data) {
                }
            })*/
        } else {
            $(this).toggleClass('voted');
            /*$.ajax({
                type: "POST",
                url: domain + '/profile/set-vote/videoid/' + videoId,
                success: function(data) {
                    //console.log($('#gallery').html());
                }
            })*/
            var script = document.createElement("script");
            script.src = domain + '/profile/set-vote/videoid/' + videoId;
            script.type = 'text/javascript';
            document.body.appendChild(script);
        }


    });
    
    $('#gallery ul li .videotitle, #gallery ul li .prewiev').live('click', function() {
        opendialod();
    });

    $('#popup #embed').live('click', function() {
        $('#popup #embedText').toggle(300, function(){
            $('#popup #embedText textarea').focus().select();
        });
    });
    $('#popup #embedText textarea').live('click', function() {
        $('#popup #embedText textarea').focus().select();
    });


});
