/**
 * Initialize homepage sliding boxes
 */

function initInfoBoxes(){

    var arrow$ = $('<div class="info-box-arrow" />').appendTo('#info-boxes'); // arrow that slides to currently opened box

    $('.large-info-box').appendTo('#large-info-boxes').hide().removeClass('hidden');

    // state variables
    var isCycleOn = true;
    var isCyclePermanentlyOff = false;
    var cycleSpeed = 3000; // cycle speed in ms

    // disable scrolling on hover
    $('#large-info-boxes, #info-boxes')
        .hover( function(){ isCycleOn = false; },
                function(){ isCycleOn = true; } );

    var infoBox$ = $('.info-box'); // little infoboxes

    infoBox$
        .css('cursor', 'pointer')
        .click(function(evt){
            isCyclePermanentlyOff = true; // turn off scrolling permanently on click
            openBox(this); // open clicked box
            evt.preventDefault();
        });

    openBox( $('.info-box:first').get(0) ); // open first box


    /**
     * Opens box
     * @param boxElm
     */
    function openBox(boxElm){

        if(!$(boxElm).hasClass('opened')){ // if box isnt already opened

            $('.info-box').removeClass('opened'); // close others

            $(boxElm).addClass('opened'); // open it

            var pos = $(boxElm).position(); // position of opened box
            var left =  pos.left + 50;

            arrow$.animate({left: left + 'px'}); // move the slider to opened box

            var largeSelector = '#large-' + $(boxElm).attr('id'); // large box to open selector
            $('.large-info-box').removeClass('opened').fadeOut(300); // close opened box
            $(largeSelector).addClass('opened').fadeIn(300); // open the bigone
        }
    }
    $('.large-info-box').live('click',function(){
        var nextBox$ = $('.info-box.opened').next('.info-box');
        if(nextBox$.length > 0) { // if next box exists
            openBox(nextBox$.get(0)); // open it
        } else { // otherwise open the first one
            openBox($('.info-box:first'));
        }
    });

    // cycle interval identifier
    var cycleInterval;

    /**
     *  Opens next box or the firstone - if the scrolling is on
     */
    var cycleBoxes = function (){

        if(isCyclePermanentlyOff) { clearInterval(cycleInterval) }; // disable set interval

        if(isCycleOn && !isCyclePermanentlyOff){
            var nextBox$ = $('.info-box.opened').next('.info-box');
            if(nextBox$.length > 0) { // if next box exists
                openBox(nextBox$.get(0)); // open it
            } else { // otherwise open the first one
                openBox($('.info-box:first'));
            }
        }

    }

    // set cycle interval
    cycleInterval  = setInterval(cycleBoxes, cycleSpeed);
}

function setIframeHeights(){
    $('.page-id-63 iframe').load(function(){
        var iframe$ = $(this);
        iframe$.attr('height',iframe$.contents().height() + 20)
               .attr('scrolling','no');
    });

}

/**
 * Opens overlay with pf image
 */
function initPF(){

    var overlay$ = $('<div id="pfOverlay"></div>').appendTo('body');

    var faceElevatorWrap$ = $('<div id="face-elevator"></div>');

    faceElevatorWrap$.css({     position:'absolute',
                                top: '208px',
                                left: '397px',
                                height: '215px',
                                width: '70px',
                                overflow: 'hidden'
                            });
    var faceElevatorWrapElm = faceElevatorWrap$.get(0);
       var fel1$ = $('<img src="/wp-content/themes/anderson-willinger/img/pf2012/face-elevator.jpg">')
           .appendTo(faceElevatorWrapElm);
       var fel2$ = fel1$.clone()
            .appendTo(faceElevatorWrapElm);

        overlay$
            .append('<div id="pf-wrap"><img src="/wp-content/themes/anderson-willinger/img/pf2012/pf-2012.png" alt="Happy new year!"></div>')
                .css({textAlign:'center', paddingTop: '100px', cursor:'pointer' });
         $('#pf-wrap').append(faceElevatorWrap$).css({display:'inline-block', position: 'relative'});


       overlay$.dialog({modal: true, width: 'auto', position:  ['right','top'] });

       overlay$.click(function(){
           $(this).dialog('close');
       });
    animateElevator(20000);


    function animateElevator(spd){

        fel1$.animate({marginTop: -1210 },spd,'linear',function(){
            fel1$.appendTo(faceElevatorWrapElm).css('marginTop',0);
            fel2$.animate({marginTop: -1210 },spd,'linear',function(){
                fel2$.appendTo(faceElevatorWrapElm).css('marginTop',0);
                animateElevator(spd);
            });
        });
    }

}
/**
 * On document ready
 */
jQuery(function($){
    if($('#info-boxes').length > 0) {
        initInfoBoxes();
    }


    if(window.location.hash == '#!/pf-2012') {
        initPF();
    }

    setIframeHeights();


            $('form.touch').html5form({
                        allBrowsers: true,
                        colorOn: '#444',
                        colorOff:'#888',
                        emptyMessage : 'Please fill all the fields!',
                        emailMessage : 'Please write a correct email adress!',
                        responseDiv: '.error-message',
                        async : false

                    });

              $('form.touch').submit(function(evt){
                    var form$ =  $(this);
                    var data_string = form$.serialize();
                    $.post(form$.attr('action'),data_string, function(data){
                        form$.replaceWith(data);
                    });

                    evt.preventDefault();
                });

})


