﻿
var slideshow = null;
var isIE = false;
var tempInterval = null;

if (navigator.userAgent.match(/MSIE/i) != null)
    isIE = true;

window.onload = function () {

    //slideshow
    $('.flexslider').flexslider({
        directionNav: false,
        controlsContainer: '.flex-container',
        manualControls: ".circle-nav li a",

        start: function (slider) {
            slideshow = slider;
            setupJWPlayer();
            //resizeTheVideo();
            /*// console.log("hello, i am the start"); */
        },
        before: function (slider) {
            tempInterval = setInterval(function () {
                $(".full img:visible").css("margin-left", -($(".full img:visible").width() / 2));
            }, 10);
        },

        after: function (slider) {
            if (slider.currentSlide == "1" && isIE) { jwplayer("thevideo").remove(); }
            resizeTheVideo();
            $(window).bind("resize", resizeTheVideo);
            nextSlide(slider);
        }
    });

    function nextSlide(slider) {
        slider.pause();

        $(".full img:visible").css("margin-left", -($(".full img:visible").width() / 2));
        var current = slider.currentSlide;
        // console.log(slider.currentSlide);
        $('#foot-2 a').hide();
        $('.slide-' + current).fadeIn();

        if (slider.currentSlide == "0") {

            //jwplayer("thevideo").play();
            //jwplayer().load("/video/homepage_960x460.mp4");
            if (isIE)
                setupJWPlayer();

            resizeTheVideo();

        } else {
            // console.log("hello, i triggered next");                                                      
            //slider.delay(10000).resume();                                                      
            slider.resume();
        }

    }

    $('#hoverelement').hover(function () {
        $(this).html(imgs[0]);
    }, function () {
        $(this).html(imgs[1]);
    });

    setupJWPlayer();
    resizeTheVideo();

    // music setup
    $("#jquery_jplayer_1").jPlayer({

        ready: function () {
            $(this).jPlayer("setMedia", {
                mp3: "/music/Ambulance-Cropped.mp3"
            }).jPlayer("play");

        },
        ended: function (event) {
            $(this).jPlayer("play");
        },
        play: function (e) {
            $("#eq").attr("class", "playing");
        },
        pause: function (e) {
            $("#eq").attr("class", "paused");
        },
        swfPath: "/flash",
        supplied: "mp3",
        cssSelectorAncestor: "#m-interface",
        cssSelector: {
            "play": ".m-play",
            "pause": ".m-pause"
        },
        volume: 0.4
    });
}           //end onload

function setupJWPlayer() {

    // video setup
    jwplayer("thevideo").setup({
        file: "/video/homepage_960x460.mp4",
        flashplayer: "/js/jwplayer/player.swf",
        autostart: true,
        controlbar: "none",
        events: {
            onBuffer: function () {
                slideshow.pause();
                // console.log("hey, i'm started!");
                // console.log(slideshow.currentSlide);
            },
            onPlay: function () {
                slideshow.pause();
                // console.log("hey, i'm started!");
                // console.log(slideshow.currentSlide);

            },
            onComplete: function () {

                // console.log("hey, i'm done now!"); 
                slideshow.flexAnimate(slideshow.getTarget("next"));
                //jwplayer("thevideo").remove();
                // console.log(slideshow.currentSlide);
            }
        }
    });

    resizeTheVideo();
}





/* VIDEO SCALING */
var vidWidth = 960;
var vidHeight = 460;
var vidPadding = 162;
var vidAspectRatio = vidWidth / vidHeight;


function resizeTheVideo() {

    var browserAspectRatio = $(window).width() / ($(window).height() - vidPadding);

    if (browserAspectRatio >= vidAspectRatio) {

        $("#thevideo").css("width", $(window).width());
        $("#thevideo").css("height", $(window).width() / vidAspectRatio);
        $("#thevideo").css("left", 0);

    } else {
        var theWidth = ($(window).height() - vidPadding) * vidAspectRatio;
        $("#thevideo").css("height", ($(window).height() - vidPadding));
        $("#thevideo").css("width", theWidth);
        console.log($("#thevideo").width());
        var leftOffset = ($(window).width() / 2) - (theWidth / 2);
        $("#thevideo").css("left", leftOffset + "px");

    }

    $(".full img:visible").css("margin-left", -($(".full img:visible").width() / 2));
    $(".slides .full").css("height", ($(window).height() - vidPadding));

}


function triggerPlayer(val) {
    player.sendEvent(val);
    //$(".subtitle").hide();
}
$(document).ready(function () {
    $(window).bind("resize", resizeTheVideo);

});
$(".music").mouseenter(function () {
    $("#band").show();
});

$(".music").mouseleave(function () {
    $("#band").hide();
});
