/**
 * Copyright 2010 Sven Johansson (johansson.sven@gmail.com)
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

$(document).ready(bindMenuActions);

function bindMenuActions() {
	$('.main-menu-item').hover(
		function() {
			// TODO: Determine background fade or not!
//			$(this).stop().animate({"backgroundColor":"#aaaa892"}, "fast");
			$(this).css("backgroundColor", "#ab9a50");
			$('#mitem-iaimg' + this.id.substring(5)).css('display', 'none');
			$('#mitem-aimg' + this.id.substring(5)).css('display', 'inline');
			var ctnId = '#msub' + this.id.substring(5);
			var parent = $('#' + this.id);
			if ($(ctnId).is(':visible')) {
				return;
			}
		
			//$(ctnId).css('top', '241px');
			$('.main-menu-sub-item-container').hide();
			$(ctnId).fadeIn(350);
			var idealWidth = parent.outerWidth();
			if ($(ctnId).width() < idealWidth) {
				$(ctnId).width(idealWidth);
			}
			var left = parent.offset().left;
			$(ctnId).offset({'left': left,'top':parent.offset().top + parent.outerHeight()});
			
		},
		function() {
			var mItem = $(this);
			// TODO: Determine if fade to color or remove bg
//			mItem.stop().animate({"background":"black"}, "fast");
			$('#mitem-iaimg' + this.id.substring(5)).css('display', 'inline');
			$('#mitem-aimg' + this.id.substring(5)).css('display', 'none');
			mItem.css("background","none");
		}
	);
	
	// Make entire sub item clickable
	$('.main-menu-sub-item').each(function(index, item) {
		$(item).click(function() {
			$(this).children("a").each(function(idx, obj) {
				window.location.href = obj;
			});		
		});
	});
	
	$('.main-menu-sub-item-container').hover(
		function() {
		
		}, 
		function() {
			// var parentId = '#mitem' + this.id.substring(4);
			$('#' + this.id).fadeOut(350);
		}
		
	); 
	
	$('.main-menu-sub-item').hover(
		function() {
			$(this).stop().animate({"backgroundColor":"#ab9a50"}, "fast");
			$('#msitem-iaimg' + this.id.substring(6)).css('display', 'none');
			$('#msitem-aimg' + this.id.substring(6)).css('display', 'inline');

		},
		function() {
			$(this).stop().animate({"backgroundColor":"white"}, "fast");
			$('#msitem-iaimg' + this.id.substring(6)).css('display', 'inline');
			$('#msitem-aimg' + this.id.substring(6)).css('display', 'none');
		}
	);

}



