$(document).ready(function() {
	$('.btn-sm').click(function(){
		$('a', $(this)).click();
	});
	$('#auto-menu .hover').hide();
	$('#auto-menu li').mouseover(function(){
		$('.visited', this).hide();
		$('.hover', this).show();
	});
	$('#auto-menu li').mouseout(function(){
		$('.visited', this).show();
		$('.hover', this).hide();
	});
	
	if(jQuery.browser.opera)
	{
		$('ul#auto-menu li a').css('margin-right','70px');
		$('ul#auto-menu li a.last').css('margin-right','0');
	}
	
	
	
	$('input[@type=text]').focus(function(){
		var currentVal = $(this).val();										// Read initial field value
		$(this).val('');														// Clear initial value

		$(this).blur(function(){
			var getNewVal = $(this).val();										// Read new input value
			if ( getNewVal == '' || getNewVal == ' ' ) {
				$(this).val(currentVal);										// Switch to initial value
			}
		});
	});

	// fix png for IE
	$('img[@src$=.png], div').ifixpng();

	// Safari CSS
	if ( $.browser.safari ) {
		$("head").append('<link href="css/safari.css" media="screen" rel="stylesheet" type="text/css" />');
	}

	// remove any borders on last LI element
	$("ul.nav").each(function(){
		 $(this).children("li:last").css({borderRight:"0",borderBottom:"0",background:"none",paddingRight:"0",marginRight:"0"});
	});

	$(".box").each(function(){
		 $(this).children("p:last").css({borderRight:"0",marginBottom:"0",background:"none",paddingRight:"0",marginRight:"0"});
	});

	if ( $('.gContent').length ) {
		if ( $('.gContent').height() > $('.colmiddle .fixedLarge').height() )
			$('.colmiddle .fixedLarge').height( $('.gContent').height() );
	}

	// help bubbles
	if ( $('.withhelp').length ) {
		$('.withhelp .helpbubble').next().children('input, textarea').focus(function(){
			$(this).parent('p').prev().fadeIn();
			var fieldType = $(this).attr('type');
			if ( fieldType == 'text' || fieldType == 'TEXT' || fieldType == 'password' || fieldType == 'PASSWORD' ) {
				$(this).parent('p').prev().children('.rounded').children('.innerbub').css('background-position','left 16px');
				$(this).parent('p').prev().css('top','-14px');
			}
			$(this).blur(function(){ $(this).parent('p').prev().fadeOut(); });
		});
	}
});

// Define indexOf for IE
if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}

jQuery.fn.log = function (msg) {
  console.log("%s: %o", msg, this);
  return this;
};

function urlencode( str ) {
	// http://kevin.vanzonneveld.net
	// +   original by: Philip Peterson
	// +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +      input by: AJ
	// +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +   improved by: Brett Zamir
	// %          note: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/
	// *     example 1: urlencode('Kevin van Zonneveld!');
	// *     returns 1: 'Kevin+van+Zonneveld%21'
	// *     example 2: urlencode('http://kevin.vanzonneveld.net/');
	// *     returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
	// *     example 3: urlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
	// *     returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'
							 
	var histogram = {}, tmp_arr = [];
	var ret = str.toString();

	var replacer = function(search, replace, str) {
		var tmp_arr = [];
		tmp_arr = str.split(search);
		return tmp_arr.join(replace);
	};

	// The histogram is identical to the one in urldecode.
	histogram["'"]   = '%27';
	histogram['(']   = '%28';
	histogram[')']   = '%29';
	histogram['*']   = '%2A';
	histogram['~']   = '%7E';
	histogram['!']   = '%21';
	histogram['%20'] = '+';

	// Begin with encodeURIComponent, which most resembles PHP's encoding functions
	ret = encodeURIComponent(ret);

	for (search in histogram) {
		replace = histogram[search];
		ret = replacer(search, replace, ret) // Custom replace. No regexing
	}

	// Uppercase for full PHP compatibility
	return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
		return "%"+m2.toUpperCase();
	});

	return ret;
}
