/*--------------------------------------------------
J F E   C O D E
http://extranet.websolute.it/jfe/
http://www.websolute.it
--------------------------------------------------*/

(function( window, undefined ) {

	var FQuery = function( selector, context ) {
		// The FQuery object is actually just the JQuery constructor 'enhanced'
		// return new FQuery -> JQuery.fn.init( selector, context );
		var j;
		// alert(typeof(selector[0]));
		// alert("selector " +selector.key);
		if(typeof(selector) == "string") {
			j = $( selector, context ).getObj();
			j.key = j.getKey();
		} else if(selector.key || typeof(selector[0]) == "string"){
			j = $( selector, context );
			j[0] = selector[0];
			j.key = selector.key;
		} else {
			j = $( selector, context ).getObj();
			j.key = j.getKey();
		}
		// alert(j[0].tagName);
		j.length = 1;
		j.find = function ($find) {
			var f = F(this);
			var s = "main";
			if (f[0] && typeof(f[0])=="string"){
				s = f[0];
			}
			var r = f.jfq({action:"find",selector:s,find:$find});
			f.length = r.length;
			f.key = this.key;
			for (var i=0;i<r.length;i++){
				f[i] = r[i];
			}
			return f;		
		}
		j.children = function () {
			var f = F(this);
			var s = "main";
			if (f[0] != null && typeof(f[0])=="string"){
				s = f[0];
			}
			// alert (s);
			var r = f.jfq({action:"children",selector:s});
			f.length = r.length;
			f.key = this.key;
			for (var i=0;i<r.length;i++){
				f[i] = r[i];
			}
			return f;		
		}
		j.each = function (callback) {
			for (var i=0;i<this.length;i++){
				// alert("each key "+this.key);
				var f = F(this[i]);
				f[0] = this[i];
				f.key = this.key;
				callback.call(f);
			}
			return this;		
		}
		j.props = function (pars) {
			for (var i=0;i<this.length;i++){
				// alert(this[i]);
				var p = {action:"attr",selector:this[i]};
				for ( var name in pars ) {
					p[name] = pars[name];
				}
				var r = this.jfq(p);
			}			
			return this;		
		}
		j.jfq = function (pars) {
			var key = this.key;
			// alert("call "+pars.action+" on "+key);
			var r;
			if ($.browser.msie){					
				r = window[key]["jfq"](pars);						
			}else{
				r = document[key]["jfq"](pars);
			}
			return r;
		}
		return j;
	}
	
	
	
	// Expose FQuery to the global object
	window.FQuery = window.F = FQuery;
})(window);

/*(function(F){

	
})(FQuery);*/


// Upgrade flash

(function($){
		  
    $.fn.extend({
		getObj: function (){
			var __e = this;
			if(typeof(this[0])=="object"){
				var tag = this[0].tagName;
				if(tag){
					__e = tag.toLowerCase() == "object" ? this : this.find("object");
				}
			}
			return __e;
		},
		getKey: function (){
			var key = null;
			if ($.browser.msie){					
				key = this.attr("id");			
			}else{
				key = this.attr("name");			
			}
			key ? null : null;// alert("JFE > key is null");
			return key;
		},
		jfq_ready: function (callback){
			return this.each(function(){
				var __e = $(this).getObj();
				if (__e.data("joined")) {
					callback.call(__e,JQuery);
				}else{
					if (key = __e.getKey()){	
						fjeready =  fje.ready[key] ? fje.ready[key] : fje.ready[key] = [];
						fjeready.push(callback);		
					}
				}
			});	
		},
		jfq: function (pars){
			return this.each(function(){
				var __e = $(this).getObj();
				if (key = __e.getKey()){	
					var conn = fje.conn[key];
					if (conn) {
						if (conn.joined) {
							if ($.browser.msie){					
								window[key]["jfq"](pars);						
							}else{
								document[key]["jfq"](pars);
							}
						} else {
							__e.jfe_push(key,"jfq",pars);	
						}
					}
				}
			});	
		},
		jfe: function(options) {			
			var defaults = {
				ver: "6,0,0",
				display: "none",
				manual: false,
				alert: false,
				console: false,
				noflash: false,
				showId: false,
				showTag: false,
				timeout: 8000
			};
			var options = $.extend(defaults, options);
			options.ver = options.ver.split (",");
			return this.each(function(){
				var __e = $(this);
				__e.data ("default", __e.html());
				
				var __o = __e.find("object:first");
				
				__o.remove();	
				// __o.css("display","none");
				
				var key = "JFE";
				var keys = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","Z"];
				while (key.length<8){
					key += keys[Math.floor(Math.random()*keys.length)];
				}
								
				var plugin = getPlugin(options.ver[0], options.ver[1], options.ver[2]);
				
				/***************** ALERTS *****************/
				
				options.console	&& options.alert ? alert ( "JFE { hasflash: " + plugin.flash + ", version: " + plugin.version + ", display: " + options.display + " }\n\nto disable this message, set alert or console flag to false\n" ) : null;
				options.manual	&& options.alert ? alert ( "JFE { display: " + options.display + " } manual mode\n\nto disable this message, set alert or manual flag to false\n" ) : null;
				options.noflash	&& options.alert ? alert ( "JFE { display: " + options.display + " } noflash mode\n\nto disable this message, set alert or noflash flag to false\n" ) : null;
				options.showId	&& options.alert ? alert ( "JFE { display: " + options.display + " } showId mode\n\nto disable this message, set alert or showId flag to false\n" ) : null;
				options.showTag && options.alert ? alert ( "JFE { display: " + options.display + " } showTag mode\n\nto disable this message, set alert or showTag flag to false\n" ) : null;
				
				/*************** SETTING UP ***************/
				
				__e.data ("options") ? null : __e.data ("options", options);
				__e.data ("key", key);
				
				var w = parseInt(__o.attr("width"));
				var h = parseInt(__o.attr("height"));
					
				if (plugin.flash && options.manual) {
					
					__e.css("width", w + "px");
					__e.css("height", h + "px");
					
					__e.find(".jfeHasFlash").empty().remove();					
					__e.find(".jfeUpgrade").empty().remove();	
					__e.find(".jfeAlternate").empty().remove();	
																		
					__e.find(".jfeManual").css({"width":"100%","height":"100%"});
					__e.find(".jfeManual").css("display","block");		
					__e.find(".jfeManual").addClass("jfeRound");
								
					__e.find(".jfeManual a.jfeManualActivate").click(function(){
						$(this).parent().fadeOut(200,function(){
							var __e = $(this).parent();
							$(this).remove();
							__e.jfe_manual ();
						});
						return false;
					});
					
				} else if (plugin.flash && !options.noflash){
					
					__e.find(".jfeUpgrade").empty().remove();
					__e.find(".jfeAlternate").empty().remove();
					
					var object;
					var s = __o.attr("data");
					
					if ($.browser.msie){
						object = "<object width='"+w+"' height='"+h+"' classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' id='"+key+"'><param name='movie' value='"+s+"' />";
					}else{
						object = "<object width='"+w+"' height='"+h+"' type='application/x-shockwave-flash' name='"+key+"' data='"+s+"'>";			
					}
					
					var add_vars = "";
					if (options.console) {
						add_vars += "jfe_console=true";
					}
					if (options.showId) {
						add_vars != "" ? add_vars += "&" : null;
						add_vars += "jfe_showId=true";
					}
					if (options.showTag) {
						add_vars != "" ? add_vars += "&" : null;
						add_vars += "jfe_showTag=true";
					}
					var has_vars = false;					
					__o.children().each(function(){
						var __p = $(this);
						var n = __p.attr("name").toLowerCase();
						var v = __p.val();						
						switch (n) {
							case "movie":
							case "allowscriptaccess":
							break;
							case "flashvars":
								has_vars = true;
								if (add_vars != "") {
									object += "<param name=\"flashvars\" value=\""+v+"&"+add_vars+"\" />";
								} else {
									object += "<param name=\"flashvars\" value=\""+v+"\" />";
								}
							break;
							default:
								object += "<param name=\""+n+"\" value=\""+v+"\" />";
						}
					});
					
					__o.empty();
					
					if (add_vars != "" && !has_vars) {
						object += "<param name=\"flashvars\" value=\""+add_vars+"\" />";
					}
					
					object += "<param name=\"allowScriptAccess\" value=\"always\" />";
					object += "</object>";
					
					var has = __e.find(".jfeHasFlash");
					has.css("display","block");
					has.insertAfter (__e);
					
					__e.html (object);
										
					if (options.console) {
						if ($.browser.msie){
							__e.append("<!-- IE Browser [has Flash = " + plugin.flash + " (ver "+ plugin.version + ") -->");	
						}else{
							__e.append("<!-- Mozilla Browser [has Flash = " + plugin.flash + " (ver "+ plugin.version + ") -->");				
						}
					}
															
					fje.conn[key] = {joined:false,calls:[],pars:[]};		
					
				}else{
					
					__e.find(".jfeHasFlash").empty().remove();					
					__e.find(".jfeManual").empty().remove();					
					__e.find(".jfeUpgrade").css({"width":"100%","height":"100%"});
					__e.find(".jfeAlternate").css({"width":"100%","height":"100%"});
					
					if((navigator.userAgent.match(/iPad/i)) || (navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
						__e.find(".jfeAlternate").css("display","block");
					}else{
						switch(options.display){
							case "upgrade":
								__e.find(".jfeUpgrade").css("display","block");
								__e.find(".jfeUpgrade").addClass("jfeRound");
								__e.find(".jfeAlternate").empty().remove();
							break;
							case "alternate":
								__e.css("width", w + "px");
								__e.css("height", h + "px");
								__e.find(".jfeUpgrade").empty().remove();	
								__e.find(".jfeAlternate").css("display","block");															
							break;
							case "both":
								__e.css("width", w + "px");
								__e.css("height", h + "px");
								__e.find(".jfeAlternate").css("display","block");
								__e.jfe_upgrade(options, key);
							break;															
							default:
								__e.find(".jfeUpgrade").empty().remove();
								__e.find(".jfeAlternate").empty().remove();
						}
					}
									
				}
				
			});
		},
		jfe_onUpgrade: function($options, $key){
				
			return this.each(function(){
				var __e = $(this);
				
				var $e = {};
				$e.id  = $key + "_upgrade";
				
				$e.code = "<div id=\"" + $e.id + "\" class=\"jfeUpgrade jfeRound\" style=\"display:none;\">\n";
				$e.code += "<a href=\"#\" class=\"jfeUpgradeClose\">close</a>\n";
				$e.code += __e.children(".jfeUpgrade").html();
				$e.code += "</div>";
				
				__e.children(".jfeUpgrade").remove();
				
				$e.top 		= parseInt(__e.offset().top) + parseInt(__e.css("padding-top"));
				$e.left 	= parseInt(__e.offset().left) + parseInt(__e.css("padding-left"));
				$e.width 	= parseInt(__e.width());					
				$e.height 	= parseInt(__e.height());
				
				// alert ($e.top+", "+$e.left+", "+$e.width+", "+$e.height);
				
				/*
				$("body").prepend($e.code);
				$e.width -= parseInt($("#" + $e.id).css("padding-left")) + parseInt($("#" + $e.id).css("padding-right"));					
				$("#" + $e.id).css({"width":$e.width + "px", "position":"absolute", "clear":"both", "top":($e.top+$e.height+2) + "px", "left":$e.left + "px"});
				*/
				
				__e.append($e.code);
				
				// alert ($e.id+", "+$("#" + $e.id).offset().top+"; "+$("#" + $e.id).offset().left);
				var id = $e.id;
				var time = $options.timeout;
				var timeout = setTimeout(function(){
					jfeFadeIn(id,time);
				},100);
				
				$(window).scroll(function () { 
					if($("#" + id).css("display")=="none"){
						jfeFadeIn(id,time);
					}
				});
	
				/*
				var timeout; 
				if ($.browser.msie){
					timeout = window.setTimeout("jfeFadeIn('" + $e.id + "', " + $options.timeout + ")",100);
				} else {
					timeout = setTimeout(jfeFadeIn,100,$e.id,$options.timeout);
				}
				*/
				/*
				var timeout = setTimeout(function($id){
					alert ($id+", "+$("#" + $id).html());
					$("#" + $id).fadeIn(200,function(){
						var timeout = setTimeout(function($id){
							$("#" + $id).fadeOut(200,function(){
								$(this).remove();
							});
						},$options.timeout,$id);
					});
				},100,$e.id);
				*/
								
				$("#" + id + " a.jfeUpgradeClose").click(function(){
					$(this).parent().fadeOut(200,function(){
						$(this).css("display","none");
						// $(this).remove();
					});
					return false;
				});
				
				$e = null;
						
			});
		},
		jfe_upgrade: function($options, $key){
			return this.each(function(){
				var __e = $(this);
				$(window).bind("load",function(){
					__e.jfe_onUpgrade($options, $key);	
				});
				/*
				$(window).load(function(){
					__e.jfe_onUpgrade($options, $key);								
				});
				*/
			});
		},
		jfe_noflash: function(){
			return this.each(function(){
				var __e = $(this);
				__e.jfe_reset ();
				
				var __o = __e.find("object:first");
				__o.remove();
				var w = __o.attr("width");
				var h = __o.attr("height");
					
				__e.find(".jfeUpgrade").css({"width":"100%","height":"100%"});
				__e.find(".jfeAlternate").css({"width":"100%","height":"100%"});
				__e.css("width", w + "px");
				__e.css("height", h + "px");
				__e.jfe_onUpgrade({timeout:8000}, __e.data ("key"));
				__e.find(".jfeAlternate").css("display","block");
			});
		},
		jfe_manual: function(){
			return this.each(function(){
				var __e = $(this);
				__e.jfe_reset ();
				
				var options = __e.data("options");
				var $options = {};
				for (var p in options) {
					$options[p] = options[p];
				}
				$options.manual = false;
				$options.ver = $options.ver.join (",");
				
				__e.jfe($options);
				
			});
		},
		jfe_restart: function(){
			return this.each(function(){
				var __e = $(this);
				__e.jfe_reset ();
				
				var options = __e.data("options");
				var $options = {};
				for (var p in options) {
					$options[p] = options[p];
				}
				$options.ver = $options.ver.join (",");
				
				__e.jfe($options);
				
			});
		},
		jfe_reset: function(){
			return this.each(function(){
				var __e = $(this);
				var key = __e.data("key");
				$("#" + key + "_upgrade").empty().remove();
				__e.html(__e.data ("default"));
				// var options = __e.data ("options");
			});
		},
		jfe_connect: function (key){
			return this.each(function(){
				var __e = $(this);
				var conn = fje.conn[key];
				if (conn) {
					conn.joined = true;
					__e.getObj().data("joined",true);
					fjeready =  fje.ready[key];
					if (fjeready){
						for (i=0;i<fjeready.length;i++) {
							fjeready[i].call(__e[0]);
						}						
						fje.ready[key] = null;
					}
					if (conn.calls.length){
						for (i=0;i<conn.calls.length;i++) {
							__e.jfe_custom(conn.calls[i],conn.pars[i]);
						}						
						conn.calls = [];
						conn.pars = [];
					}
				}
			});
		},
		jfe_custom: function (func,pars){
			return this.each(function(){
				var __e = $(this).getObj();
				if (key = __e.getKey()){	
					var res = null;
					var conn = fje.conn[key];
					if (conn) {
						if (conn.joined) {
							if ($.browser.msie){					
								res = window[key][func](pars);
							}else{
								res = document[key][func](pars);
							}
						} else {
							__e.jfe_push(key,func,pars);	
						}
					}
				}
			});
		},			
		jfe_call: function(pars){
			return this.each(function(){
				var __e = $(this).getObj();
				if (key = __e.getKey()){	
					var conn = fje.conn[key];
					if (conn) {
						if (conn.joined) {
							if ($.browser.msie){					
								window[key]["jfeMessage"](pars);						
							}else{
								document[key]["jfeMessage"](pars);
							}
						} else {
							__e.jfe_push(key,"jfeMessage",pars);	
						}
					}
				}
			});		
		},
		jfe_push: function (key,func,pars){
			return this.each(function(){
				var __e = $(this);
				var conn = fje.conn[key];
				conn.calls.push (func);
				conn.pars.push (pars);
			});			
		},
		jfe_analisy: function (){
			return this.each(function(){
				var __e = $(this);
				__e.find("object").children().each(function(){
					var __p = $(this);
					alert (__p.attr("name"));
					alert (__p.val());
				});
			});
		}						
	});
	
	$.extend({
		fje: {
			ready: {},
			conn: {},
			connect: function (key) {
				if ($.browser.msie){
					var __e = $("#"+key);
				}else{
					var __e = $("[name='" + key + "']");
				}
				__e.jfe_connect(key);
			}
		}
	});
	fje = $.fje;
	 	
})(jQuery);



	/***** STYLESHEET *****/
	$(document).ready(function() {
		$('head').append('<link rel="stylesheet" href="Flash/assets/js/jfe_style.css" type="text/css" />');
	});
	
	/***** DETECTION *****/
	
	var win = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
	// var msie  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
	// var opera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;
	
	function getActiveX(){
		var version;
		var AX;
		var $e;
		// NOTE : new ActiveXObject(strFoo) throws an exception if strFoo isn't in the registry
		try {
			// version will be set for 7.X or greater players
			AX = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
			version = AX.GetVariable("$version");
		} catch ($e) {
		}
		if (!version){
			try {
				// version will be set for 6.X players only
				AX = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");			
				// installed player is some revision of 6.0
				// GetVariable("$version") crashes for versions 6.0.22 through 6.0.29,
				// so we have to be careful. 			
				// default to the first public version
				version = "WIN 6,0,21,0";
				// throws if AllowScripAccess does not exist (introduced in 6.0r47)		
				AX.AllowScriptAccess = "always";
				// safe to call for 6.0r47 or greater
				version = AX.GetVariable("$version");
			} catch ($e) {
			}
		}
		if (!version){
			try {
				// version will be set for 4.X or 5.X player
				AX = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.3");
				version = AX.GetVariable("$version");
			} catch ($e) {
			}
		}
		if (!version){
			try {
				// version will be set for 3.X player
				AX = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.3");
				version = "WIN 3,0,18,0";
			} catch ($e) {
			}
		}
		if (!version){
			try {
				// version will be set for 2.X player
				AX = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
				version = "WIN 2,0,0,11";
			} catch ($e) {
				version = -1;
			}
		}	
		return version;
	}
	
	// JavaScript helper required to detect Flash Player PlugIn version information
	function getInfo(){	
		// NS/Opera version >= 3 check for Flash plugin in plugin array
		var np = navigator.plugins;
		var ua = navigator.userAgent.toLowerCase();
		var info = -1;	
		if (np != null && np.length > 0) {
			if (np["Shockwave Flash 2.0"] || np["Shockwave Flash"]) {
				var software = np["Shockwave Flash 2.0"] ? " 2.0" : "";
				var description = np["Shockwave Flash" + software].description;
				var words = description.split(" ");
				var splits = words[2].split(".");			
				var major = splits[0];
				var minor = splits[1];
				var revision = words[3];
				if (revision == ""){
					revision = words[4];
				}
				if (revision[0] == "d"){
					revision = revision.substring(1);
				} else if (revision[0] == "r"){
					revision = revision.substring(1);
					if (revision.indexOf("d") > 0){
						revision = revision.substring(0, revision.indexOf("d"));
					}
				}
				var info = major + "." + minor + "." + revision;			
			}
		}
		// MSN/WebTV 2.6 supports Flash 4
		else if (ua.indexOf("webtv/2.6") != -1) info = 4;
		// WebTV 2.5 supports Flash 3
		else if (ua.indexOf("webtv/2.5") != -1) info = 3;
		// older WebTV supports Flash 2
		else if (ua.indexOf("webtv") != -1) info = 2;
		else if ( $.browser.msie && win && !$.browser.opera ) {
			info = getActiveX();
		}
		return info;
	}
	
	// When called with major, minor, revision returns true if that version or greater is available
	function getPlugin(vmajor, vminor, vrevision){
		var plugin = {flash:false,version:""};
		info = getInfo ();
		plugin.version = info;
		if (info == -1 ){
			// return false;
		} else if (info != 0){
			if($.browser.msie && win && !$.browser.opera){
				// Given "WIN 2,0,0,11"
				splits         = info.split(" "); 	// ["WIN", "2,0,0,11"]
				tempString        = splits[1];			// "2,0,0,11"
				versionArray      = tempString.split(",");	// ['2', '0', '0', '11']
			}else{
				versionArray      = info.split(".");
			}
			var major      = versionArray[0];
			var minor      = versionArray[1];
			var revision   = versionArray[2];
			// is the major.revision >= requested major.revision AND the minor version >= requested minor
			// alert ("browser "+major+", "+minor+", "+revision);
			if (major > parseFloat(vmajor)){
				plugin.flash = true;
			} else if (major == parseFloat(vmajor)){
				if (minor > parseFloat(vminor))
					plugin.flash = true;
				else if (minor == parseFloat(vminor)){
					if (revision >= parseFloat(vrevision))
						plugin.flash = true;
				}
			}
			// return false;
		}
		return plugin;
	}
	
	function jfeFadeIn ($id,$out) {
		// alert ("jfeFadeIn "+$id);
		$("#" + $id).fadeIn(200,function(){
			var timeout = setTimeout(function(){
				jfeFadeOut($id);
			},$out);
			/*
			var timeout; 
			if ($.browser.msie){
				timeout = window.setTimeout("jfeFadeOut('" + $id + "')",$out);
			} else {
				timeout = setTimeout(jfeFadeOut,$out,$id);
			}
			*/			
		});		
	}
	
	function jfeFadeOut ($id) {
		// alert ("jfeFadeOut "+$id);
		$("#" + $id).fadeOut(200,function(){
			$(this).css("display","none");
			// $(this).remove();
		});	
	}
	
	/*
	 * Special event for image load events
	 * Needed because some browsers does not trigger the event on cached images.
	
	 * MIT License
	 * Paul Irish     | @paul_irish | www.paulirish.com
	 * Andree Hansson | @peolanha   | www.andreehansson.se
	 * 2010.
	 *
	 * Usage:
	 * $(images).bind('load', function (e) {
	 *   // Do stuff on load
	 * });
	 * 
	 * Note that you can bind the 'error' event on data uri images, this will trigger when
	 * data uri images isn't supported.
	 * 
	 * Tested in:
	 * FF 3+
	 * IE 6-8
	 * Chromium 5-6
	 * Opera 9-10
	 */
	(function ($) {
		$.event.special.load = {
			add: function (hollaback) {
				if ( this.nodeType === 1 && this.tagName.toLowerCase() === 'img' && this.src !== '' ) {
					// Image is already complete, fire the hollaback (fixes browser issues were cached
					// images isn't triggering the load event)
					if ( this.complete || this.readyState === 4 ) {
						hollaback.handler.apply(this);
					}
	
					// Check if data URI images is supported, fire 'error' event if not
					else if ( this.readyState === 'uninitialized' && this.src.indexOf('data:') === 0 ) {
						$(this).trigger('error');
					}
					
					else {
						$(this).bind('load', hollaback.handler);
					}
				}
			}
		};
	}(jQuery));

	/*
	jQuery.fn.margin = function() {
	var marginTop = this.outerHeight(true) - this.outerHeight();
	var marginLeft = this.outerWidth(true) - this.outerWidth();
	return {
		top: parseInt(marginTop),
		left: parseInt(marginLeft)
	}};
	*/
	
