$(document).ready(function(){
    //if (parent.frames.length > 0) top.location.replace(document.location);
    if (window.top !== window.self) { window.top.location = document.location; }

    $('.autoclear').autoclear();

    $('.is-favorite').click(function(){return false;});
    $('.watched').click(function(){return false;});

    $('#newmovies').show();
    $('#recommend-carousel').jcarousel({ });


    $("#loading").ajaxStart(function(){$(this).show();}).ajaxStop(function(){$(this).hide();});

    $(".url-more-urls").click(function(){
        $(".more-urls").attr('style', 'display:block');
        return false;
    });

    $(".mark-watch").click(function() {
        var idAr = $(this).attr('id').split('-');
        var id = idAr[1];
        $.getJSON("/members/watchlist/mark-watch/id/"+id+"/", function(response){  
            if (response.type==='success') {
                $('#watch-'+id).html("Watched");
            } else if (response.type==='error') {
                alert(response.msg);
            }
        });
        return false;
    });

    $(".add-to-watchlist").click(function(){
        var idAr = $(this).attr('id').split('-');
        var id = idAr[1];
        $.getJSON("/members/watchlist/add/id/"+id+"/", function(response){  
            if (response.type==='success') {
                $('#watchlist-'+id).html("On watchlist");
            } else if (response.type==='error') {
                alert(response.msg);
            }
        });
        return false;
    });

    $("#select-tv-shows").change(function() {
        var showID = $("#select-tv-shows :selected").attr('value');
        $.getJSON("/tv/season/id/"+showID+"/", function(json){  
            $("#select-season").removeAttr('disabled');
            var options = "<option value=''>--- Select your season ---</option>";
            for (i in json) {
                options += "<option value='"+json[i].season+"'>Season "+json[i].season+"</option>";
            }
            $("#select-season").html(options);
            $("#select-episodes").html("<option>--- Select a show and a season first ---</option>");
            $("#select-episodes").attr("disabled", "disabled");
        });
    });

    $("#select-season").change(function() {
        var showID = $("#select-tv-shows :selected").attr('value');
        var season = $("#select-season :selected").attr('value');
        $.getJSON("/tv/show/id/"+showID+"/season/"+season+"/", function(json){  
            $("#select-episodes").removeAttr('disabled');
            var options = "<option value=''>--- Select your episode ---</option>";
            for (i in json) {
                options += "<option value='"+json[i].tv_episode_id+"'>S"+json[i].season+"E"+json[i].episode+": "+json[i].episode_title+"</option>";
            }
            $("#select-episodes").html(options);
        });
    });

    $('.share-bookmark').click(function(){
        var service = $(this).attr('id');
        var baseUrl = window.location.href;
        var pageTitle = $('title').html();
        switch (service) {
            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('/tv/report-dead-link/id/' + linkID + '/', function(){});
        $("#reportlink-"+linkID).html('Thank you');
        return false;
    })
    $('.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("/tv/thumbs-number/type/"+voteArray[1]+"/id/"+voteArray[2]+"/");
        return false;
    });
    $('.a-url-link').click(function(){
        $(this).css("text-decoration", "line-through");
    });
    $('.outgoing-link').click(function(){
        var site = $(this).attr('rel');     
        pageTracker._trackPageview ('/outgoing/'+site);
    });

    $("#linkscontent").hide();
    $("a.more-links").click(function() {
        $("#videocontent").hide();
        $("#linkscontent").show();
        $("#videotab").addClass("link").removeClass("active");
        $("#linkstab").addClass("active").removeClass("link");
        return false;
    });
    $("a#videotab").click(function() {
        $("#linkscontent").hide();
        $("#videocontent").show();
        $("#videotab").addClass("active").removeClass("link");
        $("#linkstab").addClass("link").removeClass("active");
        return false;
    });
    
});
