jQuery.extend({
	param: function( a , pn, d) {
		if (!d)
			d=0;

		var t = typeof(a);
		// We're at the end of the line so return it.
		if ((t == 'string' || t == 'number' || t == 'boolean' || t == 'undefined') && pn)
			return pn+'='+encodeURIComponent(a);

		var s = [];
		if ( a.constructor == Array || a.jquery )
			// Serialize the form elements
			jQuery.each( a, function(i){
				if (this.name) {
					s.push( encodeURIComponent(this.name) + "=" + encodeURIComponent( this.value ) );
				} else {
					if (!pn) {
						s.push(jQuery.param(a[i], encodeURIComponent(i),d));
					} else {
						s.push(jQuery.param(a[i], pn+"["+encodeURIComponent(i)+"]",d));
					}
				}
			});
		// Otherwise, assume that it's an object of key/value pairs
		else
			for ( var j in a ) {
				d++;
				// prevent infinite recursion.  max of 10 levels deep.
				if (d>10)
					break;

				if (!pn)
					// recurse into siblings. with this as parent.
					s.push(jQuery.param(a[j], encodeURIComponent(j),d));
				 else
					// recurse into siblings.
					s.push(jQuery.param(a[j], pn+"["+encodeURIComponent(j)+"]",d));

			}
		// Return the resulting serialization
		return s.join("&").replace(/%20/g, "+");
	}
});


(function($) {

  	$.fn.mediaPlayer = function(options){
  		var gallery = this;
		var opts = $.extend(true, {}, $.fn.mediaPlayer.defaults, options, {loaded:false});
		
		// is type -> core function 
		function image(data)
		{			
			return 	$("<img/>")
				.hide()
				.load(function(){
					$(this).fadeIn("normal",function(){
						$(this).trigger("faded");
						if($(this).width()/$(this).height() > 3)
							panorama.call(this);
					});
				})
				.attr("src",data['full']);		
		}
		
		// used by image
		function panorama(data)
		{
			return image(data).panorama({
				viewport_width:prnt.width(),
				mode_360:false}
			);

		}
		
		function ivisit(data)
		{
			var sizer = $("<div>").css({
				"position":"absolute",
				"width":560+"px",
				"height":410+"px"
			});
			
			return sizer.append(image(data).one('faded',function()
			{				
				$(this).centerOn({'element':sizer});
				jQuery.each(data['data'], function(i, eye) 
				{
					$("<img/>")
						.attr('src',opts.eyes[eye.viewpoint-1])
						.addClass('plrEye')
						.css({
							"position":"absolute",
							"left":parseInt(eye.x)+"px",
							"top":parseInt(eye.y)+"px"
						})
						.click(function(){
							window.open(eye.popup,'_blank','width=400,height=200,scrollbars=no');
						})
						.appendTo(sizer);
				});
			}));
		}
		
		function movie(data)
		{
			var size = opts.sizes.movie || opts.sizes.full;
			
			return $('<div id="jwPlayer">')
				.bind('appended',function(){
					swfobject.embedSWF(
						data.data.player,
						'jwPlayer',
						size.width,
						size.height,
						'9.0.115','false',{
							file:data.full + '%3Ftype%3Dflv', // add flv extension for stupid flash -_-
							autostart:'true'
						}, { 
							allowfullscreen:'true', 
							allowscriptaccess:'always' 
						}, { 
							id:'jwPlayer', 
							name:'jwPlayer'
						}
					);
				});
		}
		
		function map(data)
		{
			return $('<iframe scrolling="no" frameborder="0">')
				.attr({
					src:data.full,
					width:opts.sizes.full.width,
					height:opts.sizes.full.height
				});
		}
		
		function resize()
		{
			$(".dimmer")
				.css({
					"position":"absolute",
					"width":$(document).width()+"px",
					"height":$(document).height()+"px"
				});
				
		
			$(".player").centerOn({element:document});
		}
		
		function current()
		{
			return $(".player .plrImages img").index($(".player .plrImages img.active"));
		}
		
		function create(opt)
		{
			var clone = $(opts.html).hide().appendTo("body");
			
			jQuery.each(gallery,function(i,ele){
				$(".plrImages",clone).append(
					$("<img/>")
						.attr("src",opts.files[$(ele).attr("id").match(/[0-9]+/g)]['thumb'])
						.click(function(){
							navigate(i);
						}));
			});
			
			clone
				.find(".plrTitle").text(opts.title).end()
				.find(".plrClose").click(function(){close();}).end()
				.find(".plrNext").click(function(){navigate(current()+1);}).end()
				.find(".plrPrev").click(function(){navigate(current()-1);}).end()
				.centerOn({element:document})
				.show();
			if(typeof opt == 'number')
				navigate(opt);
		}
		
		function show(opt)
		{
			$("<div></div>")
				.addClass("dimmer")
				.css({
					"opacity":0,
					"width":$(document).width()+"px",
					"height":$(document).height()+"px"
				})
				.appendTo("body")
				.fadeTo("normal",0.66,function(){
								
					// Load Options		
					if(!opts.loaded)
						$.ajax({
							type:"GET",
							cache:true,
							url:opts.data,
							dataType:'json',
							data:{sizes:opts.sizes},
							success: function(data){
								opts = $.extend({}, opts, data, {loaded:true});
								
								// Load Plugins
								ctr = opts.preload.length;
								$.each(opts.preload,function(){
									$.ajax({
										type:"GET",
										cache:true,
										url:this,
										dataType:'script',
										success: function(){
											// All Plugins Loaded ?
											if(--ctr == 0)
												create(opt);
										}
									});
								});
							}
						});
					else
						create(opt);
				})
				.click(function(e){
					close();
				});
				
			$(window).bind('resize',resize);				
		}
		
		function close()
		{
			$(window).unbind('resize',resize);
	  		$(".player").remove();
			$(".dimmer").fadeTo("fast",0,function(){
				$(this).remove();
			})				
		}
		
		function navigate(opt)
		{
			if(opt == -1) opt = gallery.length-1;
			opt = typeof gallery[opt] == 'object' ? opt : 0; 
			
			gid = $(gallery[opt]).attr("id").match(/[0-9]+/g);
			
			$(".player .plrImages img")
				.removeClass("active")
				.eq(opt)
					.addClass("active");
		
			$(".player .plrCurrent")
				.children()
					.fadeOut("normal",function(){
						$(this).remove();
					});
			
			$(".player .plrInfo")
				.text($(gallery[opt]).attr("alt"));

			eval(opts.files[gid]['type']+".call($('.player .plrCurrent'),opts.files[gid])")
				.appendTo(".player .plrCurrent")
				.load(function(){$(this).centerOn({element:$(".player .plrCurrent")})}) // if image
				.centerOn({element:$(".player .plrCurrent")}) // after append or no effect !?
				.trigger("appended");

		}
		
		return this.each(function(i,ele) {
			$(this).bind(opts.reveal,function(){
				show(i);
			});
		});
	};
	
	$.fn.mediaPlayer.centerOn = function(options){
		if(!options.element) return this;
		return this.each(function(){
			$(this).css({
				"position":"absolute",
				"top":($(options.element).height()/2-$(this).height()/2)+"px",
				"left":($(options.element).width()/2-$(this).width()/2)+"px"
			});
		});
	};
	
	$.fn.centerOn = $.fn.mediaPlayer.centerOn;
	
	$.fn.mediaPlayer.defaults = {
		title: "Gallery",
		reveal: "click",
		loading: "../images/loading.gif",
		data: "",
		sizes:{
			ivisit:{width:560,height:410},
			map:{width:560,height:410},
			thumb:{height:49,options:{panel:65,panelh:49,thumbnail:true}},
			full:{height:450}
		},
		html: 
			'<div class="player">'+
				'<div class="plrTitle"></div>'+
				'<div class="plrClose"></div>'+
				'<div class="plrNavigation">' +
					'<div class="plrImages"></div>' +
				'</div>'+
				'<div class="plrContent">'+
					'<div class="plrPrev">&lt;&lt;</div>'+
					'<div class="plrCurrent"></div>'+
					'<div class="plrNext">&gt;&gt;</div>'+
					'<div class="plrInfo"></div>'+
				'</div>'+
			'</div>'
	};
})(jQuery);


