/**
 * Adds rollover to an image.
 * This script assumes that there is a corresponding
 * image with _over appended to the filename (before the extension)
 * 
 * @param obj	A jquery object representing the image to add the rollover to
 */
function rollover(obj) {
	if(obj.attr("src").indexOf("_over") > 0) return;
	var extension = obj.attr("src").substr((obj.attr("src").length-3),3);
	obj.attr("src",obj.attr("src").replace("."+extension,"_over."+extension));
}

/**
 * Adds rollout to an image.
 * This script assumes that there is a corresponding
 * image with _over appended to the filename (before the extension)
 * 
 * @param obj	A jquery object representing the image to add the rollout to
 */
function rollout(obj) {
	if(obj.attr("src").indexOf("_over") < 0) return;
	var extension = obj.attr("src").substr((obj.attr("src").length-3),3);
	obj.attr("src",obj.attr("src").replace("_over."+extension,"."+extension));
}

/**
 * Preloads a rollover image
 * This script assumes that there is a corresponding
 * image with _over appended to the filename (before the extension).
 * 
 *  @param obj	A jquery object representing the image to preload a rollover image for
 */
function preloadRollover(obj) {
	var extension = obj.attr("src").substr((obj.attr("src").length-3),3);
	var img = new Image();
	img.src = obj.attr("src").replace("."+extension,"_over."+extension);
}function preloadImage(imageSource) {	var img = new Image();	img.src = imageSource;}

/**
 * Adds rollover/rollout functionality to an image and
 * preloads the rollover image.
 * This script assumes that there is a corresponding
 * image with _over appended to the filename (before the extension)
 * 
 *  @param obj	A jquery object representing the image to add rollover functionality to
 */
function addRollover(obj) {
	obj.each(function() {
		preloadRollover($(this));
	});
	obj.mouseover(function() {
		rollover($(this));
	});
	obj.mouseout(function() {
		rollout($(this));
	});
}

function openWindow(pageUrl,width,height,winName,scrollbars) {
	if(scrollbars != 1) scrollbars = 0;
    var newWindow = window.open(pageUrl,winName,"toolbar=0,scrollbars="+scrollbars+",location=0,statusbar=0,menubar=0,resizable=0,width="+width+",height="+height);
    return newWindow;
}
/* function to fix the -10000 pixel limit of jquery.animate */
$.fx.prototype.cur = function(){
    if ( this.elem[this.prop] != null && (!this.elem.style || this.elem.style[this.prop] == null) ) {
      return this.elem[ this.prop ];
    }
    var r = parseFloat( jQuery.css( this.elem, this.prop ) );
    return typeof r == 'undefined' ? 0 : r;
}

$(document).ready(function(){
	$(".scroll-anchor").click(function(event){
		//prevent the default action for the click event
		event.preventDefault();

		//get the full url - like mysitecom/index.htm#home
		var full_url = this.href;

		//split the url by # and get the anchor target name - home in mysitecom/index.htm#home
		var parts = full_url.split("#");
		var trgt;
		(parts.length>1) ? trgt = parts[1] : trgt = parts[0];

		//get the top offset of the target anchor
		var target_offset = $("#"+trgt).offset();
		var target_top = target_offset.top;
		
		//get the distance so that we can make a consistent velocity
		var target_distance = Math.abs(target_offset.top - $(this).offset().top);

		//goto that anchor by setting the body scroll top to anchor top
		//$('html, body').animate({scrollTop:target_top}, target_distance/1.6);
		$('html, body').animate({scrollTop:target_top}, 500, 'easeInOutQuad');
	});
});

