function runscript()
{

	if($("#where").val() == 'slideshow')
	{
		 $(window).resize(function() {
                  position('center');
                });
                position('center'); 
               
		jQuery(document).ready(function($) {
			$("#slider-1").nivoSlider({
				directionNav:true,directionNavHide:false,controlNav:false,pauseOnHover:true,slices:15,captionOpacity:0.5,			
				startSlide:0 // Avoid a trailing comma
			}); 
		}); 
	}
	
if($("#where").val() == 'video')
	{
		
					   

		
		$(window).resize(function() {
                  position('bottom');
                });
                position('bottom'); 
                
	}

if($("#where").val() == 'tekst')
	{
		if ($("#content-right").length > 0){
  			$("#content-left").css("width","645px");
			$("#content-right").css("width","278px");
		}
		Cufon.replace('h4', {
		hover: true
		});	

		jQuery(document).ready(function($) {

			$("#slider-1").nivoSlider({

				directionNav:false,directionNavHide:false,controlNav:false,pauseOnHover:false,slices:15,captionOpacity:0.8,			

				startSlide:0 // Avoid a trailing comma

			}); 

		}); 

		$(window).resize(function() {
                  position('full');
                });
                position('full'); 
               
                
	}
}

$(window).load(function() {
        $('#slider').nivoSlider();
    });

$(document).ready(function(){
			Cufon.replace('h4', {
	hover: true
});	
		$('#content-left').tinyscrollbar();
		
	});
	
Cufon.replace('#nav-topmenu-table', {
	hover: true
});
function position(where)
{

 if($(window).height() < 630)
  {
  $("#main").height(423); 
  $("#wrapper").css('min-height','630px');
  if(where == 'center')
  	{ 
	  $("#spacer-above").height((($("#main").height()-$("#content-slide").height()-96)/2)-1);
	  $("#spacer-below").height((($("#main").height()-$("#content-slide").height()-96)/2));
	 
	}
  if(where == 'bottom')
	{
	  $("#spacer-above").height((($("#main").height()-$("#thevideo").height()-96))-1);
	  $("#spacer-below").height(0);
	}
   if(where == 'full')
	{
	  $("#spacer-above").height(0);
	  $("#spacer-below").height(0);
	  $("#content-right").css('top',13);
	  $("#content-left .viewport").css('height',300);
	  $("#content-left").css('height',300);
	  $('#content-left').tinyscrollbar();
	}
  if($("#thevideo").size()) {
	//resize youtube video
	}
  }
  else
  {
  if($("#thevideo").size()) {
	//resize youtube video
  }
  $("#main").height($(window).height()-207);
  $("#wrapper").css('min-height','100%');
   if(where == 'center')
  	{ 
	  $("#spacer-above").height((($("#main").height()-$("#content-slide").height()-96)/2)-1);
	  $("#spacer-below").height((($("#main").height()-$("#content-slide").height()-96)/2));
	  
	}
  if(where == 'bottom')
	{
	  $("#spacer-above").height((($("#main").height()-$("#thevideo").height()-96))-1);
	  $("#spacer-below").height(0);
	}
    if(where == 'full')
	{

	  $("#spacer-above").height(0);
	  $("#spacer-below").height(0);
	  $("#content-right").css('top',($("#main").height()-400)/2);
	  $("#content-left .viewport").css('height',($("#main").height()-126));
	  $('#content-left').tinyscrollbar();
	}
  }
}

var oldleft = 0;
var oldwidth= 0;


function click(link)
{
var link2 = link.replace(/\s+/g, '-').toLowerCase();
if (typeof(window.history.pushState) == 'function'){
                window.history.pushState("a", "a", link2);
            } 

$('#content-content-container').html('<p id="ajax-loader"><img src="/wp-content/themes/flashy/images/ajax-loader.gif" /></p>');
$('#content-content-container').load(link2+' #content-container', function() {
																   
runscript();		
});

$('#clipboard-container').load(link2+' #clipboard', function() {
Cufon.replace('h4', {hover: true});		
});


}


function click2(link)
{
var link2 = link.replace(/\s+/g, '-').toLowerCase();
if (typeof(window.history.pushState) == 'function'){
                window.history.pushState("a", "a", link2);
            } 

$('#content-content-container').html('<p id="ajax-loader"><img src="/wp-content/themes/flashy/images/ajax-loader.gif" /></p>');
$('#content-content-container').load(link2+' #content-container', function() {
runscript();		
});

$('#clipboard-container').load(link2+' #clipboard', function() {
Cufon.replace('h4', {hover: true});		
});

}


function click3(link)
{
var link2 = link.replace(/\s+/g, '-').toLowerCase();
if (typeof(window.history.pushState) == 'function'){
                window.history.pushState("a", "a", link2);
            } 

$('#content-content-container').html('<p id="ajax-loader"><img src="/wp-content/themes/flashy/images/ajax-loader.gif" /></p>');
$('#content-content-container').load(link2+' #content-container', function() {
																   
runscript();		
});

$('#clipboard-container').load(link2+' #clipboard', function() {
Cufon.replace('h4', {hover: true});		
});


}

function active(o,position,width)
{
   					o.attr('id','sub-active');
					var position2 = $('#nav-submenu-px').position();
					$("#nav-submenu-sel").css("margin-left",position.left-position2.left+15);
					$("#nav-submenu-sel").css("width",width-30);
				  oldleft = $("#nav-submenu-sel").css("margin-left");
				  oldwidth = $("#nav-submenu-sel").css("width");
					
}


function submenu(level,link,items)
{
if($('#sub-active').length) 
{
	$("#nav-submenu-sel").css("width",0);
	$('#sub-active').attr('id','sub-inactive');
}
	// Main Link Click and Hover effects
	$('#'+level).parent().parent().children().children().css('color','#9c9a9c');
	$('#'+level).parent().parent().children().children().css('background-color','#fff');
	$('#'+level).css('background-color','#ff9933');
	$('#'+level).css('color','#fff');
	
	$('#'+level).parent().parent().children().children().hover( function(){
      $(this).css('color', '#fff');
	  $(this).css('background-color', '#ff9933');
   },
   function(){
	
	  	if($(this).attr('id') == $('#'+level).attr('id'))
		{
			$(this).css('color', '#fff');
	  		$(this).css('background-color', '#ff9933');
		}
		else
		{
			$(this).css('color', '#9c9a9c');
	  		$(this).css('background-color', '#fff');
		}
   });
	Cufon.replace('#nav-topmenu-table', {
		hover: true
	});

	// Load the link
	if(!jQuery.isEmptyObject(items))
	{
	click(link+'/'+items[0]);
	}
	else
	{
	click(link);
	}
	// If it has a submenu, load it.
	if(!jQuery.isEmptyObject(items))
	{
		output = '<div id="nav-submenu-px"></div><ul id="nav-submenu-content"><li id="nav-submenu-spl">&nbsp;</li>';
		$.each(items, function(index, value) { 
		  output = output+'<li><a href="#" onclick="javascript:click2('+"'"+'/'+level+'/'+value+"'"+');active($(this).parent(),$(this).position(),$(this).parent().width());">'+value+'</a></li>'; 
		});
		output = output+'<li id="nav-submenu-spr">&nbsp</li></ul><div id="nav-submenu-px"></div><div id="nav-submenu-sel"></div>';
		$('#nav-submenu').html(output);
		
					var position = $('#'+level).position();
					var width =  $('#'+level).outerWidth(true);
					if (($("#nav-topmenu").width()/2)>position.left)
					{
					$("#nav-submenu").css("margin-left",position.left);
					$("#nav-submenu").css("width",$("#nav-topmenu").width()-position.left);
					$("#nav-seperator").css("margin-left",position.left);
					$("#nav-seperator").css("width",width);
					$("#nav-submenu-spl").css("width",width);
					$("#nav-submenu-spr").css("width",0);
					$("#nav-submenu-content").css("text-align","left");
					}
					else
					{
					$("#nav-submenu").css("margin-left",0);
					$("#nav-submenu").css("width",$("#nav-topmenu").width()-($("#nav-topmenu").width()-position.left)+width);
					$("#nav-seperator").css("margin-left",position.left);
					$("#nav-seperator").css("width",width);
					$("#nav-submenu-spr").css("width",width);
					$("#nav-submenu-spl").css("width",0);
					$("#nav-submenu-content").css("text-align","right");
					}
		
		Cufon.replace('#nav-submenu-content');
		 $('#nav-submenu-content li').hover(
				  function() {
				   if(($(this).attr("id") != 'nav-submenu-spl') && ($(this).attr("id") != 'nav-submenu-spr'))
					{
					var position = $(this).position();
					var position2 = $('#nav-submenu-px').position();
					var width =  $(this).width();
					$("#nav-submenu-sel").css("margin-left",position.left-position2.left+15);
					$("#nav-submenu-sel").css("width",width-30);
					}
				},
				  function() {

				  if($('#sub-active').length) 
					  {
					  	$("#nav-submenu-sel").css("width",oldwidth);
					    $("#nav-submenu-sel").css("margin-left",oldleft);
					  }
				  else
					  {
	  				  	$("#nav-submenu-sel").css("width","0");
					  }
				}
				);
		}
		else
		{
			$("#nav-seperator").css("width",0);
			$("#nav-submenu").css("width",0);
		}
	}
