﻿$(function() {

    if($("#banner img").length > 1) $("#banner").cycle();
    else $("#banner img").css("display", "block");

    $("#menu li").hover(
        function() { $(this).addClass("hover"); },
        function() { $(this).removeClass("hover"); }
    );

    $("img").each(function() {
        if ($(this).attr("src") == "" || $(this).attr("src") == null) {
            $(this).css("display", "none");
        }
    });

    var bodySize = $("body").width();
    var bannerSize = 980;

    $("#menu li").each(function() {
        var link = $(this).html();
        $(this).html("<div class='left'></div><div class='center'>" + link + "</div><div class='right'></div>");
    });

    $(".tab").each(function() {
        var link = $(this).html();
        $(this).html("<div class='left'></div><div class='center'>" + link + "</div><div class='right'></div>");
    });

    $(".tab").hover(
        function() { $(this).addClass("hover"); },
        function() { $(this).removeClass("hover"); }
    );

    $(".tab").click(function() {
        var tab = $(this).attr("id");
        $(".tabpanel").addClass("ui-helper-hidden");
        $("#" + tab + "panel").removeClass("ui-helper-hidden");
        $(".tab").removeClass("hold");
        $(this).addClass("hold");
    });

    $("#itemlist li").hover(
        function() { $(this).addClass("hover"); },
        function() { $(this).removeClass("hover"); }
    );

    $("#itemlist li").each(function() {
        if ($(this).parent().hasClass("review")) {
            $(this).click(function() {
                var id = $(this).attr("id");
                window.location = "Review.aspx?id=" + id;
            });
        }
        if ($(this).parent().hasClass("gigreview")) {
            $(this).click(function() {
                var id = $(this).attr("id");
                window.location = "GigReview.aspx?id=" + id;
            });
        }
        if ($(this).parent().hasClass("top5")) {
            $(this).click(function() {
                var id = $(this).attr("id");
                window.location = "top5.aspx?id=" + id;
            });
        }
    });

    $("#register").click(function() {
        window.open('Register.aspx', 'mywindow', 'width=600,height=450,menubar=no,toolbar=no,resizable=no');
    });

    $("#forgotpassword").click(function () {
        window.open('Register.aspx?fp=1', 'mywindow', 'width=600,height=450,menubar=no,toolbar=no,resizable=no');
    });

    $("#viewprofile").click(function () { window.open("User.aspx", 'user', 'width=600,height=450,menubar=no,toolbar=no,resizable=no'); });

    $(".track").hover(
        function() { $(this).addClass("hover"); },
        function() { $(this).removeClass("hover"); }
    );

    $("#mycarousel").jcarousel();

    $("#mycarousel img").click(function() {
        $("#mainimage").attr("src", $(this).attr("src"));
    });

    $(".optionradio").click(function() {
        $(".optionradio").each(function() { $(this)[0].checked = false; });
        $(this)[0].checked = true;
    });

    $("#submitopinion").click(function(event) {
        event.preventDefault();
        var id = $(this).attr("class");
        var option = 0;

        // Find the option that is checked='true' (should only be possible for one to be this way)
        $(".optionradio").each(function() { if ($(this)[0].checked == true) option = $(this).attr("id"); });

        if (id != 0) window.location = "Opinion.aspx?id=" + id + "&option=" + option;

    });

    $(".opinionlist li").click(function() {
        window.location = "Opinion.aspx?id=" + $(this).attr("id");
    });

    $(".playlists .strong").click(function() {
        $(this).siblings("div").each(function () {
            $(this).slideToggle();
        });

        if ($(this).hasClass("minus")) {
            $(this).removeClass("minus");
        } else {
            $(this).addClass("minus");
        }
    });

    var tvId = $("#currentitemid").text();
    if (tvId != "") {
        $("#" + tvId).parent().slideToggle();
        $("#" + tvId).parent().siblings(".strong").addClass("minus")
    }

    $("#tv").click(function() {
        window.location = "TV.aspx";
    });

    $(".dots").each(function() {
        var url = $("a", this).attr("href");
        $(this).click(function() { window.location = url; });
    });
    
    $("#tvpop").click(function() {  window.open("TV.aspx?id=208&autoplay=0", 'tv', 'width=1200,height=800'); });
    
    
});

function onYouTubePlayerReady(playerId) {
    var player = document.getElementById("player");
    player.addEventListener("onStateChange", "onPlayerStateChange");
}

function onPlayerStateChange(newState) {
    
    if (newState == 0) {

        var nextID = ""
        
        $(".playlists li").each(function () {
            if ($(".hold", this).length > 0) {
                nextID = $(".hold", this).children(".next").text();
            }
        });

        var address = $("#" + nextID).children(".address").text();

        var player = document.getElementById("player");

        player.loadVideoByUrl(address, 0);

        $(".playlists li").each(function () {
            if ($(".hold", this).length > 0) {
                $(".hold", this).removeClass("hold");
            }
        });

        $("#" + nextID).addClass("hold");
    }
}

function doHoldMenu(menuIndex) {
    $("#menu li").eq(menuIndex).addClass("hold");
    
}
