/*
 * STEREO UI JS
 *
 * Copyright (c) 2011 Stereo Agency
 *
 * http://www.stereoagency.com
 *
 */

maxImgOptions = {
    isBackground: true,
    verticalOffset:(0),
    horizontalOffset:(0),
    leftSpace:(0),
    topSpace:(0),
    rightSpace:(0),
    bottomSpace:(0),
    zIndex:(-10),		
    verticalAlign:'center',
    maxFollows: 'height',
    horizontalAlign:'center'
}

$(document).ready(function() {    
    // Toggle up du footer
    $("#footer").css('display','none').slideDown('slow');
    $('img.bgmaximage').maxImage(maxImgOptions);
    
    // Execution initiale de la commande de repos des fleches
    positionNav();

    // Execution lors du redimensionnement de la page
    $(window).resize(resizePositionNav);
    
    $('div.logo2').click(function() {
        document.location='/';
    });
    $('div.logo').click(function() {
        document.location='/';
    });
});

function resizePositionNav() {
    positionNav();
    $('img.bgmaximage').maxImage(maxImgOptions).show();    
}

// La fonction de positionnement des fleches
function positionNav() {
    var bSize = getViewDim();
    $(".left").css('margin-top',(bSize.height-70-375)/2+'px');
    $(".right").css('margin-top',(bSize.height-70-375)/2+'px');  
    

}
function getViewDim() {
    var bodySize = {
        width: $("body").innerWidth(),
        height: $("body").innerHeight()
    };
    return bodySize;	
}
/**
 * Afficher les données du panneau produit
 */
function drawPanel(x) {                               
    // Définition du fond d'écran
    $("#bgimg"+currentProd+" img").attr("src", x.background);
    // Nom du client + titre produit
    $("#client_name").text(x.name);
    $("#product_link_green").text(x.title);
    // Description
    if (x.description == '') {
        $("#product_desc").html('<p>&nbsp;</p>');
    } else {
    $("#product_desc").html(x.description);
}
    // Lien media
    $("#prod_media").empty().html(x.media);
    $("#prod_media a").fancybox(); // Utile.
    // Lien produit
    if (x.link=='') {
        $("#prod_link").empty();    
    } else {
        $("#prod_link").empty(); 
        $('<a>').attr('class','website').attr('href',x.link).attr('target','_blank').text('Visit website').appendTo('#prod_link');
        //$("#prodt_link").attr('href',x.link).attr('class','visit').text('Visit website');
    }
    document.location.hash = x.anchor;
}
/**
* Déplacement panneau précédent
*/
function movePrevious() {
    oldProd = currentProd;
    if (currentProd > 0) {
        currentProd = currentProd - 1;
    } else {
        currentProd = lastProd;
    }
    $("#footer").slideUp(function() {
        $('<img />').attr('src', products[currentProd].background).load(function(){        
            $imgblock = $('<div class="bgimg" id="bgimg'+currentProd+'"></div>');                    
            $imgblock.insertBefore('#bgimg'+oldProd);
            $('#bgimg'+oldProd+' img').clone().appendTo('#bgimg'+currentProd);
            $('#bgimg'+currentProd+' img').attr('src',products[currentProd].background);            
            $('#bgimg'+currentProd).css('margin-left','-'+$("body").innerWidth()+'px');            
            $('#bgimg'+oldProd).animate({
                left: '+='+$("body").innerWidth()+'px'
            }, 1000, function() {
                $(this).remove();
                drawPanel(products[currentProd]);
                $("#footer").slideDown();                        
            });
            $('#bgimg'+currentProd).animate({
                marginLeft: '0px'
            }, 1000, function() {
                //
                });
        });
    });    
    return false;
}
/**
* Déplacement panneau suivant
*/
function moveNext() {
    oldProd = currentProd;
    if (currentProd < lastProd) {
        currentProd++;
    } else {
        currentProd = 0;
    }
    
    $("#footer").slideUp(function() {
        $('<img />').attr('src', products[currentProd].background).load(function(){
            $imgblock = $('<div class="bgimg" id="bgimg'+currentProd+'"></div>');                    
            $imgblock.insertAfter('#bgimg'+oldProd);                   
            $('#bgimg'+oldProd+' img').clone().appendTo('#bgimg'+currentProd);            
            $('#bgimg'+currentProd+' img').attr('src',products[currentProd].background);
            $('#bgimg'+currentProd).css('margin-left',$("body").innerWidth()+'px');            
            $('#bgimg'+currentProd).animate({
                marginLeft: '0px'
            }, 1000, function() {
                $('#bgimg'+oldProd).remove();
                drawPanel(products[currentProd]);                 
                $("#footer").slideDown();                        
            });
            $('#bgimg'+oldProd).animate({
                left: '-='+$("body").innerWidth()+'px'
            }, 1000, function() {
                //
                });    
        });
    });
    return false;
}

/**
* Trouver l'id dans le tableau produit correspondant à l'anchor
* retourne 0 si non trouvé
*/
function getKeyByAnchor(obj, hash) {
    var returnKey = 0;
    $.each(obj, function(key, info) {
        if (info.anchor == hash) {
            returnKey = key;
            return false;
        };   
    });    
    return returnKey;                  
}
