/*global jQuery, highlighter */

"use strict";

jQuery.noConflict();

jQuery(document).ready(function() {
    if(jQuery("#thesearchquery").length > 0) {
        var query = jQuery("#thesearchquery").val();
        if(query) {
            jQuery("#content-left").highlight(query);
        }
    }
    
    /* make cv visible when there is no post */
    var relposts = jQuery('#author-box').next();
    relposts = jQuery('.content',relposts);
    if(jQuery('.excerpt',relposts).length==0) {
    	jQuery('#author-box').removeClass("box-closed").addClass("box-open");
    	jQuery('.content','#author-box').removeClass("hidden");
    }
});


jQuery(document).ready(function() {
    jQuery(".filter input[type='radio']").click(function(){
        var uri = jQuery(this).attr('value');
        if(uri) {
            document.location.href = uri;
        }
    });
});

jQuery(document).ready(function(){
	jQuery("#commentform #submit-button").click(function(){
		var errorOccured = false;
		jQuery("#commentform #error-author").hide();
		jQuery("#commentform #error-email").hide();
		jQuery("#commentform #error-url").hide();
		jQuery("#commentform #error-terms").hide();
		jQuery("#commentform #error-comment").hide();
		
		jQuery("#commentform #error-author").text('');
		jQuery("#commentform #error-email").text('');
		jQuery("#commentform #error-url").text('');
		jQuery("#commentform #error-terms").text('');
		jQuery("#commentform #error-comment").text();
		
		
		if(jQuery("#commentform #author").length > 0 && jQuery("#commentform #author").val()=== ''){
			jQuery("#commentform #error-author").append("Please enter your name.");
			jQuery("#commentform #error-author").toggle();
			errorOccured = true;
		}
		if(jQuery("#commentform #email").length > 0 && jQuery("#commentform #email").val()=== ''){
			jQuery("#commentform #error-email").append("Please enter your email.");
			jQuery("#commentform #error-email").toggle();
			errorOccured = true;
		}else if(jQuery("#commentform #email").length > 0 && !validateEmail(jQuery("#commentform #email").val())){
			jQuery("#commentform #error-email").append("Please enter a valid email.");
			jQuery("#commentform #error-email").toggle();
			errorOccured = true;
		}
		if(jQuery("#commentform #url").length > 0 && jQuery("#commentform #url").val()=== ''){
			jQuery("#commentform #error-url").append("Please enter your institute.");
			jQuery("#commentform #error-url").toggle();
			errorOccured = true;
		}
		if(jQuery("#commentform #comment").val()=== ''){
			jQuery("#commentform #error-comment").append("Please enter your comment.");
			jQuery("#commentform #error-comment").toggle();
			errorOccured = true;
		}
		if(! jQuery("#commentform #terms").is(':checked')){
			jQuery("#commentform #error-terms").append("Please accept the terms of use.");
			jQuery("#commentform #error-terms").toggle();
			errorOccured = true;
		}
		if(errorOccured){
			return false;
		}else{
			jQuery("#commentform").submit();
			return false;
		}	
	});
});

jQuery(document).ready(function() {
    jQuery("#ec3_prev, #ec3_next").live("click", function() {
       refactorCalendar();
    });
    refactorCalendar();
});

jQuery(document).ready(function() {
    jQuery(".box-open .title, .box-closed .title").click(function() {
        jQuery(".box-open .content, .box-closed .content").slideToggle("slow");
        if(jQuery(this).parent().hasClass("box-open")) {
            jQuery(this).parent().removeClass("box-open").addClass("box-closed");
        } else if(jQuery(this).parent().hasClass("box-closed")) {
            jQuery(this).parent().removeClass("box-closed").addClass("box-open");
        }
    });
});

function validateEmail(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email) == false) {
      return false;
   }
   return true;
}

function refactorCalendar() {

    // move caption content to nav table
    var tdParent = jQuery("#wp-calendar #ec3_spinner").parent();
    jQuery("#wp-calendar #ec3_spinner").remove();
    jQuery("#wp-calendar #ec3_publish").remove();
    tdParent.addClass('caption');
    tdParent.attr('id','caption');
    if(tdParent.text() === '') {
        // tdParent.append(jQuery("#wp-calendar caption").text());
    }

    // remove text in prev/next navigation
    jQuery("#ec3_prev, #ec3_next").html("&nbsp;");
    jQuery("#wp-calendar .nav td").css("visibility", "visible");

    jQuery(".banner .banner-content a").each(function() {
        jQuery(this).parent().css('cursor', 'pointer');
        jQuery(this).parent().click(function() {
            document.location.href = jQuery(this).attr('href');
        });
    });

}

/** MailPress customization ************************************************** */

// copied from plugins/mailpress/mp-includes/js/mp_form.js
var dsctMailPressForm = {

    selectors: {
        submit: 'div.MailPress input.mp_submit',
        form: 'form.mp-form',
        formdiv: 'div.mp-formdiv',
        loading: 'div.mp-loading',
        message: 'div.mp-message'
    },

    init: function() {

        jQuery(dsctMailPressForm.selectors.submit).click(function() {
            dsctMailPressForm.ajax(jQuery(this).parents('.MailPress'));
            return false;
        });

    },

    ajax: function(div) {

        var data = {};
        jQuery(dsctMailPressForm.selectors.form + ' [type!=submit]',  div).each(function() {
            data[jQuery(this).attr('name')] = jQuery(this).val();
        });

        jQuery(dsctMailPressForm.selectors.formdiv, div).fadeTo(500, 0);
        jQuery(dsctMailPressForm.selectors.message, div).fadeTo(500, 0);
        jQuery(dsctMailPressForm.selectors.loading, div).fadeTo(500, 1);

        // ajax
        jQuery.ajax({
            data: data,
            beforeSend: null,
            type: "POST",
            url: mp_url,
            success: dsctMailPressForm.callback
        });

    },

    callback: function(r) {

        var mess = jQuery('message', r).text();
        var email = jQuery('email', r).text();
        var name = jQuery('name', r).text();
        var id = jQuery('id', r).text();
        var div = jQuery('#' + id);

        jQuery(dsctMailPressForm.selectors.form + ' [name=email]', div).val(email);
        jQuery(dsctMailPressForm.selectors.form + ' [name=name]', div).val(name);

        jQuery(dsctMailPressForm.selectors.loading, div).fadeTo(500, 0);
        jQuery(dsctMailPressForm.selectors.message, div).html(mess).fadeTo(1000, 1);

        window.setTimeout(function() { dsctMailPressForm.show(id) }, 2000);

    },

    show: function(id) {
        var div = jQuery('#' + id);
        // jQuery(dsctMailPressForm.selectors.message, div).fadeTo(1000, 0);
        jQuery(dsctMailPressForm.selectors.formdiv, div).fadeTo(500, 1);
    }

}

jQuery(document).ready(function() {

    dsctMailPressForm.init();

    if(typeof jQuery("#MailPressForm") === "undefined") { return; }

    var defaultText = function(jFormField, value) {
        jFormField.val(value);
        jFormField.blur(function() {
            var jThis = jQuery(this);
            if(jThis.val() === "") {
                jThis.val(value);
            }
        });
        jFormField.focus(function() {
            var jThis = jQuery(this);
            if(jThis.val() === value) {
                jThis.val("");
            }
        });
    };

    defaultText(jQuery("#MailPressFormName"), "Your name");
    defaultText(jQuery("#MailPressFormEmail"), "Your email");

    jQuery("#MailpressFormStyledSubmitButton").live("click", function() {
        jQuery("#MailpressFormOriginalSubmitButton").click();
    });

    jQuery("div.MailPress div.mp-loading, div.MailPress div.mp-message").fadeTo(0, 0);

});

/** /MailPress customization ************************************************* */

/* froggler */

jQuery(function () {
    var froggler = 0;
    jQuery("#page-bottom").dblclick(function (e) {
        froggler += 1;
        if (froggler === 3) {
            jQuery(".froggler").slideDown("slow");
        }
    });
});

