﻿// Yeni's Toolbox

// sometimes we need to handle quirks on IE below 7
var isQuirkIE = navigator.appVersion.indexOf('MSIE');
if(isQuirkIE) {
	isQuirkIE = parseInt(navigator.appVersion.charAt(isQuirkIE + 5));
	if(isQuirkIE < 7) {
		isQuirkIE = 1;
	} else {
		isQuirkIE = 0;
	}
}

// sometimes we need to handle some bug on Firefox 2
var isFirefox2 = navigator.userAgent.match( /Firefox\/2(\.\d+\.\d+\.\d+)/) ? 1 : 0;

// Add an load event
var tuc_StimulatedOnloadFunction;

function addLoadEvent(func) {
  var oldonload = tuc_StimulatedOnloadFunction;
  if (typeof tuc_StimulatedOnloadFunction != 'function') {
    tuc_StimulatedOnloadFunction = func;
  } else {
    tuc_StimulatedOnloadFunction = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

// Add an load event
function addLoadEvent_Original(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}


function addResizeEvent(func) {
  var oldonresize = window.onresize;
  if (typeof window.onresize != 'function') {
    window.onresize = func;
  } else {
    window.onresize = function() {
      if (oldonresize) {
        oldonresize();
      }
      func();
    }
  }
}

function isNumber(str) {
	return /^([0-9]+)$/.test(str);
}

// find the absolute position
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	return [curleft,curtop];
}

// get the mouse's position
var tuc_gMousePos = {x:0, y:0};
function mouseCoords(ev) {
	ev = ev || window.event;
	if(ev.pageX || ev.pageY){
		tuc_gMousePos = {x:ev.pageX, y:ev.pageY};
	} else {
		tuc_gMousePos = {
			x:ev.clientX + document.documentElement.scrollLeft - document.body.clientLeft,
			y:ev.clientY + document.documentElement.scrollTop  - document.body.clientTop
		};
	}
	return tuc_gMousePos;
}

function findAncestorWithClass(from, classname) {
	var ancestors = $(from).ancestors();
	for(var i = 0; i < ancestors.length; i++) {
		if(ancestors[i].hasClassName(classname)) {
			return ancestors[i];
		}
	}
	return null;
}

