var s_act_page;
var s_search;
var b_neues_licht_slided = false;
var s_flash_head = 'flash_head';
var s_map = false;
var o_navigation_sub = false;

$( document ).ready
(
	sys_document_ready
);


function sys_document_ready() 
{

	form_submit();

	catalogURL_check();
	
	form_download();
	navigation_overlay();
	weitere_kontakt_toggle();
	choose_switch();
	language_switch();
	news_toggle_assign();
	replace_flash_header();
	if ( window.location.pathname.indexOf('sites/trilux/no',5) > 0 || 
             window.location.pathname.indexOf('sites/trilux/it',5) > 0 || 
             window.location.pathname.indexOf('sites/trilux/hu',5) > 0 || 
             window.location.pathname.indexOf('sites/trilux/pl',5) > 0 || 
             window.location.pathname.indexOf('sites/trilux/es',5) > 0 || 
             window.location.pathname.indexOf('sites/trilux/cz',5) > 0  
        ) {
		// alte Version neues_licht flash
		neues_licht_slide_assign();
        }
	carousel_projekte();
	carousel_produkte();
	ie_disable_bgimage_bug();
	search();
	navigation();
}

function form_submit()
{
	$( 'form' ).submit
	(
		function()
		{
			if( window.location.host == 'web5.plan-net.de' )
			{
				alert( 'debug | no form-function on static pages' );
				return false;
			}
		}
	);
}

function catalogURL_check()
{
	var s_catalogURL = deconcept.util.getRequestParameter( 'catalogURL' );
	
	if( !s_catalogURL )
		return false;
		
	s_catalogURL = decodeURIComponent( s_catalogURL );
	$( 'iframe', $( '#content' ) ).attr( 'src', s_catalogURL );
}



function form_download()
{
	$( 'form.dl' ).unbind( 'submit' );
	$( 'form.dl' ).submit
	(
		function()
		{
			var el = $( this );
			var s_id = el.attr( 'id' ).match( /^dl\-(.*)/ )[1];
			
			var o_sel = document.getElementById( 'dl_' + s_id );
			var s_path = o_sel[o_sel.selectedIndex].value;
			
			if( !s_path || s_path == -1 )
			{
				o_sel.focus();
				return false;
			}
			
			window.open( s_path );
			
			return false;
		}
	);
}
	
function weitere_kontakt_toggle()
{
	$( '#weitere-kontakt li' ).hover
	(
		function()
		{
			var o_link = $( 'a', $( this ) );
			o_link.toggleClass( 'hide' );
		},
		function()
		{
			var o_link = $( 'a', $( this ) );
			o_link.toggleClass( 'hide' );
		}
	);
}

function choose_switch()
{
	//return false;
	
	$( '#choose select' ).change
	(
		function()
		{
			var s_url = this[this.selectedIndex].value;
			if( s_url != -1 )
			{
				window.location.href = s_url;
			}
		}
	);
}

function language_switch()
{
	$( '#language select' ).change
	(
		function()
		{
			var s_url = this[this.selectedIndex].value;
			if( s_url != -1 )
			{
				window.location.href = s_url;
			}
		}
	);
}

function news_toggle_assign()
{
	$( '#news-tab a' ).unbind( 'hover' );
	$( '#news-tab a' ).hover
	(
		function()
		{
			this.blur();
			news_toggle( 'news' );
			return false;
		},
		function()
		{
		}
	);
	
	$( '#messe-tab a' ).unbind( 'hover' );
	$( '#messe-tab a' ).hover
	(
		function()
		{
			this.blur();
			news_toggle( 'messe' );
			return false;
		},
		function()
		{
		}
	);
}

function news_toggle( s_do )
{
	if( s_do == 'news' )
	{
		$( '#news-tab' ).addClass( 'active' );
		$( '#news' ).addClass( 'active' );
		$( '#messe-tab' ).removeClass( 'active' );
		$( '#messe' ).removeClass( 'active' );
	}
	else if( s_do == 'messe' )
	{
		$( '#news-tab' ).removeClass( 'active' );
		$( '#news' ).removeClass( 'active' );
		$( '#messe-tab' ).addClass( 'active' );
		$( '#messe' ).addClass( 'active' );
	}
}

function replace_flash_header()
{
	flash_header( 'head', s_act_page, 128 );
}

/*
function flash_header( s_id, s_actPage, i_height )
{
	var o_so = new SWFObject( 'img/head/main.swf', s_flash_head, 996, i_height, 8, '#070709' ); 
	o_so.addParam( 'salign', 'TL' );
	o_so.addParam( 'scale', 'noscale' );
	o_so.addParam( 'base', 'img/head/' );
	o_so.addVariable( 'actPage', s_actPage );
	o_so.write( s_id );
}
*/

function flash_header( s_id, s_actPage, i_height, s_file )
{
	if( !s_file )
	{
		s_file = 'img/head/main.swf';
	}
		
	var o_so = new SWFObject( s_file, s_flash_head, 996, i_height, 8, '#070709' ); 
	o_so = false;
	if( o_so )
	{
		o_so.addParam( 'salign', 'TL' );
		o_so.addParam( 'scale', 'noscale' );
		o_so.addParam( 'base', 'img/head/' );
		o_so.addVariable( 'actPage', s_actPage );
		o_so.write( s_id );
	}
	else if( s_file.match( /\/head\./ ) )
	{
		$( '#' + s_id ).html( '<a href="http://www.macromedia.com/go/getflashplayer" target="_blank"><img src="img/head/fallback.jpg" alt=""></a>' );
	}
}

function neues_licht_slide_assign()
{
	$( '#nav-neueslicht a' ).unbind( 'click' );
    $( '#nav-neueslicht a' ).click
	(
		function() 
		{
			this.blur();
			neues_licht_slide();
			return false;
		}
	);

	$( 'ul#sitemap .nav-neueslicht a' ).unbind( 'click' );
	$( 'ul#sitemap .nav-neueslicht a' ).click
	(
		function()
		{
			this.blur();
			neues_licht_slide();
			return false;
		}
	);

}


function neues_licht_slide()
{
// from srcipt.js
	if( b_neues_licht_slided )
		return false;

	var o_head = $( '#head' );
		
	document.title = 'Neues Licht - Trilux';
	
	// highlight 'neues-licht'
	$( '#nav-neueslicht a' ).wrap( '<strong></strong>' );
	
	// remove highlight from last active point
	var o_li = $( '#nav-' + s_act_page );
	var o_stro = $( 'strong', o_li );
	o_stro.before( o_stro.html() );
	o_stro.remove();
	
	$( '#navigation-overlay' ).addClass( 'hide' );
	$( '#navigation' ).addClass( 'slide' );
		
	o_head.css( "height",  o_head.height() + 'px' );	
	
	b_neues_licht_slided = !b_neues_licht_slided;

	var i_height = b_neues_licht_slided
	             ? 272
				 : 128;
	
	if( jQuery.browser.msie )
	{
		i_height += 16;
	}
	
	var o_slide_overlay = $( '#slide-overlay' );
	
	if( !o_slide_overlay.attr('id') )
	{
		$( '<div id="slide-overlay">&nbsp;</div>' ).insertAfter( '#navigation' );
		var o_slide_overlay = $( '#slide-overlay' );
	}
	o_slide_overlay.show();
	
	var i_slide_height = i_height;
	i_slide_height -= 96;
	
	o_slide_overlay.height( $(document).height() - i_slide_height );
	
	$( '#' + s_flash_head ).remove();
	
	$( 'body' ).css( 'background-image', 'url( img/bg/neues-licht/head.jpg )' );
	$( 'body' ).css( 'background-repeat', 'no-repeat' );
	
	$( 'body' ).append( '<div id="flash_header"><a href="./" style="display: block; height: 128px; width: 996px;">&nbsp;</a></div>' );
	
	o_head.animate
	( 
		{ 
			height: i_height 
		}, 
		'slow'
	);
	
}


function carousel_produkte()
{
	$( '#produkte-main' ).jcarousel
	(
		{
			itemVisible: 1,
			scrollAnimation: 'slow',
			noButtons: true,
			itemFirstInHandler: carousel_produkte_itemFirstInHandler
		}
	);
}

function carousel_produkte_itemFirstInHandler( carousel, li, idx, state ) 
{
    if( state != 'init' )
        return;

    var cr = carousel;
	
    $( '#produkte-related li' ).click
	(
		function() 
		{
			var i_id;
			var s_class = $( this ).attr( 'class' );
			
			if( s_class )
				i_id = s_class.match( /item-(\d+)/ )[1];
			
			if( i_id )
			{
				cr.scroll( parseInt( i_id ) );
			}
			
			return false;
		}
	);
}

function carousel_projekte()
{
	$( '#projekte-main' ).jcarousel
	(
		{
			itemVisible: 1,
			scrollAnimation: 'slow',
        		wrap: 'both',
        		wrapPrev: 'both',
			itemFirstInHandler: carousel_projekte_itemFirstInHandler
		}
	);
	
	$( '#projekte-related' ).jcarousel
	(
		{
			orientation: 'vertical',
			itemVisible: 3,
        		wrap: 'both',
        		wrapPrev: 'both',
			scrollAnimation: 'slow'
		}
	);
}

function carousel_projekte_itemFirstInHandler( carousel, li, idx, state ) 
{
    if( state != 'init' )
        return;

    var cr = carousel;
	
    $( '#projekte-related li' ).click
	(
		function() 
		{
			var i_id;
			var s_class = $( this ).attr( 'class' );
			
			if( s_class )
				i_id = s_class.match( /item-(\d+)/ )[1];
			
			if( i_id )
			{
				cr.scroll( parseInt( i_id ) );
			}
			
			return false;
		}
	);
}

function ie_disable_bgimage_bug()
{
	try 
	{
		document.execCommand( "BackgroundImageCache", false, true );
	} 
	catch( err )
	{}
}

function search()
{
	var o_q = $( '#q' );
	
	if( o_q.size() == 0 )
	{
		return false;
	}
	
	var s_q = o_q.prev().html().toUpperCase();
	s_q = s_q.substr( 0, s_q.length - 1 );
	
	o_q.unbind( 'focus' );
	o_q.unbind( 'blur' );
	
	o_q.focus
	(
		function()
		{
			if( $( this ).val().toLowerCase() == s_q.toLowerCase() )
			{
				$( this ).val( '' );
			}
		}
	);

	o_q.blur
	(
		function()
		{
			if( $( this ).val() == "" || 
			    $( this ).val().toLowerCase() == s_q.toLowerCase() )
			{
				$( this ).val( s_q );
			}
		}
	);

	// init
	o_q.val( s_q );
}

function navigation_overlay()
{
	$( '<div id="navigation-overlay" class="hide">&nbsp;</div>' ).insertAfter( '#navigation' );
}

function navigation()
{
	/*
	$( '#navigation' ).hover
	(
		function()
		{
			navigation_do( true );
		},
		function()
		{
			navigation_do( false );
		}
	);
	//*/
	
	$( '#navigation li' ).hover
	(
		function()
		{
			if( $( this ).attr( 'id' ) )
				navigation_do( true );
		},
		function()
		{
			if( $( this ).attr( 'id' ) )
				navigation_do( false );
		}
	);
	$( '#navigation #nav-neueslicht' ).unbind( 'hover' );

	// IE FIX
	if( jQuery && jQuery.browser.msie )
	{
		$( '#navigation li' ).hover
		(
			function()
			{
				$( this ).addClass( 'hover' );
				$( this ).addClass( 'onhover' );
			},
			function()
			{
				$( this ).removeClass( 'hover' );
				$( this ).removeClass( 'onhover' );
			}
		);
	}
	
	$( '#navigation .sub' ).click
	(
		function()
		{
			var el = $( this ).parent();
			
			if( o_navigation_sub )
			{
				o_navigation_sub.toggleClass( 'show' );
			}
			o_navigation_sub = el;
			
			el.toggleClass( 'show' );
		}
	);
}

function navigation_do( b_show )
{
	var o_nav_ov = $( '#navigation-overlay' );
	
	if( o_nav_ov.size() == 0 )
	{
		$( '<div id="navigation-overlay" class="hide">&nbsp;</div>' ).insertAfter( '#navigation' );
		var o_nav_ov = $( '#navigation-overlay' );
	}
	
	//hide dropdown on navigation
	if( jQuery && jQuery.browser.msie )
	{
		$( '#choose' ).css
		( 
			'visibility', 
			( $( '#choose' ).css( 'visibility' ) == 'inherit' || $( '#choose' ).css( 'visibility' ) == 'visible' )  
				? 'hidden' 
				: 'visible' 
		);	
	}
	
	o_nav_ov.toggleClass( 'hide' );
}

function map_highlight( s_id, b_hl )
{
	var a_id = s_id.match( /_([\w\d-]+)_([\w\d-]+)$/ );
	var i_id = a_id[1];
	var s_part = a_id[2];
	
	var o_map = $( '#map__' + i_id );
	
	if( !s_map )
	{
		s_map = o_map.attr( 'src' );
	}
	
	o_map.attr( 'src', b_hl ? o_map.attr( 'src' ).replace( /\/[\w-]+\.jpg$/, '/' + s_part + '.jpg' ) : s_map );
	
}