jQuery.preloadImages = function() {
	for(var i = 0; i<arguments.length; i++) {
		jQuery("<img>").attr("src", arguments[i]);
	}
}

var $j = jQuery.noConflict();
$j(document).ready(function(){
	// -- paramerts for menu images -- START
	var font     = "uptown";
	var fontsize = 12;
	// rrggbbaa - red, green, blue, alpha(00 = not transparent, 7f = fully transparent)
        var fg       = "ffffff";
	var bg_off   = "0000007f";
	var bg_on    = "00b9e933";
	var bg_girls = "eb1e7933";
	// -- paramerts for menu images -- END

        // Change site selection list into drop down
        var sitemenu = $j("#sitemenu>ul>li>a").map( function () { return '<option value="' + $j(this).attr("href") + '">' + $j(this).text() + '</option>'; }).get().join("");
        $j("#sitemenu").empty().append('<div><form action="http://www.srfsnosk8.no/sites/" method="get"><div style="float: left;"><select name="url" onchange="with(this){top.location.href=options[selectedIndex].value;}" style="margin-right: 5px;">' + sitemenu + '</select></div><button type="submit"><img src="/img/choose146x23.png" alt="Velg webside"/></button></form></div>');

        // Javascript running - no need to show info intended for
        // non-javascript visitors
        $j(".jshide").hide();
        // Same for #printlogo but replace with a logo for printing
        $j("#printlogo").replaceWith('<img src="/img/logo_black.png" alt=".SrfSnoSk8. -Taking care of Boardriders Since 1978" class="printonly"/>');

	// Hack for wordpress
	var inner = $j("#menu li.pagenav>ul").html();
	$j("#menu li.pagenav").replaceWith(inner);

	// Page menu (before menu banner)
	if($j("#page_menu").html() != null) {
		// Show less in menu - Lill compliance
		var selector;
		if ($j("#menu>ul>.current_page_ancestor>a, #menu>ul>.current_page_item>a").text().toLowerCase() == "surf") {
			// Special treatment for Surf which is an extra level before topic is chosen
			selector = "#menu>ul>.current_page_ancestor>ul>.current_page_ancestor";
			if ($j(selector).html() == null) {
				selector = "#menu>ul>.current_page_ancestor>ul>.current_page_item";
			}
		} else {
			selector = "#menu>ul>.current_page_ancestor";
			if ($j(selector).html() == null) {
				selector = "#menu>ul>.current_page_item";
			}
		}
		if ($j(selector).html() == null) {
			selector = "#xls_include_menu";
		}
		if ($j(selector).html() != null) {
			$j("#page_menu").prepend('<ul class="' + $j(selector).attr("class") + '">'+ $j(selector).html() + "</ul>");
		} else {
			// Must be top level Surf
			selector = "#menu>ul>.current_page_item";
			$j("#page_menu").prepend('<ul class="' + $j(selector).attr("class") + '">'+ $j(selector).html() + "</ul>");
			$j("#page_menu .current_page_item li>ul>li").hide();
		}

		var inner = $j("#links").html();
		if (inner != null) {
			$j("#links").remove();
			$j("#page_menu").append(inner);
		}
	}

        // Show page menu in menu banner
        $j("#menu>ul>li>a").map( function() {
		var me = $j(this);
		var txt = me.text();
		me.html( '<img src="/text/' + font + '/' + fontsize + '/' + fg  + '/'+ bg_off +'/' + txt + '" alt="' + txt + '">');
		lctxt = txt.toLowerCase();
		if (lctxt == "girls") {
			$j.preloadImages('/text/' + font + '/' + fontsize + '/' + fg  + '/' + bg_girls + '/' + txt);
		} else if (lctxt == "butikkene") {
			$j("#top_menu>ul").prepend('<li><a href="' + me.attr("href") + '"><img src="/img/menu_2_01.png" alt="' + txt + '"/></a></li>');
			me.remove();
		} else if (lctxt == "shop") {
			$j("#top_menu>ul").prepend('<li><a href="' + me.attr("href") + '"><img src="/img/menu_2_02.png" alt="' + txt + '"/></a></li>');
        	} else {
			$j.preloadImages('/text/' + font + '/' + fontsize + '/' + fg  + '/' + bg_on + '/' + txt);
			if (lctxt == "surf") {
				me.parent().children("ul").wrap('<div class="submenu"></div>');
				me.attr("onclick", "return false;");
			}
		}
		me.parent().find("ul>li>ul").hide();
	})
        $j("#menu_banner").prepend('<div style="float:right;"><ul>' + $j("#menu>ul").html() + "</ul></div>");
        $j("#menu").empty();
	$j("div.submenu").hide();
	// Uncomment next line to keep current area open
	// $j(".current_page_ancestor>div.submenu").show();
        
	// Mouseover on menu images
        $j("#menu_banner>div>ul>li>a>img").hover(
        	function() {
        		var txt = $j(this).attr("alt");
        		if (txt.toLowerCase() == "girls") {
				$j(this).attr("src", '/text/' + font + '/' + fontsize + '/' + fg  + '/' + bg_girls + '/' + txt);
        		} else {
	        		$j(this).attr("src", '/text/' + font + '/' + fontsize + '/' + fg  + '/' + bg_on + '/' + txt);
	        	}

			$j("div.submenu").hide();
			$j(this).parents("li").find("div.submenu").show();
        	},
        	function() {
        		var txt = $j(this).attr("alt");
        		$j(this).attr("src", '/text/' + font + '/' + fontsize + '/' + fg  + '/'+ bg_off +'/' + txt);
        	}
        );

	// Thanks to Cory S.N. LaViska http://abeautifulsite.net/notebook.php?article=47
	$j('A[rel="external"]').click(function() {
		window.open($j(this).attr("href"));
		return false;
	});
});

