// JavaScript Document
var bar2margin = new Object();
bar2margin.margin = 0;
var bar3margin = new Object();
bar3margin.margin = 0;
var bar23margin = new Object();
bar23margin.margin = 0;
var barsitacmargin = new Object();
barsitacmargin.margin = 0;
barTO = "";

function adjustColPosition(col, to){
	containerHeight = $('#contbar_'+col).height()
	contentHeight = $('#contbar_'+col+' .cont').height()
	marginDiff = contentHeight - containerHeight;
	thebar = eval('bar'+col+'margin');
	if(to=="up"){
		if(thebar.margin > 0){
			thebar.margin -= 10;
		} else {
			thebar.margin = 0;
		}
	} else{
		if(thebar.margin < marginDiff){
			thebar.margin += 10;
		}
	}
	$('#contbar_'+col+' .cont').css('marginTop', '-'+thebar.margin+'px');
	barTO = setTimeout("adjustColPosition('"+col+"', '"+to+"')", 50);
}

$(document).ready(function(){
	Cufon.replace('.cufon');
	Cufon.replace('.yearconv');
	Cufon.replace('.cufontitle');
	$('#arrow_2_b').mouseover(function(){
		adjustColPosition(2, 'down');
	});
	$('#arrow_2_b').mouseout(function(){
		clearTimeout(barTO);
	});
	$('#arrow_3_b').mouseover(function(){
		adjustColPosition(3, 'down');
	});
	$('#arrow_3_b').mouseout(function(){
		clearTimeout(barTO);
	});
	$('#arrow_23_b').mouseover(function(){
		adjustColPosition(23, 'down');
	});
	$('#arrow_23_b').mouseout(function(){
		clearTimeout(barTO);
	});

	$('#arrow_2_t').mouseover(function(){
		adjustColPosition(2, 'up');
	});
	$('#arrow_2_t').mouseout(function(){
		clearTimeout(barTO);
	});
	$('#arrow_3_t').mouseover(function(){
		adjustColPosition(3, 'up');
	});
	$('#arrow_3_t').mouseout(function(){
		clearTimeout(barTO);
	});
	$('#arrow_23_t').mouseover(function(){
		adjustColPosition(23, 'up');
	});
	$('#arrow_23_t').mouseout(function(){
		clearTimeout(barTO);
	});


	$('#arrow_23_bs').mouseover(function(){
		adjustColPosition('sitac', 'down');
	});
	$('#arrow_23_bs').mouseout(function(){
		clearTimeout(barTO);
	});
	$('#arrow_23_ts').mouseover(function(){
		adjustColPosition('sitac', 'up');
	});
	$('#arrow_23_ts').mouseout(function(){
		clearTimeout(barTO);
	});




	$('#login').click(function(){
		$('#logincont').show();
		return false;
	});
	var blogpos = $('#blogentries').offset();
	$('#logincont').css('top', (blogpos.top-2)+"px");
	$('#logincont').css('left', blogpos.left+"px");
	$('#logincont').css('height', ($('#blogentries').height())+"px");

	$('#loginfrm').submit(function(){
		$.post("/login/", {
			log_email: $('#log_email').attr('value'),
			log_pwd: $('#log_pwd').attr('value')
		},
		function(data){
			if(logged){
				$('#logincont').hide();
				$('#tools').html('<a href="admin/index.php?fuseaction=blog.modForm">Agregar</a>');
			} else{
				$('#log_error').html(error);
			}
		}, "script");
		return false;
	});

	$(".frm").submit(function(){
		
		var frm = $(this);
		var errors = 0;
		frm.find("input").each(function(i) {
			var el = $(this);
			if(el.attr("required")=="true") {
				if(el.attr("type")=="text" || el.attr("type")=="password" || el.attr("type")=="file") {
					if($.trim(el.val())=="") {
						alert(el.attr("title"));
						el.focus();
						errors = 1;
						return false;
					} else if(el.attr("reqtype")=="email"){
						if(!check_email(el.attr('value'))){
							alert("Escriba una dirección de email válida");
							el.focus();
							errors = 1;
							return false;
						}
					}
				}
			}
		});
		if(errors==1) {
			return false;
		}
		frm.find("textarea").each(function(i) {
			var el = $(this);
			if(el.attr("required")=="true") {
				if($.trim(el.val())=="") {
					alert(el.attr("title"));
					el.focus();
					errors = 1;
					return false;
				}
			}
		});
		if(errors==1) {
			return false;
		}
		frm.find("select").each(function(i) {
			var el = $(this);
			if(el.attr("required")=="true") {
				if($.trim(el.val())=="0") {
					alert(el.attr("title"));
					el.focus();
					errors = 1;
					return false;
				}
			}
		});
		if(errors==1) {
			return false;
		}
	});
});

function check_email(emailStr) {
	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) {
		return false
	}
	var user=matchArray[1]
	var domain=matchArray[2]
	if (user.match(userPat)==null) {
	    return false
	}
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				return false
			}
		}
		return true
	}
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		return false
	}
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3) {
		return false
	}
	if (len<2) {
		alert(errStr)
		return false
	}
	return true;
}
