// 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 = '';

// setting global variables
var fadeSpeed = 600;
var bgFadeSpeed = 400;
var mediaArr = new Array();
var media = 0;
var mediaImg = '';
var mediaHeading = '';
var mediaContent = '';
var browserWindowWidth = ($(window).width())-20;
var browserWindowHeight = ($(window).height())-20;
var windowWidth = ($(window).width())-20;
var windowHeight = ($(window).height())-20;
var captionHeight = 0;
var contentWidth = 0; 
var imgWidth = 0;
var imgHeight = 0;
var fileWidth = 0;
var fileHeight = 0;
var boxWidth = 800;
var boxHeight = 540;
var boxPadding = 20;
var btnHeight = 26;
var boxOffsetX = -410;
var boxOffsetY = -280;

$(document).ready(function() {
	$('#content').append('<div id="galContainer"><div id="galBtns"><div id="galEnlarge">Click image to enlarge</div><div id="galBack"><img src="images/galBack.png"/> back</div><!-- gal back --><div id="galNext">next <img src="images/galNext.png"/></div><!-- gal next --><div id="galClose">close <img src="images/galClose.png"/></div><!-- gal close --></div><!-- gal btns --><div id="galCont"><div id="galContent"></div><!-- gal content --><div id="galImage"></div><!-- gal image --></div><!-- gal cont --></div><!-- gal Container -->');
	$('#galContainer').hide();
	
	function showMedia(source2,currentNumber) {
		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
		$('#galContent').empty();
		$('#galImage').empty();
		$('#galBack').empty();
		$('#galNext').empty();

		//hide the next and back buttons, depending on what file number you are on
		if(mediaNumber <= 0) {
			$('#galBack').css('cursor','default'); 
			$('#galBack').empty();
		} else {
			$('#galBack').css('cursor','pointer'); 
			$('#galBack').append('<img src="images/galBack.png"/> back');
		}
		if(mediaNumber >= mediaItem.length-1) {
			$('#galNext').css('cursor','default');
			$('#galNext').empty(); 
		} else {
			$('#galNext').css('cursor','pointer'); 
			$('#galNext').append('next <img src="images/galNext.png"/>');
		}

		//getting the bits ans pieces
		mediaSource = '';
		mediaSource = $('#media .'+source+' a').attr('href');
		mediaLarge = $('#media .'+source+' a').attr('oi');
		mediaCaption = $('#media .'+source+' .caption').text();
		mediaContent = $('#media .'+source+' .cont').html();
		if(mediaContent == null) {
			mediaContent = '';	
		}

		//getting the mine type of the file (ie. img, flv, mp3, swf or other)
		mediaSourceSplit = mediaSource.split('.');
		fileType = mediaSourceSplit[mediaSourceSplit.length-1];
		if($.browser.msie) {
			imgWidth2 = $('#media .'+source+' a').attr('w');
			imgHeight2 = $('#media .'+source+' a').attr('h');
		} else {
			imgWidth2 = $('#media .'+source+' a img.hide').attr('width');//width();
			imgHeight2 = $('#media .'+source+' a img.hide').attr('height');//.height();
		}
		
		offsetX2 = Math.floor(imgWidth2 / 2);
		offsetY2 = Math.floor(imgHeight2 / 2);
		$('#galImage').append('<a href="'+mediaLarge+'" target="_blank" onclick="return runLargeImage(\''+mediaLarge+'\',\''+source+'\');"><img src="'+mediaSource+'" width="'+imgWidth2+'" height="'+imgHeight2+'" style="margin:-'+offsetY2+'px 0 0 -'+offsetX2+'px"/></a>');
				
		$('#galContent').append('<h2 class="headings">'+mediaCaption+'</h2>');
		$('#galContent').append(mediaContent);
		
		$('#galContainer').fadeIn(500);
		$('#galCont').fadeIn(500);

	}
	//setting the actions when clicking on a gallery item
	$('#media .mediaItem').each(function(i){ 
		mediaItem[i] = 'media'+(i+1); 
		$(this).click(function(){
			showMedia(mediaItem[i],i)
			return false;
		});
	});
	
	//hide the large view when you click outside or on the close box
	$('#galClose').click(function(){ 
		$('#galContainer').fadeOut(500);
		if($.browser.msie && $.browser.version == 6) {
			$('select').each(function(){
				$(this).show();
			});
		}

		return false;
	});
	

	function emptyItems(source2,currentNumber) {
			
	}

	$('#galNext').click(function(){
		if(mediaNumber < mediaItem.length-1) {
			$('#galCont').fadeOut(500,function(){ showMedia(mediaItem[mediaNumber+1],mediaNumber+1); });
		}
		return false;
	});
	
	//the back button
	$('#galBack').click(function(){
		if(mediaNumber > 0) {
			$('#galCont').fadeOut(500,function(){ showMedia(mediaItem[mediaNumber-1],mediaNumber-1); });
		}
		return false;
	});
	
	
});


function runLargeImage(img,source) {
	/*resizeWindowVars();
	imgWidth = $('#media .'+source+' a').attr('w2');
	imgHeight = $('#media .'+source+' a').attr('h2');

	resizeItem();

	holderWidth = imgWidth;
	holderHeight = imgHeight+20; 
	offsetX = Math.floor(holderWidth / 2);
	offsetY = Math.floor(holderHeight / 2);
	
	$('body').append('<div id="mediaFader"></div><div id="mediaContainer"><div id="mediaLarge"><div id="mediaLargeClose" onclick="return closeLarge();">Close</div><div id="mediaLargeHolder"></div></div><!-- media large --></div>');
	$('#mediaLargeHolder').append('<img src="'+img+'" width="'+imgWidth+'" height="'+imgHeight+'"/>');
	$('#mediaLargeHolder').width(imgWidth).height(imgHeight);
	$('#mediaLarge').css({
		width: holderWidth,
		height: holderHeight,
		'margin-top': -offsetY,
		'margin-left': -offsetX	
	}).fadeIn(400);
	
	return false; */	
}


function closeLarge() {
	$('#mediaLarge, #mediaFader, #mediaContainer').fadeOut(400,function(){ $(this).remove(); });
	return false; 	
}


function resizeItem() {
	if (imgWidth > windowWidth) {
		imgHeight = imgHeight * (windowWidth / imgWidth); 
		imgWidth = windowWidth; 
		if (imgHeight > windowHeight)	{ 
			imgWidth = imgWidth * (windowHeight / imgHeight); 
			imgHeight = windowHeight; 
		}
	} else if (imgHeight > windowHeight) { 
		imgWidth = imgWidth * (windowHeight / imgHeight); 
		imgHeight = windowHeight; 
		if (imgWidth > windowWidth){ 
			imgHeight = imgHeight * (windowWidth / imgWidth); 
			imgWidth = windowWidth;
		}
	}
	
	imgWidth = Math.floor(imgWidth);
	imgHeight = Math.floor(imgHeight);
	
	imgWidth < 250 ? fileWidth = 250 : fileWidth = imgWidth;
	imgHeight < 250 ? fileHeight = 250 : fileHeight = imgHeight;
	

};


// resizes the main function window
function resizeWindowVars() {
	browserWindowWidth = ($(window).width())-20;
	browserWindowHeight = ($(window).height())-20;
	windowWidth = ($(window).width())-20;
	windowHeight = ($(window).height())-20;
}


