// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

//***Cross browser attach event function. For 'evt' pass a string value with the leading "on" omitted
//***e.g. AttachEvent(window,'load',MyFunctionNameWithoutParenthesis,false);
function AttachEvent(obj,evt,fnc,useCapture){
	if (!useCapture) useCapture=false;
	if (obj.attachEvent) {
		return obj.attachEvent("on"+evt,fnc);
	}
	else if (obj.addEventListener){
		obj.addEventListener(evt,fnc,useCapture);
		return true;
	}
	else{
		MyAttachEvent(obj,evt,fnc);
		obj['on'+evt]=function(){ MyFireEvent(obj,evt) };
	};
};

//The following are for browsers like NS4 or IE5Mac which don't support either
//attachEvent or addEventListener
function MyAttachEvent(obj,evt,fnc){
	if (!obj.myEvents) obj.myEvents={};
	if (!obj.myEvents[evt]) obj.myEvents[evt]=[];
	var evts = obj.myEvents[evt];
	evts[evts.length]=fnc;
};
function MyFireEvent(obj,evt){
	if (!obj || !obj.myEvents || !obj.myEvents[evt]) return;
	var evts = obj.myEvents[evt];
	for (var i=0,len=evts.length;i<len;i++) evts[i]();
};

function imgSwap(oImg)
{
   var strOver  = "-over"    // image to be used with mouse over
   var strOff = "-off"     // normal image
   var strImg = oImg.src
   if (strImg.indexOf(strOver) != -1) 
      oImg.src = strImg.replace(strOver,strOff)
   else
      oImg.src = strImg.replace(strOff,strOver)
}

function changeImage(index, change) {
	if(change) {
		$('job-2').src = '/images/common/spacer.gif' // Workaround p/ Bug no safari: http://www.quirksmode.org/bugreports/archives/2005/03/Safari_Image_src_swap_resize_bug.html
		$('job-2').src = $('job-1').src;
		Element.hide('job-1');
		Element.show('job-2');
	}
	$('job-1').src = '/images/common/spacer.gif' // Workaround p/ Bug no safari: http://www.quirksmode.org/bugreports/archives/2005/03/Safari_Image_src_swap_resize_bug.html
	$('job-1').src = images[index];
	if(change) {
		Effect.Fade('job-2', { duration: 0.5 });
		setTimeout("centerMain()",500);
		Effect.Appear('job-1', { duration: 0.5, queue: 'end'});
	}
};


function getSide(evt) {
	return (mouseX >= getClientWidth()/2) ? 'right' : 'left';
}

function hideTooltip(event) {
	Element.hide('tooltip');
}

function resetTooltip() {
	hideTooltip();
	setLeft('tooltip', 0);
	setTop('tooltip', 0);
}


function getTooltipLeft(tooltip, mouseX) {
	tooltip_width = Element.getDimensions(tooltip)['width'];
	if(mouseX+8 > getClientWidth()+getScrollX()-tooltip_width-20) {
		return getClientWidth()+getScrollX()-tooltip_width-20;
	}
	else {
		return mouseX+8;
	}
}

function getTooltipTop(tooltip, mouseY) {
	tooltip_height = Element.getDimensions(tooltip)['height'];
	if(mouseY+15 > getClientHeight()+getScrollY()-tooltip_height-20) {
		return getClientHeight()+getScrollY()-tooltip_height-20;
	}
	else {
		return mouseY+15;
	}
}

function updateTooltip(e, text) {
//	if(!npActive) return;
	mouseX=e.pageX?e.pageX:e.clientX;
	mouseY=e.pageY?e.pageY:e.clientY;

	try { text = text.replace('[n]',index+1) }
	catch(err) {	}
	Element.update('tooltip', text);
	Element.show('tooltip');
	setLeft('tooltip', getTooltipLeft('tooltip', mouseX));
	setTop('tooltip', getTooltipTop('tooltip', mouseY));

}

function npSlide(e) {
	if(!npActive) return;

	if ('right' == getSide(e)) nextSlide();
	else previousSlide();
}

function updateNavigation(e) {
	if(!npActive) return;
	mouseX=e.pageX?e.pageX:e.clientX;
	mouseY=e.pageY?e.pageY:e.clientY;

	if ('right' == getSide(e)) {
		Element.hide('arr-back');
		Element.show('arr-next');
	} 
	else {
		Element.hide('arr-next');
		Element.show('arr-back');
	}

}

function previousSlide() {
	var queue = Effect.Queues.get('global');
	if(0 != queue.effects.length) return;


	if (index == 0) index = images.length-1;
	else index--;

	changeImage(index, true);
};

function nextSlide() {
	var queue = Effect.Queues.get('global');
	if(0 != queue.effects.length) return;

	if (index >= images.length-1) index = 0;
	else index++;
	changeImage(index, true);
};

function centerMain() {
	//var main_width = Element.getDimensions('main')['width'];
	//var main_height = Element.getDimensions('main')['height'];
	var main_width = dimensions[index][0];
	var main_height = dimensions[index][1];
	var window_width = getClientWidth();
	var window_height = getClientHeight()-20;
	setLeft('main',0-(main_width-window_width)/2);
	setTop('main',0-(main_height-window_height)/2);
}

function centerSpinner() {
	var window_width = getClientWidth();
	var window_height = getClientHeight()-20;
	setLeft('spinner',0-(19-window_width)/2);
	setTop('spinner',0-(19-window_height)/2);
}

function fixBackground() {
	if(0 == img_width || 0 == img_height) {
		return;	
	}

	var window_width = getClientWidth();
	var window_height = getClientHeight();
	var new_width = img_width*window_height/img_height;
	var new_height = img_height*window_width/img_width;
	if(new_width > window_width) {
		setWidth('home-background-img',new_width);
		setHeight('home-background-img',window_height);
		setLeft('background',0-(new_width-window_width)/2);
		setTop('background',0);
	}
	else {
		setWidth('home-background-img',window_width);
		setHeight('home-background-img',new_height);
		setLeft('background',0);
		setTop('background',0-(new_height-window_height)/2);
	}
	Element.show('home-background-img')
//	if (browser.isKonqueror || browser.isSafari || browser.isOpera) {
//		setWidth('container', window_width);
//	}
};

function getScrollX()
{
  var x = window.pageXOffset ||
          document.body.scrollLeft ||
          document.documentElement.scrollLeft;

  return x ? x : 0;
}

function getScrollY()
{
  var x = window.pageYOffset ||
          document.body.scrollTop ||
          document.documentElement.scrollTop;

  return x ? x : 0;
}

function getClientWidth()
{
  var x = window.innerWidth ||
          document.documentElement.clientWidth ||
          document.body.clientWidth;

  return x ? x : 0;
};

function getClientHeight()
{
  var x = window.innerHeight ||
          document.documentElement.clientHeight ||
          document.body.clientHeight;


  return x ? x : 0;
};

function setTop(obj, x)
{
  obj = $(obj);
  obj.style ? obj.style.top = x + "px" : obj.top = x;
};

function setLeft(obj, x)
{
  obj = $(obj);
  obj.style ? obj.style.left = x + "px" : obj.left = x;
};

function setWidth(obj, x)
{
  obj = $(obj);
  obj.style ? obj.style.width = x + "px" : obj.width = x;
};

function setHeight(obj, x)
{
  obj = $(obj);
  obj.style ? obj.style.height = x + "px" : obj.height = x;
};
