(function($) {
	$.vPhoneCount = 0;
	
	var methods = {
		vPhone: function(options) {
			$.vPhoneCount++;
			
			var config = {
				ready: null,
				noServer: null,
				status: null,
				reg: null,
				incoming: null,
				disconnect: null,
				connect: null,
        micStatus: null,
				swfPath: "flash",
				quality: "high",
				cssPrefix: "jqvp",
				width: 215,
				height: 138,
				position: "static",
				bgcolor: "#ffffff"
			};

			$.extend(config, options);

			var configWithoutOptions = {
				id: $(this).attr("id"),
				swf: config.swfPath + ((config.swfPath != "") ? "/" : "") + "vphone.swf",
				fid: config.cssPrefix + "_flash_" + $.vPhoneCount,
				hid: config.cssPrefix + "_force_" + $.vPhoneCount
			};

			$.extend(config, configWithoutOptions);

			$(this).data("vPhone.config", config);
			
			var events = {
				login: function(e, telephone, username, password, realm, server, red5host, red5url) {
          var fid = $(this).data("vPhone.config").fid;
          var m = $(this).data("vPhone.getMovie")(fid);
					m.fl_login(telephone, username, password, realm, server, red5host, red5url);
				},
				logout: function(e) {
          var fid = $(this).data("vPhone.config").fid;
          var m = $(this).data("vPhone.getMovie")(fid);
					m.fl_logout();
				},
				call: function(e, dest) {
          var fid = $(this).data("vPhone.config").fid;
          var m = $(this).data("vPhone.getMovie")(fid);
					m.fl_call(dest);
				},
				dtmf: function(e, chr) {
          var fid = $(this).data("vPhone.config").fid;
          var m = $(this).data("vPhone.getMovie")(fid);
					m.fl_dtmf(chr);
				},
				accept: function(e) {
          var fid = $(this).data("vPhone.config").fid;
          var m = $(this).data("vPhone.getMovie")(fid);
					m.fl_accept();
				},
				hangup: function(e) {
          var fid = $(this).data("vPhone.config").fid;
          var m = $(this).data("vPhone.getMovie")(fid);
					m.fl_hangup();
				},
				settings: function(e) {
          var fid = $(this).data("vPhone.config").fid;
          var m = $(this).data("vPhone.getMovie")(fid);
					m.fl_settings();
				},
				setVolume: function(e, vol) {
          var fid = $(this).data("vPhone.config").fid;
          var m = $(this).data("vPhone.getMovie")(fid);
					m.fl_set_volume(vol);
				},
				setMicVolume: function(e, vol) {
          var fid = $(this).data("vPhone.config").fid;
          var m = $(this).data("vPhone.getMovie")(fid);
					m.fl_set_mic_volume(vol);
				}
			};
			
			for(var event in events) {
				var e = "vPhone." + event;
				$(this).unbind(e);
				$(this).bind(e, events[event]);
			}

			var getMovie = function(fid) {
				if ($.browser.msie) {
					return window[fid];
				} else {
					return document[fid];
				}
			};
			$(this).data("vPhone.getMovie", getMovie);
			
      var flashvars = {
        id: config.id,
        fid: config.fid
        //speex: "no"
      };
      var params = {
        allowscriptacces: "always",
        quality: "high",
        bgcolor: config.bgcolor,
        wmode: "window"
      };
      var attrs = {
        id: config.fid,
        name: config.fid
      };

      $("<div/>").attr("id", config.fid).appendTo(this);
      swfobject.embedSWF(config.swf, config.fid, config.width, config.height, "9.0.0", null, flashvars, params, attrs);

			var html_hidden = '<div id="' + config.hid + '"></div>';
			$(this).append(html_hidden);
			
			$(this).css('position', config.position);

      if (config.top != "")
        $(this).css('top', config.top);

      if (config.bottom != "")
        $(this).css('bottom', config.bottom);

      if (config.left != "")
        $(this).css('left', config.left);

      if (config.right != "")
        $(this).css('right', config.right);

			$("#"+config.hid).css({'text-indent':'-9999px'});
			
			return $(this);
		},
		
		login: function(telephone, username, password, realm, server, red5host, red5url) {
			$(this).trigger("vPhone.login", [ telephone, username, password, realm, server, red5host, red5url] );
			$(window).bind("beforeunload", function(e) { $(this).trigger("vPhone.logout"); });
		},
		
		logout: function() {
			$(this).trigger("vPhone.logout");
		},
		
		call: function(dest) {
			$(this).trigger("vPhone.call", dest);
		},
		
		dtmf: function(chr) {
			$(this).trigger("vPhone.dtmf", chr);
		},
		
		accept: function() {
			$(this).trigger("vPhone.accept");
		},
		
		hangup: function() {
			$(this).trigger("vPhone.hangup");
		},
		
		settings: function() {
			$(this).trigger("vPhone.settings");
		},
		
		setVolume: function(vol) {
			$(this).trigger("vPhone.setVolume", vol);
		},
		
		setMicVolume: function(vol) {
			$(this).trigger("vPhone.setMicVolume", vol);
		},
		
		_vphone_ready: function() { // called from Flash
			var config = $(this).data("vPhone.config");
			var readyFn = config.ready;
			if(readyFn != null) {
				readyFn();
			}
		},

		_vphone_no_server: function(msg) { // called from Flash
			var config = $(this).data("vPhone.config");
			var noServerFn = config.noServer;
			if (noServerFn != null) {
				noServerFn(msg);
			}
		},
		

		_vphone_status: function(msg) { // called from Flash
			var config = $(this).data("vPhone.config");
			var statusFn = config.status;
			if (statusFn != null) {
				statusFn(msg);
			}
		},
		
		_vphone_registered: function() { // called from Flash
			var config = $(this).data("vPhone.config");
			var regFn = config.reg;
			if (regFn != null) {
				regFn();
			}
		},
		
		_vphone_connected: function() { // called from Flash
			var config = $(this).data("vPhone.config");
			var connectFn = config.connect;
			if (connectFn != null) {
				connectFn();
			}
		},
		
		_vphone_disconnected: function() { // called from Flash
			var config = $(this).data("vPhone.config");
			var disconnectFn = config.disconnect;
			if (disconnectFn != null) {
				disconnectFn();
			}
		},
		
		_vphone_incoming: function(src) { // called from Flash
			var config = $(this).data("vPhone.config");
			var incomingFn = config.incoming;
			if (incomingFn != null) {
				incomingFn(src);
			}
		},
		
		_vphone_mic_status: function(muted) { // called from Flash
			var config = $(this).data("vPhone.config");
			var micStatusFn = config.micStatus;
			if (micStatusFn != null) {
				micStatusFn(muted);
			}
		}
	};

	$.each(methods, function(i) {
		$.fn[i] = this;
	});
})(jQuery);

