var currentUrl = window.location.href.toString().toLowerCase(),
	currentMarketplace,
	currentUrlParams = {},
	isRoot = currentUrl.indexOf("/business/") === -1 && currentUrl.indexOf("/residential/") === -1 && currentUrl.indexOf("/nonprofit/") === -1,
	downLevel = (isRoot) ? "" : "../";

$(function() {
	
	//console.log("currentUrl: " + currentUrl);
	//console.log("isRoot: " + isRoot.toString());
	
	if (!isRoot) {
		
		if (currentUrl.indexOf("/business/") > -1) { currentMarketplace = "business"; }
		if (currentUrl.indexOf("/residential/") > -1) { currentMarketplace = "residential"; }
		if (currentUrl.indexOf("/nonprofit/") > -1) { currentMarketplace = "nonprofit"; }
	
		//console.log("currentMarketplace: " + currentMarketplace);
		
	}
	
    var e,
        a = /\+/g,  // Regex for replacing addition symbol with a space
        r = /([^&=]+)=?([^&]*)/g,
        d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
        q = window.location.search.substring(1);

    while (e = r.exec(q)) {
		currentUrlParams[d(e[1])] = d(e[2]);
		//console.log("currentUrlParams: " + d(e[1]) + " = " + d(e[2]));
	}
	
	if (!currentMarketplace && "mp" in currentUrlParams) { 
		
		currentMarketplace = currentUrlParams["mp"];
		//console.log("currentMarketplace: " + currentMarketplace);
			
	}
	
});


/* Main Menu   -----------------------------------------------------------*/

$(function() {
	
	var $marketplaceListItems = $("ul#pri-menu li");
	$marketplaceListItems.each(function(i, $item) {
		
		//console.log("marketplace item: " + i.toString());
		
		var $li = $(this);
		if ($li.find("a")[0].href.indexOf(currentMarketplace) > -1) {
			
			$marketplaceListItems.removeClass("current-menu-item");
			$li.addClass("current-menu-item");
			return false;
			
		}
		
	});

	$("#header .logo a").click(function(e) {
		
		if (currentMarketplace) { 
			
			e.preventDefault();
			
			var newUrl = downLevel + currentMarketplace + "/index.html";
			//console.log("redirecting to \"" + newUrl + "\"");
			window.location.href = newUrl; 
			
		}
		
	});
	
	$("#header .sec-menu a, #tri-menu a").click(function(e) {
	
	if (currentMarketplace && this.target !== "_blank") {
		
			e.preventDefault();
		
			var newUrl = ($(this).hasClass("services")) ? currentMarketplace + "/services.html" : this.href + "?mp=" + currentMarketplace;
			//console.log("redirecting to \"" + newUrl + "\"");
			window.location.href = newUrl;
			
		}
		
	});

	k_menu(); // controls the dropdown menu
	sys_toggle();

});


function k_menu() { // k_menu controlls the dropdown menus and improves them with javascript

	$(".nav a").removeAttr('title'); 
	$(" .nav ul ").css({ display: "none" }); // Opera Fix

 	//smooth drop downs jQuery(".nav li").each(function() {

	var $list = $(this),
		$sublist = $list.find('ul:first');

	$list.hover(function() { 
		
		$sublist.stop().css({
			
			overflow:"hidden", 
			height:"auto", 
			display:"none", 
			paddingTop:0
			
		}).slideDown(400, function() {
			
			$(this).css({
				
				overflow:"visible", 
				height:"auto"
				
			}); 
		
		}); 
		
	}, function() { 
		
		$sublist.stop().slideUp(400, function() { 
			
			$(this).css({
				
				overflow:"hidden",
				display:"none"
				
			}); 
			
		}); 
		
	});
	
	// });

}

/* jQuery Toggle   -----------------------------------------------------------*/

function sys_toggle() {
	
	$(".toggle_content").hide(); 
	
	$("h4.toggle")
		.click(function() { $(this).next(".toggle_content").slideToggle(); })
		.toggle(function() { $(this).addClass("active"); }, function () { $(this).removeClass("active"); });

}



/* Plan box -----------------------------------------------------------*/

$(function() {	
	
	var $planBoxes = $("div.plan_box"), 
		$marketplaceBoxes = $("div.marketplace_box");
	
	if ($planBoxes.length) {
		
		// Vertical Sliding -
		// To switch directions up/down and left/right just place a "-" in front of the top/left attribute
		
		$planBoxes
			.hover(function() { $(".plan_info", this).stop().animate({top:'-400px'},{queue:false,duration:300}); }, 
				   function() { $(".plan_info", this).stop().animate({top:'0px'},{queue:false,duration:300}); });
	}

	if ($marketplaceBoxes.length) {
		
		$("div.marketplace_box")
			.hover(function() { toggleMarketplaceImage($(this), true); }, 
				   function() { toggleMarketplaceImage($(this), false); })
			.click(function() {
				
				var $this = $(this),
					id = $this.attr("id");
					
				window.location.href = id + "/index.html";
				
			});
		
	}
	
	function toggleMarketplaceImage($box, isHover) { 
		
		var $img = $box.find("img"),
		 	$h1 = $box.find("h1"),
			src = $img.attr("src");

		$img.attr("src", (isHover) ? src.replace(".png","_hover.png") : src.replace("_hover", ""));
		$h1.css("color", (isHover) ? "#000" : "#CCC");
		
	}
	
});
