
var TabSet = new Class({
    options: {
        activeClass: 'active', //css class
        cookieName: '',
        cookieOptions: {
            duration: 30, //30 days
            path: '/'
        },
        startIndex: 0 //start with this item if no cookie or active
    },
    Implements: [Options,Events],
    initialize: function(tabs,contents,options) {
        //handle arguments
        this.setOptions(options);
        this.tabs = $$(tabs);
        this.contents = $$(contents);
        //determine the "active" tab
        var active = (Cookie.read(this.options.cookieName) || this.options.startIndex);
        this.activeTab = this.tabs[active].addClass(this.options.activeClass);
        this.activeContent = this.contents[active].addClass(this.options.activeClass);
        //process each tab and content
        this.tabs.each(function(tab,i) {
            this.processItem(tab,this.contents[i],i);
        },this);
        //tabs are ready -- load it!
        this.fireEvent('load');
    },
    processItem:function(tab,content,i) {

        content.set('slide', {
            duration: 0,
            transition: Fx.Transitions.linear
        }).slide('out');

        this.activeContent.set('slide', {
            duration: 0,
            transition: Fx.Transitions.linear
        }).slide('in');

        //add a click event to the tab
        tab.addEvent('click',function(e) {
            //stop!
            if(e) e.stop();
            //if it's not the active tab
            if(tab != this.activeTab) {
                //remove the active class from the active tab
                this.activeTab.removeClass(this.options.activeClass);
                this.activeContent.removeClass(this.options.activeClass);
                //make the clicked tab the active tab
                (this.activeTab = tab).addClass(this.options.activeClass);
                content.addClass(this.options.activeClass);

                //tween the old tab content up
                //tween the new content down
                this.activeContent.set('slide', {
                    onComplete: function() {
                        this.activeContent = content.set('slide', {
                            duration: 400 ,
                            transition: Fx.Transitions.linear
                        }).slide('in');
                        this.fireEvent('change',[tab,content]);
                    }.bind(this),
                    duration: 400 ,
                    transition: Fx.Transitions.linear
                }).slide('out');


//                this.activeContent.set('tween',{
//                    onComplete:function() {
//                        this.activeContent = content.fade('in').set('tween',{ onComplete: $empty }).tween('height',contentHeight);
//                        //fire the tab change event
//                        this.fireEvent('change',[tab,content]);
//                    }.bind(this)
//                }).setStyles({
//                    height: contentHeight,
//                    overflow: 'hidden'
//                }).fade('out').tween('height',0);

                //save the index to cookie
                if(this.options.cookieName) Cookie.write(this.options.cookieName,i,this.options.cookieOptions);
            }
        }.bind(this));
    }
});





window.addEvent('domready', function() {

    // zakładki z mikroblogami
    var tabset = new TabSet($$('#msg_tabs li a'),$$('#msg_cont li'),{
        cookieName: 'mikroblogi'
    });


    // ukrywanie prawej części strony na początku
    var sidebarfx = new Fx.Slide('sidebar', {
            duration: 1300,
            transition: Fx.Transitions.Bounce.easeOut
        });

    var prawacookie = (Cookie.read('rightcookie') || 0)

    if(prawacookie == 0)
    {
        sidebarfx.hide();
        $('container').setStyle('width', '700px');
        $('showright').store('stan', 0);
    }
    else
    {
        $('showright').store('stan', 1);
        $('showright').setStyle('background-image', 'url(\'gfx/arr2.gif\')');
    }
    

    // wysuwanie prawej części strony
    $('showright').addEvent('click', function(c) {
        c.stop();


        var szerokoscstrony = new Fx.Morph('container', {
            duration: 600,
            transition: Fx.Transitions.linear

        });

        var funkcjaA = function(d) {
            if($('showright').retrieve('stan') == 0)
            {
                szerokoscstrony.start({
                    'width': 700
                });
            }
        }

        var funkcjaB = function(f) {
            if($('showright').retrieve('stan') == 1)
            {
                sidebarfx.hide().slideIn();
            }
        }



        if($('showright').retrieve('stan') == 1)
        {
            $('showright').store('stan', 0);

            sidebarfx.addEvent('onComplete', funkcjaA);

            sidebarfx.slideOut();

            Cookie.write('rightcookie', 0, { duration: 30, path: '/' });

            $('showright').setStyle('background-image', 'url(\'gfx/arr1.gif\')');

        }
        else
        {
            $('showright').store('stan', 1);

            szerokoscstrony.addEvent('onComplete', funkcjaB);

            szerokoscstrony.start({
                'width': 950   //Morphs the width from the current to 300px.
            });

            Cookie.write('rightcookie', 1, { duration: 30, path: '/' });

            $('showright').setStyle('background-image', 'url(\'gfx/arr2.gif\')');

        }
    });


    /* ajax przesyłanei formularza */
    $('fo_wyslane').addEvent('click', function(event) {
        //prevent the page from changing
        event.stop();

        // sprawdzanie czy zaznaczono checkbox
        var z_nostat = '';
        if($('fo_nostat').checked)
        {
             z_nostat = 'yes';
        }

        //make the ajax call
        var req = new Request.JSON({
            method: 'post',
            url: 'index.php',
            data: {
                'ajax' : 'ajax',
                'fo_linkd' : $('fo_linkd').value,
                'fo_nostat' : z_nostat,
                'fo_wyslane' : 'ajax'
            },
            onRequest: function() {
                $('fo_wyslane').setStyle('background', 'url(\'gfx/buttonload.gif\')');
            },
            onComplete: function(response) {
                if(response.code)
                {
                    $('fo_linkd').value =  response.linkd;
                    $('przerobiony').value =  response.url;

                    $('delurl').value =  response.delurl;


                    var imgsrc =  base_dir+'qrcode.php?id='+response.hash;
                    $('qrcodeimg').setProperty('src', imgsrc);


                }
                else
                {
                    alert(response.errory[0]);
                }

                $('fo_wyslane').setStyle('background', 'url(\'gfx/button.gif\')');

                _gaq.push(['_trackPageview']);
            }
        });
        req.send();
    });

    


    // zaznaczanie tekstu po kliknięciu w pole ze skróconym adresem
    $('przerobiony').addEvent('click', function(e) {
        $('przerobiony').focus();
        $('przerobiony').select();
    });
    $('delurl').addEvent('click', function(e) {
        $('delurl').focus();
        $('delurl').select();
    });

    // link do kopiowania linka do mikrobloga
    $('kopiujdt').removeProperty('style');
    $('kopiujdt').addEvent('click', function(e) {
        var domb = $('msg_cont').getElement('li.active').getElement('textarea.stattext');

        domb.value = domb.value.trim()+' '+$('przerobiony').value;
        domb.value = domb.value.trim();
        domb.focus();
        domb.fireEvent('keyup');
    });



    // liczenie długości w mikroblogach
    max_chars = 140;
    var statusy = $$('#msg_cont li fieldset');

    statusy.each(function(el) {
        var eltxt = el.getElement('textarea.stattext');
        var ellicz = el.getElement('span.statuslicz');

        eltxt.store('len', eltxt.getProperty('rel') || max_chars);
        eltxt.removeProperty('rel');

        ellicz.innerHTML = eltxt.retrieve('len');


        eltxt.addEvent('keyup', function() {

            /* calculate remaining chars */
            remaining_chars = eltxt.retrieve('len')-eltxt.value.length;

            ellicz.innerHTML = remaining_chars;

            if(remaining_chars<10){
                ellicz.setStyle('color', '#900');
            } else {
                ellicz.setStyle('color', '#444');
            }
        });

    });

    

});


