
function WaitBar(container , options){
	this.init(container , options);
};

WaitBar.prototype = {
	
	container:null ,
	imageURL:null,
	text:'' ,
	
	setImage:function(name){
		var url = Vars.get('public_url') + '/images/icons';
		switch(name){
		case 'large':
			this.imageURL = url + '/wait-large.gif';
			break;
		case 'small':
		default:
			this.imageURL = url + '/wait.gif';
		}
	},
	setText:function(text){
		this.text = text;
	},
	
	show:function() {
		var img = new Image();
		img.src=this.imageURL;
		this.container.update(img);
		
		if (typeof this.text == 'object'){
			this.container.insert( this.text );
		}else{
			this.container.insert('<span>' + this.text + '</span>');
		}
	},
	hide:function(){
		this.container.hide();
	},
	remove:function(){
		this.container.remove();
	},
	
	init:function(container , options){
		this.container = $(container);
		this.setImage( (options.image) ? options.image : 'small'  );
		this.setText( (options.text) ? options.text : '' )
	}
};
	var contenterr_txt = "Please enter text or attach a document for translation";
	var sourceerr_txt = "Please specify source language";
	var targeterr_txt = "Please specify target language";
	var counterr_txt = "Please enter a valid word count";
	var verify_count = true;
	var fileCounter = 1;
	var textBackground = "step_2_enter_background";
	var uploadBackground = "step_2_upload_background";
	var filesArray = new Array(0,0,0,0,0);
	var testimonials;
	var testimonialCoutner = 0;
	var url = Vars.get('url');
	var template
	
	
	function addFile(){
		var index = filesArray.indexOf(0);
		if ( index >= 0 ){
			fileCounter++;
			e = $('files_container');
			div = document.createElement('div');
			div.setAttribute('id','file_'+index);
			span = document.createElement('span');
			span.setAttribute('id','file_status_'+index );
			input = document.createElement('input');
			input.setAttribute('type','file');
			input.setAttribute('name','post_file'+index);
			input.setAttribute('size','17');
			input.setAttribute('id','post_file'+index);
			$(input).observe('change',function(){upload(index)});
			
			//link = document.createElement('a');
			//link.setAttribute('href','javascript:removeFile('+index+');');
			//txt = document.createTextNode("X");
			//link.appendChild(txt);
		
			div.appendChild(input);
			//if (fileCounter - 1 > 0) div.appendChild(link);
			div.appendChild(span)
			e.appendChild(div);
			filesArray[filesArray.indexOf(0)]=div;
			attachUploadEventToFileElement(index);

		}
		if (index >=5 ){
			$('add_file_link').style.display="none";
		}
	}
	
	function removeFile(num){
		re = $('file_'+num);
		if (num==0){
			$('file_status_0').remove();
			return;
		}
		if (re != null){
			$('files_container').removeChild(re);
			fileCounter--;
			filesArray[num]=0;
		}
		if (fileCounter < 5 ){
			$('add_file_link').style.display="inline";
			if (filesArray.indexOf(0) == 1){	
				
			}
		}
		
	}
	
	function showFiles(){
		$('filesTab').addClassName('active');
		$('textTab').removeClassName('active');
		$('step2_content_files').show();
		$('step2_content_text').hide();
		$('text_word_count').hide();
		
	}

	function showText(){
		$('filesTab').removeClassName('active');
		$('textTab').addClassName('active');
		$('step2_content_files').hide();
		$('step2_content_text').show();
		$('text_word_count').show();
		
	}


	function recalculate_word_count() {
		var content_elem = document.getElementById('content');
		//var string = tinyMCE.getContent();
		
		var string = content_elem.value ;
		r1 = new RegExp('[\u3000-\u4DFF]','g');
		r2 = new RegExp('[\u4E00-\u9FFF]','g');
		string = string.replace(r1,' {PNK} ');
		string = string.replace(r2,' {CJK} ');
		//string = string.replace(/(<([^>]+)>)/ig,"") ;
		string = string.replace(/(\(|\)|\*|\||\+|\"|\'|_|;|:|,|\.|\?)/ig," ") ;
		string = string.replace(/\s+/ig," ");
		//string = string.replace(/_+/ig," ");
		var a = string.split(/[\s+|\\|\/]/g); 
	  	
	  	var count = 0;
	  	var pnkCounter = 1;
	  	for (var i=0;i<a.length;i++){
	  		if (a[i]=='{PNK}' && pnkCounter % 3 == 0){
	  			count++;
	  			pnkCounter++;
	  		}else if(a[i]=='{PNK}'){
	  			pnkCounter++;
	  		}else if (a[i].length>0){
	  			count++;
	  		}
	  	}
	  var field_elem = document.getElementById('rc_cwp_meta_Words_Count');
	  field_elem.value = count ;
	  
		return false;
	}

	function verify_edit(element){
		if (!element.value){
			if (!$('err-content')){
				var h = $(document.createElement('div'));
				h.id='err-content';
				h.addClassName('err-msg-red');
				h.update(Vars.get('Please enter text for translation or upload files'));
				h.style.width = "450px";
				$('step2_content_text').appendChild(h);
			}
			
			new Effect.Highlight('err-content');
			return false;
		}
		if ($('err-content')){
			$('err-content').remove();
		}
		return true;
				
	}
	function verify_lang(element){
		if (!element.value){
			
			return false;
		}
		
		
		//checkLanguagePairAvailability();
		
		return true;
				
	}

	function toggleLeftButton(status){
		var url = Vars.get('template_url');
		if (status){
			$("button-left").observe('click',function (){
				return submitTranslation();
			});
			$("button-left").style.backgroundImage = "url("+Vars.get('template_url')+"/images/newhp/button.jpg)";
			$("button-left").style.cursor="pointer";
		}else{
			$("button-left").setAttribute('onclick','');
			$("button-left").stopObserving('click');
			$("button-left").style.backgroundImage = "url("+Vars.get('template_url')+"/images/newhp/button2.jpg)";
			$("button-left").style.cursor="default";
		}
	}
	function toggleMiddleButton(status){
		if (status){
			$("button-middle").observe('click',function (){
				return submitTranReview();
			});
			$("button-middle").style.backgroundImage = "url("+Vars.get('template_url')+"/images/newhp/button.jpg)";
			$("button-middle").style.cursor="pointer";
			
		}else{
			$("button-middle").setAttribute('onclick','');
			$("button-middle").stopObserving('click');
			$("button-middle").style.backgroundImage = "url("+Vars.get('template_url')+"/images/newhp/button2.jpg)";
			$("button-middle").style.cursor="default";
		}
	}

	function toggleRightButton(status){
		if (status){
			$("button-right").observe('click',function (){
				return submitExpert();
			});
			$("button-right").style.backgroundImage = "url("+Vars.get('template_url')+"/images/newhp/button.jpg)";
			$("button-right").style.cursor="pointer";
			$('expertiseNotice').hide();
			
		}else{
			$("button-right").setAttribute('onclick','');
			$("button-right").stopObserving('click');
			$("button-right").style.backgroundImage = "url("+Vars.get('template_url')+"/images/newhp/button2.jpg)";
			$("button-right").style.cursor="default";
			$('expertiseNotice').show();
		}
	}
	
	function checkLanguagePairAvailability(){
		var target = new Array();
		var targetCount = 0;
		$$('[name="Target_Language[]"]').each(function(element){
			
			if (element.value && element.allowSubmit){
				target[target.length] = element.value;
				targetCount++;
			}else{
				target[target.length] = '';
			}
		});
		if ($('source-lang').value && target.length > 0 && targetCount){
			
			new Ajax.Request(Vars.get('url') + '/service/checkLanguagePair/' , {
				method : 'post',
				parameters : {s : $('source-lang').value , "t[]" : target },
				evalJSON: true ,
				onSuccess : function (transport){
					languageAvailabilityResponse(transport.responseJSON);
					expertiseAvailabilityResponse(transport.responseJSON);
				}
			});
			
		}
		
		
	}

	
	function languageAvailabilityResponse(json){
		//alert(json);
		if (!json) return;
		newLanguagesErrorMessage(json);
		//expertiseAvailabilityResponse(json);
		/*
		switch (json.availability){
		case 0:
			toggleLeftButton(false);
			toggleMiddleButton(false);
			break;
		case 2:
			break;
		case 1:
		case 3:
		case 4:
			toggleLeftButton(true);
			toggleMiddleButton(true);
			break;
		}
		*/
	}
	function expertiseAvailabilityResponse(json){
		if (!json) return;
		var selectionEnabled=false;
		var select = $(document.createElement('select'));
		select.setAttribute('name','expertise');
		select.setAttribute('id','expertiseList');
		select.style.width="100%";
		var option = $(document.createElement('option'));
		option.value='';
		option.appendChild(document.createTextNode(Vars.get('Field of Expertise')));
		select.appendChild(option);
		for (i=0; i<json.expertise.length; i++){
			var option = $(document.createElement('option'));
			option.value=json.expertise[i].id;
			option.appendChild(document.createTextNode(json.expertise[i].name));
			if (json.availableExp.flatten().indexOf(json.expertise[i].id)>-1){
				selectionEnabled = true;
				//alert(Prototype.Browser.IE7);
				if ( Prototype.Browser.IE6 || Prototype.Browser.IE7){
					select.insert(option);
				}
			}else{
				
				option.setAttribute('disabled','disabled');
			}
			if ( !Prototype.Browser.IE6 && !Prototype.Browser.IE7 ){
				select.insert(option);
			}
			
			
			
			
			
		}
		
		if ($('expertiseListContainer')){
			if (selectionEnabled){
				
				$('expertiseListContainer').update('');
				$('expertiseListContainer').appendChild(select);
				select.observe('change',validateExpertise);
				//toggleRightButton(true);
			}else{
				form.removeFormError($('expertiseList'));
				
				$('expertiseListContainer').update('');
				//toggleRightButton(false);
			}
		}
	}

	function languagesErrorMessage(msg){
		
	}
	
	function newLanguagesErrorMessage(json){
		var enableAny = false;
		var enableExpert = false;
		if (!json.availability) return;
		json.availability.each(
			function (available,index){
				
				var icon = $$('.alertIcon')[index];
				var message = json.message[index];
				var disable = false;
				image = $(document.createElement('img'));
				switch (available){
				
				case 0 : 
					var imageName = 'error.png'; 
					disable = true;
					break;
				case 1:
				case 4:
					var imageName = 'alert.png'; 
					enableAny = true;
					break;
				case 3:
					enableExpert = true;
				case 2:
					
					enableAny = true;
					break;
				default: return;
				}
				if (imageName){
					image.src = image.src = Vars.get('url') + '/public/images/icons/attention_18.png';
					image.style.position = "relative";
					icon.update(image.addClassName('inlineImg'));
					if (Tip && message){
						new Tip(image , message , {
							style: 'protoblue',
							images: Vars.get('template_url')+'/images/prototip/styles/protoblue/',
							hook: { target: 'rightMiddle' , tip: 'leftMiddle' },
							//offset: { x:0 , y: -20 },
							stem: 'leftMiddle' 
						});
					}
				}else{
					icon.update('');
				}
				if (disable){
					form.target()[index].allowSubmit = false;
				}
					
				//toggleLeftButton(enableAny);
				//toggleMiddleButton(enableAny);
		});
		
	}
	
	function showSubmitWaitMessage(parentElement){
		var image = new Image();
		image.src = Vars.get('url') + '/public/images/icons/wait-large.gif';
		image.style.margin = "10px";
		image.style.verticalAlign = "middle";
		span = document.createElement('span');
		$(span).update(Vars.get('Sending...'));
		$(span).addClassName('f18');
		$(parentElement).style.background = "none";
		$(parentElement).style.height = "64px";
		
		
		
		if ($(parentElement).select('#expertiseListContainer').length > 0){
			$(parentElement).parentNode.insert( $('expertiseListContainer') );
		}
		
		
		$(parentElement).update(image);
		
		$(parentElement).appendChild(span);
		
		
		/*new WaitBar(parentElement , {
			text: span , 
			image: 'large'
		}).show();*/
	}
	
	function submitTranslation(){
		if (!validateForm()) return false;
		
		showSubmitWaitMessage($('translateButton').parentNode);
		var h = $(document.createElement('input'));
		h.setAttribute('type','hidden');
		h.setAttribute('name','submit-translation');
		h.value='submit-translation';
	
			
		document.forms['wizard'].appendChild(h);
		return submitForm();
		
	}
	function submitTranReview(){
		
		if (!validateForm()) return false;
		
		showSubmitWaitMessage($('comboButton').parentNode);
		var h = $(document.createElement('input'));
		h.setAttribute('type','hidden');
		h.setAttribute('name','submit-combo');
		h.value='submit-combo';
		document.forms['wizard'].appendChild(h);
		return submitForm();
		
	}


	function submitExpert(){
		
		if (!validateExpertise()) return false;
		if (!validateForm()) return false;
		
		showSubmitWaitMessage($('expertButton').parentNode);
		var h = $(document.createElement('input'));
		h.setAttribute('type','hidden');
		h.setAttribute('name','submit-expert');
		h.value='submit-expert';
		document.forms['wizard'].appendChild(h);
		return submitForm();
		
	}


	function submitForm(){
		
			form.clearNotAllowed();
			document.wizard.submit();
			return true;
		
	}
	function validateForm(){
		valid = true;
		valid &= form.validateSourceLanguage();
		valid &= form.validateTargetLanguage();
		valid &= form.validateContent()
		
		return valid;

	}

	function validateExpertise(){
		if (!$('expertiseList')) return false;
		if (!$('expertiseList').value){
			form.showFormError($('expertiseList') , Vars.get('Please select field of expertise') , 'bottom');
			return false;
		}
		form.removeFormError($('expertiseList'));
		return true;
	}

	function validateTargetLanguage(){
		var valid = false;
		$$('[name="Target_Language[]"]').each(function (e) {
			if( e.value ) valid=true;
		});
		return valid;
	}
	function newExpertiseErrorMessage(message){
		if (!message){ 
			if ($('err-exp')){
				$('err-exp').remove();
			}
		}
		else{
			if (!$('err-exp')){
				var h = $(document.createElement('div'));
				h.id='err-exp';
				h.addClassName('err-msg-red');
				h.update(message);
				$('expertiseListContainer').appendChild(h);
			}
			new Effect.Highlight('err-exp');
		}
		
	}
	function attachUploadEventToFileElement(id) {
		var file_stamp='post_file'+id;
		if (!$(file_stamp)) return;
		var stat_stamp="file_status_"+id;
		var url=Vars.get('url')+"/service/uploadProject/";
		var file=$(file_stamp);
		new AjaxUpload(file_stamp, {
			 action: url,
			 name: file_stamp,
			 data: {'file': file_stamp},
			 autoSubmit: true,
			 onComplete : function(file, response){
				// alert(response.responseText);
				showFileAfterUpload(id,response);
				//stat=response.responseText;
			 },
			 onSubmit : function (file,extension) {

					showWaitImage(id);
				
			 }
			});
	}

	
	function showWaitImage(filenum){
		var sp =$(document.createElement('span'));
		sp.style.background="url('"+Vars.get('wpurl')+"/images/wait.gif') no-repeat left center";
		sp.style.padding="0 0 0 20px";
		sp.update(Vars.get('Uploading file'));
		$('file_status_'+filenum).update(sp);
		
	}
	
	function showFileAfterUpload(filenum,response){
		
		try{
			
			err = eval('('+response+')');
			if (typeof(err) != null){
				
				$('file_status_'+filenum).update(err.error);
			}
			return;
		}catch (e){}

		if(response.length == 0){
			$('file_status_'+filenum).update(Vars.get('Upload failed'));
			return;
		}
		$('post_file'+filenum).remove(); 
		/*if ($('file_'+filenum).select('a').length > 0){
			$('file_'+filenum).select('a')[0].hide();
		}*/
		$('file_status_'+filenum).update(response);
		if ($('file_status_'+filenum).select('a').length > 0){
			$('file_status_'+filenum).select('a')[0].observe('click',function(){removeUploadedFile(filenum)});
		}
		
		//filesArray[filenum] = 0;
		
	}
	function removeUploadedFile(filenum){

		var url = Vars.get('url') + "/service/removeUploadedFile/";
		key = $('file_'+filenum).select('input')[0].id;
		new Ajax.Request (url , {
			method: 'post',
			parameters: { 'fi': key }
		});
		fileCounter--;
		$('file_'+filenum).remove();
		filesArray[filenum] = 0;
		//addFile();
	}

	function initFilesArray(){
		if ($('file_0')){
			filesArray[0] = $('file_0');
		}
	}

	function highlightErrors(){
		
		$$('.err-msg-red').each(
			function (e) {
				new Effect.Highlight(e,{duration:3 , startcolor:'#ffff00'});
			}
		);
	}

	

	

	function showAddTargetLanguageButton(){

		var n = $(document.createElement('sup'));
		n.appendChild(document.createTextNode(Vars.get('New')));
		n.id = 'newIcon';
		var a = $(document.createElement('a'));
		a.appendChild(document.createTextNode(Vars.get('Add Target Language')));
		a.style.cursor = 'pointer';
		a.observe('click' , function (){
			addTargetLanguage();
		}.bind(window));
		
		$('AddTargetLanguageContainer').appendChild(a);
		$('AddTargetLanguageContainer').appendChild(n);
	}

	function addTargetLanguage(){
		
		var target = $$('[name="Target_Language[]"]').first().cloneNode(true);
		
		target.value = '';
		target.observe('change',function(){form.checkLanguages()} );
		target.allowSubmit = true;
		var cell1 = $(document.createElement('td'));
		var cell2 = $(document.createElement('td'));
		var cell3 = $(document.createElement('td'));
		var row = $(document.createElement('tr'));
		var alert = $(document.createElement('span'));
		alert.className = "alertIcon";
		var removeButton =  $(document.createElement('a'));
		var im = new Image();
		im.src = Vars.get('url')+'/public/images/icons/cancel_18.png';
		removeButton.appendChild($(im).addClassName('inlineImg').addClassName('removeTargetLanguage'));
		removeButton.observe('click',function(event) {
			event.findElement('tr').remove();
			form.checkLanguages();
		});
		
		removeButton.style.cursor = "pointer";
		cell2.appendChild(target);
		cell3.appendChild(removeButton);
		cell3.appendChild(alert);
		row.insert(cell1);
		row.insert(cell2);
		row.insert(cell3);
		if (tbody = $('TargetLanguageTableBody').select('tbody')){
			tbody[0].insert(row);
		}else{
			$('TargetLanguageTableBody').insert(row);
		}
		
		//window.alert(row);
		//form.initForm();
		
	}

	
	
	function attachAllEvents(){
		$$('.removeTargetLanguage').each(function(e){
			e.style.cursor = 'pointer';
			e.observe('click',function(event) {
				event.findElement('tr').remove();
				form.checkLanguages();
			})
		}) ;
		
		
		
	}

	/*function shakeFeedback(){
		new Effect.Shake($$('.feedback a')[0] , {duration : 1});
				
	}*/

	
    
	document.observe('dom:loaded',function(){
		
		attachUploadEventToFileElement(0);
		initFilesArray();
		
		//checkLanguagePairAvailability();
		highlightErrors();
		//showAddTargetLanguageButton();
		attachAllEvents();
		form = new Form();
		
		
		//form.attachObservers();
		form.checkLanguages();
		//shakeFeedback.delay(20);
		
	});
	
	



	Form = function (){

		
		this.source = function () {
			return $('source-lang');
		}
		this.target = function () {
			return $$('[name="Target_Language[]"]');
		}
		this.alertIcon = function (){
			return $$('.alertIcon');
		}
		
		
		this.files = function (){
			return $$('.uploadFile');
		}
		this.fileContainers = function(){
			return $$('.uploadFileContainer');
		}
		this.filesStatus = function(){
			return $$('.uploadFileStatus');
		}
		this.textContent = function() {
			return $('content');
		}
		
		this.textContentWordCount = function(){
			return $('rc_cwp_meta_Words_Count');
		}
		this.uploadedFile = function() {
			//alert($$('.uploadedFile').length);
			return $$('.uploadedFile');
		}
		
		this.initForm = function(){
			this.attachObservers();
			
			if (Vars.get('show first')=='text'){
				$('directTextTab').simulate('click');
			}
			//window.files = this.files();
			if (this.files().length == 1){
				this.addFile();
			}
		}

		this.attachObservers = function() {
			
			
			this.source().observe('change' , this.checkLanguages.bindAsEventListener(this) );
			this.source().observe('blur' , this.checkLanguages.bindAsEventListener(this) );
			this.source().observe('change' , this.validateSourceLanguage.bindAsEventListener(this) );
			this.source().observe('blur' , this.validateSourceLanguage.bindAsEventListener(this) );
			this.target().each(function(e){
				e.observe('change',this.checkLanguages.bindAsEventListener(this) );
				e.observe('blur',this.checkLanguages.bindAsEventListener(this) );
			}.bind(this));
			this.target()[0].observe('change' , this.validateTargetLanguage.bindAsEventListener(this) );
			this.target()[0].observe('blur' , this.validateTargetLanguage.bindAsEventListener(this) );
			$('directTextTab').observe('click' , this.showDirectText);
			$('uploadTab').observe('click' , this.showUpload);
			if ($('translateButton')) $('translateButton').observe('click' , submitTranslation);
			if ($('comboButton')) $('comboButton').observe('click' , submitTranReview);
			if ($('expertButton')) $('expertButton').observe('click' , submitExpert);
			$('addFile').observe('click' , this.addFile.bindAsEventListener(this) );
			this.fileContainers().each(function(container){
				
				var button = container.select('.removeFile')[0];
				if (button){
					button.observe('click' , function(){
						this.removeUploadedFile(container);
						//container.remove();
					}.bind(this));
				}
			}.bind(this));
		}
		
		this.checkLanguages = function (){

			
			
			if (!this.isTargetSelected()){
				//this.showFormError(this.target()[0], Vars.get('Please enter source and target languages'))
				return false;
			}else{
				//this.removeFormError(this.target()[0]);
			}

			this.target().each(function(e){
				e.allowSubmit = true;
			});
			
			var redundant = this.getRedundantTargetLanguage();
			if (redundant.length > 0){
				this.showRedundantMessage(redundant);
				
			}
			
			if (!this.isSourceSelected()){
				return false;
			}
			checkLanguagePairAvailability();
			
			return true;
		}

		this.isTargetSelected = function (){
			var flag = false;
			if (!this.target()) return false;
			this.target().each(function (element) {
				if (element.value) {
					flag = true;
					return;
				}
			});
			return flag;
		}

		

		this.isSourceSelected = function () {
			return (this.source() && this.source().value);
		}

		this.getRedundantTargetLanguage = function () {
			var arr = new Array();
			var redundant = new Array();
			
			this.target().each(function(element , index){
				if (arr.indexOf(element.value) >= 0){
					redundant[redundant.length] = index;
				}else if(element.value){
					arr[arr.length] = element.value;
				}
			});
			return redundant;
		}

		this.showRedundantMessage = function(indexes){
			for (var i=0;i<indexes.length;i++){
				if (this.target()[indexes[i]]){
					this.showAlertIcon(indexes[i] , 'error' , Vars.get('You have already selected this language'));
					this.target()[indexes[i]].allowSubmit = false;
				}
			}
		}

		this.showAlertIcon = function (index , type , message){

				if (!this.alertIcon()[index]) return;
				var icon = this.alertIcon()[index];
				image = $(document.createElement('img'));
				switch (type){
					case 'error' : 
						var imageName = 'error.png'; 
						break;
					case 'alert':
						var imageName = 'alert.png'; 
						break;
						enableExpert = true;
					case 'ok':
						return;
					default: 
						return;
				}
				image.src = Vars.get('url') + '/public/images/icons/attention_18.png';
				image.style.position = "relative";
				icon.update(image.addClassName('inlineImg'));
				if (Tip && message){
					new Tip(image , message , {
						style: 'protoblue',
						images: Vars.get('template_url') + '/images/prototip/styles/protoblue/',
						hook: { target: 'rightMiddle' , tip: 'leftMiddle' },
						/*offset: { x:0 , y: -20 },*/
						stem: 'leftMiddle' 
					});
				}

		}

		this.clearNotAllowed = function() {
			this.target().each(function(e){
				if (!e.allowSubmit){
					e.parentNode.parentNode.remove();
				}
			});
		}
		
		this.showDirectText = function (){
			$('uploadTab').removeClassName('active');
			$('directTextTab').addClassName('active');
			$$('.upload')[0].hide();
			$$('.directText')[0].show();
		}
		
		this.showUpload = function (){
			$('directTextTab').removeClassName('active');
			$('uploadTab').addClassName('active');
			$$('.directText')[0].hide();
			$$('.upload')[0].show();
		}
		
		this.addFile = function () {
			
			var index = this.files().length + 1;
			var p = $(document.createElement('p'));
			p.addClassName('uploadFileContainer');
			/*
			input = $(document.createElement('input'));
			input.setAttribute('type','file');
			input.setAttribute('name','post_file'+index);
			input.setAttribute('size','17');
			input.setAttribute('id','post_file'+index);
			input.addClassName('uploadFile');
			*/
			this.attachUploadEventToFileElement(p);
			span = $(document.createElement('span'));
			span.addClassName('uploadFileStatus');
			//p.insert(input);
			p.insert(span);
			$('upload').insert(p);
			
			/*
			var index = filesArray.indexOf(0);
			if ( index >= 0 ){
				fileCounter++;
				e = $('files_container');
				div = document.createElement('div');
				div.setAttribute('id','file_'+index);
				span = document.createElement('span');
				span.setAttribute('id','file_status_'+index );
				input = document.createElement('input');
				input.setAttribute('type','file');
				input.setAttribute('name','post_file'+index);
				input.setAttribute('size','17');
				input.setAttribute('id','post_file'+index);
				$(input).observe('change',function(){upload(index)});
				
				//link = document.createElement('a');
				//link.setAttribute('href','javascript:removeFile('+index+');');
				//txt = document.createTextNode("X");
				//link.appendChild(txt);
			
				div.appendChild(input);
				//if (fileCounter - 1 > 0) div.appendChild(link);
				div.appendChild(span)
				e.appendChild(div);
				filesArray[filesArray.indexOf(0)]=div;
				attachUploadEventToFileElement(index);

			}
			if (index >=5 ){
				$('add_file_link').style.display="none";
			}
			*/
			
			
		}
		
		this.attachUploadEventToFileElement = function(container) {
			//window.co[window.co.length] = container;
			//var file_stamp='post_file'+id;
			//if (!$(file_stamp)) return;
			//var stat_stamp="file_status_"+id;
			var url=Vars.get('url')+"/service/uploadProject/";
			//var file=$(file_stamp);
			//var parent = $(fileElement).parentNode;
			/*
			parent.ajax = new AjaxUpload(fileElement, {
				 action: url,
				 name: fileElement.id,
				 data: {'file': fileElement.id},
				 autoSubmit: true,
				 onComplete : function(file, response){
					this.showFileAfterUpload(fileElement,response );
				 }.bind(this),
				 onSubmit : function (file,extension) {
					this.showWaitImage(fileElement);
				 }.bind(this)
			});
			*/
			
			new FileUploader(container , {
				url:url , 
				vars : {
					'file': 'post_file1'
				} ,
				onStart : function(){
					this.showWaitImage(container);
					//alert(container);
				}.bind(this),
				onSuccess : function(response){
					//alert(container);
					this.showFileAfterUpload(container,response );
				}.bind(this)
			});
			
			
		}
		
		this.showFileAfterUpload = function(container , response){
			//alert(ajaxElement);
			var parent = container;
			var st = $(parent).select('.uploadFileStatus')[0];
			
			try{
				
				err = eval('('+response+')');
				if (typeof(err) != null){
					this.fileUploadError(container, err.error);
					//st.update(err.error);
				}
				return;
			}catch (e){}

			if(!response || response.length == 0){
				this.fileUploadError(container, Vars.get('Upload failed'));
				//st.update(Vars.get('Upload failed'));
				
				return;
			}
			
			/*if ($('file_'+filenum).select('a').length > 0){
				$('file_'+filenum).select('a')[0].hide();
			}*/
			st.update(response);
			if (st.select('a').length > 0){
				st.select('a')[0].observe('click',function(){
					this.removeUploadedFile(parent)
				}.bind(this));
			}
			container.select('form')[0].remove();
			if (parent && parent.ajax) parent.ajax.destroy();
		}
		
		this.showWaitImage = function(file){
			//window.fi = file;
			var st = $(file).select('.uploadFileStatus')[0];
			var sp =$(document.createElement('span'));
			sp.style.background="url('"+Vars.get('wpurl')+"/images/wait.gif') no-repeat left center";
			sp.style.padding="0 0 0 20px";
			sp.update(Vars.get('Uploading file'));
			st.update(sp);
			
		}
		
		this.removeUploadedFile = function (fileParent){
			window.par = fileParent;
			var url = Vars.get('url') + "/service/removeUploadedFile/";
			key = $(fileParent).select('input')[0].id;
			new Ajax.Request (url , {
				method: 'post',
				parameters: { 'fi': key }
			});
			fileCounter--;
			$(fileParent).remove();
			
			//addFile();
		}
		this.fileUploadError = function(container , message){
			var st = $(container).select('.uploadFileStatus')[0];
			var image = $(document.createElement('img'));
			image.src = Vars.get('url') + '/public/images/icons/attention_18.png';
			image.style.position = "relative";
			st.update(image.addClassName('inlineImg'));
			if (Tip && message){
				new Tip(image , message , {
					style: 'protoblue',
					images: Vars.get('template_url') + '/images/prototip/styles/protoblue/',
					hook: { target: 'rightMiddle' , tip: 'leftMiddle' },
					/*offset: { x:0 , y: -20 },*/
					stem: 'leftMiddle' 
				});
			}
		}
		
		this.showFormError = function(element ,message , type){
			if(!element) return;
			if (element.prototip){
				element.prototip.remove();
			}
			var _target;
			var _tip;
			var _stem;
			
			switch (type){
			
			case 'bottom' :
				
				_target = 'bottomMiddle' ;
				_tip='topMiddle';
				_stem = 'topMiddle';
				break;
			default : 
				_target = 'rightMiddle' ;
				_tip='leftMiddle';
				_stem = 'leftMiddle';
				break;
			}
			if (Tip && message){
				new Tip(element , message , {
					style: 'protoblue',
					images: Vars.get('template_url') + '/images/prototip/styles/protoblue/',
					hook: {  target: _target , tip: _tip  },
					stem: _stem ,
					hideAfter: 6 ,
					/*offset: { x:0 , y: -20 },*/
					showOn: false , 
					hideOn: false
				});
				if(!element) return;
				if (element.prototip){
					element.prototip.show();
				}
			}
		}
		
		this.removeFormError = function(element){
			if(!element) return;
			if (element.prototip){
				element.prototip.remove();
			}
		}
		
		this.validateSourceLanguage =  function(){
			if (!this.isSourceSelected()){
				this.showFormError(this.source(), Vars.get('Please select source language'));
				//form.source().prototip.show('slow');
				return false;
			}else{
				this.removeFormError(this.source());
			}
			return true;
		}
		
		this.validateTargetLanguage =  function(){
			if (!this.isTargetSelected()){
				this.showFormError(this.target()[0], Vars.get('Please select target language'));
				//form.source().prototip.show('slow');
				return false;
			}
			var flag;
			this.target().each(function(e){
				if (e.allowSubmit)
					flag = true;
			});
			
			if (!flag) return false;
				
			this.removeFormError(this.target()[0]);
			return true;
		}
		
		this.validateContent = function (){
			valid = false;
			if (this.textContent().value && this.textContentWordCount().value > 0){
				valid |= true;
			}
			if (this.uploadedFile().length > 0 ){
				valid |= true;
			}
			
			if (valid){
				this.removeFormError($$('.step2')[0]);
				return true;
			}else{
				this.showFormError($$('.step2')[0], Vars.get('Please enter text for translation or upload files'), 'bottom')
				return false;
			}
		}
		
		this.initForm();
		
		
	}
	
	

	

var OHTTipContainer = {
	tips:new Array() ,
	add:function(tip){
		if (typeof tip == "object"){
			this.tips.push(tip);
		}
	},
	closeAll:function(){
		for(var i=0;i<this.tips.length;i++){
			this.tips[i].hide();
		}
	},
	clear:function(){
		this.tips = new Array();
	}
};

function OHTTip(linkElement , contentElement , args){
	this.init(linkElement , contentElement , args);
};

OHTTip.prototype = {
	
	linkElement:null,
	contentElement:null ,
	displayObj:null ,
	defaultStyle:{
		width: "300px",
		backgroundColor: "#ffffff" ,
		position: "absolute" 
		
	},
	defaultClass:"oht_tip_content",
	
	defaultLinkEvents:{
		open: 'click' ,
		close: null 
	},
	
	defaultContentEvents:{
		open: null,
		close: 'mouseleave'
	},
	showCloseButton: true,
	
	show:function(){
		OHTTipContainer.closeAll();
		//$(document).viewport.getDimensions().width;
		scrollLeft = $(document).viewport.getScrollOffsets()[0];
		scrollTop = $(document).viewport.getScrollOffsets()[1];
		var top= Math.floor(($(document).viewport.getDimensions().height - this.contentElement.getDimensions().height )/2) + scrollTop;
		var left = Math.floor(($(document).viewport.getDimensions().width - this.contentElement.getDimensions().width )/2) + scrollLeft;
		this.contentElement.style.position="absolute";
		this.contentElement.style.left = left+"px";
		this.contentElement.style.top = top+"px";
		//this.contentElement.show();
		new Effect.Appear(this.contentElement,{duration: 1.0, afterFinish:function(){
			$(document).fire('ohttip:opened');
		}});
		
	},
	
	hide:function(){
		this.contentElement.hide();
	},
	attachEvents:function(){
		if (this.defaultLinkEvents.open) this.linkElement.observe(this.defaultLinkEvents.open , function(e){
			Event.stop(e);
			this.show();
		}.bindAsEventListener(this));
		if (this.defaultLinkEvents.close) this.linkElement.observe(this.defaultLinkEvents.close , function(e){
			
			this.hide();
		}.bindAsEventListener(this));
		if (this.defaultContentEvents.open) this.contentElement.observe(this.defaultContentEvents.open , function(e){
			Event.stop(e);
			this.show();
		}.bindAsEventListener(this));
		if (this.defaultContentEvents.close) this.contentElement.observe(this.defaultContentEvents.close , function(){
			
			this.hide();
		}.bindAsEventListener(this));
	},
	attachCloseButton:function(){
		var span = new Element('span');
		span.update(this.contentElement.select('h3')[0].innerHTML);
		span.addClassName('float');
		var c = new Element('a');
		c.href="#";
		c.insert('X');
		c.addClassName('closeButton');
		c.observe('click',function(e){
			Event.stop(e);
			this.hide();
		}.bindAsEventListener(this));
		this.contentElement.select('h3')[0].update('');
		this.contentElement.select('h3')[0].insert(span);
		this.contentElement.select('h3')[0].insert(c);
		
	},
	setStyling:function(args){
		if (args.position) this.defaultStyle.position = args.position;
		if (args.width) this.defaultStyle.width = args.width;
		if (args.height) this.defaultStyle.height = args.height;
		if (args.backgroundColor) this.defaultStyle.backgroundColor = args.backgroundColor;
		if (args.closeButton) this.showCloseButton = args.closeButton;
		
		if (!this.contentElement.className) this.contentElement.addClassName(this.defaultClass);
		this.contentElement.style.position = this.defaultStyle.position;
		this.contentElement.style.width = this.defaultStyle.width;
		if (args.height){
			this.contentElement.style.height = args.height;
		}
		this.contentElement.style.backgroundColor = this.defaultStyle.backgroundColor;
		
		if (this.showCloseButton){
			
			this.attachCloseButton();function OHTTipDisplay(){
				
			}
			OHTTipDisplay.prototype = {
				show:function(){
				
				},
				hide:function(){
					
				}
			};
		}
		document.body.appendChild(this.contentElement);
	},
	init:function(linkElement , contentElement , args){
		
		if(args.closeOnLink) this.defaultLinkEvents.close = args.closeOnLink;
		if(args.openOnLink) this.defaultLinkEvents.open = args.openOnLink;
		if(args.closeOnContent) this.defaultContentEvents.close = args.closeOnContent;
		if(args.openOnContent) this.defaultContentEvents.open = args.openOnContent;
		
		this.linkElement = $(linkElement);
		this.contentElement = $(contentElement);
		if (this.linkElement && this.contentElement) {
			this.setStyling(args);
			this.attachEvents();
		}
		
		
	}	
};



function OHTTipParser () {
	this.init();
};

OHTTipParser.prototype = {
	links :new Array() ,
	contents:new Array() ,
	
	parsePage:function(){
		this.links = $$('.ohttip');
		if (this.links.length==0) return;
		this.links.each(function(link){
			
			this.parseLink(link);
		}.bind(this));
			
		
	},
	parseLink:function(link){
		content=null;
		link = $(link);
		rel = link.getAttribute('rel');
		if (!rel) return;
		relArr = rel.split(';');
		
		if (relArr.length>0) 
		args= new Object();
		for (var i=0;i<relArr.length;i++){
			newarr = relArr[i].strip().split('=');
			
			if(newarr && newarr.length==2) {
				switch(newarr[0]){
				case 'content':
					content=$(newarr[1]);
					break;
				default:
					name=new String(newarr[0]);
					args[name] = newarr[1];
					//alert(args[newarr[0]]);
					break;
				}
			}
		}
		if (content){
			OHTTipContainer.add(new OHTTip(link , content ,args));
		}
	},
	init: function(){
		this.parsePage();
	}
};



$(document).observe('dom:loaded',function(){
	oht_tip_parser = new OHTTipParser();
});
var Prototip = {
  Version: '2.2.0.2'
};

var Tips = {
  options: {
    paths: {                                // paths can be relative to this file or an absolute url
      images:     '../../images/prototip/',
      javascript: Vars.get('url')+'/public/js/prototip/'
    },
    zIndex: 6000                            // raise if required
  }
};

Prototip.Styles = null;                     // replace with content of styles.js to skip loading that file

eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('M.10(11,{4p:"1.6.1",2J:{24:!!X.4q("24").3q},3r:p(a){4r{X.4s("<2g 3s=\'3t/1y\' 1C=\'"+a+"\'><\\/2g>")}4t(b){$$("4u")[0].J(I G("2g",{1C:a,3s:"3t/1y"}))}},3u:p(){3.3v("2K");q a=/1D([\\w\\d-2L.]+)?\\.3w(.*)/;3.2M=(($$("2g[1C]").4v(p(b){K b.1C.25(a)})||{}).1C||"").2N(a,"");s.26=(p(b){K{T:(/^(3x?:\\/\\/|\\/)/.3y(b.T))?b.T:3.2M+b.T,1y:(/^(3x?:\\/\\/|\\/)/.3y(b.1y))?b.1y:3.2M+b.1y}}.1d(3))(s.9.26);o(!11.2h){3.3r(s.26.1y+"3z.3w")}o(!3.2J.24){o(X.4w>=8&&!X.3A.2i){X.3A.2O("2i","4x:4y-4z-4A:4B","#2j#3B")}Y{X.1a("3C:2P",p(){q b=X.4C();b.4D="2i\\\\:*{4E:2Q(#2j#3B)}"})}}s.2k();G.1a(2R,"2S",3.2S)},3v:p(a){o((4F 2R[a]=="4G")||(3.2T(2R[a].4H)<3.2T(3["3D"+a]))){3E("11 4I "+a+" >= "+3["3D"+a]);}},2T:p(a){q b=a.2N(/2L.*|\\./g,"");b=4J(b+"0".4K(4-b.1Q));K a.4L("2L")>-1?b-1:b},2U:p(a){K(a>0)?(-1*a):(a).4M()},2S:p(){s.3F()}});M.10(s,(p(){p a(b){o(!b){K}b.3G();o(b.13){b.E.1E();o(s.1i){b.1l.1E()}}s.1m=s.1m.3H(b)}K{1m:[],15:[],2k:p(){3.2l=3.1n},27:{B:"2V",2V:"B",u:"1o",1o:"u",1R:"1R",1b:"1e",1e:"1b"},3I:{H:"1b",F:"1e"},2W:p(b){K!!1S[1]?3.27[b]:b},1i:(p(c){q b=I 4N("4O ([\\\\d.]+)").4P(c);K b?(3J(b[1])<7):U})(4Q.4R),2X:(2K.4S.4T&&!X.4U),2O:p(b){3.1m.28(b)},1E:p(d){q g,e=[];1T(q c=0,b=3.1m.1Q;c<b;c++){q f=3.1m[c];o(!g&&f.C==$(d)){g=f}Y{o(!f.C.3K){e.28(f)}}}a(g);1T(q c=0,b=e.1Q;c<b;c++){q f=e[c];a(f)}d.1D=29},3F:p(){1T(q c=0,b=3.1m.1Q;c<b;c++){a(3.1m[c])}},2m:p(d){o(d==3.3L){K}o(3.15.1Q===0){3.2l=3.9.1n;1T(q c=0,b=3.1m.1Q;c<b;c++){3.1m[c].E.r({1n:3.9.1n})}}d.E.r({1n:3.2l++});o(d.Q){d.Q.r({1n:3.2l})}3.3L=d},3M:p(b){3.2Y(b);3.15.28(b)},2Y:p(b){3.15=3.15.3H(b)},3N:p(){s.15.1F("S")},V:p(c,g){c=$(c),g=$(g);q l=M.10({1c:{x:0,y:0},N:U},1S[2]||{});q e=l.1t||g.2n();e.B+=l.1c.x;e.u+=l.1c.y;q d=l.1t?[0,0]:g.3O(),b=X.1z.2o(),h=l.1t?"1U":"17";e.B+=(-1*(d[0]-b[0]));e.u+=(-1*(d[1]-b[1]));o(l.1t){q f=[0,0];f.H=0;f.F=0}q j={C:c.1V()},k={C:M.2a(e)};j[h]=l.1t?f:g.1V();k[h]=M.2a(e);1T(q i 3P k){3Q(l[i]){R"4V":R"4W":k[i].B+=j[i].H;18;R"4X":k[i].B+=(j[i].H/2);18;R"4Y":k[i].B+=j[i].H;k[i].u+=(j[i].F/2);18;R"4Z":R"51":k[i].u+=j[i].F;18;R"52":R"53":k[i].B+=j[i].H;k[i].u+=j[i].F;18;R"54":k[i].B+=(j[i].H/2);k[i].u+=j[i].F;18;R"55":k[i].u+=(j[i].F/2);18}}e.B+=-1*(k.C.B-k[h].B);e.u+=-1*(k.C.u-k[h].u);o(l.N){c.r({B:e.B+"v",u:e.u+"v"})}K e}}})());s.2k();q 56=57.3R({2k:p(c,e){3.C=$(c);o(!3.C){3E("11: G 58 59, 5a 3R a 13.");K}s.1E(3.C);q a=(M.2p(e)||M.2Z(e)),b=a?1S[2]||[]:e;3.1p=a?e:29;o(b.1W){b=M.10(M.2a(11.2h[b.1W]),b)}3.9=M.10(M.10({1j:U,1f:0,30:"#5b",1k:0,L:s.9.L,19:s.9.5c,1u:!(b.W&&b.W=="1X")?0.14:U,1q:U,1g:"1G",3S:U,V:b.V,1c:b.V?{x:0,y:0}:{x:16,y:16},1H:(b.V&&!b.V.1t)?1h:U,W:"2q",D:U,1W:"2j",17:3.C,12:U,1z:(b.V&&!b.V.1t)?U:1h,H:U},11.2h["2j"]),b);3.17=$(3.9.17);3.1k=3.9.1k;3.1f=(3.1k>3.9.1f)?3.1k:3.9.1f;o(3.9.T){3.T=3.9.T.3T("://")?3.9.T:s.26.T+3.9.T}Y{3.T=s.26.T+"3z/"+(3.9.1W||"")+"/"}o(!3.T.5d("/")){3.T+="/"}o(M.2p(3.9.D)){3.9.D={N:3.9.D}}o(3.9.D.N){3.9.D=M.10(M.2a(11.2h[3.9.1W].D)||{},3.9.D);3.9.D.N=[3.9.D.N.25(/[a-z]+/)[0].2r(),3.9.D.N.25(/[A-Z][a-z]+/)[0].2r()];3.9.D.1A=["B","2V"].5e(3.9.D.N[0])?"1b":"1e";3.1r={1b:U,1e:U}}o(3.9.1j){3.9.1j.9=M.10({31:2K.5f},3.9.1j.9||{})}o(3.9.V.1t){q d=3.9.V.1s.25(/[a-z]+/)[0].2r();3.1U=s.27[d]+s.27[3.9.V.1s.25(/[A-Z][a-z]+/)[0].2r()].2s()}3.3U=(s.2X&&3.1k);3.3V();s.2O(3);3.3W();11.10(3)},3V:p(){3.E=I G("P",{L:"1D"}).r({1n:s.9.1n});o(3.3U){3.E.S=p(){3.r("B:-3X;u:-3X;1I:2t;");K 3};3.E.O=p(){3.r("1I:15");K 3};3.E.15=p(){K(3.32("1I")=="15"&&3J(3.32("u").2N("v",""))>-5g)}}3.E.S();o(s.1i){3.1l=I G("5h",{L:"1l",1C:"1y:U;",5i:0}).r({2u:"2b",1n:s.9.1n-1,5j:0})}o(3.9.1j){3.1Y=3.1Y.33(3.34)}3.1s=I G("P",{L:"1p"});3.12=I G("P",{L:"12"}).S();o(3.9.19||(3.9.1g.C&&3.9.1g.C=="19")){3.19=I G("P",{L:"2c"}).1Z(3.T+"2c.2v")}},2w:p(){o(X.2P){3.35();3.3Y=1h;K 1h}Y{o(!3.3Y){X.1a("3C:2P",3.35);K U}}},35:p(){$(X.36).J(3.E);o(s.1i){$(X.36).J(3.1l)}o(3.9.1j){$(X.36).J(3.Q=I G("P",{L:"5k"}).1Z(3.T+"Q.5l").S())}q g="E";o(3.9.D.N){3.D=I G("P",{L:"5m"}).r({F:3.9.D[3.9.D.1A=="1e"?"F":"H"]+"v"});q b=3.9.D.1A=="1b";3[g].J(3.37=I G("P",{L:"5n 2x"}).J(3.3Z=I G("P",{L:"5o 2x"})));3.D.J(3.1J=I G("P",{L:"5p"}).r({F:3.9.D[b?"H":"F"]+"v",H:3.9.D[b?"F":"H"]+"v"}));o(s.1i&&!3.9.D.N[1].40().3T("5q")){3.1J.r({2u:"5r"})}g="3Z"}o(3.1f){q d=3.1f,f;3[g].J(3.20=I G("5s",{L:"20"}).J(3.21=I G("38",{L:"21 39"}).r("F: "+d+"v").J(I G("P",{L:"2y 5t"}).J(I G("P",{L:"22"}))).J(f=I G("P",{L:"5u"}).r({F:d+"v"}).J(I G("P",{L:"41"}).r({1v:"0 "+d+"v",F:d+"v"}))).J(I G("P",{L:"2y 5v"}).J(I G("P",{L:"22"})))).J(3.3a=I G("38",{L:"3a 39"}).J(3.3b=I G("P",{L:"3b"}).r("2z: 0 "+d+"v"))).J(3.42=I G("38",{L:"42 39"}).r("F: "+d+"v").J(I G("P",{L:"2y 5w"}).J(I G("P",{L:"22"}))).J(f.5x(1h)).J(I G("P",{L:"2y 5y"}).J(I G("P",{L:"22"})))));g="3b";q c=3.20.3c(".22");$w("5z 5A 5B 5C").43(p(j,h){o(3.1k>0){11.44(c[h],j,{1K:3.9.30,1f:d,1k:3.9.1k})}Y{c[h].2A("45")}c[h].r({H:d+"v",F:d+"v"}).2A("22"+j.2s())}.1d(3));3.20.3c(".41",".3a",".45").1F("r",{1K:3.9.30})}3[g].J(3.13=I G("P",{L:"13 "+3.9.L}).J(3.23=I G("P",{L:"23"}).J(3.12)));o(3.9.H){q e=3.9.H;o(M.5D(e)){e+="v"}3.13.r("H:"+e)}o(3.D){q a={};a[3.9.D.1A=="1b"?"u":"1o"]=3.D;3.E.J(a);3.2e()}3.13.J(3.1s);o(!3.9.1j){3.3d({12:3.9.12,1p:3.1p})}},3d:p(e){q a=3.E.32("1I");3.E.r("F:1L;H:1L;1I:2t").O();o(3.1f){3.21.r("F:0");3.21.r("F:0")}o(e.12){3.12.O().46(e.12);3.23.O()}Y{o(!3.19){3.12.S();3.23.S()}}o(M.2Z(e.1p)){e.1p.O()}o(M.2p(e.1p)||M.2Z(e.1p)){3.1s.46(e.1p)}3.13.r({H:3.13.47()+"v"});3.E.r("1I:15").O();3.13.O();q c=3.13.1V(),b={H:c.H+"v"},d=[3.E];o(s.1i){d.28(3.1l)}o(3.19){3.12.O().J({u:3.19});3.23.O()}o(e.12||3.19){3.23.r("H: 3e%")}b.F=29;3.E.r({1I:a});3.1s.2A("2x");o(e.12||3.19){3.12.2A("2x")}o(3.1f){3.21.r("F:"+3.1f+"v");3.21.r("F:"+3.1f+"v");b="H: "+(c.H+2*3.1f)+"v";d.28(3.20)}d.1F("r",b);o(3.D){3.2e();o(3.9.D.1A=="1b"){3.E.r({H:3.E.47()+3.9.D.F+"v"})}}3.E.S()},3W:p(){3.3f=3.1Y.1w(3);3.48=3.S.1w(3);o(3.9.1H&&3.9.W=="2q"){3.9.W="3g"}o(3.9.W&&3.9.W==3.9.1g){3.1M=3.49.1w(3);3.C.1a(3.9.W,3.1M)}o(3.19){3.19.1a("3g",p(d){d.1Z(3.T+"5E.2v")}.1d(3,3.19)).1a("3h",p(d){d.1Z(3.T+"2c.2v")}.1d(3,3.19))}q c={C:3.1M?[]:[3.C],17:3.1M?[]:[3.17],1s:3.1M?[]:[3.E],19:[],2b:[]},a=3.9.1g.C;3.3i=a||(!3.9.1g?"2b":"C");3.1N=c[3.3i];o(!3.1N&&a&&M.2p(a)){3.1N=3.1s.3c(a)}$w("O S").43(p(g){q f=g.2s(),d=(3.9[g+"4a"].5F||3.9[g+"4a"]);o(d=="3g"){d=="3j"}Y{o(d=="3h"){d=="1G"}}3[g+"5G"]=d}.1d(3));o(!3.1M&&3.9.W){3.C.1a(3.9.W,3.3f)}o(3.1N&&3.9.1g){3.1N.1F("1a",3.5H,3.48)}o(!3.9.1H&&3.9.W=="1X"){3.2B=3.N.1w(3);3.C.1a("2q",3.2B)}3.4b=3.S.33(p(f,e){q d=e.5I(".2c");o(d){d.5J();e.5K();f(e)}}).1w(3);o(3.19||(3.9.1g&&(3.9.1g.C==".2c"))){3.E.1a("1X",3.4b)}o(3.9.W!="1X"&&(3.3i!="C")){3.2C=p(){3.1B("O")}.1w(3);3.C.1a("1G",3.2C)}o(3.9.1g||3.9.1q){q b=[3.C,3.E];3.3k=p(){s.2m(3);3.2D()}.1w(3);3.3l=3.1q.1w(3);b.1F("1a","3j",3.3k).1F("1a","1G",3.3l)}o(3.9.1j&&3.9.W!="1X"){3.2E=3.4c.1w(3);3.C.1a("1G",3.2E)}},3G:p(){o(3.9.W&&3.9.W==3.9.1g){3.C.1x(3.9.W,3.1M)}Y{o(3.9.W){3.C.1x(3.9.W,3.3f)}o(3.1N&&3.9.1g){3.1N.1F("1x")}}o(3.2B){3.C.1x("2q",3.2B)}o(3.2C){3.C.1x("3h",3.2C)}3.E.1x();o(3.9.1g||3.9.1q){3.C.1x("3j",3.3k).1x("1G",3.3l)}o(3.2E){3.C.1x("1G",3.2E)}},34:p(c,b){o(!3.13){o(!3.2w()){K}}3.N(b);o(3.2F){K}Y{o(3.4d){c(b);K}}3.2F=1h;q e=b.5L(),d={2f:{1O:e.x,1P:e.y}};q a=M.2a(3.9.1j.9);a.31=a.31.33(p(g,f){3.3d({12:3.9.12,1p:f.5M});3.N(d);(p(){g(f);q h=(3.Q&&3.Q.15());o(3.Q){3.1B("Q");3.Q.1E();3.Q=29}o(h){3.O()}3.4d=1h;3.2F=29}.1d(3)).1u(0.6)}.1d(3));3.5N=G.O.1u(3.9.1u,3.Q);3.E.S();3.2F=1h;3.Q.O();3.5O=(p(){I 5P.5Q(3.9.1j.2Q,a)}.1d(3)).1u(3.9.1u);K U},4c:p(){3.1B("Q")},1Y:p(a){o(!3.13){o(!3.2w()){K}}3.N(a);o(3.E.15()){K}3.1B("O");3.5R=3.O.1d(3).1u(3.9.1u)},1B:p(a){o(3[a+"4e"]){5S(3[a+"4e"])}},O:p(){o(3.E.15()){K}o(s.1i){3.1l.O()}o(3.9.3S){s.3N()}s.3M(3);3.13.O();3.E.O();o(3.D){3.D.O()}3.C.4f("1D:5T")},1q:p(a){o(3.9.1j){o(3.Q&&3.9.W!="1X"){3.Q.S()}}o(!3.9.1q){K}3.2D();3.5U=3.S.1d(3).1u(3.9.1q)},2D:p(){o(3.9.1q){3.1B("1q")}},S:p(){3.1B("O");3.1B("Q");o(!3.E.15()){K}3.4g()},4g:p(){o(s.1i){3.1l.S()}o(3.Q){3.Q.S()}3.E.S();(3.20||3.13).O();s.2Y(3);3.C.4f("1D:2t")},49:p(a){o(3.E&&3.E.15()){3.S(a)}Y{3.1Y(a)}},2e:p(){q c=3.9.D,b=1S[0]||3.1r,d=s.2W(c.N[0],b[c.1A]),f=s.2W(c.N[1],b[s.27[c.1A]]),a=3.1k||0;3.1J.1Z(3.T+d+f+".2v");o(c.1A=="1b"){q e=(d=="B")?c.F:0;3.37.r("B: "+e+"v;");3.1J.r({"2G":d});3.D.r({B:0,u:(f=="1o"?"3e%":f=="1R"?"50%":0),5V:(f=="1o"?-1*c.H:f=="1R"?-0.5*c.H:0)+(f=="1o"?-1*a:f=="u"?a:0)+"v"})}Y{3.37.r(d=="u"?"1v: 0; 2z: "+c.F+"v 0 0 0;":"2z: 0; 1v: 0 0 "+c.F+"v 0;");3.D.r(d=="u"?"u: 0; 1o: 1L;":"u: 1L; 1o: 0;");3.1J.r({1v:0,"2G":f!="1R"?f:"2b"});o(f=="1R"){3.1J.r("1v: 0 1L;")}Y{3.1J.r("1v-"+f+": "+a+"v;")}o(s.2X){o(d=="1o"){3.D.r({N:"4h",5W:"5X",u:"1L",1o:"1L","2G":"B",H:"3e%",1v:(-1*c.F)+"v 0 0 0"});3.D.1W.2u="4i"}Y{3.D.r({N:"4j","2G":"2b",1v:0})}}}3.1r=b},N:p(b){o(!3.13){o(!3.2w()){K}}s.2m(3);o(s.1i){q a=3.E.1V();o(!3.2H||3.2H.F!=a.F||3.2H.H!=a.H){3.1l.r({H:a.H+"v",F:a.F+"v"})}3.2H=a}o(3.9.V){q j,h;o(3.1U){q k=X.1z.2o(),c=b.2f||{};q g,i=2;3Q(3.1U.40()){R"5Y":R"5Z":g={x:0-i,y:0-i};18;R"60":g={x:0,y:0-i};18;R"61":R"62":g={x:i,y:0-i};18;R"63":g={x:i,y:0};18;R"64":R"65":g={x:i,y:i};18;R"66":g={x:0,y:i};18;R"67":R"68":g={x:0-i,y:i};18;R"69":g={x:0-i,y:0};18}g.x+=3.9.1c.x;g.y+=3.9.1c.y;j=M.10({1c:g},{C:3.9.V.1s,1U:3.1U,1t:{u:c.1P||2I.1P(b)-k.u,B:c.1O||2I.1O(b)-k.B}});h=s.V(3.E,3.17,j);o(3.9.1z){q n=3.3m(h),m=n.1r;h=n.N;h.B+=m.1e?2*11.2U(g.x-3.9.1c.x):0;h.u+=m.1e?2*11.2U(g.y-3.9.1c.y):0;o(3.D&&(3.1r.1b!=m.1b||3.1r.1e!=m.1e)){3.2e(m)}}h={B:h.B+"v",u:h.u+"v"};3.E.r(h)}Y{j=M.10({1c:3.9.1c},{C:3.9.V.1s,17:3.9.V.17});h=s.V(3.E,3.17,M.10({N:1h},j));h={B:h.B+"v",u:h.u+"v"}}o(3.Q){q e=s.V(3.Q,3.17,M.10({N:1h},j))}o(s.1i){3.1l.r(h)}}Y{q f=3.17.2n(),c=b.2f||{},h={B:((3.9.1H)?f[0]:c.1O||2I.1O(b))+3.9.1c.x,u:((3.9.1H)?f[1]:c.1P||2I.1P(b))+3.9.1c.y};o(!3.9.1H&&3.C!==3.17){q d=3.C.2n();h.B+=-1*(d[0]-f[0]);h.u+=-1*(d[1]-f[1])}o(!3.9.1H&&3.9.1z){q n=3.3m(h),m=n.1r;h=n.N;o(3.D&&(3.1r.1b!=m.1b||3.1r.1e!=m.1e)){3.2e(m)}}h={B:h.B+"v",u:h.u+"v"};3.E.r(h);o(3.Q){3.Q.r(h)}o(s.1i){3.1l.r(h)}}},3m:p(c){q e={1b:U,1e:U},d=3.E.1V(),b=X.1z.2o(),a=X.1z.1V(),g={B:"H",u:"F"};1T(q f 3P g){o((c[f]+d[g[f]]-b[f])>a[g[f]]){c[f]=c[f]-(d[g[f]]+(2*3.9.1c[f=="B"?"x":"y"]));o(3.D){e[s.3I[g[f]]]=1h}}}K{N:c,1r:e}}});M.10(11,{44:p(d,g){q j=1S[2]||3.9,f=j.1k,c=j.1f,e={u:(g.4k(0)=="t"),B:(g.4k(1)=="l")};o(3.2J.24){q b=I G("24",{L:"6a"+g.2s(),H:c+"v",F:c+"v"});d.J(b);q i=b.3q("2d");i.6b=j.1K;i.6c((e.B?f:c-f),(e.u?f:c-f),f,0,6d.6e*2,1h);i.6f();i.4l((e.B?f:0),0,c-f,c);i.4l(0,(e.u?f:0),c,c-f)}Y{q h;d.J(h=I G("P").r({H:c+"v",F:c+"v",1v:0,2z:0,2u:"4i",N:"4h",6g:"2t"}));q a=I G("2i:6h",{6i:j.1K,6j:"6k",6l:j.1K,6m:(f/c*0.5).6n(2)}).r({H:2*c-1+"v",F:2*c-1+"v",N:"4j",B:(e.B?0:(-1*c))+"v",u:(e.u?0:(-1*c))+"v"});h.J(a);a.4m=a.4m}}});G.6o({1Z:p(c,b){c=$(c);q a=M.10({4n:"u B",3n:"6p-3n",3o:"6q",1K:""},1S[2]||{});c.r(s.1i?{6r:"6s:6t.6u.6v(1C=\'"+b+"\'\', 3o=\'"+a.3o+"\')"}:{6w:a.1K+" 2Q("+b+") "+a.4n+" "+a.3n});K c}});11.3p={4o:p(a){o(a.C&&!a.C.3K){K 1h}K U},O:p(){o(11.3p.4o(3)){K}s.2m(3);3.2D();q d={};o(3.9.V){d.2f={1O:0,1P:0}}Y{q a=3.17.2n(),c=3.17.3O(),b=X.1z.2o();a.B+=(-1*(c[0]-b[0]));a.u+=(-1*(c[1]-b[1]));d.2f={1O:a.B,1P:a.u}}o(3.9.1j){3.34(d)}Y{3.1Y(d)}3.1q()}};11.10=p(a){a.C.1D={};M.10(a.C.1D,{O:11.3p.O.1d(a),S:a.S.1d(a),1E:s.1E.1d(s,a.C)})};11.3u();',62,405,'|||this||||||options|||||||||||||||if|function|var|setStyle|Tips||top|px||||||left|element|stem|wrapper|height|Element|width|new|insert|return|className|Object|position|show|div|loader|case|hide|images|false|hook|showOn|document|else||extend|Prototip|title|tooltip||visible||target|break|closeButton|observe|horizontal|offset|bind|vertical|border|hideOn|true|fixIE|ajax|radius|iframeShim|tips|zIndex|bottom|content|hideAfter|stemInverse|tip|mouse|delay|margin|bindAsEventListener|stopObserving|javascript|viewport|orientation|clearTimer|src|prototip|remove|invoke|mouseleave|fixed|visibility|stemImage|backgroundColor|auto|eventToggle|hideTargets|pointerX|pointerY|length|middle|arguments|for|mouseHook|getDimensions|style|click|showDelayed|setPngBackground|borderFrame|borderTop|prototip_Corner|toolbar|canvas|match|paths|_inverse|push|null|clone|none|close||positionStem|fakePointer|script|Styles|ns_vml|default|initialize|zIndexTop|raise|cumulativeOffset|getScrollOffsets|isString|mousemove|toLowerCase|capitalize|hidden|display|png|build|clearfix|prototip_CornerWrapper|padding|addClassName|eventPosition|eventCheckDelay|cancelHideAfter|ajaxHideEvent|ajaxContentLoading|float|iframeShimDimensions|Event|support|Prototype|_|path|replace|add|loaded|url|window|unload|convertVersionString|toggleInt|right|inverseStem|WebKit419|removeVisible|isElement|borderColor|onComplete|getStyle|wrap|ajaxShow|_build|body|stemWrapper|li|borderRow|borderMiddle|borderCenter|select|_update|100|eventShow|mouseover|mouseout|hideElement|mouseenter|activityEnter|activityLeave|getPositionWithinViewport|repeat|sizingMethod|Methods|getContext|insertScript|type|text|start|require|js|https|test|styles|namespaces|VML|dom|REQUIRED_|throw|removeAll|deactivate|without|_stemTranslation|parseFloat|parentNode|_highest|addVisibile|hideAll|cumulativeScrollOffset|in|switch|create|hideOthers|include|fixSafari2|setup|activate|9500px|_isBuilding|stemBox|toUpperCase|prototip_Between|borderBottom|each|createCorner|prototip_Fill|update|getWidth|eventHide|toggle|On|buttonEvent|ajaxHide|ajaxContentLoaded|Timer|fire|afterHide|relative|block|absolute|charAt|fillRect|outerHTML|align|hold|REQUIRED_Prototype|createElement|try|write|catch|head|find|documentMode|urn|schemas|microsoft|com|vml|createStyleSheet|cssText|behavior|typeof|undefined|Version|requires|parseInt|times|indexOf|abs|RegExp|MSIE|exec|navigator|userAgent|Browser|WebKit|evaluate|topRight|rightTop|topMiddle|rightMiddle|bottomLeft||leftBottom|bottomRight|rightBottom|bottomMiddle|leftMiddle|Tip|Class|not|available|cannot|000000|closeButtons|endsWith|member|emptyFunction|9500|iframe|frameBorder|opacity|prototipLoader|gif|prototip_Stem|prototip_StemWrapper|prototip_StemBox|prototip_StemImage|MIDDLE|inline|ul|prototip_CornerWrapperTopLeft|prototip_BetweenCorners|prototip_CornerWrapperTopRight|prototip_CornerWrapperBottomLeft|cloneNode|prototip_CornerWrapperBottomRight|tl|tr|bl|br|isNumber|close_hover|event|Action|hideAction|findElement|blur|stop|pointer|responseText|loaderTimer|ajaxTimer|Ajax|Request|showTimer|clearTimeout|shown|hideAfterTimer|marginTop|clear|both|LEFTTOP|TOPLEFT|TOPMIDDLE|TOPRIGHT|RIGHTTOP|RIGHTMIDDLE|RIGHTBOTTOM|BOTTOMRIGHT|BOTTOMMIDDLE|BOTTOMLEFT|LEFTBOTTOM|LEFTMIDDLE|cornerCanvas|fillStyle|arc|Math|PI|fill|overflow|roundrect|fillcolor|strokeWeight|1px|strokeColor|arcSize|toFixed|addMethods|no|scale|filter|progid|DXImageTransform|Microsoft|AlphaImageLoader|background'.split('|'),0,{}));
LP = function() {
	
	this.chatButtons = function(){
		return $$('.liveperson');
	}
	
	this.showButtons = function (){
		this.chatButtons().each(function(e){
			//e.show();
			new Effect.Appear(e);
		});
	}
	
	this.hideButtons = function (){
		this.chatButtons().each(function(e){
			//e.hide();
			new Effect.fade(e);
		});
	}

	this.checkOnline = function(){
		
		new Ajax.Request(Vars.get('url') + '/service/livePersonStatus/' , {
			method : 'get',
			onSuccess : function (transport){
				this.onlineResponse(transport.responseText);
			}.bind(this)
		});
	}
	
	this.onlineResponse = function (response){
		if (parseInt(response)){
			this.showButtons();
		}else{
			this.hideButtons();
		}
	}
	
	this.open = function(){
		window.lpButtonCTTUrl = 'http://server.iad.liveperson.net/hc/256883/?cmd=file&file=visitorWantsToChat&site=256883&imageUrl=http://server.iad.liveperson.net/hcp/Gallery/ChatButton-Gallery/English/General/3c&referrer='+escape(document.location); 
		window.lpButtonCTTUrl = (typeof(lpAppendVisitorCookies) != 'undefined' ? lpAppendVisitorCookies(lpButtonCTTUrl) : lpButtonCTTUrl); 
		window.open(lpButtonCTTUrl,'chat256883','width=475,height=400,resizable=yes');
	}
	
	this.init = function() {
		this.chatButtons().each(function(e){
			e.observe('LP:click' , function(){
				this.open();
			}.bindAsEventListener(this));
		}.bind(this));
		this.chatButtons().each(function(e){
			e.observe('click' , function(event){
				
				event.element().fire('LP:click');
			});
		});
		this.checkOnline();
	}
	
	this.init();
		
};

$(document).observe('dom:loaded' , function(){
	
	LP = new LP();
});
