// check if UA is IE
var ie =  (typeof window.ActiveXObject != 'undefined');

setTimeout('wrapImages();',1300);
//setTimeout('addCorners();',1100);

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
        }
        // CAUTION: 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;
    }
};
//
$(document).ready(function(){
	// var siteViewControls='<div id="viewControls"><p>Site Viewing Options:</p>';
	// siteViewControls+='<dl><dt id="type">Type</dt><dd><ul><li id="largeTypeControl" class="type-ctl"><a href="">large type</a></li><li id="largerTypeControl" class="type-ctl"><a href="">larger type</a></li><li id="largestTypeControl" class="type-ctl"><a href="">largest type</a></li></ul></dd>';
	// siteViewControls+='<dt id="contrast">Contrast</dt><dd><ul><li id="darkOnLightControl" class="contrast-ctl"><a href="">dark on light</a></li><li id="lightOnDarkControl" class="contrast-ctl"><a href="">light on dark</a></li></ul></dd>';
	// siteViewControls+='<dt id="layout">Layout</dt><dd><ul><li id="multiColumnControl" class="layout-ctl"><a href="">multi-column</a></li><li id="singleColumnControl" class="layout-ctl"><a href="">single-column</a></li></ul></dd></dl></div>';
	// $("#header h1").before(siteViewControls);
	tagOffsiteLinks();
    var ss=location.search.indexOf('section=');
    if (ss!=-1) {
	  var letter = "";
	  if(location.search.substring(10,1) != "i"){
		var letter = "i";
	  }
      newShowContent("#"+letter+location.search.substring(9,location.search.length));
    }
	$("#viewControls a").hover(
		function(){
			var tip = "<div id=\"SiteToolsHelp\" class=\"siteToolsHelp\">"+this.innerHTML+"</div>"
			$("body").append(tip);
			var tipLeft = $(this).parent().position().left + 15;
			var tipTop = $(this).parent().position().top + 40;
			$("#SiteToolsHelp").css("left",tipLeft).css("top",tipTop);
		},
		function(){
			$("#SiteToolsHelp").remove();
		}
	);
	$("#viewControls li").click(function(){
		if($.cookie("prefs")){
		    var cv=$.cookie("prefs");
		    var fs=cv.substring(cv.indexOf('fontSize')+8,cv.indexOf('fontSize')+9);
		    var ct=cv.substring(cv.indexOf('contrast')+8,cv.indexOf('contrast')+9);
		    var lt=cv.substring(cv.indexOf('layout')+6,cv.indexOf('layout')+7);
		} else {
		    var fs="1";
		    var ct="1";
		    var lt="1";
		}
		var opt = $(this).attr("id");
		opt = opt.replace(/Control/,"");
		var optClass = $(this).attr("class");
		if(optClass == "type-ctl"){
			$("body").removeClass("largeType largerType largestType");
			$("body").addClass(opt);
			if(opt == "largeType"){ fs = "1"; }
			if(opt == "largerType"){ fs = "2"; }
			if(opt == "largestType"){ fs = "3"; }
		}
		if(optClass == "layout-ctl") {
			$("body").removeClass("singleColumn multiColumn");
			$("body").addClass(opt);
			if(opt == "multiColumn"){ lt = "1"; }
			if(opt == "singleColumn"){ lt = "2"; }
		}
		if(optClass == "contrast-ctl") {
			if(opt == "lightOnDark") {
				$("body").attr("id","reverseType");
				var ct = "2";
			} else {
				$("body").attr("id","");
				var ct = "1";
			}
		}
		$.cookie("prefs",null);
		var cookieVals = "fontSize"+fs+"qcontrast"+ct+"qlayout"+lt;
		$.cookie("prefs",cookieVals, { expires: 7, path: "/", domain: ".glaucoma.org" });
		removeCorners();
		addCorners();
		return false;
	});
	$("a.offSite").hover(
		function(){
			var tip = "<div id=\"SiteToolsHelp\" class=\"siteToolsHelp\">This is a link to a site outside of glaucoma.org</div>"
			$("body").append(tip);
			var tipLeft = $(this).offset().left + 10 + "px";
			var tipTop = $(this).offset().top + 25 + "px";
			$("#SiteToolsHelp").css("left",tipLeft).css("top",tipTop);
		},
		function(){
			$("#SiteToolsHelp").remove();
		}
	);
	$("#mainTextModulesNav a").click(function(){
		$("div#mainTextModulesNav li").removeClass("current");
		$("div.mainText").css("display","none");
		var showThis = $(this).attr("href");
		if(this.id == "all"){
			$("div.mainText").css("display","inline");
		} else {
			$("div"+showThis).css("display","inline");
		}
		$(this).parent("li").addClass("current");
		return false;
	})
	$(":radio[@name='version']").click(function(){
		if(this.value == "email"){
			$("#gleamsEmailReq").css("display","inline");
			$(".reqEmail").css("display","inline");
			$(".reqPost").css("display","none");			
		} else if(this.value == "print") {
			$("#gleamsEmailReq").css("display","none");
			$(".reqEmail").css("display","none");
			$(".reqPost").css("display","inline");			
		}
	});
	$("#other_amount_holder").focus(function(){
		$("#amtOther").attr("checked","checked");
	});
	$("#other_amount_holder").blur(function(){
		var ov=this.value;
		if (ov.indexOf(".")==-1) {ov+=".00"}; 
		$("#amtOther").val(ov);
		if(ov < 1001) {
			var msg = "Please enter a value greater than 1001. You entered " + ov + ".";
			$("#donateValMsg").css("display","block").text(msg);
			setTimeout(function () { $("#other_amount_holder").focus() }, 10);
		} else {
			$("#donateValMsg").text("");
		}
	});
//	wrapImages();
//	addCorners();

});
function newShowContent(showThis){
	$("div#mainTextModulesNav li").removeClass("current");
	$("div.mainText").css("display","none");
	$("div"+showThis).css("display","inline");
	$(showThis+"link").parent("li").addClass("current");
	return false;
}
function wrapImages(){
	var imgs = $("img");
	var num = imgs.length;
	for(var i=0; i<num; i++) {
		var img = imgs[i];
		if (img.width>50&&img.className!='noCorners') {
			var imgWrap = "<div style=\"width:"+img.width+"px;height:"+img.height+"px;\" class=\"imgWrap\" id=\"iw"+i+"\"></div>";			
			if(ie && img.parentNode.tagName == "A"){
				var imgParent = img.parentNode;
				$(imgParent).wrap(imgWrap);
			} else {
				$(img).wrap(imgWrap);
			}
			var imgCrnr = "<div class=\"tlCorner\"></div>";
			imgCrnr += "<div class=\"trCorner\"></div>";
			imgCrnr += "<div class=\"blCorner\"></div>";
			imgCrnr += "<div class=\"brCorner\"></div>";
			$("#iw"+i).append(imgCrnr);
		}
	}
}

function addCorners(){
	var imgWrappers = $("div.imgWrap");
	var wrapNum = imgWrappers.length;
	for(var i=0; i < wrapNum; i++) {
		var imgCrnr = "<div class=\"tlCorner\" id=\"tlCorner"+i+"\"></div>";
		imgCrnr += "<div class=\"trCorner\" id=\"trCorner"+i+"\"></div>";
		imgCrnr += "<div class=\"blCorner\" id=\"blCorner"+i+"\"></div>";
		imgCrnr += "<div class=\"brCorner\" id=\"brCorner"+i+"\"></div>";
		$("#iw"+i).append(imgCrnr);
	}
}
function removeCorners() {
	$(".tlCorner").remove();
	$(".trCorner").remove();
	$(".blCorner").remove();
	$(".brCorner").remove();
}
function tagOffsiteLinks() {
	var links = $("a");
	for(var i=0; i<links.length; i++) {
		if (links[i].href && (links[i].className != "no-hover") && (links[i].className != "feed-link")) {
	        if (unescape(links[i].href).indexOf('glaucoma.com')==-1 && unescape(links[i].href).indexOf('www.glaucoma.org')==-1 && unescape(links[i].href).indexOf('@glaucoma.org')==-1) {
				$(links[i]).addClass("offSite");
			}
		}
	}
}
// copied over from the old JS because I'm not sure where they might be used
function isChildOfClass(obj,parentClass) {
	while (obj.tagName && (obj.className.indexOf(parentClass)==-1))
	{
		obj = obj.parentNode;
	}
	if (!obj.className) return false;
	if (obj.className.indexOf(parentClass)!=-1) return true;
}
function toggleDisplay(elementID,changeTo) {
	var e = document.getElementById(elementID);
	if (e.style.display != changeTo) {
		e.style.display = changeTo;
	}
}
function addToElementEventAttribute(whichElement,whichEvent,codeToAdd) {
  var cav='';
  if (whichElement.getAttribute(whichEvent)) {
    cav=whichElement.getAttribute(whichEvent);
  }
  var nav=cav+codeToAdd;
  if (ie) {
    whichElement[whichEvent]=new Function(nav);
  } else {
    whichElement.setAttribute(whichEvent,nav);
  }
}
function showContent(cId) {
  if (document.getElementById) {
    var ci=document.getElementById(cId);
    var cip=ci.parentNode;
    var cisp=document.getElementById('mainTextModulesNav');
    var cis=cisp.getElementsByTagName('LI');
    for (var i=0;i<cis.length;i++) {
      removeFromElementClass(cis[i],'current');
    }
    addToElementClass(cip,'current');
    var cp=document.getElementById('mainTextModules');
    var cs=cp.getElementsByTagName('DIV');
    for (var i=0;i<cs.length;i++) {
      if (cs[i].id!='mainTextModulesNav') {
        if (cId=='all') {
          cs[i].style.display='inline';
        } else {
          cs[i].style.display='none';     
        }
      }
    }
    if (cId!='all') {
      var c=document.getElementById(cId.substring(0,cId.length-4));
      c.style.display='inline';
    }
  }
}
function returnMousePosition(e) {
  var ie =  (typeof window.ActiveXObject != 'undefined');
  if (ie) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft
    tempY = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX
    tempY = e.pageY
  }  
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}
  return [tempX, tempY];
}