/*
    Document   : home
    Created on : 26/10/2010, 09:31:00
    Author     : Maurício Strudel
    Description:
    Purpose of the javascript follows.
*/
var SWAP_TIME = 8000;
var TITLE_TIME = 5000;
var currentGalery = 0;
var currentImgContainer = 1;
var gal0Loaded = false;
var gal1Loaded = true;
var raffledArray = new Array();



var timerIntervalID = window.setInterval(animateSwapBG, SWAP_TIME);
var tittleTimeoutID = 0;

function showISee(){
    $('.world_i_see').fadeIn('slow');
    //tittleTimeoutID = window.setTimeout(hideISee, TITLE_TIME);
    
}

function hideISee(){
     window.clearTimeout(tittleTimeoutID);
    $('.world_i_see').fadeOut('slow');
    
}
function showIImagine(){
    $('.world_i_imagine').fadeIn('slow');
    //tittleTimeoutID = window.setTimeout(hideIImagine, TITLE_TIME);
    
}

 function hideIImagine(){
     window.clearTimeout(tittleTimeoutID);
    $('.world_i_imagine').fadeOut('slow');
    
}

function animateSwapBG()
    {

        // if the current galery is I Imagine
        if(currentGalery == 0){

            if(raffledArray.length == 0){
                showIImagine();
                hideISee();
            }

            //raffle the next image from the iImaginePics array
            var nextImgIndex = raffle(iImaginePics.length-1);



            if(currentImgContainer == 0 && gal0Loaded){

                currentImgContainer = 1;
                //moves the raffled element from the iImaginePics array to the raffledArray
                var removedElement1 = iImaginePics.splice(nextImgIndex,1);
                raffledArray = raffledArray.concat(removedElement1);
                //sets the flag of image loaded to false;
                gal0Loaded = false;

                $('#img1').attr({src: raffledArray[raffledArray.length-1]});

            }else if(currentImgContainer == 1 && gal1Loaded){

                currentImgContainer = 0;
                //moves the raffled element from the iImaginePics array to the raffledArray
                var removedElement = iImaginePics.splice(nextImgIndex,1);

                raffledArray = raffledArray.concat(removedElement);



                //sets the flag of image loaded to false;
                gal1Loaded = false;

                $('#img0').attr({src: raffledArray[raffledArray.length-1]});

            }


            //if all the elements from iImaginePics were raffled
            if(iImaginePics.length == 0){

                //equalize the iImaginePics with the raffledArray
                iImaginePics = iImaginePics.concat(raffledArray);

                //reset the raffledArray
                raffledArray = new Array();

                currentGalery = 1;

            }

        } else if(currentGalery == 1){

            if(raffledArray.length == 0){
                showISee();
                hideIImagine();
            }

            //raffle the next image from the iImaginePics array
            var nextImgIndexIsee = raffle(iSeePics.length-1);

            if(currentImgContainer == 0 && gal0Loaded){

                currentImgContainer = 1;
                //moves the raffled element from the iImaginePics array to the raffledArray
                var removedElement11 = iSeePics.splice(nextImgIndexIsee,1);
                raffledArray =  raffledArray.concat(removedElement11);
                //sets the flag of image loaded to false;
                gal0Loaded = false;

                $('#img1').attr({src: raffledArray[raffledArray.length-1]});

            }else if(currentImgContainer == 1 && gal1Loaded){

                currentImgContainer = 0;
                //moves the raffled element from the iImaginePics array to the raffledArray
                var removedElement10 = iSeePics.splice(nextImgIndexIsee,1);
                raffledArray =  raffledArray.concat(removedElement10)
                //sets the flag of image loaded to false;
                gal1Loaded = false;

                $('#img0').attr({src: raffledArray[raffledArray.length-1]});

            }


            //if all the elements from iImaginePics were raffled
            if(iSeePics.length == 0){

                //equalize the iImaginePics with the raffledArray
                iSeePics = iSeePics.concat(raffledArray);

                //reset the raffledArray
                raffledArray = new Array();

                currentGalery = 0;

            }
        }


    }

$(document).ready(
    function() {

         resizeBG();
         animateSwapBG();
         

         $('.imgBG').load(function(){
             

            switch($(this).attr('id')){

                case 'img1':
                    
                    gal1Loaded = true;

                    $('#img1').fadeIn('slow');
                    $('#img0').fadeOut('slow');
                break;

                case 'img0':
                    
                    gal0Loaded = true;
                    $('#img0').fadeIn('slow');
                    $('#img1').fadeOut('slow');
                break;

            }
        })
         $(window).bind("resize", resizeBG);
//Versão original da home
//
//
//         $(".world_i_imagine_over").hide();
//         $(".world_i_see_over").hide();
//
//         $('.home_ball_highlight').hide();
//
//         $(".world_i_imagine_over").click(function(){swapContent('bt_series_i_imagine')});
//         $('.world_i_see_over').click(function(){swapContent('bt_series_i_see')});
//
//          //mouse hover and mouse out behaviours for the cell titles.
//         $(".world_i_imagine").mouseover(
//            function(){
//
//                //$(this).parent().children('.home_bg_hover_right').fadeOut('fast');
//                $(this).parent().children('.home_ball_highlight').show();
//                $(this).parent().children('.home_ball_highlight').css({'margin-left':'560px','margin-top':'225px'});
//                $(this).parent().children(".world_i_imagine_over").show();
//
//
//
//            }
//         );
//
//         $(".world_i_imagine_over").mouseout(
//             function(){
//                //$(this).parent().children('.home_bg_hover_right').fadeIn('fast');
//                $(this).parent().children('.home_ball_highlight').hide();
//                $(this).hide();
//
//            }
//         );
//
//         $(".world_i_see").mouseover(
//
//            function(){
//
//                //$(this).parent().children('.home_bg_hover_left').fadeOut('fast');
//                $(this).parent().children('.home_ball_highlight').show();
//                $(this).parent().children('.home_ball_highlight').css({'margin-left':'70px','margin-top':'225px'});
//                $(this).parent().children(".world_i_see_over").show();
//            }
//         );
//
//         $(".world_i_see_over").mouseout(
//             function(){
//                //$(this).parent().children('.home_bg_hover_left').fadeIn('fast');
//                $(this).parent().children('.home_ball_highlight').hide();
//                $(this).hide();
//
//            }
//         );

      //////////////////////////////////////////////////////////////////////////
      ////////////////////////////////Nova Home/////////////////////////////////
      //////////////////////////////////////////////////////////////////////////
    
    $(".world_i_see").hide();
    $('.home_ball_highlight').hide();




    }
);

    

    /*
     * Raffle an integer number from the total amont passed in the param length
     * @param nLength - an integer number to be raffle from
     *
     */
    function raffle(nLength)
    {

        
        return Math.round(Math.random()*nLength);
    }

    function resizeBG(){

        var panMarginLeft = $('.menu').width();
        var panW = windowW - panMarginLeft;
        var panH = windowH;

        var titleMarginLeft = panW - $(".world_i_imagine").width() - 80;
        var tittleMarginTop = (panH/2) - ($(".world_i_imagine").height()/2);

        $('.home_bg_container').css({'margin-left': panMarginLeft});
        $('#img0').attr({height: windowH});
        $('#img1').attr({height: windowH});

        $(".world_i_imagine").css({'margin-left': titleMarginLeft});
        $(".world_i_see").css({'margin-left': titleMarginLeft});

        $(".world_i_imagine").css({'margin-top': tittleMarginTop});
        $(".world_i_see").css({'margin-top': tittleMarginTop});


//        var containerWidth = (windowW - 290);
//        var bgCssW = containerWidth+'px';
//        var bgCssH = windowH+'px';
//
//        $('.home_bg_container').css({'width': bgCssW, 'height':bgCssH});
//
//        if(containerWidth > 920 && containerWidth < 1620){
//
//
//            //920 <=> -530
//            //containerWidth <=> xOffset
//
//            var xOffset = ((530 * containerWidth) / 920) - 530;
//            var groupMarginLeft = xOffset + 'px';
//
//            $('#bg_group').css('margin-left',groupMarginLeft);
//        }
//
//        if(windowH > 768 && windowH < 1080){
//
//            // - 156 - 768
//            //yOffset - windowH
//            //var yOffset = (156*(windowH /768)) -156;
//
//            //hard coded values... cabrito
//            var yOffset = 1.48*(windowH/2) - (1080/2);
//            var groupMarginTop = yOffset + 'px';
//
//            $('#bg_group').css('margin-top',groupMarginTop);
//
//        }
    }

   
