/*========
 * flying-w.net/js/base.js
 *
 * (c) Copyright 1998-2011 (v5.5s) Flying @ Flying's World
 * 吴颖晖 Freddie
 *
 *#req*/
if("object"!=typeof window["F"]){
	var err="Missing prerequisite: Fjl.js";
	alert(err);
	throw new Error(err);
}/*======*/

/**<<<	Gallery masonry	>>>*/
F.Gallery=function(gallery,items,colWidth){
	var base=$("link[rel='canonical']").attr("href")+"/js/jQuery";
	$.getScript(base+"/jquery.masonry-2.0.111015.min.js",function(){
		$(gallery).imagesLoaded(function(){
			$(this).masonry({
				itemSelector:items,
				isResizable:true,
				isFitWidth: true,
				columnWidth:colWidth,
				isAnimated:true,
				animationOptions:{
					queue:false,
					duration:1000
				}
			});
		});
	});
	return this;
};
	
/**<<<	Multimedia controls	>>>*/
F.Media=function(id){
	this.obj=$('#'+id);
	var o=this.obj.find("a[type],object[type]");
	F.assert(o.size()>0,"Missing multimedia MIME type object in #"+id);
	this.mime=o.attr("type");
	this.init=F.Media[this.mime];
	F.assert("function"==$.type(this.init),"No handler for #"+id+' of type "'+this.mime+'"');
	return this;
};

//>>>	Adobe Flash
F.Media["application/x-shockwave-flash"]=function(version,flashVars,params,attrs){
	var self=this,base=$("link[rel='canonical']").attr("href")+"/js/SWFObject";
	self.old=$(self.obj).clone();
	$.getScript(base+"/swfobject.js",function(){
		if (!attrs)
			attrs={};
		attrs["styleclass"]=self.obj.attr("class");
		swfobject.embedSWF.call(this,
			self.obj.find("a[href$='.swf']").text(),
			self.obj.attr("id"),
			self.obj.width(),
			self.obj.height(),
			version,
			base+"/expressInstall.swf",
			flashVars,
			$.extend({allowScriptAccess:"sameDomain"},params),
			attrs,
			function(e){
				F.Media.FlashControl.call(self,e);
			}
		);
	});
}
F.Media.FlashControl=function(e){
	if(!e.success){
		$('#'+e.id).replaceWith(this.old);
		return false;
	}
	var buttons=$("div.mm-control button[value='"+e.id+"']");
	buttons.each(function(){
		try{
			var cmd=F.Media.FlashControl.commands[$(this).attr("class")];
			F.assert("function"==$.type(cmd),"Unknown Flash control command: "+$(this).attr("class"));
			return $(this).click(function(){cmd(e.ref)});
		}catch(ex){
			$(this).attr("disabled","disabled");
		}
	});
}
F.Media.FlashControl.commands={
	play:function(o){o.Play()},
	pause:function(o){o.StopPlay()},
	rewind:function(o){o.Rewind()},
	"zoom-in":function(o){o.Zoom(100*2/3)},
	"zoom-out":function(o){o.Zoom(100/2*3)}
};

//>>>	RealMedia
// This doesn't seem to work well due to the fact that RealPlayer plugin is forever having problems...
F.Media["audio/x-pn-realaudio-plugin"]=function(){
	//Firefox seems to have problem with <param name="CONTROLS" .../>
	$("html.gecko #"+this.obj.attr("id")+"-std").each(function(){
		var noembed=$("<noembed>"),embed=$("<embed>",{
			type:this.type,
			src:this.data,
			width:this.width,
			height:this.height,
			id:this.id
		});
		$(this).children("param").each(function(){
			embed.attr(this.name,this.value);}
		).end().children(":not(param)").each(function(){
			noembed.append($(this).clone());
		}).end().before(embed).after(noembed).remove();
	});
};

//>>> MP4 video
F.Media["video/mp4"]=function(){
	var self=this,base=$("link[rel='canonical']").attr("href")+"/js/Flowplayer";
	$.getScript(base+"/flowplayer-3.2.6.min.js",function(){
		flowplayer(self.obj.attr("id"),base+"/flowplayer-3.2.7.swf",{
			clip:{
				url:self.obj.find("a[href$='.f4v'],a[href$='.flv'],a[href$='.mp4']").text(),
				autoPlay:true,
				autoBuffering:true
			}
		});
	});
};

