var firstRun = true;

if( window.dhtmlHistory )
	window.dhtmlHistory.create();

function pageInit()
{	
	if( firstRun )
	{
		// alert( 'PageInit(): First run!' );
		firstRun = false;

		try
		{
			window.dhtmlHistory.initialize();
			window.dhtmlHistory.addListener(historyChange);
			window.dhtmlHistory.add( "nieuws_history", { "htype":"content", "target":"nieuws" } );
		}
		catch( e )
		{
		}
	}
	
	fixAllLinks();
	fixInputTags();

	if( typeof initRoundedCorners != 'undefined' )
		initRoundedCorners();

	// fixBannerTargets();
};

function showElement( target, show )
{
	var obj = document.getElementById( target );
	
	if( obj != null )
	{
		if( show )
		{
			obj.style.visibility = 'visible';
			obj.style.display = 'block';
		}
		else
		{
			obj.style.visibility = 'hidden';
			obj.style.display = 'none';
		}
	}
	/* Dont return anything
	else
		return false;

	return true;
	*/
}

function fixBannerTargets()
{
	var obj = document.getElementById( 'banners' );
	
	if( obj != null )
	{
		var banners = obj.getElementsByTagName( 'a' );

		for( i = 0; i < banners.length; ++i )
			banners[i].target = '_blank';
	}
}


function fixInputTags()
{
	var obj = document.getElementsByTagName( 'input' );

	for( i = 0; i < obj.length; ++i )
		if( obj[i].name == "" )
			obj[i].name = obj[i].id;

	obj = document.getElementsByTagName( 'select' );

	for( i = 0; i < obj.length; ++i )
		if( obj[i].name == "" )
			obj[i].name = obj[i].id;

	obj = document.getElementsByTagName( 'textarea' );

	for( i = 0; i < obj.length; ++i )
		if( obj[i].name == "" )
			obj[i].name = obj[i].id;
}

function fixAllLinks()
{
	var strClassName = "internal";
	var links = document.getElementsByTagName( 'a' );
	// var re = new RegExp( '\\b' + strClassName + '\\b', 'ig');

	for( i = 0; i < links.length; i++ )
	{
		// console.log( 'Testing (' + links[i].href + '): ' + links[i].className + ' Returned: (' + typeof( re.test( links[i].className ) ) + '): ' + re.test( links[i].className ) );
		
//		if( re.test( links[i].className ) == false )
		if( !links[i].className.match( strClassName ) )
		{
			links[i].target = '_blank';
			// console.log( 'Target is now: ' + links[i].target + ' for: ' + links[i].className + '(' + re.test( links[i].className ) + ')' );
		}
		else
		{
			// console.log( 'This item is left alone: ' + links[i].innerHTML );
		}
	}
}

function playVideo( mid, type )
{
	playVideoInTarget( mid, 'videoItem', type, true );
}

function playVideoInTarget( mid, target, type, focus )
{
	playVideoInTargetWithTitle( mid, target, type, focus, '' );
}

function playVideoInTargetWithTitle( mid, target, type, focus, title )
{
/*
	var playerBody = ' \
				<object id="movie_player"  classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"> \n\
					<param NAME="_cx" VALUE="11906"> \n\
					<param NAME="_cy" VALUE="9790"> \n\
					<param NAME="Movie" VALUE="http://www.youtube.com/player2.swf?video_id=' + mid + '&amp;l=60&amp;t=OEgsToPDskIVMq8nQrCUHHeLW6sXlG13&amp;s=E9241075BFA6D65B:53C662DC848F75CD"> \n\
					<param NAME="Src" VALUE="http://www.youtube.com/player2.swf?video_id=' + mid + '&amp;l=60&amp;t=OEgsToPDskIVMq8nQrCUHHeLW6sXlG13&amp;s=E9241075BFA6D65B:53C662DC848F75CD"> \n\
					<param NAME="WMode" VALUE="Window"> \n\
					<param NAME="Play" VALUE="0"> \n\
					<param NAME="Loop" VALUE="-1"> \n\
					<param NAME="Quality" VALUE="High"> \n\
					<param NAME="Menu" VALUE="-1"> \n\
					<param NAME="Scale" VALUE="NoScale"> \n\
					<param NAME="DeviceFont" VALUE="0"> \n\
					<param NAME="EmbedMovie" VALUE="0"> \n\
					<param NAME="BGColor" VALUE="FFFFFF"> \n\
					<param NAME="SeamlessTabbing" VALUE="1"> \n\
					<param NAME="Profile" VALUE="0"> \n\
					<param NAME="ProfilePort" VALUE="0"> \n\
					<param NAME="AllowNetworking" VALUE="all"> \n\
					<param NAME="AllowFullScreen" VALUE="false"> \n\
 \n\
					<embed src="http://www.youtube.com/player2.swf?video_id=' + mid + '&amp;l=60&amp;t=OEgsToPDskIVMq8nQrCUHHeLW6sXlG13&amp;s=E9241075BFA6D65B:53C662DC848F75CD" type="application/x-shockwave-flash" wmode="transparent"></embed> \n\
 \n\
				</object> \n\
';
*/
	var playerBody = '<h1>' + title + '</h1>';
	
	switch( type )
	{
		case 1:						// Youtube
			var url = 'http://www.youtube.com/v/' + mid + '&amp;fs=1&amp;enablejsapi=1&amp;playerapiid=YouTubePlayer';
			
			playerBody += ' \
				<object id="YouTubePlayer" width="300" height="230" data="' + url + '" type="application/x-shockwave-flash" standby="Loading..."> \n\
					<param name="movie" value="' + url + '"></param> \n\
					<param name="wmode" value="transparent"></param> \n\
					<param name="allowScriptAccess" value="always"></param> \n\
					<param name="allowFullScreen" value="true"></param> \n\
 \n\
					<!--[if IE]> \n\
					<embed id="YouTubePlayer" src="' + url + '" type="application/x-shockwave-flash" wmode="transparent" width="300" height="230" allowfullscreen="true"></embed> \n\
					<[endif]--> \n\
 \n\
				</object> \n\
';

			break;
		case 2:						// Google Video
			playerBody += ' \
				<div>Not implemented yet!</div> \n\
';
			break;
		case 3:						// Video
//				<embed type="application/x-mplayer2" pluginspage="http://www.microsoft.com/Windows/MediaPlayer/" src="http://wastedtv.nl/hpp.wmv" name="MediaPlayer" autostart="1" allowscan="0" enablecontextmenu="0" showcontrols="1" showpositioncontrols="0" enablepositioncontrols="0" showtracker="1" sendplaystatechangeevents="0" animationatstart="0" showstatusbar="1" volume="0" displaysize="0" height="314" width="319">

			playerBody += ' \
				<object type="video/x-ms-wmv" data="' + mid + '" width="300" height="300"> \n\
					<param name="src" value="' + mid + '" /> \n\
					<param name="ShowControls" value="1" /> \n\
					<param name="AnimationAtStart" value="1" /> \n\
					<param name="ShowStatusBar" value="1" /> \n\
					<param name="ShowDisplay" value="0" /> \n\
					<param name="DefaultFrame" value="Slide" /> \n\
					<param name="Autostart" value="0" /> \n\
					<param name="TransparentAtStart" value="0" /> \n\
					<param name="stretchToFit" value="0" /> \n\
					<param name="DisplaySize" value="1" /> \n\
					<param name="AllowChangeDisplaySize" value="1" /> \n\
					<param name="WindowlessVideo" value="0" /> \n\
					<param name="InvokeURLS" value="1" /> \n\
				</object> \n\
';

			break;
		
		case 4:
			var so = new SWFObject('flvplayer/flvplayer.swf','mpl','300','230','7');
			so.addParam('allowfullscreen','true');
			so.addParam('allowscriptaccess','always');
			so.addVariable('file', mid );
			so.addVariable('height','230');
			so.addVariable('image','Images/testbeeld.jpg');
			so.addVariable('width','300');
			so.addVariable('javascriptid','mpl');
			so.addVariable('enablejs', 'true');
			so.addVariable('location','flvplayer/flvplayer.swf');
			so.addVariable('autostart','false');
			so.write( target );
			
			playerBody = '';
			
			break;

		case 5:
			playerBody += ' \
				<object codebase="http://go.divx.com/plugin/DivXBrowserPlugin.cab" height="230" width="300" classid="clsid:67DABFBF-D0AB-41fa-9C46-CC0F21721616"> \n\
					<param name="autoplay" value="false"> \n\
					<param name="src" value="' + mid + '" /> \n\
					<param name="custommode" value="Stage6" /> \n\
					<param name="showpostplaybackad" value="false" /> \n\
 \n\
					<embed type="video/divx" src="' + mid + '" pluginspage="http://go.divx.com/plugin/download/" showpostplaybackad="false" custommode="Stage6" autoplay="false" height="230" width="300" /> \n\
				</object> \n\
';
			break;
		
		case 6:
			playerBody += ' \
				<embed \n\
					src="' + mid + '" \n\
					allowFullScreen="true" \n\
					width="300" \n\
					height="230" \n\
					bgcolor="#000000" \n\
					type="application/x-shockwave-flash" \n\
					pluginspage="http://www.macromedia.com/go/getflashplayer"> \n\
				</embed> \n\
';
			break;
		default:
			break;
	}
				
	// var obj = document.getElementById( 'videoItem' );
	var obj = document.getElementById( target );
	
	if( obj != null )
	{
		if( playerBody != '' )
			obj.innerHTML = playerBody;
		
		showElement( obj.parentNode.id, true );
		
		if( focus )
			bytefx.scroll( obj, 50, function() {} );
	}
}

/*** Add & Remove event handlers, browser safe ***
 *
 ** useCapture
 *
 * As to the true or false that is the last argument
 * of addEventListener, it is meant to state whether
 * the event handler should be executed in the
 * capturing or in the bubbling phase. If you’re
 * not certain whether you want capturing or bubbling,
 * use false (bubbling).
 */
function addEvent(obj, evType, fn, useCapture)
{
  if (obj.addEventListener){
    obj.addEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be attached");
  }
}

function removeEvent(obj, evType, fn, useCapture)
{
  if (obj.removeEventListener){
    obj.removeEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.detachEvent){
    var r = obj.detachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
}

function openPage( target )
{
	var targeturl = '';
	
	switch( target )
	{
		case 'nieuws':
			targeturl = 'nieuws.phtml';
			break;
		
		case 'fotos':
			targeturl = 'fotos.phtml';
			break;

		case 'downloads':
			targeturl = 'downloads.phtml';
			break;

		case 'links':
			targeturl = 'links.phtml';
			break;

		case 'clixs':
			targeturl = 'clixs.phtml';
			break;

		case 'contact':
			targeturl = 'contact.phtml';
			break;

		case 'advertise':
			targeturl = 'adverteren.phtml';
			break;

		case 'archive':
			targeturl = 'archief.phtml';
			break;

		default:
			alert( 'No page: "' + target + '".' );

	}
	
	// var contentframe = frames['contentframe'];
	
	if( targeturl != '' )
	{
		window.dhtmlHistory.add( target + '_history', { "htype":"content", "target":target } );

		// contentframe.location = targeturl;
		getContent( targeturl );
	}
}

function historyChange(newLocation, historyData) {
	/*
	var historyMsg = (typeof historyData == "object" && historyData != null
		? historyStorage.toJSONString(historyData)
		: historyData
	);
	*/
	// alert( 'Navigating: ' + newLocation + ' \"' + historyMsg + '\"' );
	if( newLocation == null || historyData == null )
		return;

	switch( historyData.htype )
	{
		case 'content':
			openPage( historyData.target );
			break;

		default:
			break;
	}
};

function showPluginExample( target )
{
	var version = 'ff';
	
	switch( BrowserDetect.browser )
	{ 
		case 'Explorer':
			if( BrowserDetect.version == 7 )
				version = 'ie7';
			else
				version = 'ie';
			break;
		case 'Firefox':
			version = 'ff';
			break;
		case 'Opera':
			version = 'op';
			break;
		default:
			version = 'ff';
			break;
	}

	var obj = document.getElementById( 'pluginExample' );
	
	obj.style.backgroundImage = 'url(Images/stage6_warning_' + version + '.png)';

	showElement( obj.id, true );
}

	
function addCSSClass( target, className )
{
	var obj = document.getElementById( target );
	
	if( obj != null )
	{
		if( !obj.className.match( className ) )
			obj.className += ' ' + className;
	}
}

function removeCSSClass( target, className )
{
	var obj = document.getElementById( target );
	
	if( obj != null )
	{
		if( obj.className.match( className ) )
			obj.className = obj.className.replace( className, '' );
	}
}

function validateEMailAddress( email )
{
	return email.match( "^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$" );
};

function fullscreenYoutube()
{
	var player = document.getElementById( 'YouTubePlayer' );
	
	if( player != null )
	{
		player.style.position = 'fixed';
		player.style.top = 0;
		player.style.left = 0;
		player.style.width = '100%';
		player.style.height = '100%';
		player.style.zIndex = 999999;
	}
	else
		alert( 'No youtube video found!' );
}

// Allow for firefox logging statements, even in browsers that don't support it!
if (!("console" in window) || !("firebug" in console))
{
    var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
    "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];

    window.console = {};
    for (var i = 0; i < names.length; ++i)
        window.console[names[i]] = function() {}
}
