NICK.namespace("search");
NICK.search.validate = function() {
	var term = document.getElementById("search-term").value;
	//alert("search: "+term);
	if(term.length <= 0) return false;

	var searchUrl = "/search/?term="+escape(term);
	
	if(KIDS.get("adfree") === 'true') {
		//alert("search: af: "+searchUrl);
		NICK.utils.openBumper('fullPage', searchUrl,null,null,null,false);
	} else {
		document.location.href = searchUrl;
	}
	return false;
	
}

$(function() {
	// Give the requested mamabar items bumper links
	$("a.bumper, #nick-arcade-bumper a").each(function() {
		$(this).click(function(e) {
			e.preventDefault();
			NICK.utils.openBumper("paysite", $(this).attr("href"));
			return false;
		});
	});

	$("#global-bar .down-arrow-extra").click(function() {
		var pos = 0, next = $(this).parent().parent().next();
		if ( next.size() > 0 ) {
			var pos = next.siblings().andSelf().index(next);
		}
		$(this).parent().parent().parent().animate({
			top: pos * -next.height()
		}, "fast");
	});

	$("#top-menu-more,#global-sites-more").hover(function() {
			$(this).addClass("hover");
		}, function() {
			$(this).removeClass("hover");
		}
	);

	if ( $("#submenu-more").size() > 0 ) {
		$("#submenu-more").click(openMoreSubMenu).children("a").removeClass("active");
	}

	$(".png,h1.icon").supersleight({
		shim: "/assets/transparent.gif",
		apply_positioning: false
	});

	$(document).bind("authStatus loggedIn loggedOut", function(){
		setHeaderStatus();
	});

	var setHeaderStatus = function(){
		NICK.utils.doLog("authStatus: setHeaderStatus:" + NICK.userData.loggedIn + NICK.userData.screenName);
		if(NICK.userData.loggedIn == "true"){
			$(".mynick-noauth").hide();
			$(".mynick-auth").show();
			if(NICK.userData.screenName == ''){
				var displayName = NICK.userData.nickName;
			}else{
				var displayName = NICK.userData.screenName;
			}
			$("#mynick-nav-main").html('<a onClick="NICK.utils.goMyNick(this.href); return false;" href="/mynick/nickpages/index.jhtml?username=' + displayName.toLowerCase() + '">' + displayName + '</a>');
		} else {
			$("#mynick-nav-main").html('<a onClick="NICK.utils.goMyNick(this.href); return false;" href="/mynick/">MYNICK</a>');
			$(".mynick-auth").hide();
			$(".mynick-noauth").show();	
		}
	}

	/*$("#submenu a").each(function() {
		if ( $(this).attr("href") == window.location.pathname ) {
			$(this).addClass("active");
		}
	});*/
});

function openMoreSubMenu(e) {
	e.preventDefault();

	var wrapper = $("#submenu-wrapper");
	var content = $("#submenu-content");
	var text    = $("#submenu-more").find("a span");

	if ( wrapper.hasClass("expanded") ) {
		content.slideUp("fast", function() { wrapper.removeClass("expanded"); });
		text.text("More");
		text.attr("title", "More");
	} else {
		wrapper.addClass("expanded");
		content.slideDown("fast");
		text.text("Close");
		text.attr("title", "Close");
	}
}

$(document).ready(function() {
	NICK.utils.doLog("header.js: initLogin!");
		
	$(document).bind("loggedIn", function(){
		NICK.utils.doLog("header.js: loggedIn");
	})
	$(document).bind("logInFail", function(){
		NICK.utils.doLog('header.js: logInFail');
		setErrorContent();
	});

	var setErrorContent = function(){
		$('#loginError').show();
		$('#nickLogin').show();
		$('#loginLoader').hide();
	}
});

function sendLogin(){
	var loginData = {
		username: $(".o_popup_login input[name='nickUsername']").val(),
		password: $(".o_popup_login input[name='nickPassword']").val()
	};

	NICK.login.doLogIn(loginData);
}