
// General setup, mousetracking, et al functions...

var nav = (document.layers);
var x = 0;
var y = 0;
if(nav) document.captureEvents(Event.MOUSEMOVE);
document.onmousemove = get_mouse;

function get_mouse(e) {
  x = (nav) ? e.pageX : event.clientX+window.parent.screenLeft;
  y = (nav) ? e.pageY : event.clientY+window.parent.screenTop;
  y = y + 5;
  x = x + 5;
  document.status = x,y;
}

function on(nos) {
	if (document.images) {
	document[nos].src = eval(nos + "on.src"); } }

function off(nos) {
	if (document.images) {
	document[nos].src = eval(nos + "off.src"); } }

function new_window(url,wi,hi) {
  if (y+245>window.screen.availHeight) {
    y = ((window.screen.availHeight)-275);
  }
  if (x+260>window.screen.availWidth) {
    x = ((window.screen.availWidth)-275);
  }
  link = window.open(url,"Link","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width="+wi+",height="+hi+",left="+x+",top="+y+"");
  link.focus();
}


// ***************************************************
// Begin image swap functions...


/* 
*************************************************************************
Alternate image swap params setup in disjointedRollover function vars: 
1) id of image (main window), 
2) id of object containing mouseover links, 
3) id of overlay to be created for fade effects, 
4) extension of disjointed image sprite

The name and location of the sprite is stripped from the default main image attributes. 
It just has ".sprite" added. So ensure it is named accordingly and placed in the same folder.

Author: Mike Foskett
http://websemantics.co.uk/resources/cross-fading_disjointed_image_rollover/
*/

function setup(){
  disjointedRollover('mainimg','imagelinks',"overlay",".sprite")
}



// test DOM functions are supported and run setup on page load
if (isDom()){
  addLoadEvent(setup);
}

// author: Simon Willisons - http://simon.incutio.com/archive/2004/05/26/addLoadEvent
function addLoadEvent(f){var o=window.onload;if(typeof window.onload!='function'){window.onload=f}else{window.onload=function(){o();f()}}}

// standard functions
function $id(id){return(document.getElementById(id)?document.getElementById(id):false)}
function idExists(id){return($id(id)?true:false)}
function isDom(){return (document.getElementById&&document.getElementsByTagName&&document.createElement)?true:false}

function setOpacity(id,opacity){
  var obj=$id(id).style;
  obj.opacity=opacity/100;
  obj.MozOpacity=opacity/100;
  obj.KhtmlOpacity=opacity/100;
  obj.filter="alpha(opacity="+opacity+")";
}

/* author: Steffen Rusitschka - http://www.ruzee.com/blog/2006/07/retrieving-css-styles-via-javascript/ */
function hyphenToCamel(s){for(var exp=/-([a-z])/;exp.test(s);s=s.replace(exp,RegExp.$1.toUpperCase()));return s;};

function getStyleProperty(id,property){
  // note this function is not 100% generic for all CSS properties
  var obj=$id(id),value='';
  if(window.getComputedStyle){
    value=window.getComputedStyle(obj,null).getPropertyValue(property);
  }else{
    if(obj.currentStyle){
      value=obj.currentStyle[hyphenToCamel(property)];
    }
  }
  return value;
}

function disjointedRollover(id,linkContainer,overlayID,spriteExt){
  if (idExists(id)){

    // get original background-image name
    var newImg=getStyleProperty(id,'background-image');

    //remove text styling "url()"
    newImg=newImg.replace('url(','').replace(')','');

    // remove IEs ""
    newImg=newImg.replace(/"/g,'');

    // replace .extension with .sprite.extension
    var ext=newImg.substring(newImg.lastIndexOf("."));
    newImg=newImg.replace(ext,spriteExt+ext);

    // preload rollover image and attach variables
    var img=new Image();
    img.overlayID=overlayID;
    img.spriteExt=spriteExt;
    img.disjointID=id;

    img.onload=function(){

      // image loaded so replace background image
      $id(id).style.backgroundImage="url("+img.src+")";

      // create an overlay span for fading
      var sp=document.createElement('span');
      sp.id=img.overlayID;
      sp.className=$id(id).className;
      sp.style.backgroundImage="url("+img.src+")";

      // main graphic acts as a link too
      sp.onclick=function(){
        window.location=$id(this.className).href;
      }

      //  add overlay to disjoint-image block 'mainimg'
      $id(id).appendChild(sp);

      var rollover=function(){
            var obj=$id(img.overlayID)
            // set overlay initial opacity and position (via class)
            setOpacity(img.overlayID,0);
            obj.className=this.id;

            // fade in overlay: adjust 101 for fade steps, currently there's 4.
            for(var i=25;i<201;i+=50){
              setTimeout("setOpacity('"+img.overlayID+"',"+i+")",i*2);
            }

            // set bg img position
            setTimeout("$id('"+img.disjointID+"').className='"+this.id+"'",i*2);

            // switch off overlay
            setTimeout("setOpacity('"+img.overlayID+"',0)",i*2);
      }


/*
*********************************************************
Altered for alternate image change on area mouseover
Andis Blukis, 2009-02-19 17:34:21 -0500 (Thu, 19 Feb 2009)
*/

      // add mouseover actions to areas
      var liAs=$id(linkContainer).getElementsByTagName('area');
      for (var i=0;i<liAs.length;i++){
        liAs[i].onmouseover=rollover;
        liAs[i].onfocus=rollover;
      }
    }
    img.src = newImg;
  }
}

// End alternate image sprite adjust functions... 




/*
***************************************************
Begin map highlight functions...

Author: David Lynch
http://davidlynch.org/js/maphilight/docs/

Uses /lib/jquery-1.3.2.js
See end of code block for style/CSS params. 
*/


// jquery.maphilight.js

(function($) {
	var has_VML, create_canvas_for, add_shape_to, clear_canvas, shape_from_area,
		canvas_style, fader, hex_to_decimal, css3color, is_image_loaded;
	has_VML = document.namespaces;
	has_canvas = document.createElement('canvas');
	has_canvas = has_canvas && has_canvas.getContext;
	
	if(!(has_canvas || has_VML)) {
		$.fn.maphilight = function() { return this; };
		return;
	}
	
	if(has_canvas) {
		fader = function(element, opacity, interval) {
			if(opacity <= 1) {
				element.style.opacity = opacity;
				window.setTimeout(fader, 10, element, opacity + 0.1, 10);
			}
		};
		
		hex_to_decimal = function(hex) {
			return Math.max(0, Math.min(parseInt(hex, 16), 255));
		};
		css3color = function(color, opacity) {
			return 'rgba('+hex_to_decimal(color.substr(0,2))+','+hex_to_decimal(color.substr(2,2))+','+hex_to_decimal(color.substr(4,2))+','+opacity+')';
		};
		create_canvas_for = function(img) {
			var c = $('<canvas style="width:'+img.width+'px;height:'+img.height+'px;"></canvas>').get(0);
			c.getContext("2d").clearRect(0, 0, c.width, c.height);
			return c;
		};
		add_shape_to = function(canvas, shape, coords, options) {
			var i, context = canvas.getContext('2d');
			context.beginPath();
			if(shape == 'rect') {
				context.rect(coords[0], coords[1], coords[2] - coords[0], coords[3] - coords[1]);
			} else if(shape == 'poly') {
				context.moveTo(coords[0], coords[1]);
				for(i=2; i < coords.length; i+=2) {
					context.lineTo(coords[i], coords[i+1]);
				}
			} else if(shape == 'circ') {
				context.arc(coords[0], coords[1], coords[2], 0, Math.PI * 2, false);
			}
			context.closePath();
			if(options.fill) {
				context.fillStyle = css3color(options.fillColor, options.fillOpacity);
				context.fill();
			}
			if(options.stroke) {
				context.strokeStyle = css3color(options.strokeColor, options.strokeOpacity);
				context.lineWidth = options.strokeWidth;
				context.stroke();
			}
			if(options.fade) {
				fader(canvas, 0);
			}
		};
		clear_canvas = function(canvas, area) {
			canvas.getContext('2d').clearRect(0, 0, canvas.width,canvas.height);
		};
	} else {
		document.createStyleSheet().addRule("v\\:*", "behavior: url(#default#VML); antialias: true;"); 
		document.namespaces.add("v", "urn:schemas-microsoft-com:vml"); 
		
		create_canvas_for = function(img) {
			return $('<var style="zoom:1;overflow:hidden;display:block;width:'+img.width+'px;height:'+img.height+'px;"></var>').get(0);
		};
		add_shape_to = function(canvas, shape, coords, options) {
			var fill, stroke, opacity, e;
			fill = '<v:fill color="#'+options.fillColor+'" opacity="'+(options.fill ? options.fillOpacity : 0)+'" />';
			stroke = (options.stroke ? 'strokeweight="'+options.strokeWidth+'" stroked="t" strokecolor="#'+options.strokeColor+'"' : 'stroked="f"');
			opacity = '<v:stroke opacity="'+options.strokeOpacity+'"/>';
			if(shape == 'rect') {
				e = $('<v:rect filled="t" '+stroke+' style="zoom:1;margin:0;padding:0;display:block;position:absolute;left:'+coords[0]+'px;top:'+coords[1]+'px;width:'+(coords[2] - coords[0])+'px;height:'+(coords[3] - coords[1])+'px;"></v:rect>');
			} else if(shape == 'poly') {
				e = $('<v:shape filled="t" '+stroke+' coordorigin="0,0" coordsize="'+canvas.width+','+canvas.height+'" path="m '+coords[0]+','+coords[1]+' l '+coords.join(',')+' x e" style="zoom:1;margin:0;padding:0;display:block;position:absolute;top:0px;left:0px;width:'+canvas.width+'px;height:'+canvas.height+'px;"></v:shape>');
			} else if(shape == 'circ') {
				e = $('<v:oval filled="t" '+stroke+' style="zoom:1;margin:0;padding:0;display:block;position:absolute;left:'+(coords[0] - coords[2])+'px;top:'+(coords[1] - coords[2])+'px;width:'+(coords[2]*2)+'px;height:'+(coords[2]*2)+'px;"></v:oval>');
			}
			e.get(0).innerHTML = fill+opacity;
			$(canvas).append(e);
		};
		clear_canvas = function(canvas) {
			$(canvas).empty();
		};
	}
	shape_from_area = function(area) {
		var i, coords = area.getAttribute('coords').split(',');
		for (i=0; i < coords.length; i++) { coords[i] = parseFloat(coords[i]); }
		return [area.getAttribute('shape').toLowerCase().substr(0,4), coords];
	};
	
	is_image_loaded = function(img) {
		if(!img.complete) { return false; } // IE
		if(typeof img.naturalWidth != "undefined" && img.naturalWidth == 0) { return false; } // Others
		return true;
	}

	canvas_style = {
		position: 'absolute',
		left: 0,
		top: 0,
		padding: 0,
		border: 0
	};
	
	$.fn.maphilight = function(opts) {
		opts = $.extend({}, $.fn.maphilight.defaults, opts);
		return this.each(function() {
			var img, wrap, options, map, canvas, canvas_always, mouseover;
			img = $(this);
			if(!is_image_loaded(this)) { return window.setTimeout(function() { img.maphilight(opts); }, 200); }
			options = $.metadata ? $.extend({}, opts, img.metadata()) : opts;
			map = $('map[name="'+img.attr('usemap').substr(1)+'"]');
			if(!(img.is('img') && img.attr('usemap') && map.size() > 0 && !img.hasClass('maphilighted'))) { return; }
			wrap = $('<div>').css({display:'block',background:'url('+this.src+')',position:'relative',padding:0,width:this.width,height:this.height});
			img.before(wrap).css('opacity', 0).css(canvas_style).remove();
			if($.browser.msie) { img.css('filter', 'Alpha(opacity=0)'); }
			wrap.append(img);

			canvas = create_canvas_for(this);
			$(canvas).css(canvas_style);
			canvas.height = this.height;
			canvas.width = this.width;
			
			mouseover = function(e) {
				var shape, area_options;
				area_options = $.metadata ? $.extend({}, options, $(this).metadata()) : options;
				if(!area_options.alwaysOn) {
					shape = shape_from_area(this);
					add_shape_to(canvas, shape[0], shape[1], area_options);
				}
				// add image change in main window on mouseover




			};
			
			if(options.alwaysOn) {
				$(map).find('area[coords]').each(mouseover);
			} else {
				if($.metadata) {
					// If the metadata plugin is present, there may be areas with alwaysOn set.
					// We'll add these to a *second* canvas, which will get around flickering during fading.
					$(map).find('area[coords]').each(function() {
						var shape, area_options;
						area_options = $.metadata ? $.extend({}, options, $(this).metadata()) : options;
						if(area_options.alwaysOn) {
							if(!canvas_always) {
								canvas_always = create_canvas_for(img.get());
								$(canvas_always).css(canvas_style);
								canvas_always.width = img.width();
								canvas_always.height = img.height();
								img.before(canvas_always);
							}
							shape = shape_from_area(this);
							add_shape_to(canvas_always, shape[0], shape[1], area_options);
						}
					})
				}
				$(map).find('area[coords]').mouseover(mouseover).mouseout(function(e) { clear_canvas(canvas); });
			}
			
			img.before(canvas); // if we put this after, the mouseover events wouldn't fire.
			img.addClass('maphilighted');
		});
	};
	$.fn.maphilight.defaults = {
		fill: true,
		fillColor: '808000',
		fillOpacity: 0.2,
		stroke: true,
		strokeColor: '808080',
		strokeOpacity: 1,
		strokeWidth: 1,
		fade: true,
		alwaysOn: false
	};
})(jQuery);


// End map highlight functions... 
// ***************************************************

