var defaultSecondaryNavTimeout = -1;
var defaultSecondaryNav = -1;
$(function() {
	var currentSubitem = $('#MainNavigationSubArea .navCurrentPage');
	
	if(currentSubitem.size()==0) {
		var currentItem = $('#MainNavigationArea .navCurrentPage');
		if(currentItem.size()==0) {
			defaultSecondaryNav = -1;
		} else {
			defaultSecondaryNav = Math.floor(currentItem.index()/2);
		}
	} else {
		defaultSecondaryNav = currentSubitem.parent('.mainNavigationSubItemData').index();
		/*var subitemPosition = currentSubitem.position();
		var subitemArrow = $('#MainNavigationSubArea .subItemArrow');
		var asdf = $('#MainNavigationSubArea .navCurrentPage').width();
		var sd = subitemArrow.outerWidth();
		var subitemArrowLeft = subitemPosition.left+(currentSubitem.width()-subitemArrow.width())/2;
		subitemArrow.css({'left': subitemArrow+'px', 'display': 'block'});*/
	}
	
	showMainSub(defaultSecondaryNav);
	
	$('.mainNavigationItem').hover(function() {
		if(defaultSecondaryNavTimeout!=-1) {
			clearTimeout(defaultSecondaryNavTimeout);
			defaultSecondaryNavTimeout = -1;
		}
		var index = Math.floor($(this).index()/2);
		showMainSub(index);
	}, function(){});
	
	
	$('#MainNavigationArea').hover(function() {
	
		clearTimeout(defaultSecondaryNavTimeout);
		defaultSecondaryNavTimeout = -1;
		
	}, function() {
	
		defaultSecondaryNavTimeout = setTimeout(function() {
			var index = defaultSecondaryNav;
			
			showMainSub(index);
			
			defaultSecondaryNavTimeout = -1;
		}, 1000);
		
	});
});
function showMainSub(index) {
	$('.mainNavigationSubItemData').filter(function(){
		return ($(this).index()!=index);
	}).stop(true, false).animate({'opacity':'0.0'}, 250, function(){$(this).css('display','none');});
	if(index != -1 && (parseFloat($('.mainNavigationSubItemData').eq(index).css("opacity"))!=1.0 || $('.mainNavigationSubItemData').eq(index).css("display").toLowerCase()=='none')) {
		$('.mainNavigationSubItemData').eq(index).css('display','block'); 
		$('.mainNavigationSubItemData').eq(index).stop(true, false).animate({'opacity': '1.0'}, 250, function(){$(this).css('filter','none');});
	}
}
/* Extender Menu Stuff */
$(function() {
	$('.scriptExtenderMenuContent').each(function() {
		$(this).css({'height':$(this).find('.scriptExtenderMenuContentTitle').outerHeight()+'px'});
		var hitArea = $(this).parent('.scriptExtenderMenuHitArea');
		hitArea.hover(function() {
			var interval = jQuery.data(this, 'collapseInterval');
			if(interval!=null)
				clearTimeout(interval);
			$(this).find('.scriptExtenderMenuContent').stop(true,false);
			$(this).find('.scriptExtenderMenuContent').animate({'height': $(this).find('.scriptExtenderMenuContentTitle').outerHeight()+$(this).find('.scriptExtenderMenuContentItems').outerHeight()+'px'}, 300);
		}, function() {
			var thisHitArea = $(this)
			var interval = window.setTimeout(function() {
				thisHitArea.find('.scriptExtenderMenuContent').stop(true,false);
				thisHitArea.find('.scriptExtenderMenuContent').animate({'height': thisHitArea.find('.scriptExtenderMenuContentTitle').outerHeight()+'px'}, 300);
			}, 400);
			jQuery.data(this, 'collapseInterval', interval);
			//$(this).find('.scriptExtenderMenuContent').stop(true,false);
			//$(this).find('.scriptExtenderMenuContent').animate({'height': $(this).find('.scriptExtenderMenuContentTitle').height()+'px'}, 250);
		});
	});
});

//You can Add any Additional Parameters that will be passed to the CompletedFunction in an Array
function CallAjax(Data, URL, CompletedFunction) {
    var extraParameters = new Array();
    var count = 0;
    if(CallAjax.arguments.length > 3) {
        for(var index = 3; index < CallAjax.arguments.length; index++) {
            extraParameters[count] = CallAjax.arguments[index];
            count++;
        }
    }
    var ajaxVar = $.ajax({
        url: URL,
        data: Data,
        type:"POST",
        cache: false,
        async: true,
        success: function(response) {
            var fn = window[CompletedFunction];
            
            try {
                response = jQuery.parseJSON(response);
            }catch(Exception)
            {}
            
            if(count == 0) {
                fn(response);
            }else {
                fn(response, extraParameters);
            }
        }
    });
}
