// Jquery cookie plugin.
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options = $.extend({}, options); // clone object since it's unexpected behavior if the expired property were changed
            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
        }
        // NOTE Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        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;
    }
};


function autoSelect(selectTarget) {
 	if(selectTarget != null && ((selectTarget.childNodes.length == 1
      && selectTarget.childNodes[0].nodeName == "#text") || (selectTarget.tagName == "INPUT"
      && selectTarget.type == "text"))) {
  		if(selectTarget.tagName == 'TEXTAREA' || (selectTarget.tagName == "INPUT" && selectTarget.type == "text")) {
  			 selectTarget.select();
  		} else if(window.getSelection) { // FF, Safari, Opera
   			var sel = window.getSelection();
   			var range = document.createRange();
   			range.selectNode(selectTarget.firstChild);
   			sel.removeAllRanges();
   			sel.addRange(range);
  		} else { // IE
   			document.selection.empty();
   			var range = document.body.createTextRange();
   			range.moveToElementText(selectTarget);
   			range.select();
  		}
 	}
}

function externalLinks(){
	if(!document.getElementsByTagName) return;
		var anchors = document.getElementsByTagName("a");
		for(var i=0; i<anchors.length; i++){
			var anchor = anchors[i];
			if(anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") anchor.target = "_blank";
		}
}
window.onload = externalLinks;


$(function(){
  	// Document is ready
	//Check to see if there has been a background cookie set.
	if ($.cookie('background_cookie') != null) {
		// alert($.cookie('background_cookie'));
		var background = 'url(../_graphics/header_image' + $.cookie("background_cookie") + '.jpg)';
		$('#li_back' + $.cookie('background_cookie')).css('opacity' ,'0.5');
		$("#mainwrap").css("background-image", background)
	} else {
		//No Cookie set so make the first thumbnail opaque to show its selected.
		$('#li_back1').css('opacity' ,'0.5');
	}	
	
	//Function for changing the background image setting a cookie and fading in/out the buttons
	function changeBackground(backgroundNumber){
		// alert(backgroundNumber);
		var background = 'url(../_graphics/header_image' + backgroundNumber + '.jpg)';		
		$('#ul_background li').css('opacity' ,'1');
		$('#li_back' + backgroundNumber).css('opacity' ,'0.5');
		$("#mainwrap").css("background-image", "none")
		.css("background-image", background)
	}	

	$('.bgselect1').click(function(){
		changeBackground(1);
		$.cookie('background_cookie', 1);
		return false;
	});
	
	$('.bgselect2').click(function(){
		$.cookie('background_cookie', 2);
		changeBackground(2);
		return false;
	});
	
	$('.bgselect3').click(function(){
		$.cookie('background_cookie', 3);
		changeBackground(3);
		return false;
	});
});