//google.load("jquery", "1");
//google.load("jqueryui", "1");

// on page load complete
//google.setOnLoadCallback(function() {
$(document).ready(function() {
    // middles panels
    $('#link_concept').hover(function(e) {
        e.preventDefault();
        if (!$(this).hasClass('active'))
            home_links_clean(home_show_concept);
    }, function() {});
    $('#concept').hover(function(){}, function(e) {
        e.preventDefault();
        home_links_clean(null);
    });
    
    $('#link_product').hover(function(e) {
        e.preventDefault();
        if (!$(this).hasClass('active'))
            home_links_clean(home_show_product);
    }, function() {});
    $('#product').hover(function(){}, function(e) {
        e.preventDefault();
        home_links_clean(null);
    });
    
    $('#link_project').hover(function(e) {
        e.preventDefault();
        if (!$(this).hasClass('active'))
            home_links_clean(home_show_project);
        else
            home_links_clean(null);
    }, function() {});
    $('#project').hover(function(){}, function(e) {
        e.preventDefault();
        home_links_clean(null);
    });
    
    $('#link_news').hover(function(e) {
        e.preventDefault();
        if (!$(this).hasClass('active'))
            home_links_clean(home_show_news);
        else
            home_links_clean(null);
    }, function() {});
    $('#news').hover(function(){}, function(e) {
        e.preventDefault();
        home_links_clean(null);
    });
    
    
    // product animation
    $('#product .col').hover(function() {
        $('.content', this)
            .css({display: 'block'})
            .clearQueue()
            .animate(
                     {width: '280px'},
                     'slow',
                     'easeInCubic'
                    );
    }, function() {
        $('.content', this)
            .clearQueue()
            .animate(
                     {width: '0px'},
                     'slow',
                     'easeInCubic',
                     function() {$(this).css({display: 'none'});}
                    );
    });
    
    // bottom descriptions
    $('#description').hoverIntent(function() {
        $('#description2').animate({
            marginTop:0
            },'slow', 'easeOutExpo');
    }, function() {
        $('#description2').animate({
            marginTop:'-165px'
            },'slow', 'easeInExpo');
    });    
//    var video_player = document.getElementById("video_file");
//    video_player.addListener('ended', sayended, true);
    $('#home_video, #replay').click(function () {
        $('#video_file').css('z-index', '1');
        $('#home_video2').show();
        $('#home_video').hide();
        var flash = getFlashObject("video_file");
        flash.SetVariable("player:jsPlay", '');
        return false;
    });
    //setTimeout ( "videoPlay()", 1000 );
});

function getFlashObject(movieName) {
    if (window.document[movieName]) {
        return window.document[movieName];
    }
    if (navigator.appName.indexOf("Microsoft Internet") == -1) {
        if (document.embeds && document.embeds[movieName])
            return document.embeds[movieName]; 
    } else {
        return document.getElementById(movieName);
    }
}

var myListener = new Object();
myListener.onFullscreen = function(state) {
    var flash = getFlashObject("video_file");
    if ($.browser.msie && (jQuery.browser.version == 8.0))
        $('#video_file').hide();
    if ($('#video_file').css('top') == '0px') {
        $('#video_file')
            .css({
                'top' : '159px',
                'left' : '287px',
                'width' : '448px',
                'height' : '252px'
            })
            .attr('width','448')
            .attr('height','252');
        flash.SetVariable("player:jsResize", '448x252');
    }
    else {
        $('#video_file')
            .css({
                'top' : '0px',
                'left' : '0px',
                'width' : '1024px',
                'height' : '576px'
            })
            .attr('width','1024')
            .attr('height','576');
        flash.SetVariable("player:jsResize", '1024x576');
    }
    if ($.browser.msie && (jQuery.browser.version == 8.0))
        $('#video_file').show();
}

myListener.onStop = function(state) {
    var flash = getFlashObject("video_file");
    $('#video_file').css({
            'top' : '159px',
            'left' : '287px',
            'width' : '448px',
            'height' : '252px',
            'z-index' : '0'
        });
    flash.SetVariable("player:jsResize", '448x252');
}

myListener.onNext = function(state) {
    var flash = getFlashObject("video_file");
    $('#video_file').css({
            'top' : '159px',
            'left' : '287px',
            'width' : '448px',
            'height' : '252px',
            'z-index' : '0'
        });
    flash.SetVariable("player:jsResize", '448x252');
}

function videoPlay() {
    var flash = getFlashObject("video_file");
    flash.SetVariable("player:jsPlay", '');
    setTimeout ( "videoPause()", 500 );
}
function videoPause() {
    var flash = getFlashObject("video_file");
    flash.SetVariable("player:jsPause", '');
}


/**
 * hide all panel then show the one needed if any
 *
 * @param call_back {function} the function that show the panel
 */
function home_links_clean(call_back) {
    // stop all panels animations
    $('#concept, #product, #project, #news').clearQueue();
    
    // hide all visible panel and then call the call_back
    if ($('#link_concept').hasClass('active')) {
        $('#concept').animate({
            right:'-'+$('#concept').css('width')
            },'slow', 'easeOutCubic', call_back);
        call_back = null;
    }
    if ($('#link_product').hasClass('active')) {
        $('#product').animate({
            right:'-'+$('#product').css('width')
            },'slow', 'easeOutCubic', call_back);
        call_back = null;
    }
    if ($('#link_project').hasClass('active')) {
        $('#project').animate({
            left:'-'+$('#project').css('width')
            },'slow', 'easeOutCubic', call_back);
        call_back = null;
    }
    if ($('#link_news').hasClass('active')) {
        $('#news').animate({
            left:'-'+$('#news').css('width')
            },'slow', 'easeOutCubic', call_back);
        call_back = null;
    }
    $('#link_concept, #link_product, #link_project, #link_news').removeClass('active');
    // if call_back not yet called, call it
    if (typeof call_back == 'function')
        call_back();
}


/**
 * show the concept panel
 */
function home_show_concept() {
    $('#link_concept').addClass('active');
    $('#concept').animate({
        right:0
        },'slow', 'easeInCubic');
}

/**
 * show the product panel
 */
function home_show_product() {
    $('#link_product').addClass('active');
    $('#product').animate({
        right:'-200px'
        },'slow', 'easeInCubic');
}

/**
 * show the project panel
 */
function home_show_project() {
    $('#link_project').addClass('active');
    $('#project').animate({
        left:0
        },'slow', 'easeInCubic');
}

/**
 * show the news panel
 */
function home_show_news() {
    $('#link_news').addClass('active');
    $('#news').animate({
        left:0
        },'slow', 'easeInCubic');
}

