/**
* firebug - prevent error when firebug is not available
*/	
if (console === undefined) {
	if (typeof console == "undefined" || typeof console.log == "undefined")
		var console = {log: function(){}};
} 
if (log === undefined) {
	if (typeof console == "undefined" || typeof console.log == "undefined") 
		var log = function(){};
	else 
		var log = function($str){
			console.log($str);
		}
}


/**
* Array length
*/	
function array_length(arr) {
	var length = 0;
	for(val in arr) {
		if(!(val in Array.prototype))
			length++;
	}
	return length;
}

/**
* Array clone (to use 'passing by value')
*/	
function array_clone(arr){
    var a = new Array();
    for (var property in arr) {
        a[property] = typeof(arr[property]) == 'object' ? array_clone(arr[property]) : arr[property]
    }
    return a;
}

/**
* Array - is Array?
*/	
function isArray(obj) {
   if (obj.constructor.toString().indexOf("Array") == -1)
      return false;
   else
      return true;
}

/**
* Array - element in Array?
*/	
function inArray($arr, $value, $caseSensitive){
	if($caseSensitive === undefined) $caseSensitive = false;	
	
	for($i=0; $i<$arr.length; $i++) {
		if(!$caseSensitive){ //performs match even the string is case sensitive
			if ($arr[$i].toLowerCase() == $value.toLowerCase())
				return true;
		}else{
			if ($arr[$i] == $value)
				return true;
		}
	}
	return false;
};

/**
* Get real position of an element in the DOM
*/	
function findPos(obj, start) {
	if(start === undefined) start = 0;
	
	var i = 1;
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			if(i > start){
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;				
			}
			i++;
		}
		while (obj = obj.offsetParent);
	}
	return [curleft, curtop];
}



/**
* Get  filename from an URL 
*/	
function getFilename(url)
{
   if (url)
   {
      var m = url.toString().match(/.*\/(.+?)\./);
      if (m && m.length > 1)
      {
         return m[1];
      }
   }
   return "";
}



/**
* Z-index - bug in IE6
*/	
var iaz_preserved_elements = [];
var iaz_preserved_zindexes = [];

$ie_apply_zindex = function(element, zindex, context) {
   // default values
   if (undefined == zindex) { zindex = 1; }
   if (undefined == context) { context = $(document.body); }

   // undo past ie_apply_zindex()
   for (i = iaz_preserved_elements.length-1; i >= 0; i--) {
      iaz_preserved_elements[i].css({'z-index': iaz_preserved_zindexes[i]});
   }
   iaz_preserved_elements = [];
   iaz_preserved_zindexes = [];

   // find relative-positioned ancestors of element within context
   element.parents().each(function(){
         if ($(this).css('position') == 'relative') {
		 	$elZindex = $(this).css('z-index');			
			$elZindex = $elZindex == '' || $elZindex == 'auto' ? zindex : $elZindex;
			
            // preserve ancestor's current z-index
            iaz_preserved_elements.push($(this));
            iaz_preserved_zindexes.push($elZindex);

            // apply z-index to ancestor
            $(this).css({'z-index': $elZindex});
         }
		 
         if ($(this).get(0) == $(context).get(0)) { return false; }
      }
   );
}
