//	Project scroller

var boxHeight = 450;
var repeatHeight = 0;
var stopScroll = 0;
var x;  // Timer

function dp_project_scroll() {
	
	clearTimeout( x )
	
	if( stopScroll == 1)
		return;
		
	$('highlights').scrollTop = $('highlights').scrollTop + 1;
	
	if( $('highlights').scrollTop == repeatHeight )
		$('highlights').scrollTop = 0;
		
	x = setTimeout( "dp_project_scroll()", 40 );
}

function dp_init_project_scroll() {
	
	if( $('highlights') ) {
		repeatHeight = $('highlights').scrollHeight;

		$('highlights').innerHTML = $('highlights').innerHTML + $('highlights').innerHTML;
		x = setTimeout( "dp_project_scroll()", 1000 );
	}
	
}

dp_init_project_scroll();


// Global alert popup

var t; // Timer

function dp_init_globalPopup( headline_text, subline_text, timer_length ) {

	clearTimeout( t );

	var content_string = "";
	
	if( headline_text && headline_text != "" )
		content_string += "<h1>" + headline_text + "</h1>";
	
	if( subline_text && subline_text != "" )
		content_string += "<h2>" + subline_text + "</h2>";

	if( $("global_popup") && $("global_popup_content") ) {
		if( content_string != "" )
			$("global_popup_content").update( content_string );
	}
	else {
		
		var global_popup_wrapper = new Element( "div", { "id": "global_popup" } );
		var global_popup_content = new Element( "div", { "id": "global_popup_content" } ).update( content_string );
		var global_popup_close = new Element( "div", { "id": "close_button" } ).update( "<a href=\"#\" onclick=\"dp_init_globalPopup(); return false;\">x</a>" );
		
		global_popup_wrapper.style.zIndex = "1000";
		global_popup_wrapper.style.display = "none";
		
		global_popup_content.appendChild( global_popup_close );
		global_popup_wrapper.appendChild( global_popup_content );
		$("page_container").appendChild( global_popup_wrapper );
	}
	
	$("global_popup").setOpacity(".9");
	Effect.toggle( "global_popup", "slide", { duration:.3 } );
	
	if( timer_length && timer_length > 0 )
		dp_init_globalPopup_timer( timer_length );

}

function dp_init_globalPopup_timer( timer_length ) {

	t = setTimeout( "dp_init_globalPopup()", timer_length );

}



// Text page functions

function as_bookmarkPage(){
	
	if( window.sidebar ) // firefox
		window.sidebar.addPanel( document.title, document.location.href, "" );
	else if( window.opera && window.print ) { // opera
		var elem = document.createElement('a');
			elem.setAttribute( 'href', document.location.href );
			elem.setAttribute( 'title', document.title );
			elem.setAttribute( 'rel', 'sidebar');
			elem.click();
	} 
	else if(document.all )// ie
		window.external.AddFavorite( document.location.href, document.title );
	else
		alert("Din webbläsare stödjer inte denna funktionalitet, var god välj \"lägg till bokmärke\" från bokmärkesmenyn");
}

function as_printPage() {

	window.print();

}

// Cache overflowing menu options

function dp_cache_overflow_init() {

	dp_cache_overflow( 'top_level' );
	dp_cache_overflow( 'sub_level' );
	dp_cache_overflow( 'tabContainer_0');

}

var menu_className = "overflowing_menu_items";
var overflowing_elements = null;
var menu_timer;

function dp_cache_overflow( container ) {
	
	overflowing_elements = new Array();
	
	if( $("sub_level") && $( container ) ) {

		var container_dimensions = $( container ).getDimensions();
		var menu_width = 250;
		
		$( container ).childElements().each(
			function( element ) {
				if( element.positionedOffset().top > container_dimensions.height )
					overflowing_elements[overflowing_elements.length] = element;
			}
		);
		
		// Build new menu
		
		if( overflowing_elements.length > 0 ) {
			
			var	drop_menu_trigger = new Element( 'a', { href: '#' } ).update("Fler...");
				drop_menu_trigger.addClassName( "overflowing_menu_trigger" );
				drop_menu_trigger.id = "trigger_" + container;
				drop_menu_trigger.onclick = dp_cache_overflow_show;
				drop_menu_trigger.onmousemove = dp_cache_overflow_active;
			
			var	drop_menu = null;
				drop_menu = new Element( 'div', { id: "menu_" + container } );
				drop_menu.addClassName( menu_className );
				drop_menu.setStyle( { width: menu_width + 'px', display: 'none' } );
			
			dp_cache_overflow_position( drop_menu, container );
			
			overflowing_elements.each( function( element ) {
				element.style.display = "block";
				drop_menu.appendChild( element );
			});
			
			// Append to container objects
			
			$( container ).appendChild( drop_menu_trigger );
			$( document.body ).appendChild( drop_menu );
			
			
		}
		
	}

}

function dp_cache_overflow_position( obj, parent_obj ) {
	
	var parent_obj_dimensions = $(parent_obj).getDimensions();
	var obj_width = parseInt( $(obj).getStyle( "width" ).replace("px","") );
	
	$(obj).clonePosition( $(parent_obj), { offsetTop: parent_obj_dimensions.height, offsetLeft: (parent_obj_dimensions.width - obj_width), setWidth: false, setHeight: false } );
	
}

function dp_cache_overflow_menu_timer() {
	
	clearTimeout( menu_timer );
	menu_timer = setTimeout( "dp_cache_overflow_hide()", 3000 );
	
}

function dp_cache_overflow_active() {
	
	$( this.id.replace("trigger_","menu_") ).observe('mouseout', function(event){
		dp_cache_overflow_menu_timer();
	});

}

function dp_cache_overflow_show() {
	
	$( this.id.replace("trigger_","menu_") ).toggle();	
	$( this.id.replace("trigger_","menu_") ).observe('mouseout', function(event){
		dp_cache_overflow_menu_timer();
	});

	
}

function dp_cache_overflow_hide() {
	
	$( menu_id ).hide();	
	
}

// Init onload

dp_cache_overflow_init();