(function(){
	
	
		GM.validationobject = new GM_Validation('form1', {
			immediate: true,
			messageOverlibInset: 'immediateancestor',
			onSubmit: false,
			changeDefaultAnchorPosition: true,
			defaultAnchorPosition: {
				posclass: 'form_formlabel_12'
			}
		});
		var RecaptchaOptions = {
			theme: 'clean',
			tabindex: 2
		};
		
		if (lightwin != undefined && lightwin != null) {
			lightwin.addMethods({
				fireCaptcha: function(link){
					var linkpassed = arguments[1];
					var wrapper = $('form_wrapper');
					
					if (this.activated && linkpassed.value == "Done") {
						this._addLoadingWindowMarkup();
						this._clearWindowContents(false);
						
						new Effect.Fade(wrapper, {
							from: 1.0,
							to: 0.0,
							duration: 0.2,
							afterFinish: function(){
								if (this.submittedInitial != undefined && this.submittedInitial) {								
									if(Prototype.Browser.IE){
										GM.download.submitProfile(link, linkpassed);
									}else{
										GM.download.submitProfile(link);
									}									
								}
								else {
									GM.validationobject.refresh();
									var formsetUp = wrapper.down('#gm_formSetup').hide();
									
									var captcha = wrapper.down('#gm_recaptcha').show();
									$$('.noticeoptions').invoke('hide');
									var dims = this.getHiddenElementDims(wrapper);
									linkpassed.value = "Finished";
									this.resizeTo.height = dims.height;
									this.resizeTo.width = dims.width;
									this.resizeTo.parentheight = this.resizeTo.height + this.options.contentOffset.height;
									this.resizeTo.parentwidth = this.resizeTo.width + this.options.contentOffset.height;
																		
									var elems = {};									
									
									if(!Prototype.Browser.IE){
										elems.elemToStop = $A([link.element()]);
										this.clearListeners(elems);	
									}
									
									this._objectAnimation();
									
									if (this.submittedInitial == undefined) {
										this.submittedInitial = true;
									}
								}
							}.bind(this)
						});
					}
					else {
						if (linkpassed.value == "Finished") {
							GM.download.submitProfile(link);
						}
					}
				},
				
				genericAction: function(caseval){
					switch (caseval) {
						case "highlight":
							$('recaptcha_widget_div').down('input#recaptcha_response_field').addClassName('highlightlab');
							$$('input.highlightlab').invoke('observe', 'focus', highlightlabel);
							$$('select.highlightlab').invoke('observe', 'click', highlightlabel);
							$$('input.highlightlab').invoke('observe', 'blur', removerlib);
						break;
						
						case "downtype":
							var downloadType = GM.lt.presentTrigger.readAttribute('downtype');
							
							if(downloadType != null){
								var contentForm = GM.lt.popontentWrap.down('FORM');
								input = contentForm['downloadtype'];
								Form.Element.setValue(input, downloadType);
							}
							
						break;
					}
				},
				submitSignin: function(link){
					GM.download.submitProfile(link);
				},
				actSigninForm: function(link){
					var content = this.popontentWrap.down().remove();
					content.hide();
					content.addClassName('cached');
					Element.insert(document.body, {
						bottom: content
					});
					
					this._addLoadingWindowMarkup();
					this._clearWindowContents(false);
					
					var signinObj = {
						contentverify: '',//wrapper.innerHTML,
						postreg: $('downloadtype').value
					}
					
					var processInfo = new AJAXgetinfo('getsigninForm', {
						source: '/gmasync/getsigninform'
					}, {
						pars: signinObj
					});
					AJAXgetinfo.addMethods({
						afterProcess: function(data){
							var results = data.responseText.evalJSON();
							
							var message = results.signinform;
							this.popontentWrap.update(message);
							this.popontentWrap.down().hide();
							var dims = this.getHiddenElementDims(this.popontentWrap.down());
							
							this.resizeTo.height = dims.height;
							this.resizeTo.width = dims.width;
							this.resizeTo.parentheight = this.resizeTo.height + this.options.contentOffset.height;
							this.resizeTo.parentwidth = this.resizeTo.width + this.options.contentOffset.height;
							
							this.clearListeners({
								'elemToStop': $A([link.element()])
							});
							this._objectAnimation();
							
						}.bind(this)
					});
					processInfo.getinfoGen2();
				}
			});
		
	}

	if (Download != undefined && Download != null) {
		Download.addMethods({
			loading: function(){
				GM.lt._addLoadingWindowMarkup();
				GM.lt._clearWindowContents(false);
			},
			
			postDownload: function(data){
				var response = data.responseText.evalJSON();
				var loadingMask = this.FUNCTION_CACHE_MAPPING.get('loadinginst');
				
				if (loadingMask != undefined) {
					loadingMask.fadeMask();
				}
				
				
				this.refreshFormFieldColors();
				
				if (!response.err) {
					this.finishDownload = true;
					
					if (response.confirm != undefined) {
						this.setPixelConfirm(response.confirm);
					}
					
					if(response.tracking != undefined){
						this.setTracking(response.tracking);
					}
					
					if(GM.lt != undefined && GM.lt.activated){
						GM.lt.deactivate();
					}
					
					if (response.type == this.productType.foxaddon) {						
						this.extensionDownloadNotice(response);
					}
					
					if (response.type == this.productType.browser) {
						this.browserDownloadNotice(response);
					}
					else {
						if (response.redirect) {
							document.location = response.url;
							return;
						}
					}
				}
				else {
				
					$(response.errcontainer).addClassName('ui-state-error');
					this.testandReloadCaptcha(response.errcollection);
					
					var formsetup = $('gm_formSetup');
					
					if (formsetup != null) {
						$('gm_formSetup').show();
					}
					
					str = this.createErrorString(response.errcollection);
					$(response.errcontainer).update(str);
					$$('.noticeoptions').invoke('show');
					
					if (response.formaction == 'submit-signin') {
						var dims = GM.lt.getHiddenElementDims(GM.lt.popontentWrap.down());
					}
					else {
						if (response.formaction == 'submit-join') {
							var wrapper = $('form_wrapper');
							wrapper.down('#gm_formSetup').show();
							
							var captcha = wrapper.down('#gm_recaptcha').show();
							var dims = GM.lt.getHiddenElementDims(wrapper);
							this.actionTrigger.value = "Done";
						}
					}
					
					GM.lt.resizeTo.height = dims.height;
					GM.lt.resizeTo.width = dims.width;
					GM.lt.resizeTo.parentheight = GM.lt.resizeTo.height + GM.lt.options.contentOffset.height;
					GM.lt.resizeTo.parentwidth = GM.lt.resizeTo.width + GM.lt.options.contentOffset.height;
					
					GM.lt.clearListeners({
						'elemToStop': $A([this.actionTrigger])
					});
					
					GM.lt._objectAnimation.apply(GM.lt);
					
					//TODO:throwing an error debug
					this.FUNCTION_CACHE_MAPPING.get('popupdiaginst').bodyOverflow().bind(this);
				}
			}
		});
	}	
		
	function removerlib(e){
		Event.stop(e);
		targetelemid = e.element().identify();
		GM.overlib.disabletip(targetelemid);
	}
	
	function highlightlabel(evt){		
		var elem = evt.element();
		Event.stop(evt);
		var highlightcaptcha = false;
		
		if (elem.nodeName != 'SELECT') {		
			$$('input.highlightlab').each(function(el){
				if(el.id == 'recaptcha_response_field'){
					el.up('div#recaptcha_widget_div').up(0).down('label').style.color = '#999999';	
				}else{
					el.up(0).down('label').style.color = '#999999';	
				}			
			});
			
			if (elem.id == 'recaptcha_response_field') {
				return;
				var captchaWidget = elem.up('div#recaptcha_widget_div');
				captchaWidget.up(0).down('label').style.color = '#2E4292';
				highlightcaptcha = true;
			}else{
				elem.up(0).down('label').style.color = '#2E4292';
			}
			
			var messageid = '_' + elem.id;
			var keywordHint = $(messageid).innerHTML;
			
			
			var bubbledark = $('bubbledark').clone(true);
			bubbledark.show();
			bubbledark.down('p#bubble_content').innerHTML = keywordHint;
			var div = new Element('div', { 'class': 'table-wrapper' });
			var popelem =  bubbledark.wrap(div);
				
			GM.overlib.generateoverlib({unique:true},
				{	
					bubbleselect:'anchor', 
					innercontent:popelem.innerHTML, 
					elem:elem, 
					bubbleidpostfix:elem.identify(),
					regex:''	
				}
			);
			
		}else{
			$$('input.highlightlab').each(function(el){
				el.up(0).down('label').style.color = '#999999';
			});
		}
	}
})();

function checkDefaultSearchEngine(e){
	var elem = e.element();
	
	if(elem.checked){			
		return;
	}			
	
	var diagActions = {
		actionparams : {lightwin:GM.lt},
		okAction:function(actparams){
			//GM.lt.gmRecaptcha = $('gm_recaptcha_clone');
			var searchwrap = GM.lt.FUNCTION_CACHE_MAPPING.get('popupdiaginst_captcha').dialogbody.down().remove();
			Element.insert(GM.lt.popontentWrap.down(), {bottom:searchwrap.hide()});
			GM.lt.FUNCTION_CACHE_MAPPING.get('popupdiaginst_captcha').terminateDialog();
		}.bind(GM.lt)
	}
	
	var message = $('searchengine-select-content').remove();

	params = {header:'Choose you default search engine?', message:message.show(), actions:diagActions, doverlay:'down_overlay', buttonnames:{'ok':'Finish'}};
	options = {width:300, buttons:['ok'], override:true, overflow:true, animations:false};
			
	var popdiag = new Popup_dialog(options, params);
	GM.lt.FUNCTION_CACHE_MAPPING.set('popupdiaginst_captcha', popdiag);
}