/*
$.fn.copyTo = function(to) {
    var to = $(to);
    for ( var i = 1; i < arguments.length; i++ )
        to.set( arguments[i], this.get(0)[ arguments[i] ] );
    return this;
};
*/
/**
* Form validation code - Be sure to use the naming conventions . type of validation {alt: type}.
* message div = (this)_msg, image div = (this)_img etc.
*/
new function() {
    $.fn.validate = function() { $.validate.check( $(this)[0] ) }
    $.validate = {
        check: function(o) {
          if ($.attr(o,'alt') == 'username')		this.username(o);
          else if ($.attr(o,'alt') == 'profilename') this.profilename(o);
          else if ($.attr(o,'alt') == 'unique_profilename') this.unqiue_profilename(o);
          else if ($.attr(o,'alt') == 'password')	this.password(o);
          else if ($.attr(o,'alt') == 'email')		this.email(o);
          else if ($.attr(o,'alt') == 'dob')		this.dob(o);
        },
        username: function(o) {
          var user = /[(\*\(\)\[\]\+\.\,\/\?\:\;\'\"\`\~\\#\$\%\^\&\<\>)+]/;
          if (o.value.length < 1) {
          	doError(o,'Cannot be empty');
          }
          else {
           if (!o.value.match(user)) {
             doValidate(o, 'Acceptable username');
            } else {
             doError(o,'no special characters allowed');
            };
          }
        },
        profilename: function(o) {
          var user = /^[a-zA-Z0-9 \.-]+$/;
          if (o.value.length < 1) {
          	doError(o,'Cannot be empty');
          }
          else {
           if (o.value.match(user)) {
             doValidate(o, 'Acceptable profile name');
            } else {
             doError(o,'Invalid characters used. Please use only a-z, A-Z, 0-9, space, ., and -');
            };
          }
        },
        unqiue_profilename: function(o) {
          var user = /^[a-z0-9_-]+$/;
          if (o.value.length < 1) {
          	doError(o,'Cannot be empty');
          }
          else {
           if (o.value.match(user)) {
             doValidate(o, 'Acceptable profile name');
            } else {
             doError(o,'Invalid characters used. Please use only a-z (lowercase), 0-9, _ and -');
            };
          }
        },
        password: function(o) {
          var pass = /[(\*\(\)\[\]\+\.\,\/\?\:\;\'\"\`\~\\#\$\%\^\&\<\>)+]/;
           if (!o.value.match(pass)) {
             doValidate(o, 'Acceptable password');
            } else {
             doError(o,'no special characters allowed');
            };
        },
        email: function(o) {
          var email  = /^.+@.+\..+$/;
           if (o.value.match(email)) {
		doSuccess(o, 'Acceptable Email');
            } else {
              doError(o,'Please enter a valid email');
            };
        },
        dob: function(o) {
          var dob  = /(0[1-9]|1[012])+\/(0[1-9]|[12][0-9]|3[01])+\/(19|20)\d\d/;
            if (o.value.match(dob)) {
              doSuccess(o, 'Within accepted date range');
            } else {
              doError(o,'not a valid date');
            };
        }
     };

     function doSuccess(o,m) {
              $('#' + o.id + '_img').html('<img src="/images/accept.gif" />');
              $('#' + o.id + '_li').removeClass("error");
              $('#' + o.id + '_msg').html(m);
              $('#' + o.id + '_li').addClass("success");
     }

     function doError(o,m) {
              $('#' + o.id + '_img').html('<img src="/images/exclamation.gif" />');
              $('#' + o.id + '_li').addClass("error");
              $('#' + o.id + '_msg').html(m);
              $('#' + o.id + '_li').removeClass("success");
     }
     //private helper, validates each type after check
     function doValidate(o, m) {
      	$('#' + o.id + '_img').html('<img src="/images/loading.gif" />');
      	$.post('/register/registerValidationAjax', { id: o.id, method: $.attr(o,'alt'), value: o.value }, function(json) {
              	eval("var args = " + json);
                    if (args.success == true)
              	{
              	  doSuccess(args, m);
              	}
              	else
              	{
                      doError(args,args.error_msg);
              	}
        });
    };

};
$.fn.match = function(m) {
	$('#' + this.get(0).id + '_img').html('<img src="/images/loading.gif" />');
	if ($(this).val() == $(m.match).val()) {
          $('#' + this.get(0).id + '_img').html('<img src="/images/accept.gif"/>');
          $(m.error).removeClass("error");
          $(m.error).addClass("success");
          $('#' + this.get(0).id + '_msg').html("Password matched correctly");
        } else {
          $('#' + this.get(0).id + '_img').html('<img src="/images/exclamation.gif"/>');
          $(m.error).addClass("error");
          $(m.error).removeClass("success");
          $('#' + this.get(0).id + '_msg').html("The passwords don't match");
        };
};
function hideLogin() {
        $("#TB_window").hide();
        $("#TB_overlay").hide();
        $("#TB_HideSelect").css({display:"none"});
};


  function parseQuery ( query ) {
     var Params = new Object ();
     if ( ! query ) return Params; // return empty object
     var Pairs = query.split(/[;&]/);
     for ( var i = 0; i < Pairs.length; i++ ) {
        var KeyVal = Pairs[i].split('=');
        if ( ! KeyVal || KeyVal.length != 2 ) continue;
        var key = unescape( KeyVal[0] );
        var val = unescape( KeyVal[1] );
        val = val.replace(/\+/g, ' ');
        Params[key] = val;
     }
     return Params;
  }

  function getPageScrollTop(){
        var yScrolltop;
        if (self.pageYOffset) {
                yScrolltop = self.pageYOffset;
        } else if (document.documentElement && document.documentElement.scrollTop){      // Explorer 6 Strict
                yScrolltop = document.documentElement.scrollTop;
        } else if (document.body) {// all other Explorers
                yScrolltop = document.body.scrollTop;
        }
        arrayPageScroll = new Array('',yScrolltop)
        return arrayPageScroll;
  }




$(document).ready(function(){

  //TO-DO set the position w/ javaScript.


  $("//select[@class=submit]").change(function() {
    $('form[@class=submit]').get(0).submit();
  });
  $("//input[@class=delete]").click(function() {
    return confirm('Are you sure you want to delete selected items?');
  });





  $("//input[@class=validated]").blur(function() { $(this).validate() });
  $("#r_confirmpass").blur(function() {
          $(this).match({match: '#r_password', error: '#r_confirmpass_li'});
  });
  $("#embed_html input").click(function(){ this.select(); });
  $("#embed_html textarea").click(function(){ this.select(); });
  $('#change_captcha').click( function() {
      $('#captcha_img').get(0).src = "/imagebuilder.php?" + (new Date()).getTime();
       return false;
  });
  $(".minput").oneclick(function(){  $(this).val('');  });
  $("a.ajax").click(function() {

      var url = this.href;
      var action = url.split("?");
      var queryString = url.replace(/^[^\?]+\??/,'');

      var params = parseQuery( queryString );
      $("#" + params.target).fadeTo('slow', .01);
      $.post(action[0] + '/ajax' + (action[1] ? '?' + action[1] : ''), '', function(json) {
           //console.info(json);
           eval("var args = " + json);
           //console.info(params.target + " => " + args.html);
           //console.info($("#" + params.target));
           $("#" + params.target).html(args.html);
           $("#" + params.target).fadeTo('slow', 100);

           $("#ajaxMessageStack").html(args.msg);
           $("#ajaxMessageStack").fadeIn('slow', function(){
                setTimeout(function(){ $("#ajaxMessageStack").fadeOut('slow'); }, 2000);
           });
      });
      return false;
  });

  var html = $('.tabBlock');
  var links = $('.tabLink');
  ( function() {
    links.click(function() {
      for(i = 0; i < links.length; i++) {
        if(this == links[i]) {
        	$(html[i]).show();
        	$(links[i]).addClass('selected');
        } else {
        	$(html[i]).hide();
        	$(links[i]).removeClass('selected');
        }
      }
      return false;
    });
  })();

  $(".form li").mouseover(function() {
          $(this).addClass("selected");
  });
  $(".form li").mouseout(function() {
          $(this).removeClass("selected");
  });
});
