if (typeof DigiMat == 'undefined') DigiMat = {};

DigiMat.pageManager = function() {
	
	var currentPanel = 'btnDesign';
	var currentNavItem = 'navHome';
	
	function moveMarker(element, newDimensions) {
		element.animate({ left: newDimensions.pos, width: newDimensions.width }, 
						500,
						'linear');
	}
	
	return {
		modalOK: function() {
			addtlCode = addtlCode || function() {};
		    Smith.Base.CloseModal();			
	    },
	    openModal: function() {
		
			$('#modalForeground').css('height', 0);			
			
			this.positionModal();
			$('#modalBackground').show();
	        $('#modalForeground').animate({ width: 220, 
											height: 200,
											marginTop: 0,
											marginLeft: 0, 
											opacity: 1 
										   }, 250, 'linear',
										   function() {
										   	 $('#ModalBody').fadeIn('fast');
										   });			
			
	    },
	    closeModal: function() {
	        $('#modalForeground')
			  .animate({ width: 0, 
			  		     height: 0, 
						 opacity: 0,
						 marginTop: 100,
						 marginLeft: 100 
					    }, 250, 'linear',
			  		   function() {
						  	 $('#ModalBody').fadeOut('fast');
					    });
	       
			$('#modalBackground').hide();
	    },
	    positionModal: function() {
	    
	        var w = 0;
	        var h = 10;
	
	        if (window.innerWidth) {
	            w += window.innerWidth;
	            h += window.innerHeight
	        }
	        else {
	            w += document.body.offsetWidth;
	            h += document.body.offsetHeight
	        }
	        if (document.documentElement) {
	            w += document.documentElement.scrollLeft;
	            h += document.documentElement.scrollTop;
	        }
	        else {
	            w += document.body.scrollLeft;
	            h += document.body.scrollTop;
	        }
	
	        if (self.pageYOffset) {
	            dialogH = self.pageYOffset;
	        }
	        else
	            if (document.documentElement && document.documentElement.scrollTop) {
	            dialogH = document.documentElement.scrollTop;
	        }
	        else
	            if (document.body) {
	            dialogH = document.body.scrollTop;
	        }
	        dialogH = dialogH + 200;
	
	        var dialogW;
	        dialogW = w / 2;
	        dialogW = dialogW - 150;
	
	        $('#modalForeground').css('left', dialogW + 'px').css('top', dialogH + 'px');
	        $('#modalBackground').css('width', w + 'px').css('height', h + 'px');
	    },
		resetNav: function() {
			moveMarker($('#navMarker'), { pos: 17, width: 61});	
		},
		showRenovationMessage: function() {
			$('#ModalLabel').html('<h3>Coming Soon</h3><p>We\'re sorry. We are currently renovating our site at the moment. ' + 
			                      'For more information on our services, please contact us at ' + 
								  '<a href="mailto:info@digitalmateria.com">info@digitalmateria.com</a></p>');
			
			$('#ModalOK').click(function() {
				DigiMat.pageManager.resetNav();
				DigiMat.pageManager.closeModal();
				
				$(this).click(function() { DigiMat.pageManager.closeModal(); });
				$('#menuItems').children('a').css('color', '#8793AA');	
				$('#navHome').css('color', 'White');
			});
			this.openModal(function() { 
				DigiMat.pageManager.resetNav();		
			});				
			
		},
		toggleMenu: function(sender) {
			var newDimensions = { pos: 0, width: 0 };
			
			var id = $(sender).attr('id');
			
			if (id == currentNavItem) 
				return;
					
			$('#menuItems').children('a').css('color', '#8793AA');			
			switch(id) {
				case 'navHome':
					newDimensions.pos = 17;
					newDimensions.width = 61;									
					break;
				case 'navCompany':
					newDimensions.pos = 113;
					newDimensions.width = 94;				
					break;
				case 'navServices':
					newDimensions.pos = 242;
					newDimensions.width = 86;				
					break;
				case 'navPortfolio':
					newDimensions.pos = 363;
					newDimensions.width = 86;								
					break;
				case 'navContact':
					newDimensions.pos = 483;
					newDimensions.width = 79;				
					break;									
			}
			
			currentNavItem = id;
			$(sender).css('color', 'white');
			moveMarker($('#navMarker'), newDimensions);
			
			this.showRenovationMessage();				  
			
		},
		switchPanel: function(sender) {
			var id = $(sender).attr('id');
			var newDimensions = { width: 0, height: 0 };
			
			if (id == currentPanel)
				return;
	
			$('.panelContent').hide();
			
			$('.selectedButton, .Button').children('span').css('color', '#B9C5DE');
			switch(id) {
				case 'btnDesign':
					$('#pnlDesign').fadeIn('slow');
					
					$('#btnDesign').attr('class', 'selectedButton');
					$('#btnDevelopment').attr('class', 'Button');
					$('#btnConsulting').attr('class', 'Button');
					
					newDimensions.width = 120;
					newDimensions.pos = 75;
										
					break;
				case 'btnDevelopment':
					$('#pnlDevelopment').fadeIn('slow');
					
					$('#btnDesign').attr('class', 'Button');
					$('#btnDevelopment').attr('class', 'selectedButton');
					$('#btnConsulting').attr('class', 'Button');
					
					newDimensions.width = 243;
					newDimensions.pos = 341;
					break;
				case 'btnConsulting':
					$('#pnlConsulting').fadeIn('slow');
					
					$('#btnDesign').attr('class', 'Button');
					$('#btnDevelopment').attr('class', 'Button');
					$('#btnConsulting').attr('class', 'selectedButton');
					
					newDimensions.width = 136;
					newDimensions.pos = 711;
					break;	
			}
			
			currentPanel = id;
			
			$(sender).children('span').css('color', 'white');
			moveMarker($('#subSplashMarker'), newDimensions);
		},
		init: function() {
			$('.panelContent').hide();
			$('.Button').mouseover(function(){
				DigiMat.pageManager.switchPanel(this);
			});
			
			$('#btnDesign').attr('class', 'selectedButton').children('span').css('color', 'white');
			
			$('#pnlDesign').fadeIn('slow');
			
			// Handle the nav items
			$('#menuItems').children('a').click(function(){
				 DigiMat.pageManager.toggleMenu(this);				
			});	
			
			/* Modal Dialog Stuff */
			$('#modalForeground').css('margin-top', 100).css('margin-left', 100);
			
			
			 $(window).resize(function() {
            	DigiMat.pageManager.positionModal();
        	 });

	        $(window).scroll(function() {
    	       DigiMat.pageManager.positionModal();
        	});

        	$('#ModalOK').click(function() { DigiMat.pageManager.closeModal() });
			
			// Show renovation message if user clicks panel READ MORE
			$('.panelContent').children('a').click(function() {
				DigiMat.pageManager.showRenovationMessage();				
			});

		}		
	}
	
}();


$(document).ready(function() {
	DigiMat.pageManager.init();
});

