﻿var firstVideoSrc = null;
var firstVideoImgSrc = null;
$(document).ready(function() {
    function createPlayer() {
        var flashvars = {
            controlbar: "over",
            autostart: "false"
        };
        var params = {
            wmode: "transparent",
            allowfullscreen: "true",
            allowscriptaccess: "always"
        };
        var attributes = {
            id: "player1",
            name: "player1"
        };
        swfobject.embedSWF("/flash/player.swf", "mediaplayer_container", "464", "260", "9.0.115", false, flashvars, params, attributes);
    }
    createPlayer();

    var $filmStripWrapper = $('.filmStripWrapper');
    var filmStripWidth = 0;
    $filmStripWrapper.find('li').each(function() {
        filmStripWidth += $(this).outerWidth();
    });
    var $filmStrip = $filmStripWrapper.find('.filmStrip');

    var arrowInterval = -1;
    var $leftArrow = $('.arrows > a.left')
    .hover(function() {
        if (arrowInterval == -1) {
            var left = parseInt($filmStrip.css("left"));
            arrowInterval = setInterval(function() {
                if (left < 0) {
                    left += 2;
                    $filmStrip.css("left", left);
                }
            }, 1);
        }
    }, function() {
        clearInterval(arrowInterval);
        arrowInterval = -1;
    })
    .click(function() { return false; });
    var $rightArrow = $('.arrows > a.right')
    .hover(function() {
        if (arrowInterval == -1) {
            var left = parseInt($filmStrip.css("left"));
            arrowInterval = setInterval(function() {
                if (filmStripWidth + left > 720) {
                    left -= 2;
                    $filmStrip.css("left", left);
                }
            }, 1);
        }
    }, function() {
        clearInterval(arrowInterval);
        arrowInterval = -1;
    })
    .click(function() { return false; });


    $filmStrip.find('a').click(function() {
        var $this = $(this);
        var $li = $this.parent();
        if ($li.is('.selected')) return false;
        $filmStrip.find('li.selected').removeClass('selected').find('img').animate({
            opacity: 0.15
        });
        $li.addClass('selected');
        var videoSrc = $this.attr("href");
        var imgSrc = videoSrc.replace(".flv", ".jpg");
        mediaplayer.sendEvent('STOP');
        mediaplayer.sendEvent('LOAD', { file: videoSrc, image: imgSrc });
        return false;
    })
    .hover(function() {
        if ($(this).parent().is(':not(.selected)')) {
            $(this).find('img').animate({
                opacity: 1
            });
        }
    }, function() {
        if ($(this).parent().is(':not(.selected)')) {
            $(this).find('img').animate({
                opacity: 0.15
            });
        }
    });

    $('.wallpapers li a').hover(function() {
        $(this).prevAll('img:first').stop().animate({
            opacity: 1
        });
    }, function() {
        $(this).prevAll('img:first').stop().animate({
            opacity: 0.15
        });
    });

});

function playerReady(thePlayer) {
    mediaplayer = window.document[thePlayer.id];
    if (firstVideoSrc != null) {
        mediaplayer.sendEvent('LOAD', { file: firstVideoSrc, image: firstVideoImgSrc });
    }
}
