/*  Copyright: 2010 GFS Communications AG
Author: Fabio Santschi
*/



(function ($) {
    if (!$.contento) {
        $.contento = new Object();
    };

    $.contento.accordion = function (container, options) {
        // To avoid scope issues, use 'base' instead of 'this'
        // to reference this class from internal events and functions.



        var base = this;

        base.urlParams = {};
        (function () {
            var e,
        a = /\+/g,  // Regex for replacing addition symbol with a space
        r = /([^&;=]+)=?([^&;]*)/g,
        d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
        q = window.location.search.substring(1);

            while (e = r.exec(q))
                base.urlParams[d(e[1])] = d(e[2]);
        })();

        // Access to jQuery and DOM versions of element
        base.$container = $(container);
        base.container = container;

        // Add a reverse reference to the DOM object
        base.$container.data("contento.accordion", base);

        base.init = function () {
            base.options = $.extend({}, $.contento.accordion.defaultOptions, options);
            base.options.headers = $(base.container).find(base.options.header);
            base.options.active = base.findActive(base.options.headers, base.options.active);
            base.$container.children(base.options.header).addClass(base.options.headerClass);
            base.$container.children(base.options.content).addClass(base.options.contentClass);

            base.$container.addClass(base.options.containerClass);
            if (base.urlParams["t"] != "") {
                base.$container.children(base.options.header).eq(base.urlParams["t"]).addClass(base.options.currentClass);
                base.$container.children('.' + base.options.currentClass).next(base.options.content).show();
                $(window).scrollTop(base.$container.children('.' + base.options.currentClass).offset().top);
                /*$(window).scrollTop(base.$container.children(base.options.content).offset().top)
                alert(base.$container.children(base.options.content).offset().top);*/
            }
            /*base.$container.children(base.options.header).eq(0).addClass(base.options.currentClass); */


            // Put your initialization code here
        };



        base.clickHandler = function () {

        };

        base.findActive = function (headers, selector) {
            return selector != undefined
		? typeof selector == "number"
			? headers.filter(":eq(" + selector + ")")
			: headers.not(headers.not(selector))
		: selector === false
			? $([])
			: headers.filter(":eq(0)");
        };



        // Run initializer
        base.init();


        //base.$container.children(base.options.header).css("color","blue");

        if (base.options.event)
            base.$container.bind((base.options.eventType) + ".contento-accordion", base.clickHandler);


        base.$container.children(base.options.header).click(function () {
            $(this).css('outline', 'none');
            if ($(this).hasClass('current')) {
                $(this).next('div').slideUp(1000);
                $(this).removeClass('current');

            } else {
                base.$container.children('.current').next(base.options.content).slideUp(1000);
                $(this).siblings(base.options.header).removeClass('current');


                $(this).next('div').slideToggle(1000);
                $(this).addClass('current');



            }
            return false;
        });

    };

    $.contento.accordion.defaultOptions = {
        header: "h3",
        content: "div",
        eventType: "click",
        containerClass: "contento_accordion",
        currentClass: "current",
        headerClass: "accordion_toggle",
        contentClass: "accordion_content"
    };

    $.fn.contento_accordion = function (options) {
        return this.each(function () {
            (new $.contento.accordion(this, options));
        });
    };



})(jQuery);

