jQuery.fn.extend({
	embedSWF: function(settings , pluginOptions ){
			var o = jQuery.extend({ expressInstall:"js/swfobject/expressInstall.swf",version:'8.0.0'}, settings);
			if(swfobject.flagSwitchOff){
				swfobject.switchOffAutoHideShow();
				swfobject.flagSwitchOff = false;
			}
			for(var key in o.flashvars) o.flashvars[key] = encodeURIComponent(o.flashvars[key]);
			
			return this.each(function() {
				
				var swfId = this.id||("swfobject_"+Math.round(Math.random()* 1000));
				var swfName = this.name||swfId;
				var swfWidth = o.width||$(this).width();
				var swfHeight = o.height||$(this).height();
				swfobject.embedSWF(o.src, swfId, swfWidth, swfHeight, o.version,o.expressInstall ,o.flashvars,$.extend({wmode:o.wmode||"opaque"},o.params));
			});
		},
		doFocusInput:function(settings){
		return this.each(function() {
			var me = this;
			var options = jQuery.extend({
				defaultWord:this.defaultValue,
				focusWord:this.defaultValue,
				focus:{
					color:"#242424"
				},
				blur:{
					color:"#666"
				},
				wait:{
					color:"#bbb"
				},
				alert:{
					color:"#FF0000"
				}
			}, settings);
			$("form:has(#" + me.id + ")").bind("submit" , function(){
				var meValue = $( "#"+me.id , this).val();
				if( meValue == "" || meValue==options.defaultWord) {
					$(me).val(options.focusWord||options.defaultWord);
					$(me).css(options.alert).stop().animate(options.focus,5000);
					return false;
				}
			});
			
			$(this).css(options[($.trim(this.value) == options.defaultWord || $.trim(this.value)=="")?"blur":"focus"])
			.val(($.trim(this.value)=="")?options.defaultWord:this.value)
			.bind("focus",function(){
				if($.trim(this.value) == options.defaultWord || $.trim(this.value)==""){
					this.value= options.focusWord;
					$(this).setCursorAtBegin();
					$(this).css(options.wait);
				}else{
					$(this).css(options.blur);
					this.select();
				}
			})
			.bind("keyup",function(){
				if($.trim(this.value) == ""){
					this.value = options.focusWord;
					$(this).css(options.wait);
					$(this).setCursorAtBegin();
				}
			})
			.bind("keydown",function(){
				
				if($.trim(this.value) == options.defaultWord || $.trim(this.value) == options.focusWord ){
					$(this).css(options.focus);
					this.value = "";
				}else{
					$(this).css(options.focus);
				}
			})
			.bind("blur",function(){
				if($.trim(this.value) == "" || $.trim(this.value) == options.defaultWord || $.trim(this.value) == options.focusWord ){
					this.value = $.trim(options.defaultWord);
					$(this).css(options.blur);
				}else{
					$(this).css(options.focus);
				}
			});
		});
	},
	setCursorAtEnd:function (){
		return this.each(function() {
			var node = this;
			$(node).setCursor(0);
			setTimeout(function(){
				 $(node).setCursor(this.value.length);
			},100);
		});
	},
	setCursorAtBegin:function (){
		return this.each(function() {
			var node = this;
			$(node).setCursor(0);
			setTimeout(function(){
				$(node).setCursor(0);
			},100);
		});
	},
	setCursor:function (pos){
		return this.each(function() {
			var node = this;

			if(!node){
				return false;
			}else if(node.createTextRange){
				var textRange = node.createTextRange();
				textRange.collapse(true);
				  textRange.moveEnd('character',pos);
				  textRange.moveStart('character',pos);
				textRange.select();
				return true;
			}else if(node.setSelectionRange){
				node.focus();
				node.setSelectionRange(pos,pos);
				return true;
			}
			return false;
		});
	}
});

var newWindow;
function openPopup(popup_url,popup_width,popup_height,var_name){
	var windowHeight = popup_height;
	var windowWidth = popup_width;
	var windowName = var_name;
	var windowUri = popup_url;

	var centerWidth = (window.screen.width - windowWidth) / 2;
	var centerHeight = (window.screen.height - windowHeight) / 2;

	if ( !newWindow || newWindow.closed ){
		try{
			var dimension = "";
			if(windowHeight && windowWidth){
				dimension = ',width=' + windowWidth + 
				',height=' + windowHeight + 
				',left=' + centerWidth + 
				',top=' + centerHeight;
			}
		
			newWindow = window.open(windowUri, windowName,'addressbar=0,menubar=0,location=0,status=0,scrollbars=yes,resizable=yes'+dimension);
				//newWindow.location.href; // throws exception if after reload
				//newWindow.location = windowUri;
		}catch(e){};
	}else if ( newWindow && ! newWindow.closed ){
		newWindow.location.replace(windowUri);
		newWindow.focus();
	}
}

