// hide email
jQuery.fn.mailme = function(test) {
    var at = / at /;
    var dot = / dot /g;
    this.each( function() {
        var addr = jQuery(this).text().replace(at,"@").replace(dot,".");
        var title = jQuery(this).attr('title')
        $(this)
            .after('<a href="mailto:'+addr+'" title="'+title+'" class="'+test+'">'+ addr +'</a>')
            .remove();
    });
};

/*
 * Email Defuscator - jQuery plugin 1.0 alpha
 *
 * Copyright (c) 2007 Joakim Stai
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 * Revision: $Id$
 *
 */

/**
 * Converts obfuscated email addresses into normal, working email addresses.
 *
 * @name defuscate
 * @param Boolean link If true, all defuscated email addresses will be turned into links, defaults to true (optional)
 * @descr Converts obfuscated email addresses into normal email addresses
 */

jQuery.fn.defuscate = function( settings ) {
    settings = jQuery.extend({
        link: true
    }, settings);
    var regex = /\b([A-Z0-9._%-]+)\([^)]+\)((?:[A-Z0-9-]+\.)+[A-Z]{2,6})\b/gi;
    return this.each(function() {
        if ( $(this).is('a[@href]') ) {
            // If it's an <a> element, defuscate the href attribute
            $(this).attr('href', $(this).attr('href').replace(regex, '$1@$2'));
            // Make sure that the element's contents is not made into a link
            var is_link = true;
            //alert($(this).attr('href'));
        }
        // Defuscate the element's contents
        $(this).html($(this).html().replace(regex, (settings.link && !is_link ? '<a href="mailto:$1@$2">$1@$2</a>' : '$1@$2')));
  });
}

/**
 * jQuery.labelify - Display in-textbox hints
 * Stuart Langridge, http://www.kryogenix.org/
 * Released into the public domain
 * Date: 25th June 2008
 * @author Stuart Langridge
 * @version 1.3
 *
 *
 * Basic calling syntax: $("input").labelify();
 * Defaults to taking the in-field label from the field's title attribute
 *
 * You can also pass an options object with the following keys:
 *   text
 *     "title" to get the in-field label from the field's title attribute 
 *      (this is the default)
 *     "label" to get the in-field label from the inner text of the field's label
 *      (note that the label must be attached to the field with for="fieldid")
 *     a function which takes one parameter, the input field, and returns
 *      whatever text it likes
 *
 *   labelledClass
 *     a class that will be applied to the input field when it contains the
 *      label and removed when it contains user input. Defaults to blank.
 *  
 */
jQuery.fn.labelify = function(settings) {
  settings = jQuery.extend({
    text: "title",
    labelledClass: ""
  }, settings);
  var lookups = {
    title: function(input) {
      return $(input).attr("title");
    },
    label: function(input) {
      return $("label[for=" + input.id +"]").text();
    }
  };
  var lookup;
  var jQuery_labellified_elements = $(this);
  return $(this).each(function() {
    if (typeof settings.text === "string") {
      lookup = lookups[settings.text]; // what if not there?
    } else {
      lookup = settings.text; // what if not a fn?
    };
    // bail if lookup isn't a function or if it returns undefined
    if (typeof lookup !== "function") { return; }
    var lookupval = lookup(this);
    if (!lookupval) { return; }

    // need to strip newlines because the browser strips them
    // if you set textbox.value to a string containing them    
    $(this).data("label",lookup(this).replace(/\n/g,''));
    $(this).focus(function() {
      if (this.value === $(this).data("label")) {
        this.value = this.defaultValue;
        $(this).removeClass(settings.labelledClass);
      }
    }).blur(function(){
      if (this.value === this.defaultValue) {
        this.value = $(this).data("label");
        $(this).addClass(settings.labelledClass);
      }
    });
    
    var removeValuesOnExit = function() {
      jQuery_labellified_elements.each(function(){
        if (this.value === $(this).data("label")) {
          this.value = this.defaultValue;
          $(this).removeClass(settings.labelledClass);
        }
      })
    };
    
    $(this).parents("form").submit(removeValuesOnExit);
    $(window).unload(removeValuesOnExit);
    
    if (this.value !== this.defaultValue) {
      // user already started typing; don't overwrite their work!
      return;
    }
    // actually set the value
    this.value = $(this).data("label");
    $(this).addClass(settings.labelledClass);

  });
};



// Custom Tabs functie
// Copyright 2010 Tim De Rop
  $.fn.tabs = function(content){
  $(content).each(function(){$(this).children().not('li:first').hide()});
  $(this).children().first().find('a').addClass('active').end().end().click(
    function () {
    var pos = $(this).index();
    $(this).parent().children().find('a').removeClass('active').end().end().end().find('a').addClass('active');      
    $(content).children().fadeOut(500).eq(pos).fadeIn(1500);
    return false;
    }
  );  
  };

// Enhanced Tabs - Automatic slideshow
// Copyright 2010 Tim De Rop
/* Slider
------------------------------------------------------- */
$.fn.slideShow = function(timeOut) {
   var $content = $('#tabs-panes');
   var $nav= $('#tabs-nav');
   $content.children(':gt(0)').hide();
   var total = $content.children().size();
   stopTimer = 0;
   i = 1;
   setInterval(function() {
  if (stopTimer == 0) {
    if (i < total) {    
    $content.children().hide();
    $content.children().eq(i).show();
    $nav.children().children().removeClass('active');
    $nav.children().eq(i).children().addClass('active');    
    i++;
    } else {
     i = 0;
     $content.children().hide();
    $content.children().eq(i).show();
    $nav.children().children().removeClass('active');
    $nav.children().eq(i).children().addClass('active');    
    i++;
    }}
   else {
    clearInterval();}
   }, timeOut || 3000);
    // Slideshow navigation
  $nav.children().children().click(function() {
  // Which position in array
   var position = $.inArray(this, $nav.children().children());
  // Hide all the steps
  $content.children().hide();
  // Remove active class
  $nav.children().children().removeClass('active');
  // Show the step that matches the clicked element
  $content.children().eq(position).show();
  // Add active class
  $(this).addClass('active');
  i = position+1;
  stopTimer = 1;  
  return false;  
  });
};

function CheckFieldValue(FieldElement) {
    var Correct = true;
    FieldElement.removeClass('error');

    var LabelText = $("label", FieldElement.parent()).text();
    if (FieldElement.val() == LabelText || FieldElement.val() == '') {
        Correct = false;
    }
    if (FieldElement.hasClass('checkmail') && !IsValidEmail(FieldElement.val())) {
        Correct = false;
    }
    if (!Correct) {
        FieldElement.addClass('error');
    }
    return Correct;
}

function IsValidEmail(email) {
    var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
    return filter.test(email);
}
