function MainUI(){
	this.oldMap = [];
	this.busy = true;
	this.direction;
	MainUI.instance = this;
	var ref = this;
	this.buildNav();
	 jQuery('#wait').sprite({fps: 6, no_of_frames: 12}).spStop();
	$(document).bind('URL_CHANGE' , $.proxy(this.urlChange , this));
	jQuery(window).resize(function(){
		ref.resize();
	});
}

MainUI.getInstance  = function(){
	if(MainUI.instance != undefined)
		return MainUI.instance;
}


MainUI.alignContent  = function(param_id , param_force_height){
	var percent = Math.round( $(window).width() * .8) ;
	$(param_id).width(percent);
	var toX =  Math.round( $(window).width() * .12);
	$(param_id).css('left' , toX < 180 ? 180 : toX );
	if(param_force_height != undefined){
		var toY = Math.round(( $(window).height() - param_force_height) / 2);
		$(param_id).css('top' , toY);
	}
	else
		$(param_id).centerY();
}


MainUI.getCufonWidth = function(param_id){
	var max = 0;
	$(param_id).each(function(){
		if($.browser.opera){
			canvas = $(this).find('canvas');
			canvas.remove();
		}
		if($(this).width() > max)
			max = $(this).width();
		if($.browser.opera)
			$(this).append(canvas);
	});
	
	return max;
}




MainUI.prototype = {
				
	addSpan:function(param_id , param_html , param_separator){
		var isHTML = param_html == undefined ? false : param_html;
		var sep = param_separator == undefined ? ' ' : param_separator;
		
		if(isHTML == true)
			var tabLines = String(jQuery(param_id).html().toLowerCase()).split(sep);
		else
			var tabLines = String(jQuery(param_id).text()).split(sep);
			
		var newText = '';
		for(var i in tabLines){
			if(typeof (tabLines[i]) == 'string' )
				newText +='<span>' + tabLines[i] + '</span>';
		}
		
		return newText;
	},
	
	buildNav:function(){
		var loc = Main.getInstance().localization;
		jQuery('#langselector li a').removeAttr('title');
		jQuery('#nav li a').click(this.pressNavigationLink);
		jQuery('#nav li a').removeAttr('title');
	},
			
	
	calulateContentSize:function(param_ratio , param_max_width , param_max_height){
		var toHeight;
		var toWidth;
		var maxWidth = param_max_width != undefined ? param_max_width : jQuery(window).width() - 300;
		var maxHeight = param_max_height != undefined ? param_max_height : jQuery(window).height() - 200;
		if(maxWidth * param_ratio <= maxHeight){
			toWidth = maxWidth;
			toHeight = Math.floor(maxWidth * param_ratio);
		}
		else{
			toHeight = maxHeight;
			toWidth = Math.floor(toHeight / param_ratio);
		}
		
		return {width:toWidth ,  height:toHeight};
	},
		
	
	resetSectionContent:function(param_definition){
		this.direction = this.findDirection(this.oldMap , Navigator.getInstance().getCurrentMap());
		this.oldMap =  Navigator.getInstance().getCurrentMap();
			
		jQuery('#pagination li a').removeAttr('title').mouseover(this.rollPage).mouseout(this.rollOutPage).click(this.pressPaginationLink);
		$('#pagination li a.selected').unbind('mouseover').unbind('mouseout').unbind('click').css('cursor','default');
		
		jQuery('span.emailme').each(function(index) {
			var isClear = jQuery(this).hasClass('clearlink');
			var isDark = jQuery(this).hasClass('darklink');
			
			var at = / at /;
			var dot = / dot /g;
			var addr = jQuery(this).text().replace(at,"@").replace(dot,".");
			jQuery(this).after('<a href="mailto:'+addr+'" class="email '+ (isClear ? 'whitelink' : isDark ? 'darklink' : '')+'">'+ addr +'</a>')
			.hover(function(){window.status="Send a letter!";}, function(){window.status="";});
			jQuery(this).remove();
		});
	},
	
		
	changeTheme:function(param_type){
		$('body').removeClass('clear dark').addClass(param_type);		
	},
		
		
	resize:function(){
		SectionManager.getInstance().currentSection.resize();
	},
	
	
	pressNavigationLink:function(e){
		e.preventDefault();
		if(MainUI.getInstance().busy)
			return;
		
		Main.getInstance().navigator.gotoURL($(this).attr('href'));
	},
	
	pressPaginationLink:function(e){
		e.preventDefault();
		if(MainUI.getInstance().busy)
			return;
		
		var url = '/' + Main.getInstance().localization.getLocale() + '/' + $(this).attr('href')
		Main.getInstance().navigator.gotoURL(url);
	},
	
	
	urlChange:function(e , url , map){
		_gaq.push(['_trackPageview', SWFAddress.getValue()]);
		jQuery('#nav li a').removeClass('selected');
		jQuery('#nav li a:eq('+ map[0] +')').addClass('selected');
		this.oldMap = map;
	},
	
	
	findDirection:function(param_old_map , param_new_map){
		if(param_old_map == undefined)
			param_old_map = this.oldMap;
		if(param_new_map == undefined)
			param_new_map = Navigator.getInstance().getCurrentMap();
			
		
		var direction = undefined;
		if(param_old_map.length > 0){
			if( param_new_map[0] != param_old_map[0] ){
				direction = param_old_map[0] < param_new_map[0] ? 'bottom' : 'top';
			}
			else{
				if(param_new_map[1] != undefined && param_old_map[1] != undefined )
					direction = param_old_map[1] < param_new_map[1] ? 'right' : 'left';
				else if(param_new_map[1] == undefined &&param_old_map[1] != undefined )
					direction = "left";
				else if(param_new_map[1] != undefined && param_old_map[1] == undefined )
					direction = "right";	
			}
		}
		return direction;
	},
	
	
	rollPage:function(e){
		jQuery('.tooltip').remove();
		var coords = jQuery(this).offset();
		jQuery('body').append('<div class="tooltip"><span>'+ jQuery(this).text() +'</span></div>');
		var toolTipWidth = jQuery('.tooltip').width();
		var toX = coords.left + 19 - toolTipWidth;
		
		jQuery('.tooltip').stop();
		jQuery('.tooltip').css('opacity' , '0');
		jQuery('.tooltip').css('top' , coords.top - 30);
		jQuery('.tooltip').css('left' , toX - 20);
		jQuery('.tooltip').animate({opacity:1 , left: toX} , 300);
	},
	
	
	rollOutPage:function(e){
		jQuery('.tooltip').stop();
		var coords =  jQuery('.tooltip').offset();
		if(coords != null)
			jQuery('.tooltip').animate({opacity:0 , left:coords.left - 20} , 200 , null );
	},
	
	
	toggleWait:function(param_to){
		if(param_to)
			jQuery('#wait').spStart().show();
		else
			jQuery('#wait').hide().spStop();
	},
	
	
	centerX:function(param_target){
		var toX = Math.round((jQuery(window).width() - 110 - $(param_target).width()) / 2) + 110;
		 $(param_target).css('left' ,toX );
	},
	
	
	centerY:function(param_target){
		var toX = Math.round((jQuery(window).height() - 60 - $(param_target).height()) / 2) + 60;
		 $(param_target).css('top' ,toX );
	},
	
};
