$(document).ready(function(){ 
	//Sitespecific javascript here
	//Greybox closebox
$(".greybox").click(function(){
			$("#GB_window").css("background-color","#aaaaaa").prepend('<span class="closeMe">St&auml;ng</span>');
			$(".closeMe").click(GB_hide);
			});


               //Searchbox clear and fill
	function search_input_values()
	{	
		var searchBox = $('#search input');
		$('#search input').focus(function(){
			if (searchBox.val() != "") {
				
				var theValue = searchBox.val();
				searchBox.attr('temp', theValue)
				searchBox.val('');
			};
		
		});
		$('#search input').blur(function(){
			if (searchBox.val() == "") {
				var theValue = searchBox.attr('temp');
				searchBox.val(theValue);
			};
			
		});
	}
	search_input_values();
	
	//Even and odd rows in tables
	/* if Environment is NOT Editor */
		$("table tr").mouseover(function() {
			$(this).addClass("over");}).mouseout(function() {$(this).removeClass("over");
		});
		$("table tr:nth-child(even), table tr td:nth-child(even)").addClass("even");
		$("table tr:nth-child(odd), table tr td:nth-child(odd)").addClass("odd");
		$("table tr:first-child").removeClass('even').addClass("th");
		$('table tr td:first, ul li:first').addClass('first');
		$('table tr td:last, ul li:last').addClass('last');


		
		//Show links with flowplayer as embedded media
		if ($('p > a').attr('title') == 'flowplayer') {
			$('a[title=flowplayer]').jmedia(
			{version:"6,0"},
			{width:"630", 
			height:"355", 
			autoplay: true,
			maintainaspect:"true"});
		};


	
	/*Accordion menu*/
	//Hide submenus
	$('#sub_nav li ul').hide();
	//If menu option is current, show sub menu
	
			$('#sub_nav ul:first-child > li.current > ul').slideToggle('slow');
			
			
			
		
	//When clicking the top level, show submenu, but not sub sub menus
	//only do this IF there is a sub menu, otherwise treat as normal link
	$('#sub_nav ul:first-child > li > a:not(:only-child)').click(function() {

		//if ($('#container').hasClass('p_content')) {}
		//if link has no siblings, or submenus, return the link. 
		
			$(this).parent().toggleClass('current'); //set class of the parent to current for styling
			$(this).next().slideToggle('slow'); //slide down the next ul after the a tag
			return false; //commenting this out will stop links from working. We want this for content pages.
		
		
	});

        //$('#nav_ul li ul li').click(function() {
      // the clicked LI
      var clicked = $("#nav_ul li ul li#current");

      // all the LIs above the clicked one
      var previousAll = clicked.prevAll();

      // only proceed if it's not already on top (no previous siblings)
      if(previousAll.length > 0) {
        // top LI
        var top = $(previousAll[previousAll.length - 1]);

        // immediately previous LI
        var previous = $(previousAll[0]);

        // how far up do we need to move the clicked LI?
        var moveUp = clicked.attr('offsetTop') - top.attr('offsetTop');

        // how far down do we need to move the previous siblings?
        var moveDown = (clicked.offset().top + clicked.outerHeight()) - (previous.offset().top + previous.outerHeight());

        // let's move stuff
        clicked.css('position', 'relative');
        previousAll.css('position', 'relative');
        clicked.animate({'top': -moveUp});
        previousAll.animate({'top': moveDown}, {complete: function() {
          // rearrange the DOM and restore positioning when we're done moving
          clicked.parent().prepend(clicked);
          clicked.css({'position': 'static', 'top': 0});
          previousAll.css({'position': 'static', 'top': 0});
        }});
      }
    //});

    //Create mobile selectbox of menu
$("<select id=\"nav_select\" />").appendTo("#nav");

// Create default option "Go to..."
	$("<option />", {
	   "selected": "selected",
	   "value"   : "",
	   "text"    : "Meny"
	}).appendTo("#nav_select");

	// Populate dropdown with menu items
	$("#nav > ul > li > a").each(function() {
		
		
		
	 var el = $(this);
	 $("<option />", {
	     "value"   : el.attr("href"),
	     "text"    : el.text()
	 }).appendTo("#nav_select");
	 if ($(this).parent().hasClass('current')) {
			$(this).next('ul').find('a').each(function(){
				
				switch($(this).parents('ul').length){
					case 2:
					var eldash = "-- ";
					break;
					case 3:
					var eldash = '--- ';
					break;
					default:
					
					break;
				}
				 var el = $(this);
				 $("<option />", {
				     "value"   : el.attr("href"),
				     "text"    : eldash+el.text()
				 }).appendTo("#nav_select");
			});
		}
	});
	$("#nav_select").change(function() {
	  window.location = $(this).find("option:selected").val();
	});


	

});
