/*
 * This modual is created by OAP Technology who provides with 
 * software outsourcing services to the global world. You could 
 * visite the site: http://www.oaptech.com to get more information
 * of this project, and about us as well.  
------------------------------------------- */
/* declare a class that represents the dropdown block*/
function DropdownBlock(_dropdown) {
	this.ddblock = _dropdown;
	this.timerId = 0;
	this.setTimerId = function(id) {
		this.timerId = id;
	};
	this.getTimerId = function() {
		return this.timerId;
	};
}


var ddbQ = new Array();

function initMenu() {
	/*-------------- menu layout --------------------------------*/
	/*make drop-down arrow for menu items that has child items*/
	$('#menu-bar .menu-item').each(
		function (i) {
			if($(this).hasClass('has-children')) {
				$(this).append('<span class="drop-down-arrow">&nbsp;</span>');
			}
		}
	);
	
	$('.drop-down-children UL LI').hover(
		function () {
			$(this).css('color', 'black');
		}, 
		function () {
			$(this).css('color', '');
		}
	);
	
	/*add an underline when mouse hover a menu item*/
	$('#menu-bar .menu-item').hover(
		function() {
			// mouse over
			hideAllDropdownBlock();
			$(this).css('border-bottom', 'solid 1px #66AC2D');
			if($(this).hasClass('has-children')) {
				// then the sub-menu items are visible
				var x = $(this).offset().left - $('#main').offset().left;
				var y = $(this).offset().top + 10;
				dropdown = $(this).next('.drop-down-children');
				var ddb = new DropdownBlock(dropdown);
				ddbQ.push(ddb);

				if(dropdown.css('display') == 'none') {
					dropdown.css('left', x + 'px').css('top', y + 'px');
					dropdown.show();
				}
			}
		},
		function() {
			// mouse out
			$(this).css('border-bottom', 'none');
			for( var i = 0; i < ddbQ.length; i ++ ) {
				if( ddbQ[i].getTimerId() == 0 ) {
					ddbQ[i].setTimerId(setTimeout('fadeOutDropdown()', 500));
				}
			}
		}
	);
	
	$('.drop-down-children').each(
		function (i) {
			$(this).hover(
				// mouse over
				function () {
					if(ddbQ.length > 0 ) {
					  var ddb = ddbQ.shift();
					  clearTimeout(ddb.getTimerId());
					}
				},
				// mouse out
				function () {
					$(this).hide();
				}
			);
		}
	);
}

function fadeOutDropdown() {
	if(ddbQ.length > 0 ) {
		ddb = ddbQ.shift();
		ddb.ddblock.hide();
	}
}

function hideAllDropdownBlock() {
	for (var i = 0; i < ddbQ.length; i ++ ) { 
		clearTimeout(ddbQ[i].getTimerId());
		ddbQ[i].ddblock.hide();
	}
	ddbQ = new Array();
}
