var synth = new function(){
	
	this.displayMenu = function(object){
		if($('#fullText').length != 0){
			$('#fullText').remove();
		}
		/** Is there the available information? **/
		var elID = $(object).attr('id');
		
		var container = '#menu' + elID;
		var menu = 'menu'; 
		if($(object).attr('class').indexOf('expert') != -1){
			var expert = $(object).attr('rel').substr(1);
			container = '#' + expert + ' .' + elID;
			menu = 'expert/' + expert;
		} else {
			var expert = false;
			$('#headImage').hide(); 
			$('#social').empty();
		}
		
		if($(container).length != 0){
			
			/** Swap classes **/
			$('.navItem').removeClass('active');
			$(object).addClass('active');
			
			/** Build Pagination **/
			var pageString = new String;
			
			$('#pageNav').empty().hide();
			
			$(container + ' .page').each(function(i){
				
				pageID = ++i;
				var pageCount = i.length > 1 ? '.' + pageID : '.0' + pageID;
				var selected = pageID == 1 ? 'active' : '';
				pageString = '<li><a href="#/' + menu + '/' + elID + '/' + pageID + '" class="page ' + selected + '" id="' + pageID + '">'+ pageCount +'</a></li>';				 
				$('#pageNav').append(pageString);
				
				/** Bind onclick event **/
				$('#pageNav #' + pageID).click(function(){					
					synth.showPage(object, i);						
				});
			});	
			
			
			if($('#pageNav').children().length != 1){
				/** has pages, add the more button **/
				$('#pageNav').prepend('<li><a class="nextPage">more &raquo;</a></li>').show();
				
				$('.nextPage').click(function(){
					synth.showFullText(object, expert);
				});
			}	else	{
				$('#pageNav').empty();
			}
			
			/** There is, populate **/			
			if(expert !== false){
				link = '/expert/' + expert + '/' + elID;
			}	else	{
				link = '/menu/' + elID;
			}
			pageTracker._trackPageview(link); 
			$('#blockTitle').empty().append($(container + ' .' + elID + 'Title').attr('value'));
			$('#blockSubTitle').empty().append($(container + ' .' + elID + 'SubTitle').attr('value'));
			$('#blockText').fadeOut("medium").empty().append($(container + ' .' + elID + '1').attr('value')).fadeIn("medium");			
		}
	};
	
	this.showFullText = function(object, expert){
		$('.nextPage').empty().html('less &laquo;');
		$('.page').hide();
		
		var fullText = '<br />';
		
		var elID = $(object).attr('id');
		if(expert != false){
			$('#'+ expert + ' .' + elID + ' .page').each(function(i){				
					fullText +=  '<p>' + $(this).attr('value') + '</p><br />';				
			});
		} else {
			$('#menu' + elID + ' .page').each(function(i){
					fullText += '<p>' + $(this).attr('value') + '</p><br />';
			});
		}	
			
			if($('#fullText').length == 0){
				$('#blockText').hide();
				$('<p id="fullText"></p>').insertAfter($('#blockText'));
				$('#fullText').append(fullText);
				$('#fullText').show('slide', {direction: "up"}, 600);
			}	else	{
				$('#blockText').hide();
				$('#fullText').empty().append($(fullText));
				$('#fullText').show('slide', {direction: "up"}, 600);
			}
			$('.nextPage').unbind('click').click(function(){
				synth.showLessText(object, expert);
			});
		};
	
	this.showLessText = function(o, e){
		$('.nextPage').empty().html('more &raquo;').unbind('click').click(function(){
			synth.showFullText(o, e);
		});;
				
		$('#fullText').hide('slide', {direction: "down"}, 600);
		$('#fullText').remove();
		$('.ui-effects-wrapper').remove();
		
		$('.page').show();
		$('#blockText').show();
	};
	
	this.showPage = function(object, iterator){
		if($('#fullText').length != 0){
			$('#fullText').remove();
		}
		var elID = $(object).attr('id');
		var container = '#menu' + elID;
		
		if($(object).attr('class').indexOf('expert') != -1){
			var expert = $(object).attr('rel').substr(1);
			
			
			container = '#' + expert + ' .'+ elID; 
		}	else	{
			expert = false;
		}
		
		$('#pageNav .page').removeClass('active');
		$('#pageNav #' + iterator).addClass('active');
		 
		if($('#pageNav').children().length == 0){
			$(container + ' .page').each(function(i){
				

				pageID = ++i;
				var pageCount = i.length > 1 ? '.' + pageID : '.0' + pageID;
				var selected = pageID == 1 ? 'active' : '';
				pageString = '<li><a href="#/' + expert + '/' + elID + '/' + pageID + '" class="page ' + selected + '" id="' + pageID + '">'+ pageCount +'</a></li>';
				
				$('#pageNav').append(pageString);
				
				/** Bind onclick event **/
				$('#pageNav #' + pageID).click(function(){					
					synth.showPage(object, i);						
				});
			});	
			
			
			if($('#pageNav').children().length != 1){
				/** has pages, add the more button **/
				$('#pageNav').prepend('<li><a class="nextPage">more &raquo;</a></li>').show();
				
				$('.nextPage').click(function(){
					synth.showFullText(object, expert);
				});
			}	else	{
				$('#pageNav').empty();
			}
		}		

		/** Update text **/
		synth.buildSocial(expert);
		if(expert !== false){
			link = '/expert/' + expert + '/' + elID + '/' + iterator;
		}	else	{
			link = '/menu/' + elID + '/' + iterator;
		}
		pageTracker._trackPageview(link);
		
		$('#blockText').hide("slide", { direction: "left" }, 200);
		$('#blockText').empty().append($(container + ' .' + elID + iterator).attr('value')).hide();	
		$('#blockText').show("slide", { direction: "right" }, 300);
	};
	
	this.showNextPage = function(object){
		if($('#fullText').length != 0){
			$('#fullText').remove();
		}
		/** Get current page **/
		var cPage = $('#pageNav .active');
		var total = $('#pageNav .page').length;
		if(parseInt($(cPage).attr('id')) >= total){
			var nextPage = 1;
		} else	{
			var nextPage = parseInt($(cPage).attr('id')) + 1;
		}
		$('#pageNav .page').removeClass('active');
		$('#pageNav #' + nextPage).addClass('active');
		
		var elID = $(object).attr('id');
		var container = '#menu' + elID;
		
		if($(object).attr('class').indexOf('expert') != -1){
			var expert = $(object).attr('rel').substr(1);
			container = '#' + expert + ' .'+ elID; 
		}
		
		$('#blockText').empty().append($(container + ' .' + elID + nextPage).attr('value'));	
	};
	
	this.showExpert = function(object, element, page){	
		if($('#fullText').length != 0){
			$('#fullText').remove();
		}
		if($('#fullText').length > 0){
			$('#fullText').hide('slide', {direction: "up"}, 400).remove();
			$('.ui-effects-wrapper').remove();
		}
		
		$('.experts').show();		
		var expert = $(object).attr('id').substr(1);
		
		if($("#" + expert + ' .eMenu').length > 0){
			
			$('#navigation').empty();			
			
			$("#" + expert + ' .eMenu').each(function(i){
			var selected = (i== 0) ? 'active' : '';
			var id = $(this).attr('class');
			id = id.replace('active', '');
			id = id.replace('eMenu', '');
			id = $.trim(id);
		     	
			var item = '<li>';
	        	item += 	'<a href="#/expert/'+ expert + '/' + id +'" id="' + id + '" class="navItem expert '+ selected +'" rel="#'+expert+'"></a>';
	        	item += '</li>';
	        	 
	        	$('#navigation').append($(item));
	        	
	        	$('#navigation #' + id).unbind('click').click(function(){        		
	        		synth.displayMenu(this);
	        		});	        	
			});
			if($('#headImage').attr('src').indexOf(expert) == -1){			
			$(object).hide('slide', {direction: "left"}, 400, function(){
				if($.browser.msie){
					$('#headImage').attr('src', 'imgs/'+ expert + '_big.gif').show();
				} else {
					$('#headImage').attr('src', 'imgs/'+ expert + '_big.png').show();
				}
				synth.buildSocial(expert);
				$('#headImageContainer').show("slide", { direction: "left" }, 400, function(){
					/** Build menus **/
					
					if(element == false){
						var local = $("#navigation .navItem:first");
					}	else	{
						var local = $('#navigation #' + element);
						
					}					
					
					if(page != false){					
						synth.showPage(local, page);
						
					} else {
						
						synth.displayMenu(local);
					}
				});
				
			});
			
			}
		
		}
	};	
	
	this.buildSocial = function(expert){
		var menuSocial = $('#' + expert + ' .social');
		if(menuSocial.length != 0){
			$('#social').empty();
			$('#' + expert + ' .social .item').each(function(i){
				var elSocial = $(this).attr('class').replace('item ', '');
				var elSrc = $(this).text();
				
				
				
				var link = '<a id="' + elSocial + '" class="social" target="_blank" href="' + elSrc + '"></a>';
				$("#social").append($(link));
			});
			$('#social').show();
		}	else	{
			$('#social').empty();
		}
	};
	
	this.displayPage = function(o, page){
		var elID = $(o).attr('id');
		/** remove image **/
		/*$('#headImage').css('visibility', 'hidden');*/
		$('.navItem').removeClass('active');
		
		var container = 'page' + elID;
		
		$('#pageNav').empty();
		
		if($('#' + container + ' .page').length > 0){
			$('#' + container + ' .page').each(function(i){
				pageID = ++i;
				var pageCount = i.length > 1 ? '.' + pageID : '.0' + pageID;
				
				if(page == false){
					var selected = (i== 1) ? 'active' : '';
				} else {
					var selected = (page == i) ? 'active' : '';
				}
				var id = $(this).attr('id');
				id = id.replace('page', '');
				id = id.substr(0, parseInt(id.length)-1);
				
				var item = 'page' + id;
								
				pageString = '<li><a href="#/page/' + id + '/' + pageID + '" class="page ' + selected + '" id="' + pageID + '">'+ pageCount +'</a></li>';
				
				$('#pageNav').append(pageString);
				
				/** Bind onclick event **/
				$('#pageNav #' + pageID).click(function(){					
					synth.displayPage(o, i);						
				});	
			});
			
			if($('#pageNav').children().length != 1){
				/** has pages, add the more button **/
				$('#pageNav').prepend('<li><a class="nextPage">more &raquo;</a></li>');
				$('.nextPage').unbind('click').click(function(){
					synth.displayNextPage(o);
				});
			}
		}
		
		if($('#'+ container).length > 0){
		/** Insert texts, populate **/
			$('#blockTitle').empty().append($('#' + container + 'Title').attr('value'));
			$('#blockSubTitle').empty().append($('#' + container + 'SubTitle').attr('value'));
			if(page == false){
				$('#blockText').empty().append($('#' + container + '1').attr('value'));
			} else {
				$('#blockText').empty().append($('#' + container + page).attr('value'));
			}
		}
	};
	
	this.displayNextPage = function(object){	
		
		/** Get current page **/
		var cPage = $('#pageNav .active');
		var total = $('#pageNav .page').length;
		if(parseInt($(cPage).attr('id')) >= total){
			var nextPage = 1;
		} else	{
			var nextPage = parseInt($(cPage).attr('id')) + 1;
		}
		
		synth.displayPage(object, nextPage);
	};
	
	this.buildFromLink = function(){
		
		
		
		var pubLocation = location.href; 
		
		if(pubLocation.indexOf('#') != -1){
			var currentPage = pubLocation.substring((pubLocation.lastIndexOf("#"))+2);
		
		
			var paramArray = new Array();
			paramArray = currentPage.split('/');
			var paramCount = paramArray.length;		
		}	else	{
			paramCount = 0;
		}
		switch(paramCount){
			case 2:				
				if(paramArray[0] == 'menu'){	
					o = $('#' + paramArray[1]);
					synth.displayMenu(o);
				}	else	{
					if(paramArray[0] == 'page'){
						o = $('#' + paramArray[1]);
						synth.displayPage(o);
					} else {
						o = $('#e' + paramArray[1]);					
						synth.showExpert(o, false, false);
					}
				}
				break;
			case 3:				
				if(paramArray[0] == 'menu'){	
					o = $('#' + paramArray[1]);
					synth.displayMenu(o);
					synth.showPage(o, paramArray[2]);
				}	else	{
					if(paramArray[0] == 'page'){	
						o = $('#' + paramArray[1]);						
						synth.displayPage(o, paramArray[2]);
					}	else	{
						o = $('#e' + paramArray[1]);
						m = $('.navItem #' + paramArray[2]);					
						synth.showExpert(o, paramArray[2], false);
					}
				}
				break;
			case 4:
				o = $('#e' + paramArray[1]);
				m = $('.navItem #' + paramArray[2]);					
				synth.showExpert(o, paramArray[2], paramArray[3]);
				break;
			default:
				o = $('#navigation .navItem:first');
				synth.displayMenu(o);
				break;
		}
		
	};
};