$(document).ready(function(){
    $("#fake-stars").stars({cancelShow: false});
    $("#stars-wrapper").stars({
        inputType: "select",
        cancelShow: false,
        captionEl: $("#stars-cap"),
        callback: function(ui, type, value) {
            ui.disable();
            $.getJSON("/movie/rate/value/"+value+"/", null, function(json) {
                if (json.result === true) {
                    $("#fake-stars").stars('select', Math.round(json.avg));
                    $("#fake-stars-cap").text(parseFloat(json.avg).toFixed(2) + " (" + json.votes + " votes)");
                } else {
                    $("#stars-msg").attr("style", 'color:#ff0000').text(" Error: "+json.msg);
                }
            });
        }
    });
    $(".ratings").show();
    $('.share-bookmark').click(function(){
        var service = $(this).attr('id');
        var baseUrl = window.location.href;
        var pageTitle = $('title').html();
        switch (service) {
            case 'sb-ovguide':
                window.open('http://www.ovguide.com/Mocero-siteinfo-10817.html?show=comment');
                break;
            case 'sb-digg':
                window.open('http://digg.com/submit?phase=2&url='+baseUrl+'&title='+pageTitle);
                break;
            case 'sb-mixx':
                window.open('http://www.mixx.com/submit?page_url='+baseUrl);
                break;
            case 'sb-stumble':
                window.open('http://www.stumbleupon.com/submit?url='+baseUrl+'&title='+pageTitle);
                break;
            case 'sb-facebook':
                window.open('http://www.facebook.com/sharer.php?u='+baseUrl+'&t='+pageTitle);
                break;
            case 'sb-twitter':
                window.open('http://twitter.com/home?status='+baseUrl);
                break;
            default:
                break;
        }
        return false;
    })
    $(".dead_link").click(function(){
        var c = confirm("Report this link as broken?");
        if (c === false) {
            return false;
        }
        var idVal = $(this).attr('id').split('_');
        var linkID = idVal[2];
        var movieID = idVal[3];
        $.get('/movie/report-dead-link/id/' + linkID + '/', function(){});
        $("#report_"+linkID).html('[Thank you]');
        return false;
    })
    $("#searchsite").click(function(){
        if ($(this).attr('value') == 'Search...') {
            $(this).attr('value', ''); 
        }
    });    
    $("#searchsite").blur(function(){
        if ($(this).attr('value') == '') {
            $(this).attr('value', 'Search...'); 
        }
    });    
    $('.movie-type').click(function(){
        var aID = $(this).attr('id');
        $('#ul-'+aID).toggle();
        if ($(this).attr('title') === 'Click to expand') {
            $(this).attr('title', 'Click to collapse');
            $('#span-'+aID).attr('class', 'arrow-show');
        } else {
            $(this).attr('title', 'Click to expand');
            $('#span-'+aID).attr('class', 'arrow-hide');
        }
        return false;
    });
    $('.thumb-vote').click(function(){
        var vote = $(this).attr('id');
        var voteArray = vote.split('-');
        $('#thumbs-number-'+voteArray[2]).load("/movie/thumbs-number/type/"+voteArray[1]+"/id/"+voteArray[2]+"/");
        return false;
    });
    $('.a-url-link').click(function(){
        $(this).css("text-decoration", "line-through").css('background', '#DDDDDD none repeat scroll 0 0');
    });
    $('.related-movies').show();
    $('#related-carousel').jcarousel({ });
    $('.outgoing-link').click(function(){
        var site = $(this).attr('rel');     
        pageTracker._trackPageview ('/outgoing/'+site);
    });
});
