$(document).ready(function () {
    var curId;
    var auto;

    showImage = function (obj) {
        var id = obj.attr("data-id");
        //var color = obj.attr("data-color");
        //var image = obj.attr("data-image");

        if (curId != id) {
            curId = id;
            $(".landingRotatorImages img").attr("src", "../../Content/Images/Rotator/" + id + ".jpg");
            $(".landingRotatorImages ul li a").css("background-color", "#afa077");
            $("a", obj).css("background-color", "#f8f1db");
            $(".category").css("display", "none");
            $("#" + id).fadeIn('fast');
        }
    };

    var rotate = function () {
        var getNext = false;
        var found = false;

        $(".landingRotatorImages ul li").each(function (index) {
            if ((getNext) && (!found)) {
                showImage($(this));
                found = true;
            }

            if ($(this).attr("data-id") == curId) {
                getNext = true;
            }
        });

        if (!found)
            initialize();
    };

    initialize = function () {
        var obj = $(".landingRotatorImages ul li:first");
        showImage(obj);
        auto = true;
    };
    $(".landingRotatorImages ul li").click(function () {
        auto = false;
        showImage($(this));
    }, function () {
        auto = false;
    });
    $(".landingRight").hover(function () {
        auto = false;
    }, function () {
        auto = false;
    });
    $(".product").hover(function () {
        auto = false;
    });
    initialize();

    window.setInterval(function () {
        if (auto)
            rotate();
    }, 10000);

    $('.newWindow').click(function (event) {
        var url = $(this).attr("href");
        var windowName = "Viewer";
        //$(this).attr("name");
        var width = 660;
        var height = 620;
        var left = parseInt((screen.availWidth / 2) - (width / 2));
        var top = parseInt((screen.availHeight / 2) - (height / 2));

        var windowSize = "resizable=yes,menubar=no,directories=no,status=no,location=no,toolbar=no,width=" + width + ",height=" + height + ",left=" + left + ",top=" + top;
        window.open(url, windowName, windowSize);

    });
    $('.internalWindow').click(function (event) {
        var file = $(this).attr("data-key");

        var video = '<div id="internalPlayer"><video width="640" height="360" src="../../Content/Documents/General/' + file + '" type="video/mp4"  id="player2" controls="controls" preload="none"></video></div>';

        $("#screen").fadeIn("fast");
        $("#player").fadeIn("fast");

        $("#player").append(video); ;

        $('audio,video').mediaelementplayer({
            success: function (player, node) {
                $('#' + node.id + '-mode').html('mode: ' + player.pluginType);
            }
        });

        auto = false;

        event.preventDefault();
    });
    $('#player img').click(function (event) {

        $("#internalPlayer").remove();

        $("#screen").fadeOut("fast");
        $("#player").fadeOut("fast");

        auto = true;
    });
});
