var basehref = $('base').attr('href'); // set with jQuery $('base').attr('href');

var ShBoxOptions = {
		language:				'en',
		counterType:			'skip',//'default',//'skip',
		overlayColor:			'#fff',
		overlayOpacity:	   		'0.8',
		initialHeight:			'450',//'160',
		initialWidth:			'550',//'320',
		resizeDuration:			'0.2',//'0.55',
		fadeDuration:			'0.15',//'0.35',
		animSequence:			'sync',//'wh',
		handleOversize:			'resize',//'drag',
		continuous:				true,
		onOpen:					shadowboxMod,
		players:				["iframe","img"]
};
// Shadowbox start
Shadowbox.init(ShBoxOptions);

// after DOMload:
$(function() {

	// override default plugin settings
	$.fn.media.defaults.flvPlayer = 'gui/swf/anarchy_media/flvplayer.swf';//flowplayer-3.2.2.swf';//flvplayer.swf';
	$.fn.media.defaults.params = { wmode: 'transparent' };
	$.fn.media.mapFormat('mpg', 'winmedia');

	// media
	$('a.media',$('#content'))
	.each(function(){
		embedMedia($(this),false);
	});
	
	// fade images
	if( !$('#outer.type_s690').length ){
		
	new cycleFromDir({
		'delay':		-2500,
		'timeout':		6000,
		'speed':		2000,
		'ImgHolder':	'#headerimages', // parent van image node
		'XHRpath':		basehref+'gui/headerimages/random.php', // XHR request JSON 'filelist'
		'XHRreq':		{
				//folder:'s690',
			filelist:"true"
		}, // GET vars
		'randDir':		basehref + 'gui/headerimages/' // imgs directory (vóór XHR result)
	});
	}
	
	// table styling in JS
	$('#content table')
	.not('.projectstable')
	.each(function(){
		
		var thetable = $(this);
		
		if( $('tr td',thetable).length = 2){
			
			$('td:first',
				$('tr',
					$(thetable)
					.removeAttr('width')
					.removeAttr('style')
					.addClass('twocolumn')
				)
			)
			.addClass('left');
		}
	});
	
	// shipdock wil dat alleen de content scrolled.... christ...
	contentScroller();
	
	// panels clickable
	$('.home #right div')
	.each(function(){
	
		var url =	$('a:first[href!="#"]',$(this))
					.attr('href');
		
		if( $('a:first[href!="#"]',$(this)).length ){
		
			$(this)
			.css('cursor','pointer')
			.click(function(){
				
				var target = $('a:first[href!="#"]',$(this)).attr('target');
				
				if(target == '_blank'){
					window.open(url);
				} else {
					window.location.href = url;
				}
				
				return false;
			});
		}
		
	});

	
	
	/// mooooooi mooi mooi moi moi....
	$(".type_s690 #left #navigation ul li a").textShadow({
		x:      1, 
		y:      1, 
		radius: 1,
		color:  "#ffffff"
	});

});

// on resize
$(window).resize(function(){
	contentScroller();
});
/*
	Functions
*/

function embedMedia(mediaAnchor,autoplay){
	
	var theurl = $(mediaAnchor).attr('href');
	var extension = theurl.split('.').pop().toLowerCase();
	var playing = autoplay;
			
	$(mediaAnchor)
	.css({'margin-top':'12px'})
	.media({ 
		width:     462, 
		height:    347, 
		autoplay:  autoplay, 
		flashvars:{
			'video':theurl,
			'autoplay':autoplay,
			'autoStart':autoplay
		},
		params:	{
			'z-index':1,
			'wmode':'transparent',
			'controller':false,
			'ShowControls':false,
			'ShowStatusBar':true,
			'ShowDisplay':true,
			'windowlessVideo':true,
			'uiMode':'none',
			'autoStart':autoplay
		},
		attrs:	{
			'scale':'aspect',
			'class':'movie',
			'z-index':1,
			'wmode':'transparent',
			'controller':false,
			'ShowControls':false,
			'ShowStatusBar':true,
			'ShowDisplay':true,
			'windowlessVideo':true,
			'uiMode':'none',
			'autoStart':autoplay
		},
		caption:   false // supress caption text 
	}),
	$('.video.embed.media')
	.mousedown(function(){
		
		if(!playing || playing == false){
		
			playing = true;
			
			$('.mask',this).hide();
			
			if(extension == 'flv'){
				
				var newAnchor = $('<a/>').attr('href',theurl);
				$(this).html(newAnchor);
				
				embedMedia(newAnchor,true);
				
				$('.mask',this).remove();
				
			} else if( $('embed').length ){
				$('embed',$(this))[0].Play();
			} else if( $('object').length ){
				$('object',$(this))[0].controls.play();
			}
			
		} else {
		
			playing = false;
			
			if(extension == 'flv'){
				
				var newAnchor = $('<a/>').attr('href',theurl);
				$(this).html(newAnchor);
				
				embedMedia(newAnchor,false);
								
			} else if( $('embed').length ){
				$('embed',$(this))[0].Stop();
			} else if( $('object').length ){
				$('object',$(this))[0].controls.stop();
			}
			
			$('.mask',this).show();
		}
		
	})
	.prepend( 
		$('<img/>')
		.attr('src','gui/backgrounds/videomask.png')
		.css({'background-image':function(){
			return "url('"+ $('.video.embed.media').prev('.videothumb').attr('src') +"')";
		}})
		.addClass('mask')
	);
	
	$('.video.embed.media').css({'display':'inline-block'});
}

function winopen(url, winname, width, height)
{
	/*
	var popup = window.open(url,winname,'toolbar=no,location=no,menubar=no,scrollbars=yes,width='+width+',height='+height+',resizable=yes,status=no');
	popup.focus();
	*/
	
    Shadowbox.open({
        content:    url,
        player:     "iframe",
        title:      winname == 'imgpop' ? ' ' : winname,
       // onFinish:	function(e){},
        height:     height == '300' ? '555' : height,
        width:      width == '300' ? '660' : width
    });
}

// shipdock wil dat alleen de content scrolled.... christ...
function contentScroller()
{
	/*
	$('table',$('#outer[class!="home"] #content',$('body,html').css('overflow','hidden'))
		.css({
			'overflow':'auto'
		}).height( ($(window).height() -245) + 'px')
	).width('95%');
	*/
	
	$('table',$('#outer[class!="home"][class!="type_s690"] #content')
		.css({
			'overflow':'auto'
		}).height( ($(window).height() -245) + 'px')
	);//.width('95%');
	
};
	
// modify shadowbox na load
function shadowboxMod()
{

	$('div a.menu_l').hide();
	
	// close button in titlebalk
	$('#sb-title-inner')
	.after(
		$('<a/>')
		.html(
			$('<img/>')
			.attr('src',basehref + 'gui/shadowbox/close_white.png')
			.css({
				
			})
		)
		.css({
			'margin':'5px',
			'cursor':'pointer',
			'z-index':'30',
			'float':'right',
			'position':'absolute',
			'top':'0px',
			'right':'0px',
			'display':'block'
		})
		.click(function(){
			Shadowbox.close();
		})
	);
	
}

function wc()
{
	if(window.console)
	{
		for(var i = 0; i < arguments.length; i++)
		{
			window.console.log(arguments[i]);
		}
	}
}
