var csPopup = {};
var csHovercard = {}; // Latest opened hovercard
var csSlidePanel = {};
$(function(){
    csSlidePanel = cs.components().slidePanel({version: 2});
});
_r(function(){
	window.___cs.onLoad();
});

function post(url, params, urlEncoded, newWindow) {

    var form = $('<form />').hide();
    form.attr('action', url)
        .attr('method', 'POST')
        .attr('enctype', urlEncoded ? 'application/x-www-form-urlencoded' : 'multipart/form-data');
    if(newWindow) form.attr('target', '_blank');

    function addParam(name, value, parent) {
        var fullname = (parent.length > 0 ? (parent + '[' + name + ']') : name);
        if(value instanceof Object) {
            for(var i in value) {
                addParam(i, value[i], fullname);
            }
        }
        else $('<input type="hidden" />').attr({name: fullname, value: value}).appendTo(form);
    };

    addParam('', params, '');

    $('body').append(form);
    form.submit();
}

//TITLE QTIP
_r(function() 
{
	cs.components().hovercards('data-hovercard', {});
	cs.components().titleTips(false, {});
	cs.link('a[data-hash]', {});
    
    // all elements with class js-ellipsis will have dots after texts
    cs.components().ellipsis(false, {});
});

// custom window.onPushState function for csPager listener 
// this is handy function that knows when pushState was fired
// since there is no default window.onpushstate we created our own
(function(window){
    var history = window.history;
    var pushState = history.pushState;
    history.pushState = function(state) {
        // Generate a custom event for pushstate
        var event = new CustomEvent('pushstate', {
            detail: {
                state: state
            }
        });
        window.dispatchEvent(event);

        // Continue with the normal execution of the default pushState method
        return pushState.apply(history, arguments);
	};

    // Set up a flag that keeps track if the page's hash has changed
    // Needed because the CsPager code that sets its own listeners will load asynchronously
    function trackHashchange(e) {
        window.___cs.hashlistener.changed = true;
    }
    (window.___cs || (window.___cs = {})).hashlistener = {
        changed: false,
        clear: function() {
            $(window).off('hashchange', trackHashchange);
        }
    };
    $(window).on('hashchange', trackHashchange);
})(window);

// Make this method available on all pages
window.scrollIntoView = function(el, extraOffset) {
    el = $(el);
    if ('number' != typeof extraOffset) {
        extraOffset = 15;
    }

    var currentScroll = $(window).scrollTop(),
        windowHeight = $(window).height(),
        elOffset = el.offset().top,
        newOffset = (cs.barManager().obstructedHeight + extraOffset),
        targetScroll = elOffset - newOffset;

    if (
        currentScroll > targetScroll ||                 // User has already scrolled past the element
        elOffset > (currentScroll + 0.8 * windowHeight) // Element is below 80%  of the window's visible area
    ) {
        $('body, html').animate({
            scrollTop: targetScroll
        });
    }
}