$(function() {

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        var path = options.path ? '; path=' + options.path : '';
        var domain = options.domain ? '; domain=' + options.domain : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

$.fn.enable = function() {
	return this.each(function(){
		this.disabled = false;
	});
}

$.fn.disable = function() {
	return this.each(function(){
		this.disabled = true;
	});
}
});


function createCookie(name,value,days) {
	$(function() {
		$.cookie(name,value,{path: "/", expires:days});
	});
}

function readCookie(name) {
	$(function() {
		return $.cookie(name);
	});
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function getRandom(size) {
	return Math.floor(Math.random()*size);
}

/* Used by selectBox.tag */
function selectItem(elementId, selectedOption)
{
	options = document.getElementById(elementId).options
	for (var i = 0; i < options.length; i++)
	{
		if (options[i].value == selectedOption)
			options[i].selected = true;
	}
}
/**
 * context bind method
 * in JS, the keyword 'this' means the current closure instead of the
 * object in which the method is defined. This causes problems in callback
 * methods. Use this bind method to bind object to the method
 * got it from http://fn-js.info/snippets/bind
 * @param obj
 * @param fun
 * @param args
 */
function bindContext(obj, fun, args) {
  return function() {
    if (obj === true)
      obj = this;
    var f = typeof fun === "string" ? obj[fun] : fun;

    return f.apply(obj, Array.prototype.slice.call(args || [])
        .concat(Array.prototype.slice.call(arguments)));
  };
}

/** Clone function used for prototypal inheritance */
function clone(object){
	function F() {}
	F.prototype = object;
	return new F;
}

$(function() {
	$(".bgRight").each(function(){this.id = 'inImg' + (getRandom(4) + 1);});
});

/**
 * Replaces special MS Word characters (non UTF-8, e.g. smart quotes)
 * with their ASCII equivalents.
 * Got it from: http://www.kevinkorb.com/post/37
 */
function removeMSWordChars(str) {
	var replacements = new Array();
	replacements[8211] = 45;
	replacements[8212] = 45;
	replacements[8216] = 39;
	replacements[8217] = 39;
	replacements[8218] = 39;
	replacements[8220] = 34;
	replacements[8221] = 34;
	replacements[8222] = 34;
	replacements[8224] = 43;
	replacements[8226] = 46;
	//replacements[8230] = "...";
	replacements[8249] = 60;
	replacements[8250] = 62;
	for (var i = 0; i < str.length; i++)
	{
		var code = str.charCodeAt(i);
		if (replacements[code] != undefined)
		{
			var intReplacement = replacements[code];
			str = str.substr(0, i) + String.fromCharCode(intReplacement) + str.substr(i + 1);
		}
		else if (code == 8230)
		{
			str = str.substr(0, i) + String.fromCharCode(46) + String.fromCharCode(46) + String.fromCharCode(46) + str.substr(i + 1);
		}
	}
	return str;
}

/**
 * Calls removeMSWordChars on all textareas.
 */
$(function() {
	$("textarea").blur(
	function()
	{
		$(this).val(removeMSWordChars(this.value));
	});
});

/***
 * Bookmark the current page. Currently just bookmarks the page that the link is on (location.href)
 *
 * Usage: <a href="#" title="NAME OF BOOKMARK" class="bookmarkME">blah blah</a>
 */
$(function() {
	if($.browser.opera) {
		if ($("a.bookmarkMe").attr("rel") != ""){
			$("a.bookmarkMe").attr("rel","sidebar");
			$("a.bookmarkMe").attr("href",location.href);
		}
	}

	$("a.bookmarkMe").click(function(e){
		e.preventDefault();

		var url = location.href;
		var title = $(this).attr("title");

		if ($.browser.mozilla) {
			window.sidebar.addPanel(title, url,"");
		} else if( $.browser.msie ) {
			window.external.AddFavorite( url, title);
		} else if($.browser.opera) {
			return false;
		} else {
			 alert('Unfortunately, this browser does not support the requested action,'
			 + ' please bookmark this page manually.');
		}

	});
});


$(function(){
    try {
    	placeholderMimic();
    } catch (error){

    }
});

function placeholderMimic(){

    if(!supports_input_placeholder()){
    		$(".placeholderMimic").css('color','#c3c3c3');
            $(".placeholderMimic").focus(function(){
               if($(this).val() == $(this).attr("placeholder")){
                       $(this).attr("value","");
                       $(this).css('color','');
               }
            }).blur(function(){
               if($(this).val() == ""){
                       $(this).attr("value",$(this).attr("placeholder"));
                            $(this).css('color','#c3c3c3');
               }
            });

            $(".placeholderMimic").each(function(){
                    if($(this).val() == ""){
                            $(this).val($(this).attr("placeholder"));
                            $(this).css('color','#c3c3c3');
                    }
            });
    }
}

function supports_input_placeholder() {
	var i = document.createElement('input');
	return 'placeholder' in i;
}

/*
 * TODO: These coremetrics functions are being kept here even though we have removed coremetrics
 * so that we can leave the logic in those pages that call these functions so that in the future
 * we can convert those calls to generate GA events instead.
 */
function cmCreateDefaultPageviewTag(categoryID) {}
function cmCreatePageviewTag(pageID, categoryID, searchString, searchResults) {}
function cmCreateErrorTag(pageID, categoryID) {}
function cmCreateManualPageviewTag(pageID, categoryID,DestinationURL,ReferringURL) {}

