﻿
$(document).ready(function() {
    $('.top-menu-cell').each(function(i, item) {
        item._menuIndex = i;
    });
    
    $('.top-menu-cell').hover(showAnimation, hideAnimation);
    
    $('.home-cell').click(function() { window.location = 'Default.aspx'; });
});

_showAnimation = null;
_hideAnimation = null;

MENU_ITEM_HEIGHT = 16;

function showAnimation() {
    var element = this;
    
    var div = $('#topMenuRepeater_ctl0' + element._menuIndex + '_divSubMenuPopup') || $('#ctl00_topMenuRepeater_ctl0' + element._menuIndex + '_divSubMenuPopup');
    
    div = div.get(0);
    
    if (div == null)
        div = $('#ctl00_topMenuRepeater_ctl0' + element._menuIndex + '_divSubMenuPopup').get(0);
        
    var linkCount = div.getElementsByTagName('a').length;
    
    if (linkCount < 1)
        return;
        
    $(div).stopTime('showAnimation' + element._menuIndex);
    $(div).stopTime('hideAnimation' + element._menuIndex);
    
    var pos = findPos(element);
    
    div.style.left     = (pos[0] + 3) + 'px';
    div.style.top      = (pos[1] + element.offsetHeight - 10) + 'px';
    div.style.height   = '0px';
    div.style.overflow = 'auto';
    div.style.display  = 'block';
    
    var links = div.getElementsByTagName('a');
    var widestSize = element.offsetWidth;
    
    for (var i = 0; i < links.length; ++i) {
        if (links[i].offsetWidth > widestSize)
            widestSize = links[i].offsetWidth;
    }
    
    div.style.overflow = 'hidden';
    div.style.width = widestSize + 'px';
    
	$(div).everyTime(70, 'showAnimation' + element._menuIndex, function(i) {
	    var anchorIndex = parseInt(div.offsetHeight / MENU_ITEM_HEIGHT);
	    var anchors = div.getElementsByTagName('a');

        var thisAnchor = anchors[anchorIndex];
        
        //in 50 milliseconds, change the color slightly
        if (anchors.length > 0 && anchorIndex <= anchors.length - 1) {
            anchors[anchorIndex].style.color = '#e0e0e0';
            $(anchors[anchorIndex]).oneTime(50, function() { this.style.color = 'ccc'; });
		}
		
		//in 100 milliseconds, change the color again... making it look like a blend
		if (anchorIndex == anchors.length - 1) {
		    $(anchors[anchorIndex]).oneTime(100, function() { this.style.color = ''; });
		}
		
		--anchorIndex;
		
        if (anchors.length > 0 && anchorIndex > -1 && anchorIndex <= anchors.length - 1) {
            anchors[anchorIndex].style.color = '';
		}
        
		div.style.height = Math.min(linkCount * MENU_ITEM_HEIGHT, (div.offsetHeight - 2) + thisAnchor.offsetHeight) + 'px';
		
		if (div.offsetHeight >= linkCount * MENU_ITEM_HEIGHT)
	        $(div).stopTime('showAnimation' + element._menuIndex);
	});
}

function hideAnimation() {
    var element = this;
    var linkCount = element.getElementsByTagName('a').length;
    
    var div = $('#topMenuRepeater_ctl0' + element._menuIndex + '_divSubMenuPopup') || $('#ctl00_topMenuRepeater_ctl0' + element._menuIndex + '_divSubMenuPopup');
    
    div = div.get(0);
    
    if (div == null)
        div = $('#ctl00_topMenuRepeater_ctl0' + element._menuIndex + '_divSubMenuPopup').get(0);
        
        
    $(div).stopTime('hideAnimation' + element._menuIndex);
    $(div).stopTime('showAnimation' + element._menuIndex);
    
	$(div).everyTime(70, 'hideAnimation' + element._menuIndex, function(i) {
	    var anchorIndex = parseInt(div.offsetHeight / MENU_ITEM_HEIGHT) - 1;
	    var anchors = div.getElementsByTagName('a');

        var thisAnchor = null;
        
        if (anchors.length > 0 && anchorIndex > -1 && anchorIndex <= anchors.length - 1) {
            thisAnchor = anchors[anchorIndex];
            anchors[anchorIndex].style.color = '#e0e0e0';
		}
		
		--anchorIndex;
		
        if (anchors.length > 0 && anchorIndex > -1 && anchorIndex <= anchors.length - 1) {
            anchors[anchorIndex].style.color = '#ccc';
		}
		
		var incHeight = thisAnchor ? thisAnchor.offsetHeight : MENU_ITEM_HEIGHT;
		
		div.style.height = Math.max(0, ((div.offsetHeight - 2) - incHeight)) + 'px';
		
		if (div.offsetHeight < 3) {
	        div.style.display = 'none';
	        $(div).stopTime('hideAnimation' + element._menuIndex);
	    }
	});
}

function showMenu(event, element, menu) {
}

function hideMenu(event, element, menu) {
}


function findPos(obj) {
	var curleft = curtop = 0;
	
	if (obj.offsetParent) {
	    do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
	    } while (obj = obj.offsetParent);
	}
	return [curleft, curtop];
}
