// written by Matthias on 21/10/2008
// edited by Matthias on 13/02/09
var mediaNumber = 0;
var mediaItem = [];
var mediaSource = '';
var mediaContent = '';
var mediaCaption = '';
//developer customisation controls
	//video player 
		videoWMode = '';
		videoBGColor = '';
	//swf player
		swfWMode = '';
		swfBGColor = '';
	//background of the holder	
	mediaBG = '';
$(document).ready(function() {
	$('body').append('<div id="mediaFader"></div><!-- media fader --><div id="mediaContainer"><div id="mediaHolder"><div id="mediaButtons"><div id="mediaClose">Close X</div><!-- media close --><div id="mediaNext">Next <span class="mediaDir">&raquo;</span></div><!-- media next --><div id="mediaBack"><span class="mediaDir">&laquo;</span> Back</div><!-- media back --></div><!-- media buttons --><div id="mediaFile"></div><!-- media file --></div><!-- media holder --></div><!-- media container -->');
	if($.browser.msie && $.browser.version == 6) {
		$('#mediaFader, #mediaContainer').css({
			position: 'absolute'
		});
		$('body').css('overflow-y','auto');
		$('html').css('overflow-y','hidden');
		$('#nav li').each(function(){ $(this).css('position','static'); });
	}
	$('#mediaFader').hide();
	$('#mediaContainer').hide();
	$('#mediaDownload').hide();
	
	function showMedia() {
		if($.browser.msie && $.browser.version == 6) {
			$('select').each(function(){
				$(this).hide();
			});
		}

		/*source = source2;
		//console.log(source);
		
		mediaNumber = currentNumber;*/
		
		//emptying out content which may be in the gallery already
		$('#mediaFile').empty();
		$('#mediaBack').empty();
		$('#mediaNext').empty();

		$('#mediaFader').fadeIn(500);
		$('#mediaContainer').fadeIn(500);


		//hide the next and back buttons, depending on what file number you are on
		if(mediaNumber <= 0) {
			$('#mediaBack').css('cursor','default'); 
			$('#mediaBack').empty();
		} else {
			$('#mediaBack').css('cursor','pointer'); 
			$('#mediaBack').append('<span class="mediaDir">&laquo;</span> Back');
		}
		if(mediaNumber >= mediaItem.length-1) {
			$('#mediaNext').css('cursor','default');
			$('#mediaNext').empty(); 
		} else {
			$('#mediaNext').css('cursor','pointer'); 
			$('#mediaNext').append('Next <span class="mediaDir">&raquo;</span>');
		}
		
		//getting the bits ans pieces
		mediaSource = '';
		mediaSource = $('#guide').attr('href');

		//getting the mine type of the file (ie. img, flv, mp3, swf or other)
		mediaSourceSplit = mediaSource.split('.');
		fileType = mediaSourceSplit[mediaSourceSplit.length-1];
		
		//getting window dimensions for image re-sizing.  Im removing 200px so there is a nice gap around the container box when image is resized.
		windowWidth = ($(window).width())-200;
		windowHeight = ($(window).height())-200;
		
		resizeWindow();	
		
		/*console.log('resized images are now:::.....');
		console.log('imgWidth: '+imgWidth);
		console.log('imgHeight: '+imgHeight);*/
				
		//console.log('-------------------------------------------------------');
	}
	
	function resizeWindow() {
		fileWidth = 900;
		fileHeight = 600;
		$('#mediaFile').flash({
			src: mediaSource,
			width: fileWidth,
			height: fileHeight,
			wmode: swfWMode,
			bgcolor: swfBGColor
		});
		


		mediaContentWidth = 640;	
		
		mediaBtnsHeight = $('#mediaButtons').innerHeight();
		holderPaddingTB = $('#mediaHolder').innerHeight() - $('#mediaHolder').height();
		holderPaddingLR = $('#mediaHolder').innerWidth() - $('#mediaHolder').width();
		//if there is no content, dont add the content width.  this will allow the image to span full with of the holder
		holderWidth = 900;
		holderHeight = 650;
		offsetTop = -(holderHeight / 2)-(holderPaddingTB/2);
		offsetLeft = -(holderWidth / 2)-(holderPaddingLR/2);
		
		
		//animate the box
		$('#mediaHolder').animate({
			width:holderWidth+'px',
			height:holderHeight+'px',
			marginTop:offsetTop+'px',
			marginLeft: offsetLeft+'px',
			'background-color': mediaBG
			},{
				duration: 'slow'
			});
		//console.log(documentWidth);
			$('#mediaFile').animate({
				width:fileWidth+'px',
				height:fileHeight+'px',
				opacity: 1
				},{
					duration: 'slow'
				});
		
	}
	
	//setting the actions when clicking on a gallery item
	$('#guide').each(function(i){ 
		mediaItem[i] = 'media'+(i+1); 
		$(this).click(function(){
			showMedia()
			return false;
		});
	});
	
	//hide the large view when you click outside or on the close box
	$('#mediaContainer, #mediaClose').click(function(){ 
		$('#mediaFader').fadeOut(500); 
		$('#mediaContainer').fadeOut(500);
		if($.browser.msie && $.browser.version == 6) {
			$('select').each(function(){
				$(this).show();
			});
		}

		return false;
	});
	
	//overriding the above statement
	$('#mediaHolder').click(function(){
		return false;																		
	});

	$('#mediaNext').click(function(){
		if(mediaNumber < mediaItem.length-1) {
			showMedia(mediaItem[mediaNumber+1],mediaNumber+1);															 
		}
		return false;
	});
	
	//the back button
	$('#mediaBack').click(function(){
		if(mediaNumber > 0) {
			showMedia(mediaItem[mediaNumber-1],mediaNumber-1);															 
		}
		return false;
	});
	
	
});