var Mobajl = {};
var scrollbox;
var newsscroller;

Mobajl.Debug = function(msg) {
	try {
		if (debug) {
			if (window.console !== undefined) {
				console.debug(msg);
			} else {
				//alert(msg);
			}
		} 
	} catch(e) { }
};

Mobajl.NewsScroll = function() {
	this.scroller = new Fx.Scroll(window, {duration: 500, link: 'cancel', offset: {'x': -200, 'y': -50} });
	this.entries = $$('.latest > .postentry');
	
	this.init = function() {
		for(var i=0; i < this.entries.length; i++) {
			this.entries[i].addEvent('click', function(e) {
				newsscroller.scrollTo($(this).getElement('span.tag').get('text'));
			});
		}
		
		if (announcement_goto.match(/^entry-\d+$/)) {
			this.scrollTo(announcement_goto);
		}
	};
	
	this.scrollTo = function(i) {
		this.scroller.toElement(i);
	};
	
	this.init();
},
Mobajl.Feeds = function() {
	var jsonRequest = new Request.JSON({url: "/ajax/feeds", onSuccess: function(feeds) {
		['blog', 'other'].each(function(e, i) {
			for(var i=0; i < feeds[e].length; i++) {
				var date = new Date(feeds[e][i].date * 1000);
				var h2 = document.createElement('h2');
				h2.set('text', date.format('yyyy-mm-dd'));
				$(e).appendChild(h2);
				var p = document.createElement('p');
				p.set('text', feeds[e][i].details);
				$(e).appendChild(p);
			}
		});
	}}).get();

};

Mobajl.Decorations = function() {
	$$("a[href^=mailto:]").each(function(e) {
		var wrapper = new Element('div', {'class': 'emailwrapper'});
		wrapper.wraps(e);
	});
};

Mobajl.Boxlinks = function() {
	this.bindLinks = function() {
		$$("[id^=box_]").each(function(e) {
			var number = e.id.slice(4);
			e.addEvent('click', function() { window.location='/services/' + number; });
		});		
	};
	
	this.bindLinks();
};

Mobajl.Scrollbox = function() {
	this.box = $('scrollbox');
	this.position = 0;
	
	this.init = function() {
		if (! this.box)	return;
		
		this.boxcontent = this.box.getElement('span');
		this.fx = new Fx.Tween(this.boxcontent);
		
		this.columnwidth = this.box.getElement('.column').getSize().x;
		this.columnstotal = this.box.getElements('.column').length;
		this.columnsvisible = this.box.getSize().x / this.columnwidth;
		
		Mobajl.Debug('Column width: ' + this.columnwidth);
		Mobajl.Debug('Columns total: ' + this.columnstotal);
		Mobajl.Debug('Columns visible: ' + this.columnsvisible);

        var offset = Cookie.read('ScrollBoxPos');
		if (parseInt(offset) > 0) { // Start with previously loaded page
			Mobajl.Debug('offset: ' + offset);
			this.position = offset;
			this.boxcontent.setStyle('left', -(this.position * this.columnwidth) + 'px');			
		}
		$('scrollLeft').addEvents({
			'click': function(e) { scrollbox.previous(); },
			'mouseover': function(e) { if (scrollbox.hasPrevious()) $(this).addClass('hover'); },
			'mouseout': function(e) { $(this).removeClass('hover'); }
		});
		
		$('scrollRight').addEvents({
			'click': function(e) { scrollbox.next(); },
			'mouseover': function(e) { if (scrollbox.hasNext()) $(this).addClass('hover'); },
			'mouseout': function(e) { $(this).removeClass('hover'); }
		});
		
		this.toggleButtons();
		
		this.box.style.visibility = 'visible';
	};
	
	this.toggleButtons = function() {
		var right = $('scrollRight');
		var left  = $('scrollLeft');
		if (! this.hasNext()) {
			right.hide();
		} else {
			right.show();
		}
		
		if (! this.hasPrevious()) {
			left.hide();
		} else {
			left.show();
		}
	};
	
	this.hasNext = function() {
		return ((this.columnstotal - this.columnsvisible - this.position) > 0) ? true : false;
	};
	
	this.hasPrevious = function() {
		return (this.position > 0) ? true : false;
	};
	
	this.next = function() {
		if (this.hasNext()) {
			Mobajl.Debug('Scrolling to next column');
			this.position++;
            Cookie.write('ScrollBoxPos', this.position, {'path': '/'});
			var currentPos = parseInt(this.boxcontent.getStyle('left').slice(0,-2));
			this.fx.start('left', currentPos - this.columnwidth + 'px');
		} else {
			Mobajl.Debug('Limit reached');
		}
		this.toggleButtons();
	};
	
	this.previous = function() {
		if (this.hasPrevious()) {
			Mobajl.Debug('Scrolling to previous column');
			this.position--;
            Cookie.write('ScrollBoxPos', this.position, {'path': '/'});
			var currentPos = parseInt(this.boxcontent.getStyle('left').slice(0,-2));
			this.fx.start('left', currentPos + this.columnwidth + 'px');
		} else {
			Mobajl.Debug('Limit reached');
		}
		this.toggleButtons();
	};
	
	this.init();
};

window.addEvent('domready', function() { 
	new Mobajl.Boxlinks();
	new Mobajl.Decorations();
	scrollbox = new Mobajl.Scrollbox();

    $('logo').addEvent('click', function() { window.location = '/' });
    
    switch(action) {
        case 'announcement':
            newsscroller = new Mobajl.NewsScroll();
            break;

        case 'blog':
            $$('.latest > .postentry').addEvent('click', function(e) {
               window.location = '/blog/' + $(this).getElement('span.tag').get('text');
            });
            break;
        case 'partners':
            $('submit').addEvent('click', function() { $('partnerform').submit(); e.stop(); });
            break;
    }
});