// JavaScript Document

//comprueba tamaño máximo de un campo, introduce la información de caracteres
// restantes en el div indicado por infoCaracteres
function comprobarTamanho(campo,tamanhoMax,infoCaracteres){
	if($F(campo).length >tamanhoMax){
		$(campo).value=$F(campo).substring(0,tamanhoMax);
	}
	$(infoCaracteres).innerHTML=$F(campo).length;

}

//init paramteres
var drag="";

//other functions

function updateTextPosition(){
	$('text-top').value=$('email-text').style.top;	
	$('text-left').value=$('email-text').style.left;	
}
function updateTitlePosition(){
	$('title-top').value=$('email-title').style.top;	
	$('title-left').value=$('email-title').style.left;	
}
function updateFooterPosition(){
	$('footer-top').value=$('email-footer').style.top;	
	$('footer-left').value=$('email-footer').style.left;	
}
function updateLogoPosition(){
	$('logo-top').value=$('my_logo').style.top;	
	$('logo-left').value=$('my_logo').style.left;	
}


function continueSmsCampaign(){
	
	if( $('continue_send_sms_form') ){
		
		$('continue_send_sms').hide();
		$('progress').show();
		setTimeout ("$('continue_send_sms_form').submit()", 1000); 
		//sleep(100);
		//$('continue_send_sms_form').submit();
	}
	
};


//behaviour rules
var myrules = {
	'#message' : function(element){
		element.onkeyup = function(){
			comprobarTamanho(element,160,'nCaracteres');
			return false;
		}
	},	
	'#demo-access' : function(element){
		element.onclick = function(){
			$('user').value="demo";
			$('password').value="demo";
			return false;
		}
	},
	'#start-text' : function(element){
		element.onmouseover = function(){
			if( $('email-title') ){
				new Draggable('email-title',{onEnd:function(){updateTitlePosition();}});
				//drag.onEnd();
			}
			if( $('email-text') ){
				new Draggable('email-text',{onEnd:function(){updateTextPosition();}});
				//drag.onEnd();
			}
			if( $('email-footer') ){
				new Draggable('email-footer',{onEnd:function(){updateFooterPosition();}});
				//drag.onEnd();
			}
			return false;
		}
	},
	'#add-text' : function(element){
		element.onclick = function(){
			$('editor').style.top="0";
			return false;
		}
	},
	'.selectable_logo' : function(element){
		element.onclick = function(){
			$('my_logo').innerHTML='<img name="logo" src="'+element.src+'" width="200"/>';
			$('logo-src').value=element.src;
			$('logo-src2').value=element.src;
			new Draggable('my_logo',{onEnd:function(){updateLogoPosition();}});
			$('logo_window').hide();
			return false;
		}
	},
	'#show_logos' : function(element){
		element.onclick = function(){
			$('logo_window').show();
			return false;
		}
	},
	'.show_progress' : function(element){
		element.onclick = function(){
			$('progress').show();
			$('info_caracteres').hide();
			$('send_button').hide();
			//element.hide();
			
			//return false;
		}
	}
	
	
};

Behaviour.register(myrules);

Behaviour.addLoadEvent(function(){ continueSmsCampaign() });
