var totalTimeSeek;
var seeksliding;
var totalWidth = 100;
var pauseButton = 'paused-button';

function setPostion()
{
    var fullscreenWidth = $(window).width();
    var fullscreensHeight = $(window).height();
    $('.swf_space, object, .backgroundPlayer, .wrapper').width(fullscreenWidth).height(fullscreensHeight - 30);
} 

$(document).ready(function()
{
	
    $(window).resize(function()
    {
        setPostion();
    });

    $('#video-controls').bind(
    {
        mouseenter: function()
        {
            controller_show();
        },
        mouseleave: function()
        {
            controller_hide();
        }
    });

    $('.play_btn').bind(
    {
        click: function()
        {
            var isPlay = $('.play_btn').hasClass(pauseButton);
            if (isPlay) pause();
            else play();
        }
    });
	
    setInterval("setPostion()", 1);
        
    
    //#main
    var $Overlay = $(".overlay");
    var $Main = $("#main");
    var $TitleSpace = $("#info");
    var $OpenLink = $("a.open", $TitleSpace);
    var $CloseLink = $("a:not(.open)", $TitleSpace);
    var $CloseBtn = $(".close-btn", $Main);
    
    $Main.css('display','none');
    $Overlay.css('display','none');
    $CloseLink.css('display','none');
    
    
    var closeMainPanel = function(){
        $CloseLink.css('display','none');
        $OpenLink.css('display','block');
        $Main.fadeOut('normal', function(){
            $Overlay.fadeOut();
        });
    };
    
    var openMainPanel = function(){
        $OpenLink.css('display','none');
        $CloseLink.css('display','block');
        $Overlay.fadeIn('normal', function(){
            $Main.fadeIn();
        });
    };

    var setupMainPanel = function(){
        $CloseLink.css('display','none');
        $OpenLink.css('display','block');
		$Overlay.css('display','none');
    };
    
    setupMainPanel();
    $OpenLink.click(openMainPanel);
    $CloseLink.click(closeMainPanel);
    $CloseBtn.click(closeMainPanel);    
    //#main
    
    //logo
    var nTextHeight = 23;
    var nImageHeight = 129; //23+10+129
    var nAnimationTime = 500;
    var nHoveroutDelay = 1000;
    //    var $LogoTxt = $("#logo_txt");
    var $LogoImg = $("#logo_img");
    var $LogoContainer = $("#HomeLink");
    var oHoveroutTimerID;
    var sLogoStatus = "text";
    
    $LogoImg.css('display', 'none');
    
    var onHoverIn = function(){
        if(sLogoStatus == "entering" || sLogoStatus == "exiting"){
            return;
        }
        if(sLogoStatus == "text"){
            sLogoStatus = "entering";  
            $LogoContainer.animate({
                height: nImageHeight+"px"
            }, nAnimationTime, function(){
                //                $LogoTxt.fadeOut(nAnimationTime);
                $LogoImg.fadeIn(nAnimationTime, function(){
                    sLogoStatus = "image";
                });
            });
        }else if(sLogoStatus == "image"){
            if(oHoveroutTimerID){            
                window.clearTimeout(oHoveroutTimerID)
                oHoveroutTimerID = null;
            }
        }
    };
    
    var onHoverOut = function(){
        if(sLogoStatus == "exiting"){
            return;
        }
        sLogoStatus = "exiting";
        oHoveroutTimerID = window.setTimeout(function(){
            $LogoImg.fadeOut(nAnimationTime, function(){
                $LogoContainer.animate({
                    height: nTextHeight+"px"
                }, nAnimationTime, function(){
                    sLogoStatus = "text";
                });
            });
        }, nHoveroutDelay);
    };
    
    $LogoContainer.hover(onHoverIn, onHoverOut);
    //logo
    
    //overlays  
    var $MainMenu = $("#main_menu");
      
    var showOverlay = function(){
        $Overlay.fadeIn();
    };
    
    var hideOverlay = function(){
        if(!$Main.is(":visible")){
            $Overlay.fadeOut();
        }
    };
    
    $LogoContainer.bind('mouseenter', showOverlay);
    $LogoContainer.bind('mouseleave', hideOverlay);
    
    $MainMenu.bind('mouseenter', showOverlay);
    $MainMenu.bind('mouseleave', hideOverlay);
    //overlays 
    
    
    
    
    //menu
    $(".film_list").hide();
    var animationSpeed = "slow";
    
    //Open Menu on Hover Starts
    //    $("li.name").hover(function(){
    //        $(this).addClass("active").find(".film_list").slideDown(animationSpeed);
    //    },function(){
    //        $(this).removeClass("active").find(".film_list").slideUp(animationSpeed);
    //    });
    //Open Menu on Hover Ends
    
    
    //Open Menu On Click Starts    
    var onMouseLeave = function(e){
        var $this = $(this);
        var handle = window.setTimeout(function(){
            closeMenu($this);
        }, 3000);
        $this.data('timeouthandle', handle);
    };
    var cancelTimer = function($Item){         
        var timeoutHandle = $Item.data('timeouthandle');
        if(timeoutHandle){
            window.clearTimeout(timeoutHandle);
            $Item.removeData('timeouthandle');
        }
    };
    var closeMenu = function($Item){
        $Item.removeClass("active").find(".film_list").slideUp(animationSpeed);
        $Item.unbind('mouseleave mouseenter');
        cancelTimer($Item);
    }
    
    $("li.name").click(function(){
        var $this = $(this);
        if($this.hasClass('active')){
            closeMenu($this);
        }else{
            closeMenu($("li.name.active"));
            $this.addClass("active").find(".film_list").slideDown(animationSpeed);
            $this.bind('mouseenter', function(){
                cancelTimer($(this));
            });
            $this.bind('mouseleave', onMouseLeave);
        }
    });
    //Open Menu On Click Ends
    //menu - END
    
    
    
    //credits
    var $Credit = $("a.credit");
    var $Credit_hover = $("a.credit_hover");
    var onCreditHover = function(){
        $Credit.unbind('mouseenter');
        $(this).fadeOut('normal');
        $Credit_hover.fadeIn('normal', function(){
            $Credit_hover.bind('mouseleave', onCreditHoverOut);
        });
    };
    
    var onCreditHoverOut = function(){
        $Credit_hover.unbind('mouseleave');
        $(this).fadeOut('normal');        
        $Credit.fadeIn('normal', function(){
            $Credit.bind('mouseenter', onCreditHover);
        });
    };
    $Credit.bind('mouseenter', onCreditHover);
    //credits
    
	
});

function thisMovie(movieName)
{
    if (navigator.appName.indexOf("Microsoft") != -1)
    {
        return window[movieName];
    }
    else
    {
        return document[movieName];
    }
}

function playerReady()
{
}

function play()
{
    $('.play_btn').addClass(pauseButton);
    thisMovie("player").playVideo();
}

function pause()
{
    $('.play_btn').removeClass(pauseButton);
    thisMovie("player").pauseVideo();
}

function controller_show()
{
    $('#video-controls').stop().animate(
    {
        opacity: 1
    });
}

function controller_hide()
{
    $('#video-controls').stop().animate(
    {
        opacity: 0
    });
}

function updateMetadata()
{
    controller_show();
    play();
    createSeek();
}

function playbackEnd()
{
    pause();
}

function updateTime(currTime)
{
    $('.video-timer').text(currTime);
}

function updateProgress(currTime, totalTime)
{
    totalTimeSeek = totalTime
    var currTimeSeek = (100 / totalTime) * currTime;
    seekUpdate(currTimeSeek);
}

function createSeek()
{
    var video_duration = totalTimeSeek;
    $('.video-seek').slider(
    {
        value: 0,
        step: 0.01,
        orientation: "horizontal",
        range: "min",
        max: video_duration,
        animate: false,
        slide: function()
        {
            seeksliding = true;
        },
        stop: function(e, ui)
        {
            seeksliding = false;
            thisMovie('player').seekVideo(ui.value, totalWidth);
        }
    });
}

function seekUpdate(currTimeSeek)
{
    if (!seeksliding) $('.video-seek').slider('value', currTimeSeek);
}



Cufon.replace ('h2, h3, h4, .name, .film_list, .video-timer, #footer_left li a, #video-title a, #share-btn a, #copyright, a, p', {
    fontFamily: 'TitlingGothicFBStd', 
    hover: 'true'
});
Cufon.replace ('#a.close-btn, #HomeLink a', {
    fontFamily: 'Univers LT 47 CondensedLT', 
    hover: 'true'
});

