$(window).load(function() {

    setTimeout(function() {
        if( typeof onPageShow == 'function' ) {
            onPageShow();
        }
    }, 500);

    $('.image-container').each(function() {

        var handler = this;
        var image_counter = $('span.image', this).length;

        var prev_position = $(this).css('position');
        $(this).css('position', 'relative').append('<div class="image-loader"></div>');

        $('span.image', this).each(function() {

           var span_handler = this;
           var img = new Image();
           img.src = $(this).attr('data-src');

           img.onload = function() {
               
               image_counter--;
               $(span_handler).replaceWith(img);

               if( image_counter == 0 ) {
                    image_container_loaded(handler, prev_position);
                }
           }
           
           img.onerror = function() {
               image_counter--;
               
               if( image_counter == 0 ) {
                   image_container_loaded(handler, prev_position);
               }

               $(span_handler).parent().remove();
           }           
        });
    });

    $('input[placeholder]').placeholder();

});

function image_container_loaded(handler, restore_position) {
    $(handler).css('position', restore_position).find('div.image-loader').remove();
   
    if( typeof onSlidesLoad == 'function' ) {
        onSlidesLoad();
    }
}

function show_expert(css, photo_selector, description_selector) {

    $(description_selector).children('.bio').html( team[css]['bio'] );
    $(description_selector).children('.name').html( team[css]['name'] );
    $(description_selector).children('.position').html( team[css]['title'] );

    $(photo_selector).children('img').attr('src', '/img/team/small/' + team[css]['photo_name'] + '.jpg');

    var email = team[css]['email'].length > 25 ? team[css]['email'].substr(0,25) + '...' : team[css]['email'];

    $(photo_selector).find('a.linkedin').attr('href', team[css]['linkedin']).css('display', team[css]['linkedin'] ? 'inline' : 'none' );
    $(photo_selector).find('a.email').attr('href', 'mailto:' + team[css]['email']).html(email).css('display', team[css]['email'] ? 'inline' : 'none');

}


function home_notify_email(email) {
   
}

function show_expert_popup(css_name, onClose) {

    $('#expert_overlay').fadeIn(600, function() {
        $('html,body').animate({scrollTop: 0}, 600);
        
        show_expert(css_name, '#expert .expert-popup-body div.photo', '#expert .expert-popup-body div.description');
        $('#expert > .expert-popup-body').show();
    });

    $('#expert img.close').click(function() {
        $('#expert > .expert-popup-body').hide();
        $('#expert_overlay').fadeOut(600, function() {
            if( typeof onClose == 'function' ) {
                onClose();
            }
        });
    });
}

function popup(obj) {
    var html = $('<div id="popup_overlay" class="popup_overlay"></div>' +
    '<div class="popup" style="display: none;" id="popup">' +
        '<img class="close" src="/img/btn_close.png" onclick="popup_close();"/>' +
        obj.html() +
    '</div>').appendTo('body');
 
    $('#popup').css('marginLeft', '-' + ($('#popup').width() / 2) + 'px' );
    $('#popup').css('marginTop', '-' + ($('#popup').height() / 2) + 'px' );

    $('#popup').fadeIn(600);
}

function popup_close() {
    $('#popup').fadeOut(600);
    setTimeout( function() {
        $('#popup,#popup_overlay').remove();
    }, 600 );
}

var current_person = 0;

function slide_person_to(num) {
    slide_person( num - current_person );
}

function slide_person(step) {

    var next_person = current_person + step;
    var max_count = 4;

    if( next_person < 0 || next_person >= max_count ) {
        return;
    }

    $('.logos > div:eq(' + current_person + ')').fadeOut(300);

    $('.people > div:eq(' + current_person + ')').css('z-index', '0');
    current_person = next_person;

    $('.left_arrow').css('display', current_person ? 'block' : 'none');
    $('.right_arrow').css('display', current_person == max_count-1 ? 'none' : 'block');    

    var left = current_person * 1000;

    setTimeout( function() {
        $('.logos > div:eq(' + current_person + ')').fadeIn(600);
    }, 300);

    setTimeout(function() {
        $('.people > div:eq(' + current_person + ')').css('z-index', '2');
    }, 1000);

    $('.people').animate({left: '-' + left}, 1000);
    $('#switcher > div').removeClass('selected');
    $('#switcher > div:eq(' + current_person + ')').addClass('selected');
}

var album_expanded = 0;

function change_folder(handler, folderId) {

    $(handler).parent().children().removeClass('selected');

    $(handler).addClass('selected');
    if( !album_expanded ) {
        $('.articles .article_embed').animate({height: $('.articles .article_embed').height() - 215}, 1000);
        $('.articles .year_selector').animate({bottom: parseInt($('.articles .year_selector').css('bottom').replace('px', '')) + 215}, 1000);
    }

    $('#folder_selector > div').hide();
    $('#folder' + folderId).show();

    if( $('#folder' + folderId + ' .folder ').width() > 980 ) {
        $('#slide_right,#slide_left').show();
    } else {
        $('#slide_right,#slide_left').hide();
    }
    $('#folder_selector').scrollLeft(0);

    album_expanded = 1;
}

function change_article(documentId, title) {

    $('#flash').html('');

    $('#flash').flash({
        src: 'http://static.issuu.com/webembed/viewers/style1/v1/IssuuViewer.swf?mode=embed&amp;layout=http%3A%2F%2Fskin.issuu.com%2Fv%2Fcolor%2Flayout.xml&amp;backgroundColor=FFFFFF&amp;showFlipBtn=true&amp;documentId=' + documentId + '&amp;docName=socialcurrency&amp;username=vpgroup&amp;loadingInfoText=' + title + '&amp;et=1289316799259&amp;er=67',
        width: '100%',
        height: '100%'
    });

}

function publications_slide(step) {

    $('#slide_right,#slide_left').show();

    if( step == 1  ) {$('#slide_right').hide();}
    if( step == -1 ) {$('#slide_left').hide();}

    $('#books_container').animate({scrollLeft: '+=' + step * 980 + 'px'}, 1000);

}

function articles_slide(step) {
    $('#folder_selector').animate({scrollLeft: step * 400}, 800);
}

var current_grow = 0;
//var grows = ['Grow', 'Build strong brands', 'Create innovation playbooks', 'Prepare for future growth', 'Formulate growth strategies',
//           'Create global management programs', 'Identify new opportunities for growth', 'Become social'];

function change_grow() {
    
    current_grow++;

    if( current_grow >= grows.length ) {
        current_grow = 0;
    }

    $('#grow').fadeOut(400);
    
    setTimeout(function() {
        $('#grow').html(grows[current_grow]);
        $('#grow').fadeIn(400);
    }, 400);
}

function show_profile(name) {

    var index = 0;

    for(var i = 0; i< profiles.length; i++) {
        if( profiles[i].name == name ) {
            index = i;
            break;
        }
    }

    $('#profile_name').html( profiles[index].name );
    $('#profile_position').html( profiles[index].title );
    $('#profile_description').html( profiles[index].description );
    $('#profile_photo').attr('src', '/img/photos/' + profiles[index].name + '.png' );
    
    //email
    if(profiles[index].email !== undefined) {
            $('#profile_email').html("<a class='icon_link' href='mailto:" + profiles[index].email + "'></a>");
            $('#profile_email').show();
    } else {
            $('#profile_email').hide();
            $('#profile_email').html('');
    }

    //linkedin
    if(profiles[index].linkedin !== undefined) {
            $('#prodile_linkedin').html("<a class='icon_link' href='" + profiles[index].linkedin + "'></a>");
            $('#prodile_linkedin').show();
    } else {
            $('#prodile_linkedin').hide();
            $('#prodile_linkedin').html('');
    }

    $('#profiles').prev().fadeTo(400, 0.2);
    $('#profiles > div:first-child').fadeIn(400);
}

function close_profile() {
    $('#profiles').prev().fadeTo(400, 1);
    $('#profiles > div').fadeOut(400);
}

function show_video(id) {
    $('<div id="video_overlay" style="position: fixed; top: 0px; left: 0px; width: 100%; height: 100%; background: black; z-index: 4; opacity: 0; filter: alpha(oapcity=0);"></div>').appendTo('body');
    $("#video_frame").attr("src", "http://www.youtube.com/embed/" + (typeof id == 'undefined' ? '9_wQQQ7gReM' : id ) + "?rel=0&autoplay=1");
    $('#video_overlay').fadeTo(600, 0.75);
    $('#video').show();
}

function close_video(handler) {
	$("#video_frame").attr("src", "");
    $(handler).parent().hide();
    $('#video_overlay').remove();
}
function show_articles(handler, selected_year){
    $("[class^=articles_][class=articles_year]").hide();
    $("#articles_" + selected_year).show();
    $("#articles_" + (selected_year - 1)).show();
    if(selected_year == "2000") {
            for(i = 2; i<10; i++){
                    $("#articles_" + (selected_year - i)).show();
            }
    }
    if(handler != null){
            $(handler).parent().children().removeClass('selected').removeClass("triangle");
            $(handler).addClass("selected");
            $("#triangle_" + selected_year).addClass("triangle");
    }
}

function trim(s) {
    return s.replace(/(^\s+)|(\s+$)/g, "");
}

function mailchimp_show_thank_you() {
    $('.mailchimp #thankyou').prev().hide();
    $('.mailchimp #thankyou').show();
}

function getPageScroll() {
    
        var yScroll;    
        if (self.pageYOffset) {
            yScroll = self.pageYOffset;
        } else if (document.documentElement && document.documentElement.scrollTop) {
            yScroll = document.documentElement.scrollTop;
        } else if (document.body) {
            yScroll = document.body.scrollTop;
        }
        return yScroll;
}

