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

$(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 = $("#sitemenu>ul>li>a").map( function () { return '<option value="' + $(this).attr("href") + '">' + $(this).text() + '</option>'; }).get().join("");
        $("#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
        $(".jshide").hide();
        // Same for #printlogo but replace with a logo for printing
        $("#printlogo").replaceWith('<img src="/img/logo-engros-black.png" alt=".SrfSnoSk8.Engros - National importer of leading coresports brands since 1978" class="printonly"/>');

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

	// Page menu (before menu banner)
	if($("#page_menu").html() != null) {
		// Show less in menu - Lill compliance
		var selector;
		if ($("#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 ($(selector).html() == null) {
				selector = "#menu>ul>.current_page_ancestor>ul>.current_page_item";
			}
		} else {
			selector = "#menu>ul>.current_page_ancestor";
			if ($(selector).html() == null) {
				selector = "#menu>ul>.current_page_item";
			}
		}
		if ($(selector).html() == null) {
			selector = "#xls_include_menu";
		}
		if ($(selector).html() != null) {
			$("#page_menu").prepend('<ul class="' + $(selector).attr("class") + '">'+ $(selector).html() + "</ul>");
		} else {
			// Must be top level Surf
			selector = "#menu>ul>.current_page_item";
			$("#page_menu").prepend('<ul class="' + $(selector).attr("class") + '">'+ $(selector).html() + "</ul>");
			$("#page_menu .current_page_item li>ul>li").hide();
		}

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

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

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

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

