/* /js/modal.js */

function loadPlayer(url){
	$.modal.init();
	$.ajax({
		'type': 'get',
		'dataType': 'json',
		'url': url,
		'error': function(data,textStatus,errorThrown){
				$.dialog.error(data,textStatus,errorThrown);
			},
		'success': function(data,textStatus,errorThrown){
				if(typeof(data)!='object'){
					$.dialog.error(data,textStatus,errorThrown);
				}else{
					$.modal.reveal(data.content);
					setTimeout('$.modal.playerEmbed("'+data.player+'","'+data.image+'","'+data.file+'","'+data.width+'","'+data.height+'","'+data.autostart+'")',1250);
					return false;
				}
			}
	});
}

$(function(){
	$.modal.bind();
});

(function($){
	$.modal = function(obj){
		$.modal.init(obj);
		$.modal.fetch($(obj).attr('href'));
		return false;
	}

	$.extend($.modal, {
		settings: {
			loading: '<div class="loading">Loading...</div>',
			width: 640,
			height: 480,
			photo: '<div id="albumPhoto"><a href="javascript:$.modal.close()"><img class="photo" src="" alt="Click to Close" /></a></div>'
		},
		
		init: function(){
			if($('#modal').length!=1){
				$('body').append('<div id="modal"><div class="close"><a title="Close"><span>Close</span></a></div><div class="body"><div class="content"></div></div></div>');
			}
			if($('#screen').length!=1){
				$('body').append('<div id="screen"></div>');
				$('#screen').click($.modal.close);
			}
			$('div.close','#modal').hide().children('a').click($.modal.close);
		},
		
		bind: function(){
			$('a[rel*=modal]').bind('click',function(){
				$.modal(this);
			 	return false;
			});
			
			$('a[rel*=player]').bind('click',function(){
				$.modal.init();
				$.modal.player(this);
				return false;
			});

			$('a[rel*=photo]').bind('click',function(){
				$.modal.init();
				$.modal.photo(this);
				return false;
			});

			$(window).bind('resize',$.modal.screen);
		},
		
		fetch: function(url){
			if(url.substr(0,1)=='#'){
				$('#modal .content').html($(url).html());
			}else{
				$.ajax({
					'type': 'get',
					'url': url,
					'error': function(data,textStatus,errorThrown){
							$.dialog.error(data,textStatus,errorThrown);
						},
					'success': function(data,textStatus,errorThrown){
							var dataT = trim(data);
							if(dataT==''||dataT.substr(0,9)=='<!DOCTYPE'){
								$.dialog.error(data,textStatus,errorThrown);
							}else{
								$.modal.reveal(data);
							}
						}
				});
			}
		},
		
		reveal: function(data){
			$.modal.screen();
			$('#screen').show().fadeTo(50,0.5);
			$('#modal .content').html($.modal.settings.loading);
			$('#modal').css({
					top: ($(window).height()/2),
					left: ($(window).width()/2),
					width: 0
				}).animate({
					top: Math.max(25,($(window).height()/2)-($('#modal').height()/2)),
					left: ($(window).width()/2)-($.modal.settings.width/2),
					width: $.modal.settings.width
					},500,function(){
						$('#modal .content .loading').fadeOut(500,function(){
							$('#modal .content .loading').remove();
							$('#modal .content').html(data).fadeIn(500);
							$('#modal div.close').fadeIn(250);
							$('#modal').animate({
								top: Math.max(25,($(window).height()/2)-(Math.max($.modal.settings.height,$('#modal').height())/2)),
								left: ($(window).width()/2)-($.modal.settings.width/2)
							},250);
							$.modal.complete();
						});
					}
				);
		},
		
		close: function(){
			$('#modal').fadeOut(250,function(){
				$('#modal .content').empty();
				$('#screen').hide();
			});
		},
		
		screen: function(){
			$('#screen').css({
				width: $(window).width(),
				height: $(window).height()
			});
		},
		
		player: function(obj){
			$.modal.init();
			var url = $(obj).attr('href');
			$.ajax({
				'type': 'get',
				'dataType': 'json',
				'url': url,
				'error': function(data,textStatus,errorThrown){
						$.dialog.error(data,textStatus,errorThrown);
					},
				'success': function(data,textStatus,errorThrown){
						if(typeof(data)!='object'){
							$.dialog.error(data,textStatus,errorThrown);
						}else{
							$.modal.reveal(data.content);
							setTimeout('$.modal.playerEmbed("'+data.player+'","'+data.image+'","'+data.file+'","'+data.width+'","'+data.height+'","'+data.autostart+'")',1250);
						}
					}
			});
		},
			
		playerEmbed: function(p,i,f,w,h,a){
			$('#'+p).flash({
				swf: _web+'/media/admin/player.swf',
				flashvars: {
					'image': 	i,
					'file':		f,
					'controlbar':	'bottom',
					'stretching':	'uniform',
					'autostart':	a,
					'volume':	75,
					'backcolor': 'FFFFFF'
				},
				wmode: 'transparent',
				allowfullscreen: 'true',
				allowscriptaccess: 'always',
				height:	h,
				width: w
			});
		},
		
		complete: function(){
			$('#modal a[rel*=dialog]').bind('click',function(){
				$('#modal').fadeOut(250,function(){
					$('#modal .content').empty();
				});
				$.dialog(this);
				return false;
			});
		},
		
		photo: function(obj){
			$.modal.init();
			var url = $(obj).attr('href');
			url = _web+url;
			$.modal.reveal($.modal.settings.photo);
			setTimeout('$.modal.photoEmbed("'+url+'",600,400)',1250);
		},
		
		photoEmbed: function(s,w,h){
			$('#modal img.photo').attr({
				src: s
			});
		}
	});
})(jQuery);