/**
 * Worker object for itworks.lt navigation.
 *
 * (c) itWorks.lt, 2009
 * Mail: info{eta}itworks.lt
 */
var worker = {
    current_pos: null,
    px_left: 500, px_top: 300,
    cache: {},

    init: function() {
        // Nav. paveiksleliu pasikeitimas
        $('#nav a').hover(
            // in
            function() {
                worker.navMouseOver(this);
            },
            // out
            function() {
                worker.navMouseOut(this);
            }
        );
        // Yra kur nueiti?
        var href = window.location.href;
        var pos = href.indexOf('#');
        if(-1 != pos) {
            var link = href.substring(pos+1);
            if($('#innav_'+link).length == 1)
                this.nav(link);
        }
    },
    getImgSrc: function(id) {
        return 'img/' + id.substring(6) + '_a.png';
    },
    
    nav: function(to, e) {
        // if(e)
        //     this.stopLink(e);
        if(this.current_pos == to)
            return;
        this.navMouseOut('#innav_'+to);
        this.current_pos = to;
        
        var nav = $('#nav'), txt = $('#text'), othr = $('#other'),
            nav_vars = {}, txt_vars = {}, othr_vars = {};
        
        switch(to) {
        case 'ka':
            nav_vars = {left: this.px_left, top: this.px_top};
            txt_vars = {left: 0};
            othr_vars = {left: this.px_left, top: 0};
            break;
        case 'kontaktai':
            nav_vars = {left: 0, top: this.px_top};
            txt_vars = {left: this.px_left};
            othr_vars = {left: 0, top: 0};
            break;
        case 'garantija':
            nav_vars = {left: this.px_left, top: 0};
            txt_vars = {left: 0};
            othr_vars = {left: this.px_left, top: this.px_top};
            break;
        case 'darbai':
            nav_vars = {left: 0, top: 0};
            txt_vars = {left: this.px_left};
            othr_vars = {left: 0, top: this.px_top};
            break;
        default:
            alert('Labu. Čia mūsų klaida, ar tamsta žaidžiate? :)');
            return;
        }
        
        // Visa "animacija"
        txt.fadeOut(150, function() {
            txt.css(txt_vars);
            // TODO: update content
            txt.html(worker.load(to));
        });
        othr.fadeOut(150, function(){othr.css(othr_vars).attr('class', 'o_'+to)});
        nav.animate(nav_vars, 600);
        
        txt.fadeIn(300);
        othr.fadeIn(300);
        
        // Seklys morka ajax`ui
        pageTracker._trackPageview('/' + to);
    }, // end nav

    stopLink: function(e) {
        if(e.preventDefault)
            e.preventDefault();
        else
            window.event.returnValue = false;
    },
    
    load: function(url) {
        if(!this.cache[url]) {
            this.cache[url] = $.ajax({async: false, url: url + '?rnd=' + (new Date()).getTime()}).responseText;
        }
        return this.cache[url];
    },
    
    navMouseOver: function(o) {
        if($(o).attr('id') != 'innav_'+this.current_pos) {
            var src = this.getImgSrc($(o).attr('id'));
            $(o).css('background-image', 'url("'+src+'")');
        }
    },
    navMouseOut: function(o) {
        if($(o).attr('id') != 'innav_'+this.current_pos)
            $(o).css('background-image', '');
    }
}

