// --------------------------------------------------
// init flv player
// --------------------------------------------------
function init_flv_player()
{
	// only when the coutnry dd ist on page
	if( $( 'div.flvplayer' ).length != 0 )
	{
		$( 'div.flvplayer img' ).each( function () {
			// hide image
			jQuery( this ).hide();
			
			// data fwom weblication
			if( jQuery( this ).attr( 'width' ) != undefined )
			{
				playerWidth=	jQuery( this ).attr( 'width' );
			} else
			{
				playerWidth=	"480";
			}		
			if( jQuery( this ).attr( 'height' ) != undefined )
			{
				playerHeight=	jQuery( this ).attr( 'height' );
			} else
			{
				playerHeight=	"290";
			}					
			fpFileURL=		jQuery( this ).attr( 'src' );
			
			// player definition
			colorScheme=	"8ecff5";		// ACE hellblau
//			colorScheme=	"FE9900";		// ACE orange
			
			playerFile=		"/global/player/mcmp_0.8.swf";
			mcflashvars = 'fpFileURL='+fpFileURL;
			mcflashvars += '&colorScheme='+colorScheme;

			// generate player code
			var str='';
			str+='<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http:\/\/download.macromedia.com\/pub\/shockwave\/cabs\/flash\/swflash.cab#version=7,0,19,0" width="'+playerWidth+'" height="'+playerHeight+'">\n';
			str+='<param name="movie" value="'+playerFile+'">';
			str+='<param name="allowScriptAccess" value="always">';
			str+='<param name="quality" value="high">';
			str+='<param name="allowFullScreen" value="true">';
			str+='<param name="FlashVars" value="'+mcflashvars+'">\n';
			str+='<embed src="'+playerFile+'" width="'+playerWidth+'" height="'+playerHeight+'" quality="high" allowFullScreen="true" allowscriptaccess="always" pluginspage="http:\/\/www.macromedia.com\/go\/getflashplayer" type="application\/x-shockwave-flash" FlashVars="'+mcflashvars+'"><\/embed>\n';
			str+='<\/object>';
			
			// write into html
			jQuery( this ).parent().children( 'div.player' ).html( str );
		});
	}
}

// --------------------------------------------------
// handle country dd
// --------------------------------------------------
function init_country_dd()
{
	// only when the coutnry dd ist on page
	if( $( '#countrydd' ).length != 0 )
	{
		// redirect page
		$( '#countrydd' ).change( function(){
			var newURL= $( '#countrydd option:selected' ).attr( 'value' );
			parent.location= newURL;
		});
		
		// init on first element
		$( "#countrydd option:first" ).attr( "selected", "selected" );

	}
}


// --------------------------------------------------
// hover fake sliders
// --------------------------------------------------
function init_sliders()
{
	// toggle visibility on click
	$( 'h5.toggle' ).click( function () {
		toggleElementID= jQuery( this ).attr( 'rel' );
		
		if( $( this ).hasClass( 'selected' ) )
		{
			$( '#' + toggleElementID ).fadeOut( 'slow' );
		} else
		{
			$( '#' + toggleElementID ).slideDown( 'slow' );
		}
		
		$( this ).toggleClass( 'selected' );
	});
	
	
	
	// hide all on init
	$( 'h5.toggle' ).each( function () {
		toggleElementID= jQuery( this ).attr( 'rel' );
		$( '#' + toggleElementID ).hide();
	});
}


// --------------------------------------------------
// hide all tags
// --------------------------------------------------
function hide_tags()
{
	$("#content hr + p").css( "display", "none" );
	$("#content hr ").css( "display", "none" );
}

// --------------------------------------------------
// init the lightbox
// --------------------------------------------------
function init_lightbox()
{
	$('a[@name*=zoom]').lightBox({
		overlayBgColor:'#FFF',
		overlayOpacity:0.8,
		imageBlank:'/global/img/lightbox/blank.gif',
		imageLoading:'/global/img/lightbox/loading.gif',
		imageBtnClose:'/global/img/lightbox/close.gif',
		imageBtnPrev:'/global/img/lightbox/prev.gif',
		imageBtnNext:'/global/img/lightbox/next.gif',
		containerBorderSize:10,
		containerResizeSpeed:350,
		txtImage:'',
		txtOf:''
	});
}

// --------------------------------------------------
// init file uploader
// --------------------------------------------------
function init_file_uploader()
{
	//
	// upload handler
	//
	if( $( '#uploadGo' ).length != 0 )
	{
		new AjaxUpload(	'uploadGo',
		{
			action:		'/global/file_upload_handler.php',
			data:		$( '#uploadGo' ).attr( 'rel' ),				// session id
			name:		'userfile',
		
			onSubmit:	function( fileURL, ext )
			{
				$( '#uploadStatus' ).show();
			},
		
			onComplete:	function( fileURL, response )
			{
				$( '#uploadStatus' ).hide();
				$( '#uploadList' ).append( '<li rel="' + response + '">' + fileURL + '&nbsp; --> &nbsp;<a href="#" rel="' + response + '" class="uploadRemove">löschen</a></li>' );		
				init_file_remover();
			}
		});
	}
}

// --------------------------------------------------
// init file remover
// --------------------------------------------------
function init_file_remover()
{
	$( 'a.uploadRemove' ).click( function ()
	{
		var sid= $( '#uploadGo' ).attr( 'rel' );
		
		// get file to remove	
		fileURL= $( this ).attr( 'rel' );
		
		// store parent li
		var li= $( this ).parent();
		
		// remove file
		$.ajax({
			type: "POST",
			url: "/global/file_upload_remover.php",
			data: "sid=" + sid + "&file=" + fileURL,
			cache: false,
			success: function( msg )
			{
				if( msg == "1" )
				{
					// remove file from list
					$( "li[rel='" + fileURL + "']" ).empty().remove();				
				}
			}
		});
		return false;
	});
}


// --------------------------------------------------
// switch languages
// --------------------------------------------------
function switchLang()
{
	var newUrl= $( "#langSelect :selected" ).val();
	window.location.href= newUrl;
}

// --------------------------------------------------
// highlight search term
// --------------------------------------------------
jQuery.fn.extend({
	highlightSearchTerm: function( search )
	{
		var regex= new RegExp( "(<[^>]*>)|(" + search.replace(/([-.*+?^${}()|[\]\/\\])/g,"\\$1") + ")", "ig" );
		return this.html( this.html().replace( regex, function( a, b, c )
		{
			return ( a.charAt( 0 ) == "<" ) ? a : "<span class=\"searchHighlight\">" + c + "</span>";
      	}));
	}
});


// --------------------------------------------------
// init
// --------------------------------------------------
//$.noConflict();
$( document ).ready( function()
{
		if( $( '#highlightSearch' ).length != 0 )
		{
			var term= $( '#highlightSearch' ).attr( 'rel' );
			$( 'div.container' ).highlightSearchTerm( term, 1 );
		}
		
		init_lightbox();
		init_sliders();
		init_country_dd();
		init_file_uploader();
		init_file_remover();
		init_flv_player();

		
});


