var MAG = Object.extend({}, MAG || {}), banner_click_url;

MAG.Tabs = {
    cached: [],
    options: {},

    Init: function(options) {
        Object.extend(this.options, options || {});
        this.setupTabs();
    },

    setupTabs: function() {
        $$('#menu li a').each(function(elem, i) {
            elem.observe('click', function() {
                MAG.Tabs.removeClass(i);
                elem.addClassName('active');
                MAG.Tabs.getProducts(i);

            });
        });
    },

    removeClass: function(i) {
        $$('#menu li a').each(function(e, index) {
            if (index != i) {
                e.removeClassName('active');
            }
        });
    },

    getProducts: function(idx) {
        if (!this.cached[idx]) {
            new Ajax.Updater('tabs', this.options.url,
			{
			    parameters: { method: 'get_products', index: idx },
			    method: 'get',
			    onCreate: this.startLoading.bind(this),
			    onComplete: function(idx, req) {
			        this.cached[idx] = req.responseText;
			        idx == 5 ? this.productsObserve(idx) : null;
			    } .bind(this, idx)
			});
        }
        else {
            Element.update.defer('tabs', this.cached[idx]);
            idx == 4 ? this.productsObserve(idx) : null;
        }
    },

    startLoading: function() {
        $('tabs').innerHTML = '<div class="loader"><img src=' + this.options.img + '></div>';
    },

    productsObserve: function(idx) {
        if (navigator.userAgent.indexOf('MSIE 6') > -1) {
            $$('ul.mobile li').each(function(elem) {
                elem.observe('mouseenter', function() {
                    $(this).addClassName('over');
                }).observe('mouseleave', function() {
                    $(this).removeClassName('over');
                });
            });
        }

        this.pos = -1;
        this.drag();
    },

    drag: function() {
        var self = this;

        $('drag').observe('click', function() {
            var MagMove = Class.create(Effect.Move, {
                update: function(position) {
                    this.element.setStyle({
                        left: (this.options.x * position + this.originalLeft).round() + 'px'
                    });
                }
            });

            new MagMove($('hometabs'), {
                x: self.pos == -1 ? -262 : 0,
                mode: 'absolute',
                beforeStart: function() {
                    $('hometabs').setOpacity(.5);
                },
                afterFinish: function() {
                    $('hometabs').setOpacity(1);
                    $('drag')[(self.pos == -1 ? 'add' : 'remove') + 'ClassName']('opened');
                    self.pos = self.pos - (2 * self.pos);
                }
            });
        });
    }
};

function bannerClick(url, target, id) {
    new Ajax.Request(banner_click_url.replace('/0/', '/' + id + '/'), { asynchronous: false });
	var w = window.open(url, target);
    w.focus();
}

document.observe("dom:loaded", function(){
	var inputs = $$('#my_vivacom input[type="text"]','#my_vivacom input[type="password"]');
	inputs.each(function(el){
		el.observe('focus', function(){
			if(!$(this).fist_text) $(el).fist_text = this.value;
			if(this.value == $(this).fist_text) this.value = '';
		}).observe('blur', function(){
			if(this.value.empty()) this.value = $(this).fist_text;
		});
	});
});
