$j = jQuery.noConflict();

var currentMegaMenu = undefined;
var currentMegaTimer = undefined;

(function($) {

	var fadeNext = function() {
		var 
			current = parseInt($("#featured ul").attr('current')),
			total = parseInt($("#featured ul").attr('total'));
			
		if(current+1 >= total) {
			current = 0;
		} else {
			current++;
		}
		
		fadeIn.call(this, current);
	};
	
	var fadeIn = function(index) {
		var 
			current = parseInt($("#featured ul").attr('current'));
			
		var liA = $("#featured li:eq("+current+")");
		liA.fadeTo('slow', 0.0);
		$("div.info", liA).css("display", "none").css("z-index", 10);
		
		var liC = $("#featured-slider ul li:eq("+current+")").removeClass("active");
		$("img", liC).fadeTo('slow', 0.5);
		
		var liB = $("#featured li:eq("+index+")");
		liB.fadeTo('slow', 1.0);
		$("div.info", liB).css("display", "block").css("z-index", 15);
		
		var liS = $("#featured-slider ul li:eq("+index+")").addClass("active");
		$("img", liS).fadeTo('slow', 1.0);
		
		$("#featured ul").attr('current', index);
	};

	
	var unbindMegaMenu = function(li) {
		
		if(li) $(li).removeClass("active");
		
		if(currentMegaMenu && currentMegaMenu.is(":visible")) currentMegaMenu.fadeOut('normal', function() {  });
		
		currentMegaMenu = undefined;
		
		if(currentMegaTimer) clearTimeout(currentMegaTimer);
		
		return false;
	}

	$(document).ready(function() {

		var slider = $("<ul />");
	
		$(".topNews_Home:first").append(
			$("<div />")
				.attr("id", "featured-slider")
				.append(slider)
		);
		
		//alert($("#featured li img").length);
		$("#featured li img").each(function(indx) {
			var img = $(this);
			
			var li = 
				$("<li />").append(
					$("<a />").append(
						$('<img />').attr('src', img.attr('rel')).attr('height', '70').attr('width', '105')
							.fadeTo(0, 0.5)
							.hover(function() {
								$(this).fadeTo('slow', 1.0);
							}, function() {
								if(!$(this).parent().parent().hasClass("active")) {
									$(this).fadeTo('slow', 0.5);
								}
							})
					).click(function() {
						fadeIn.call(this, indx);
					})
				);
			
			img.parent().attr('index', indx).fadeTo(0,0);
			
			$("#featured-slider ul").append(li);
			
			img.parent().parent().attr('total', indx + 1);
		});
		
		$("#featured ul").attr('current', -1);
		
		fadeIn.call(this, 0);
		setInterval(fadeNext, 7000);
		
		$(document).mousemove(function(e) {
			$(document)
				.data('pageX', e.pageX)
				.data('pageY', e.pageY);
		});		
		
		$("#naviLevel1 li.mega-menu").each(function(indx) {
			
			var li = $(this).prev(":first");
			var a = $("a:first", li);
			var liM = $(this);
			
			var container = 
				$("<div />")
					.attr("id", "mega-menu-"+(indx))
					//.css({ top : (aPos.top + a.outerHeight() + 1) + "px", left : aPos.left + "px" })
					.addClass("mega-menu-container");
			
			$("ul.level-3", liM).each(function() {
				var p = $(this).parent();
				p.prev("li.level-2:first").append($(this).detach());
				p.remove();
			});
			
			$("li.level-2", liM).each(function() {
				var ul = $("<ul />").addClass("level-2").append(this);
				container.append(ul);
			});
			
			liM.remove();
			
			$("#ctl00_topNavigationPane").append(container);
			
			var table = $("<table />");
			var tr = $("<tr />");
			
			table.append(tr);
			
			var height = container.height();
			var heightSection = Math.ceil(height / 2);
			var heightTmp = 0;
			var sectionId = 0;
			var td = $("<td />").addClass("first");
			
			$("ul.level-2", container).each(function(i) {

				if(heightTmp + $(this).height() > heightSection && i != 0) {
					tr.append(td);
					td = $("<td />");
					sectionId++;
					heightTmp = 0;
				}
				
				$(this).addClass("section-"+sectionId);
				heightTmp += $(this).height();
				td.append($(this).detach());
			});
			
			tr.append(td);
			
			container.append(table);

			li.hover(
				function(e) {
					$(this).addClass("hover");
					
					if(!$(this).hasClass("active")) {
					
						$("#naviLevel1 li").removeClass("active");
						$(this).addClass("active");
						
						unbindMegaMenu.call(li);
						
						container.css("left", "0px");
						
						var aPos = a.offset();
						currentMegaMenu = container;
						
						var left = aPos.left;
						
						if(currentMegaMenu.outerWidth() + left > $(document).width()) {
							left += $(document).width() - (currentMegaMenu.outerWidth() + left);
						}
						
						container
							.css({ top : (aPos.top + a.outerHeight() + 1) + "px", left : left + "px" })
							.fadeIn('normal');
						
					}
				},
				function(e) {
					$(this).removeClass("hover");
					//currentMegaTimer = setTimeout(function() { hideMegaMenu(li); }, 1500);
				}
			);
			
			if(indx != 0) {
				$(container).css({ display : 'none' });
			}
		});
		
		function mouseIn(x,y, pos, e) {
			//console.log("X: " + x + ", Y: " + y + ", left:" + pos.left + ", top:" + pos.top + ", leftMax:" + (pos.left + e.outerWidth()) + ", topMax:" + (pos.top + e.outerHeight()));
			return (
				(x > pos.left && x < pos.left + e.outerWidth())
				&&
				(y > pos.top && y < pos.top + e.outerHeight())
				);
			
		}
		
		setInterval(function() {
			if(currentMegaMenu) {
				// we have an active menu
				
				var pos = currentMegaMenu.position();
				
				var liActive = $("#naviLevel1 li.active:first");

				var x = parseInt($(document).data('pageX')), y = parseInt($(document).data('pageY'));
				
				//console.log("is hover: " + liActive.hasClass("hover"));
				
				if(  !(liActive.hasClass("hover") || mouseIn(x,y, pos, currentMegaMenu))  )
				{
					unbindMegaMenu.call(this, liActive);
				}
			}
		
		}, 500);

	    // Diashow
	    $('#mycarousel').jcarousel({
	        auto: 10,
	        scroll: 3,
	        wrap: 'last',
	        initCallback: function(carousel) {
				carousel.buttonNext.bind('click', function() {
					carousel.startAuto(0);
				});
			
				carousel.buttonPrev.bind('click', function() {
					carousel.startAuto(0);
				});
			
				carousel.clip.hover(function() {
					carousel.stopAuto();
				}, function() {
					carousel.startAuto();
				});
			}
	    });

	    $('img.captify').captify();

	});


}) (jQuery);

