var site = function(){
	var dispatcher;
	var overlay;
	var overlayContent;
	var ajaxLoaderInterval;

	function init(){
		dispatcher = new SimpleDispatcher();

		// checks if the window is scrolled to the bottom
		$(window).scroll(onWindowScroll);

		overlay = $('#overlay');
		overlayContent = $('#overlay-content');
		overlay.click(doHideOverlay);

		$('#ajaxMessageLoading').ajaxStart(ajaxBeforePost);
		$('#ajaxMessageLoading').ajaxStop(ajaxAfterPost);

		$('.tabbar').each(orderTabs);

		setupToggles();
		setupHelp();
		setupShadows();

		// ex - ginteras stuff, still necessary ?
		$('#reminder_show').click(function(){
			$('#reminder_holder').slideToggle('slow');
		});



		checkLayout();
	}

	function ajaxBeforePost(){
		$('#ajaxMessageLoading').show().fadeTo(0,0.85).html('Loading');
		ajaxLoaderInterval = setInterval(showLoaderAnimation, 500);
	}

	function ajaxAfterPost(){
		clearInterval(ajaxLoaderInterval);
		$('#ajaxMessageLoading').hide().html('');
	}

	function showLoaderAnimation(){
		var s = $('#ajaxMessageLoading').html().match(/\.+$/);

		if(!s || s.toString().length < 3){
			$('#ajaxMessageLoading').text($('#ajaxMessageLoading').text() + '.');
		} else {
			$('#ajaxMessageLoading').text($('#ajaxMessageLoading').text().replace(/\.+/, ''));
		}
	}

	function orderTabs(){
		var zIndex = 1000;
		$('li', this).each(function(){
			if(!$(this).hasClass('selected')){
				$(this).css('zIndex',zIndex);
				zIndex-=10;
			} else {
				$(this).css('zIndex',1010);
			}
		});
	}

	function resetLayout(){
		$('.resizable').each(function(i){
			$(this).height('100%');
		});
	}

	function setupToggles(parent){
		if(parent){
			$('.toggleVisibility', parent).each(setupToggleVisibility);
		} else {
			$('.toggleVisibility').each(setupToggleVisibility);
		}
	}

	function setupToggleVisibility(){
		var id = $(this).attr('id').replace('Link', '');
		$(this).toggle(toggleVisibilityOn, toggleVisibilityOff);
		$('#'+id).hide();
	}

	function toggleVisibilityOn(e){
		var id = $(this).attr('id').replace('Link', '');
		$('#'+id).fadeIn('normal', checkLayout);
	}

	function toggleVisibilityOff(e){
		var id = $(this).attr('id').replace('Link', '');
		$('#'+id).fadeOut('normal', checkLayout);
	}

	function setupHelp(parent){
		if(parent){
			$('.help-link', parent).each(setupHelpItem);
		} else {
			$('.help-link').each(setupHelpItem);
		}
	}

	function setupHelpItem(){
		var id = $(this).attr('id') + '-content';
		$('#'+id).hide();
		$(this).mouseover(showHelp);
		$(this).mouseout(site.hideHover);
	}

	function showHelp(e){
		e.preventDefault();

		var id = $(this).attr('id') + '-content';
		var content = $('#'+id).html();
		site.showHover(e.pageX - 260, e.pageY+200, content);
	}

	function setupShadows(){
		$('.shadow50').each(function(){addShadowImage($(this), 50);});
		$('.shadow40').each(function(){addShadowImage($(this), 40);});
		$('.shadow25').each(function(){addShadowImage($(this), 25);});
	}

	function addShadowImage(el, size){
		var pos = el.offset();
		el.removeClass('shadow'+size);
		var img = $('<img src="/img/facebook/innershadow'+size+'.png"></img>').css({position:'absolute',top:pos.top,left:pos.left});
		$(document.body).append(img);
	}

	function checkLayout(){
		var boxes = $('.resizable');
		var h,y,max = 0;
		for(var i = 0, l = boxes.length; i < l; i++){
			h = $('.box-content', boxes[i]).outerHeight();
			y = $(boxes[i]).offset().top;
			//alert($(boxes[i]).attr('id') + ' -> ' + h + ' / ' + y);
			//alert('Max before: ' + max);
			if(max < h + y){
				max = h + y;
			}
			//alert('Max after: ' + max);
		}

		for(var i = 0, l = boxes.length; i < l; i++){
			y = $(boxes[i]).offset().top;
			var box = $(boxes[i]);
			box.height(max - y);
		}
	}

	function showHover(x, y, html){
		$('#hoverContent').html(html);
		y = Math.max(y - $('#hover').height(), 0);
		x = Math.min($(document).width() - $('#hover').width(), x);
		$('#hover').css({top:y,left:x}).show();
	}

	function hideHover(){
		$('#hover').hide();
	}

	function showOverlay(html, w, h, bgColor){
		bgColor = bgColor != undefined ? bgColor : '#ffffff';

		overlay.css({
			height: $(document).height()
		});

		var top = $(document.documentElement).scrollTop() || 0;

		overlayContent.html(html);
		overlayContent.css({
			backgroundColor: bgColor,
			width : w + 'px',
			height : h + 'px',
			marginLeft : (-0.5 * w) + 'px',
			marginTop : (-0.5 * h) + 'px'

		})
		overlay.show();
	}

	function hideOverlay(){
		overlay.hide();
		overlayContent.empty();
	}

	function doHideOverlay(e){
		e.preventDefault;
		hideOverlay();
	}

    function onWindowScroll(){
        // is the scrollbar on the bottom?
        var h = $(document).height();
        var p = $(document).scrollTop();
        var ch = $(window).height();
		dispatcher.dispatchEvent(new WindowEvent(WindowEvent.SCROLL, p));

        if((h - p) == ch){
			dispatcher.dispatchEvent(new WindowEvent(WindowEvent.SCROLL_BOTTOM, p));
        }
    }

	function getDispatcher(){
		return dispatcher;
	}

	return {
		ajaxBeforePost : ajaxBeforePost,
		ajaxAfterPost : ajaxAfterPost,
		getDispatcher : getDispatcher,
		showHover: showHover,
		hideHover: hideHover,
		showOverlay: showOverlay,
		hideOverlay: hideOverlay,
		resetLayout: resetLayout,
		checkLayout: checkLayout,
		setupToggles : setupToggles,
		setupShadows : setupShadows,
		init: init
	}
}();

function SimpleDispatcher(){}

SimpleDispatcher.prototype.listeners = [];

SimpleDispatcher.prototype.dispatchEvent = function(e){
	if(this.listeners[e.type]){
		for(var i = 0, l = this.listeners[e.type].length; i < l; i++){
			this.listeners[e.type][i](e);
		}
	}
};

SimpleDispatcher.prototype.addEventListener = function(type, func){
	if(!this.listeners[type]){
		this.listeners[type] = [];
	}

	// check if function isnt already there
	for(var i = 0, l = this.listeners[type].length; i < l; i++){
		if(this.listeners[type][i] == func){
			return false;
		}
	}

	// no its not, so lets add it
	this.listeners[type].push(func);
};

SimpleDispatcher.prototype.removeEventListener = function(type,func){
	if(!this.listeners[type]){
		return false;
	} else {
		for(var i = 0, l = this.listeners[type].length; i < l; i++){
			if(this.listeners[type][i] == func){
				this.listeners[type].splice(i,1);
				return true;
			}
		}
		return false;
	}
};

function SimpleEvent(type, data){
	this.type = type;
	this.data = data;
}

SimpleEvent.EVENT = 'se_event';
SimpleEvent.CUSTOM_TYPE = 'se_custom_type';

SimpleEvent.prototype.type = '';
SimpleEvent.prototype.data = '';

function WindowEvent(type, data){
	this.type = type;
	this.data = data;
}

WindowEvent.SCROLL = 'we_scroll';
WindowEvent.SCROLL_BOTTOM = 'we_scroll_bottom';


//////////
//INIT //
//////////
	
$(document).ready(site.init);

