var oldheight= 300;
function lightbox(){
	
	
	if(jQuery('.lightboxbg').length == 0){
		//add modal background
		jQuery('<div />').addClass('lightboxbg').appendTo('body');
		//add modal window
		jQuery('<div />').addClass('modal').appendTo('body');
		jQuery('<div />').addClass('modal-frame').appendTo('.modal');
		jQuery('<div />').addClass('close-modal-frame').appendTo('.modal-frame');
		jQuery('<div />').addClass('modal-frame-container').appendTo('.modal-frame');
		jQuery('<img />').addClass('close-img').appendTo('.close-modal-frame');
		jQuery('.close-img').attr('src', baseurl+'img/close.png');
		jQuery('.close-img').click(function (event){
			hideLightbox();
		})
	}

	jQuery('a[rel="iframe"]').click(function(event){
		event.preventDefault();
		var w = 480;
		var h = 360;
		if(jQuery(this).attr('size') != undefined){
			w = parseInt(jQuery(this).attr('size').split(',')[0]);
			h = parseInt(jQuery(this).attr('size').split(',')[1]);
		}
		jQuery('<iframe />', {
			name: 'iframe',
			id:   'iframe',
			src: jQuery(this).attr('href')
		}).appendTo('.modal-frame-container');
		jQuery('#iframe').css('width', w)
		jQuery('#iframe').css('height', h)
		showLightbox(w, h);
	});
	

	jQuery('a[rel="image"]').click(function(event){
		event.preventDefault();
		jQuery('<img id="jwimage" />').appendTo('.modal-frame-container');
		jQuery('#jwimage').attr('src', jQuery(this).attr('href'));
		var newImg = new Image();
		
		jQuery(newImg).load(function(){
			var height = newImg.height;
			var width = newImg.width;
		
			showLightbox(width, height);
		}).attr('src', jQuery(this).attr('href'));
	});
	jQuery('a[rel="youtube"]').click(function(event){
		event.preventDefault();
		jQuery('<div id="jwplayer" />').appendTo('.modal-frame-container');
		var w = 480;
		var h = 360;
		jwplayer("jwplayer").setup({
			flashplayer: baseurl+"files/player.swf",
			file: jQuery(this).attr('href'),
			height: h,
			width: w,
			autostart: true,
			events: {
				onComplete: function(event) {
					hideLightbox();
				}
			}
		});
		showLightbox(w, h);
	});
	jQuery('a[rel="vimeo"]').click(function(event){
		event.preventDefault();
		var code = jQuery(this).attr('code');
		jQuery('<iframe src="http://player.vimeo.com/video/'+code+'?color=ffffff" width="640" height="480" frameborder="0"></iframe><p><a href="http://vimeo.com/'+code+'">Between Two Points</a> from <a href="http://vimeo.com/motiphe">motiphe</a> on <a href="http://vimeo.com">Vimeo</a>.</p>').appendTo('.modal-frame-container');
		showLightbox(640, 480);
	});
	jQuery('a[rel="video"]').click(function(event){});
	
	
	jQuery('a[rel="mp3"]').click(function(event){
		event.preventDefault();
		
		var w = 600;
		var h = 120;
		var urlmp3= jQuery(this).attr('href');
		
		jQuery(
			'<div id="jquery_jplayer_1" class="jp-jplayer"></div>'+
			'<div class="jp-audio">'+
				'<div class="jp-type-single">'+
					'<div id="jp_interface_1" class="jp-interface">'+
						'<ul class="jp-controls">'+
							'<li><a href="#" class="jp-play" tabindex="1">play</a></li>'+
							'<li><a href="#" class="jp-pause" tabindex="1">pause</a></li>'+
							'<li><a href="#" class="jp-stop" tabindex="1">stop</a></li>'+
							'<li><a href="#" class="jp-mute" tabindex="1">mute</a></li>'+
							'<li><a href="#" class="jp-unmute" tabindex="1">unmute</a></li>'+
						'</ul>'+
						'<div class="jp-progress">'+
							'<div class="jp-seek-bar">'+
								'<div class="jp-play-bar"></div>'+
							'</div>'+
						'</div>'+
						'<div class="jp-volume-bar">'+
							'<div class="jp-volume-bar-value"></div>'+
						'</div>'+
						'<div class="jp-current-time"></div>'+
						'<div class="jp-duration"></div>'+
					'</div>'+
					'<div id="jp_playlist_1" class="jp-playlist">'+
						'<ul>'+
							'<li><a href="'+urlmp3+'" target="_blank">'+urlmp3+'</a></li>'+
						'</ul>'+
					'</div>'+
				'</div>'+
			'</div>'
		).appendTo('.modal-frame-container');
		
		
		
		jQuery("#jquery_jplayer_1").jPlayer({
			ready: function () {
				jQuery(this).jPlayer("setMedia", {
					mp3: urlmp3
				}).jPlayer("play");
			},
			ended: function (event) {
				//jQuery(this).jPlayer("play");
				hideLightbox();
			},
			swfPath: baseurl+"files/Jplayer.swf",
			supplied: "mp3"
		});
		
		showLightbox(w, h);
	});
	
	jQuery('.lightboxbg').click(function (event){
		hideLightbox();
	});
	
};

function showLightbox(w, h){
	if(w == undefined || h == undefined || w < 0 || h < 0){
		w = 0;
		h = 0;
	}
	jQuery('body').css('overflow', 'hidden');
	jQuery('.modal-frame').css('width', (w));
	jQuery('.modal-frame').css('height', (h + 40));
	jQuery('.modal-frame').css('top', -Math.ceil((h + 40)/2));
	jQuery('.modal-frame-container').css('width', w);
	jQuery('.modal-frame-container').css('height', h);
	jQuery('.close-img').attr('src', baseurl+'img/close.png');
	jQuery('.close-img').click(function (event){
		hideLightbox();
	});
	jQuery('.lightboxbg').show();
	jQuery('.modal').show();
	
	
	if (jQuery.browser.mse){
		
		if(parent.document.getElementById("iframe-multimedia").offsetHeight < 580 ){
			oldheight= parent.document.getElementById("iframe-multimedia").offsetHeight;
			parent.document.getElementById("iframe-multimedia").style.height = 580+'px';
		}
	}else{
		
		if(window.parent.document.getElementById("iframe-multimedia").offsetHeight < 580 ){
			oldheight= window.parent.document.getElementById("iframe-multimedia").offsetHeight;
			window.parent.document.getElementById("iframe-multimedia").style.height =580+'px';
		}
	}
	
		
}

function hideLightbox(){
	jQuery('.lightboxbg').hide();
	jQuery('.modal').hide();
	jQuery('body').css('overflow', 'auto');
	jQuery('.modal-frame-container').empty();
	if (jQuery.browser.mse){
		
		if(oldheight< parent.document.getElementById("iframe-multimedia").offsetHeight ){
			parent.document.getElementById("iframe-multimedia").style.height = oldheight+'px';
		}
	}else{
		
		if(oldheight < window.parent.document.getElementById("iframe-multimedia").offsetHeight ){
			window.parent.document.getElementById("iframe-multimedia").style.height =oldheight+'px';
		}
	}
}
