/********************************
 * STARTUP FUNCTIONS CALL
 *******************************/ 
$(window).load(function() {
	Debuger.checkConsole(true);
	MainNavigation.init();
	CrossBrowser.init();
	ImageScroll.init();
});

/********************************
 * MAIN NAVIGATION CLASS
 *******************************/ 
var MainNavigation = {
	easingType : 'easeInCirc',
	easingType2 : 'linear',
	init : function() {
		this.adaptDropDown();
		this.setUI();
		//this.addShadow();
	},
	adaptDropDown : function() {
		console.log('::adaptDropDown');
		var speed = 500;
		// SUBELEMENTS OVERLAYS
		$('#mainNavigation li.level0').each(function(i)	{
			// :DOES THE OVERLAY FADE IN / FADE OUT
			$(this).hover(function()			
			{
				$(this).find('ul.level1, .dropShadow').css({ display:'block' });
				$(this).find('ul.level1').stop(true, false).animate({ opacity: .95 }, speed, this.easingType2);
				$(this).find('.dropShadow').stop(true, false).animate({ opacity: .3 }, speed, this.easingType2);
				
			},function()
			{
				$(this).find('ul.level1, .dropShadow').stop(true, false).animate({ opacity: 0 }, 200, this.easingType2, function()
				{ 
					$(this).css({ display:'none' }); 
				});
			});
			
			// :DELETES EMTPY COLUMNS 
			$(this).find('div.column').each(function()
			{
				// ::REMOVES WHITESPACES FOR CONTENT DETECTION
				var columnContent = $(this).find('ul.level2').html().replace(/^\s+|\s+$/g, ''); 
				
				// ::CHECKS IF THE CELL IS EMPTY AND DELETES IT, IF SO 
				//if(columnContent = '') $(this).parent().remove(); 
				if(columnContent == '') $(this).parent().remove();
			});
			
			// :ADAPTS THE DROPDOWN WIDTH IN FUNCTION OF THE NUMBER OF COLUMNS 
			var columnsAmount = $(this).find('div.column').length;
			console.log(columnsAmount);
			//
			newWidth = columnsAmount > 1 ? 245 + ((columnsAmount - 1) * 245) : $(this).find('ul.level1').width();
			$(this).find('ul.level1').css({width:newWidth});
			
			// :REMOVES UNNECCESSARY DELIMITERS AT THE BOTTOM OF EACH COLUMN
			$(this).find('div.column').each(function(y)
			{
				$(this).find('li.level2:last').css({background: '#f0f0f0'});
			});
			
			// :CALCULATES THE SPACE TO THE LEFT CORNER
			var coordinates = $(this).position();
			//var parentCoordinates = $(this).position();
			var left;
			//
			if(!$.browser.msie){
				// ::TRIES TO PLACE THE OVERLAY IN THE VISIBLE AREA
				left = left + newWidth < 970 ? coordinates.left : 970 - newWidth;
				// ::TRIES TO PLACE THE DIV DIRECTLY UNTER THE SELECTED MENU POINT
				if(coordinates.left + newWidth < 970) left = coordinates.left;
			}
			else{
				left = coordinates.left + newWidth > 970 ? (coordinates.left * -1) -197 : 0;
				if(i == 3) left = -110;
			}
			// :PLACES THE OVERLAY TO THE LEFT CORNER
			$(this).find('ul.level1').css({ left: left })
			// :ENABLES A ITEM TO ALLOW DEBUGING
			// if(i == 0) $(this).find('ul.level1').eq(0).css({ opacity:.95, display:'block' });
		});		
	},
	setUI : function() {
		// REPLACES THE MAIN BUTTONS INTERACTION
		console.log('::setUI');
		$('#mainNavigation li.level0').each(function() {
			// :GRABS THE CURRENT LABEL
			var currentAnchor = $(this).find('a').eq(0);
			var label = $(currentAnchor).text();
			var link = $(currentAnchor).attr('href');
			// :ADDS THE OVERLAY LAYER 
			$(this).append('<div class="overlay">' + label + '</div>');
			
			// INITIALY HIDES THE OVERLAY LAYER
			var overlay = $(this).find('.overlay').eq(0);
			$(overlay).css({opacity: 0});
			
			// :ADDS THE ELEMENTS INTERACTIVITY
			$(overlay).hover(function() {
				$(this).stop(true, false).animate({ opacity: 1 }, 800, this.easingType);
			},
			function() {
				$(this).stop(true, false).animate({ opacity: 0 }, 300, this.easingType);
			});
			if(link !== undefined) {
				$(overlay).click(function() {
					document.location = link;
				});
			}
		});
	},
	addShadow : function() {
		$('#mainNavigation ul.level1').dropShadow({blur:0, left: -8, top:32});
		$('#mainNavigation .dropShadow').css({display: 'none', opacity: 0});
	}
	
}
/********************************
 * CROSS BROWSER ADAPTION CLASS 
 *******************************/
var CrossBrowser = {
	init : function() {
		this.setupIE();
		this.setupIPad();
	},
	setupIE : function() {
		if($.browser.msie) {
			//: adapts stylesheets
			$('#mainNavigation li.level0').css({ position: 'relative' });
			$('#mainNavigation li.level0 a, #mainNavigation li.level0 div.overlay').css({ top:0, height:20 });
			$('#mainNavigation .a0, #mainNavigation li.level0 .overlay').css({ top:0, height:30 });
			$('#mainNavigation li a').css({ display:'inline-block', display:'block' });
			$('#mainNavigation li.level1').css({ float:'left' });
			$('#mainNavigation ul.level1').css({ top: 30 });
			$('#mainNavigation ul.level4').css({ whiteSpace: 'nowrap' });
			$('#leftNavi li.level1.active a, #leftNavi li.level2.active a').css({color:'#ffffff'});
			
			//: checks if scrollbars are needed for the content, because the stylesheet does not properly handle ie horizontal body width
			this.checkBodyOverflow();
			setInterval(this.checkBodyOverflow, 300);
			
			//: does a special treatment for ie6
			if($.browser.version.substr(0, 3) < 7) {
				console.log('IE6');
				$('#mainNavigation ul.level1').eq(3).css({ left: -100 });
				$('#mainNavigation li.level0').css({ clear: 'right' });
				$('#mainNavigation').css({ left: 180, top:-47 });
			} 
		}	
	},
	checkBodyOverflow : function() {
		$('body').width() < 1000 ? $('body').css({overflowX: 'scroll' }) : $('body').css({overflowX: 'hidden' });  
	},
	setupIPad : function() {
		//ipad and iphone fix
		if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
			console.log('IPad');
		    /*$("#mainNavigation li.level0 a").click(function(){
		        //we just need to attach a click event listener to provoke iPhone/iPod/iPad's hover event
		        //strange
		    });
		    $("#mainNavigation li.level0 a").bind('touchstart', function(){
	            console.log("touch started");
	        });
		    $("#mainNavigation li.level0 a").bind('touchend', function(){
		        console.log("touch ended");
		    });*/
			//$('#mainNavigation div.column').css({ background:'#f0f', float:'left !important'});
			//$('#mainNavigation div.column ul li ul').css({ display: 'none'});
		}
	}
}
var ImageScroll = {
	init : function() {
		//console.log('::Set scroll');
		//$("div.scrollable-horizontal").scrollable();
	}
} 
/********************************
 * DEBUGER CLASS, DETECTS FIREBUG 
 *******************************/
var Debuger = {
	enabled : true,
	names : ['log', 'debug', 'info', 'warn', 'error', 'assert', 'dir', 'dirxml', 'group', 'groupEnd', 'time', 'timeEnd', 'count', 'trace', 'profile', 'profileEnd'],
 	checkConsole : function(enabled) {
		this.enabled = enabled;
		//
		if(this.enabled) {
			// CHECKS IF DEBUGING CONSOLE IS AVAILABLE
			if('console' in window && 'firebug' in console){
				//: FIREBUG AVAILABLE
			}
			else if(window.console = 'undefined'){
				//: NO CONSOLE AVAILABLE, CREATES DEBUGGER WINDOW
				$('body').append('<div id="debugger" style="position:absolute; z-index:1000; bottom:0px; right:0; display:block; background:#000; color:#fff; padding:3px; border:5px solid #036; width:300px; height:200px; overflow:scroll; overflow-x:hidden;"><b>:::Debugger</b></div>');
				$('#debugger').css({opacity: .8, display:'none'});
				window.console = {};
				for (var i = 0; i < this.names.length; ++i) {
					window.console[this.names[i]] = function(str){
						$('#debugger').html($('#debugger').html() + '<br/>'  + str)
					}
				};
				//: adds keyhandler
				$(document).keydown(function(event) {
					console.log(event.keyCode);
					if (event.keyCode == '106') {
						$('#debugger').css('display') == 'none' ? $('#debugger').css({display:'block'}) : $('#debugger').css({display:'none'});;
						event.preventDefault();
					};
				});
			}
		}
		else {
			//: CONSOLE DISABLED, CREATES EMPTY OBJECT TO AVOID RUNTIME ERRORS
			if(!('console' in window)) window.console = {};
    		for (var i = 0; i < this.names.length; ++i) window.console[this.names[i]] = function() {};
		}
	}
}

