/**
 * @auhtor A.Erdenetsogt
 * @created 01.12.2011
 * @description
 * @update 02.12.2011 {contentuud zereg neegdeed baisniig boliulav, slide hiih hurdiig custom-r ogdog bolgov}
 *
 */

if(typeof SODON == 'undefined') { SODON = {}; SODON.widget = {}; SODON.example = {}} else { if( typeof SODON.widget == 'undefined') { SODON.widget = {} } if( typeof SODON.example == 'undefined') { SODON.example = {} } }

(function() {

    SODON.widget.JQAccordian = function(config) {
        this.init.apply(this, arguments);
    };

    SODON.widget.JQAccordian.prototype = {

        accordContainer: '',
        tabClassName: '', /* tab class name */
        tabItems: {}, /* tab items */
        contentClassName: '', /* content class name */
        contentWidth: null,
        contentItems: {}, /* tab items */
        defaultIndex: 0,
        slideSpeed:400,
        currIndex: 0,

        init: function(config) {

            this.tabClassName = config.tabClassName;
            this.contentClassName = config.contentClassName;
            this.accordContainer = config.accordContainer;
            this.contentWidth = config.contentWidth;

            if ( typeof config.defaultIndex != "undefined" ) {
                this.defaultIndex = config.defaultIndex;
            }

            if ( typeof config.slideSpeed != "undefined" ) {
                this.slideSpeed = config.slideSpeed;
            }

            this.currIndex = this.defaultIndex;

            this.tabItems = $('#' + this.accordContainer + " ." + this.tabClassName);
            this.tabItemsStr = '#' + this.accordContainer + " ." + this.tabClassName;
            this.contentItems = $('#' + this.accordContainer + " ." + this.contentClassName);
            this.contentItemsStr = '#' + this.accordContainer + " ." + this.contentClassName;

            //
            this.__initEvent(this.defaultIndex);

            this.__startEvent();

        },

        __initEvent: function(index){
            var self = this;

            self.contentItems.css("width",0);
            self.contentItems.hide();

            $(self.contentItemsStr + ':eq(' + index + ')').show();
            $(self.contentItemsStr + ':eq(' + index + ')').css("width", self.contentWidth);

            $(self.tabItemsStr + ':eq(' + index + ')').hide();
        },

        __startEvent: function(){
            var self = this;

            self.tabItems.each(function(i,item){
                $(item).bind("click", function(event){

                    if ($(self.contentItemsStr + ':eq(' + self.currIndex + ')').width() == self.contentWidth){

                        $(self.contentItemsStr + ':eq(' + i + ')').show();
                        $(self.contentItemsStr + ':eq(' + i + ')').animate({width:self.contentWidth}, self.slideSpeed);

                        $(self.contentItemsStr + ':eq(' + self.currIndex + ')').animate({width:0},  self.slideSpeed, function(){
                            $(self.tabItemsStr + ':eq(' + self.currIndex + ')').show();
                            $(self.tabItemsStr + ':eq(' + i + ')').hide();
                            self.currIndex = i;
                        });

                    }

                })
            });

        }



    }

}());

