(function($) {
	$.fn.translate = function(args) {
		this.args = args;
		var t = this, dl = document.location.toString(), mst = '<div id="MicrosoftTranslatorWidget" style="width: 100%; min-height: 57px; border-color: #3A5770; background-color: #78ADD0;"><a ref="http://www.microsofttranslator.com/bv.aspx?a=' + escape(document.location) + '">Translate this page</a><br />Powered by <a ref="http://www.microsofttranslator.com">Microsoft® Translator</a></div>';
		
		this.tr = function ()
		{
			var n = (dl.indexOf('notr=1') != -1) ? false : true;			
			if (dl.indexOf('utilities') != -1)
				n = ($('#notr').length == 0) ? true : false;
			return n;
		}

		this.gready1 = function ()
		{
			try 
			{
				new google.translate.TranslateElement();
			}
			catch (e)
			{
                    		setTimeout(t.gready1, 500);
                    		return;
			}
			t.gready2();
		}

		this.gready2 = function ()
		{
            		var nt = dl + ((dl.indexOf('?') == -1) ? '?' : '&') + 'notr=1', c = 0;
                    	$('.goog-te-banner-frame').contents().find("tr").each(function(){
                        	if (this.id == ':1.finishSection')
                            	{
                                	var bs = this.getElementsByTagName('button');
                                	try { 
                            	    		bs[0].onclick = function () 
                            	    		{ 
                    	    	    			top.location = nt; 
                            	    		} 
                            	    	}
                            	    	catch(e){}
				}
                    		c++;
            		});
            		if (c == 0)
            		{
                    		setTimeout(t.gready2, 1000);
            		}
    			else
            		{
                    		$('.goog-te-banner-frame').contents().find("a").each(function(){
                            		if (this.id == ':1.close')
                        		{
                                    		this.onclick = function () 
                                    		{ 
    	        	            	    	    	if (dl.indexOf('utilities') == -1)
        	                	        	{
                                    				top.location = nt; 
		                                	}
            			                	else
                            				{
                            					var r = (/userid\=([^\&]+)\&xs\=([\d]+)/).exec(document.location);
                            					$.get('/utilities/translatortoggle.dhtml?' + r[0], {json: 1, action : 'update', session_end: 1}, function (){});
                                				$('.goog-te-banner-frame').hide();
		                                	}
                                    		}
                            		}
                    		});
                    		if(t.args && t.args.auto)
                    		{
					t.getPrefLang(function () {
						var f = true;
                    				var l = $('.goog-te-banner-frame').contents().find('.goog-te-menu-value span').text().toLowerCase().substr(0, 2);
                    				if (t.args)
                    				{
                    					if (l == t.args.lang)
                    						f = false;
                    				}
	                    			if (!f) return;
	                    			$('.goog-te-banner-frame').contents().find("button").each(function(){
    	                				if (this.id == ':1.confirm')
        	            				{
            							$(this).trigger('click');
                    					}
                    				});
                    				
                    			});
                    		}
            		}
		}

		this.msready1 = function ()
		{
			if ($('#MSTWGoButton').length == 0) return;
			t.getPrefLang(function () {
				try {
					var r = null, t = null, t1 = null;
					$('#MSTWMenu option').each(function () {
						if (this.value == tr_lang)
						{
							r = tr_lang;
						}
						else if (this.value == tr_lang_ext)	
						{
							r = tr_lang_ext;
						}
					});
					
					if (r)
					{
						$('#MSTWMenu').val(r);
						t = setInterval(function(){
							$('#MSTWMenu').val(r);
						}, 10);
						
						setTimeout(function (){
							clearInterval(t);
							$('#MSTWGoButton').trigger('click');
						}, 5000);

						setTimeout(function (){
							clearInterval(t1);
						}, 50000);
					}
				}catch(e){};
			});
			clearInterval(t.interval);
		}
		
		this.getPrefLang = function(callback)
		{
			$.getScript(((location.href.indexOf('https') == 0) ? 'https' : 'http') + '://www.website.ws/utilities/prefered_language.dhtml', function () {
				callback();
			});
		}
		
		this.load = function ()
		{
			if (!this.tr()) return;
			var lang = (dl.indexOf("language=spanish") == -1 && dl.indexOf("language=espanol") == -1) ? 'en' : 'es';
			var type = ($.browser.msie || navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPod/i)) ? 'ms' : 'gt';
			
			if (t.args.type)
				type = t.args.type;
	
			if (type == 'ms')
			{
				$.getScript (((location && location.href && location.href.indexOf('https') == 0) ? "https://ssl.microsofttranslator.com" : "http://www.microsofttranslator.com" ) + "/ajax/v2/widget.aspx?mode=monual&from=" + lang + "&layout=ts", function() {
					$(mst).insertBefore ($(document.body.firstChild));
    	                		if(t.args && t.args.auto)
    	                		{
    	                			t.interval = setInterval(t.msready1, 1000);
					}
				});
			}
			else
			{
				$.getScript ('//translate.google.com/translate_a/element.js', function() {
					t.gready1();
				});
			}		
		}
		
		this.load();
	};
})(jQuery);
