/* GLOBALS */
var fbEnable = false;
var slidebar_top_original = 0;

// fsSlideBox globals
var fsSlideBox_currentIndex = -1;
var fsSlideBox_count = 0;
var fsSlideBox_busy = false;
var fsSlideBox_interval = null;

// wait for doc to finish loading
$(document).ready(function() {
    // load cookies
    loadCookies();

    // enable colorbox
    $('a.cb.inline').colorbox({
        inline:         true,
        transition:     "none",
        href:           $(this).attr('href'),
        returnFocus:    false
    });

    // enable social share privacy
    if($('#socialshareprivacy').length > 0){
        $('#socialshareprivacy').socialSharePrivacy({
            services : {
                facebook : {
                  'dummy_img': '/wp/wp-content/themes/english-study2011/lib/socialshareprivacy/images/dummy_facebook.png'
                },
                twitter : {
                    'dummy_img': '/wp/wp-content/themes/english-study2011/lib/socialshareprivacy/images/dummy_twitter.png'
                },
                gplus : {
                    'dummy_img': '/wp/wp-content/themes/english-study2011/lib/socialshareprivacy/images/dummy_gplus.png'
                }
              },
              'cookie_domain' : 'english-study.de'
        });
    }

    // start tabs
    $("#indextabs").tabs({
        cookie:     { expires: 1, path: '/', secure: true },
        fx:         { opacity: 'toggle' }
    });

    $('#sidebartabs').tabs({
        cookie:     { expires: 1 },
        fx:         { opacity: 'toggle' }
    });

    // enable facebook fanbox toggle
    $('#fbFanBoxToggle').click(function() {
        // already activated?
        if(typeof(FB) !== 'undefined' && FB != null) {
            // switch off
            fbToggleFanBox(false);

        } else {
            // switch on
            fbToggleFanBox(true);
        }

        // prevent default click behavior
        return false;
    });

    //  load fanbox via cookie?
    if(fbEnable) fbToggleFanBox(true);
    
    // enable toggle boxes
    EnableToggleBoxes();

    // init slidebar
    slidebar_init();

    // init slidebox
    if($('#fsSlideBox').length > 0)
        fsSlideBox_init();
});

function EnableToggleBoxes() {
    // collapse
    $('div.togglebox').each(function() {
        $(this).removeClass("collapsed");
    });

    // add switch
    $('div.togglebox h2').each(function() {
        var switchLink = '<a href="#" class="toggleswitch">' + $(this).html() + '</a>';
        $(this).html(switchLink);
    });

    // hook up event handlers
    $('div.togglebox h2 a.toggleswitch').click(function() {
        // retrieve box
        var box = $(this).parent().parent();

        // toggle
        box.toggleClass('collapsed');
        return false;
    });
}
function loadCookies() {
    // fb FanBox
    var c = $.cookie('fbEnable');
    fbEnable = (c != null && c == 'true') || false;
}
function fbToggleFanBox(toggle) {
    // replace wrapper contents
    if(toggle) {
        // switch on

        // load facebook js
        $.getScript('http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php/en_US', function() {
            // initialize
            FB.init("211812135565159");
        });

        // show compact switch
        $('#fbFanBoxSwitchOff').addClass('compact');
        $('#fbFanBoxSwitchOff a').html('Fanbox ausschalten');

        // show fan box
        $('#fbFanBoxWrapper').html('<fb:fan profile_id="152410778110259" stream="0" connections="8" logobar="0" height="270" width="260" css="http://www.english-study.de/fbFanBox.css?22"></fb:fan>');

        // set cookie
        $.cookie('fbEnable', 'true', { expires: 7, path: '/' });

        // set active
        $('#fbFanBoxToggle').addClass('on');

    } else {
        // clear fanbox
        $('#fbFanBoxWrapper').html('');

        // show full switch
        $('#fbFanBoxSwitchOff').removeClass('compact');
        $('#fbFanBoxSwitchOff a').html('Fanbox einschalten');

        // set inactive
        $('#fbFanBoxToggle').removeClass('on');

        // unset cookie and reload page
        // "Note when clearing cookies, you must pass the exact same options that were used to initially set the cookie."
        // see: https://github.com/carhartl/jquery-cookie/blob/7e8d8af14d65c4c3dcbb68c05ac7275531c36588/README.rdoc
        $.cookie('fbEnable', null, { expires: 7, path: '/' });
    }
}
function slidebar_init() {
    // check for existence
    if($('#slidebar').length == 0) return;

    // save initial top
    slidebar_top_original = $('#slidebar').offset().top;

    // hook up window scroll event
    $(window).scroll(function() {
        try {
            // get viewport scroll top
            winScrollTop = $(window).scrollTop();
            footerTop = $('#footer_es').position().top;
            maxScrollTop = Math.min(winScrollTop, footerTop - $('#slidebar').height() - 10);

            // slide bar
            if(maxScrollTop > (slidebar_top_original)) {
                var intDiff = maxScrollTop - slidebar_top_original;
                $('#slidebar').css('top', intDiff + 'px');

            } else {
                $('#slidebar').css('top', '0');
            }

        } catch(err) {
            // release event hook
            $(window).unbind('scroll');

            // reset
            $('#slidebar').css('top', '0');
        }
    });

    // fire once
    $(window).scroll();
}
function fsSlideBox_init() {
    // count slides
    fsSlideBox_count = $('#fsSlideBox ul.slides li').length;

    // hook up click events
    $('#fsSlideBox ul.selector li a').click(function() {
        // retrieve index
        var index = $(this).parent().index();

        // slide in child
        fsSlideBox_slideIn(index, false);

        // prevent default click behavior
        return false;
    });

    // slide in first and set interval
    fsSlideBox_slideIn(0, true);
}
function fsSlideBox_slideIn($index, $setInterval) {
    // is busy?
    if(fsSlideBox_busy) return false;

    // is current index?
    if(fsSlideBox_currentIndex == $index) return false;

    // set busy
    fsSlideBox_busy = true;

    // clear interval
    window.clearInterval(fsSlideBox_interval);

    // calc item left
    var childIndex = $index + 1;

    // move items
    var targetChild = $('#fsSlideBox ul.slides li:nth-child(' + childIndex + ')');
    var target = $('#fsSlideBox ul.slides');
    var itemLeft = targetChild.position().left;

    $(target).animate({
        left: -itemLeft + 'px'
    }, 500, 'swing', function() {
        // set new index
        fsSlideBox_currentIndex = $index;

        // unset busy
        fsSlideBox_busy = false;

        // set interval
        if($setInterval)
            fsSlideBox_interval = window.setInterval("fsSlideBox_slideInNext()", 5000);
    });

    // activate selector
    $('#fsSlideBox ul.selector li').removeClass('active');
    $('#fsSlideBox ul.selector li:nth-child(' + childIndex + ')').addClass('active');

    // done
    return true;
}
function fsSlideBox_slideInNext() {
    // calc next
    var $next = (fsSlideBox_currentIndex + 1) % fsSlideBox_count;

    // slide in
    fsSlideBox_slideIn($next, true);
}
