if(document.addEventListener)
    document.addEventListener('DOMContentLoaded', cmxform, false);

jQuery(function() {
    jQuery(".txt").focus(function() {
        this.style.backgroundColor='#FFE';
        this.style.color='#333';
    }).blur(function() {
        this.style.backgroundColor='#F7F5F0';
        this.style.color='#A1A1A1';
    });
    jQuery('label.displayed').labelOver('over-apply');
    jQuery(".navigation > div > a").addClass('btn nav');
    jQuery("#c-rss-link").html('<img src="'+scriptpath+'/img/feed.png" width="16" height="16" />');
    jQuery(".noticia1 > p.date").hide().insertAfter('.entry > p > img').show();
    jQuery(".noticia1 > .entry > p > img").wrap('<div class="bordeador"></div>');
    jQuery('form#newsletter').suscribe();
    var interview = jQuery(".entrevistas > li > p > img");
    jQuery(interview).remove();
    jQuery(interview).insertBefore(".subt");
    jQuery("#comment-personaldetails").hide();
    jQuery("span#showinfo").bind("click", function() {
        jQuery(this).hide();
        jQuery("#hideinfo").show();
	jQuery("#comment-personaldetails").fadeIn("slow", function() {
            jQuery(this).slideDown();
        });
	return false;
    }).mouseover(function() {$(this).addClass("over");}).mouseout(function() {$(this).removeClass("over");}).show();
    jQuery("span#hideinfo").bind("click", function() {
        jQuery(this).hide();
        jQuery("#showinfo").show();
	jQuery("#comment-personaldetails").fadeOut("slow", function() {
            jQuery(this).slideUp();
        });
	return false;
    }).mouseover(function() {$(this).addClass("over");}).mouseout(function() {$(this).removeClass("over");}).hide();
    jQuery(".email").defuscate();
    Growl.init(336,154,2);
    behaviours();
});

jQuery.fn.dots = function() {
    return this.each(function() {
        var t = jQuery(this);
        var str = jQuery(this).html();

        if (str.substring(str.length-3) == '...')
            t.html(str.substring(0, str.length-3));
        else
            t.html(t.html() + '.');

        setTimeout(function() {t.dots()}, 200);
    });
};

jQuery.fn.suscribe = function() {
    if (jQuery(this)) {
	var email = $('form#newsletter input#email_suscription');
	var smit = $('form#newsletter input#submit');
	jQuery(this).submit(function(e) {
	    e.preventDefault();
            Growl.register();
	    smit.attr('disabled','disabled');
	    jQuery.ajax({                                                                                                                                                              
		type: "POST",
                timeout: 5000,
		url: scriptpath + "/suscribe.php",                                                                                                                           
		data: jQuery.param(jQuery(this).formToArray()),                                                                                                                   
		dataType: "json",                                                                                                                                                 
		success: function(json) {                                                                                                                                         
		    var r = json.response[0];                                                                                                                                     
		    Growl.show('<p>' + r.msg + '</p>');                                                                                                                       
		}
	    });
	    smit.attr('disabled','');
	});
    }
};

jQuery.fn.labelOver = function(overClass) {
    return this.each(function(){
	var label = jQuery(this);
	var f = label.attr('for');
	if (f) {
	    var input = jQuery('#' + f);
	    this.hide = function() {
	        label.css({ textIndent: -10000 })
	    }
	    this.show = function() {
	        if (input.val() == '') label.css({ textIndent: 0 })
	    }
	    input.focus(this.hide).blur(this.show);
	    label.addClass(overClass).click(function(){ input.focus() });
	    if (input.val() != '') this.hide(); 
	}
    })
}

jQuery.fn.defuscate = function() {
    return this.each(function(){
        var email = String($(this).html()).replace(/\s*\(.+\)\s*/, "@");
        $(this).before('<a href="mailto:' + email + '">' + email + "</a>").remove();
    });
}; 

jQuery.fn.formToArray = function() {                                                                                                                                               
    var a = [];                                                                                                                                                               
    var q = 'input,select,textarea';                                                                                                                                                 
    jQuery(q, this).each(function() {                                                                                                                                              
        var n = this.name;                                                                                                                                                    
        var t = this.type;                                                                                                                                                    
        a.push({name: n, value: this.value});                                                                                                                                 
    });                                                                                                                                                                       
    return a;                                                                                                                                                                 
}; 

var behaviours = function() {
    jQuery("form#poll-form").submit(function() {
        var inputs = [];
        
        jQuery(':input',this).each(function() {
            if(!jQuery(this).is(':checkbox, :radio') || this.checked) {
                inputs.push(this.name + '=' + unescape(jQuery(this).val()));
            }
        });
        jQuery.ajax({
            data: inputs.join('&'),
            url: this.action,
            timeout: 2000,
            error: function() {
                Growl.show('Error al enviar el formulario.');
            },
            success: function(r) {
                jQuery('.poll > li:eq(2)').html(r);
                behaviours();
            }
        });
        return false;
    });
};

var Growl = {
    init:function(width,height,duration) {
	Growl.msg = [];
	Growl.active = false;
	Growl.duration = (duration || 2)*1000;
	Growl.height = height/2;
	Growl.width = width/2;
	jQuery(function() {Growl.create();});
    },
    create:function() {
	jQuery('body').append('<p id="growlmsg"></p><div id="growl" class="growl"></div>');
    },
    queue:function() {
	var msg = Growl.msg.pop();
	if(msg) Growl.show(msg);
    },
    show:function(msg) {
	if(Growl.active) {
    	    Growl.msg.push(msg);
	    return;
	}
	Growl.active = true;
	var top = document.documentElement.scrollTop || window.pageYOffset || 0;
	var left = document.documentElement.scrollLeft || window.pageXOffset || 0;
	var h = (window.innerHeight || document.documentElement.clientHeight || 0)/2;
	var w = (window.innerWidth || document.documentElement.clientWidth || 0)/2;
	var he = top+h-Growl.height, we = left+w-Growl.width;
	jQuery('#growlmsg').css({top: he+'px', left: we+'px'}).show().html(msg).fadeTo(1000,1);
	jQuery('#growl').css({top: he+'px', left: we+'px'}).show().fadeTo(1000,0.8);
	setTimeout(function() {Growl.hide();}, Growl.duration);
    },
    hide: function() {
	jQuery('#growlmsg').fadeOut("fast",function() {Growl.display($(this));});
	jQuery('#growl').fadeOut("fast",function() {
	    Growl.display(jQuery(this));
	    Growl.active = false;
	    setTimeout(function() {Growl.queue();}, 200);
	});
    },
    register: function() {
        Growl.show('<span id="loading"></span><br />loading...');
        jQuery().ajaxComplete(function() {
            Growl.hide();
        });
    },
    display: function(e) {
	e.hide();
    }
};

function getVotes(path, obj) {
    jQuery(obj).dots();
    jQuery('.poll > li:eq(2)').load(path, function(data) {
        jQuery(this).html(data);
        behaviours();
    });
    return false;
}

function cmxform() {
  jQuery('form.cmxform').hide().end();
  jQuery('form.cmxform').find('li > label').not('.nocmx').each(function(i) {
    var labelContent = this.innerHTML;
    var labelWidth = document.defaultView.getComputedStyle(this, '').getPropertyValue('width');
    var labelSpan = document.createElement('span');
        labelSpan.style.display = 'block';
        labelSpan.style.width = labelWidth;
        labelSpan.innerHTML = labelContent;
    this.style.display = '-moz-inline-box';
    this.innerHTML = null;
    this.appendChild(labelSpan);
  }).end();
  
  jQuery('form.cmxform').find('.required').each(function(i) {
        jQuery('<div class="field-hint-inactive"><div>requerido</div></div>').insertBefore(this);
        jQuery(this).focus(function() {
            if($(this).val() == '') {
                jQuery(this).prev().attr('class', 'field-hint');
            }
        }).blur(function() {    
            jQuery(this).prev().attr('class', 'field-hint-inactive');
        });
  }).end();
  jQuery( 'form.cmxform' ).show().end();
}