/**
  *javascript common library file
  */
  
 



//////////////////////////////////////////////////////////////////////////////////////////////////////////
//Array - methods extensions
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//list
//array.clear()
//array.contains(p_element)
//array.getIndex(p_element)
//array.copy()
//array.clearDuplicities()



/**
 *removes all elements from an array
 *@return an array of removed elements
 *@type Array
 *@param -
 *@addon
 */
Array.prototype.clear = function()
{ 
  return this.splice(0);  
}

/**
 *returns true if array contains an element or false if not. 
 *@return boolean
 *@type boolean
 *@param {-} p_element an element which should be find in the Array
 *@addon 
 */                           
Array.prototype.contains = function(p_element)
{
 for(var i=0; i < this.length; i++)
  {
    if(this[i].toString() == (p_element.toString())) return true;
  }
 return false;
} 


/**
 *returns the index of an element in the Array
 *@return Integer
 *@type Integer
 *@param {-} p_element an element of an array
 *@addon 
 */
Array.prototype.getIndex = function(p_element)
{
  for(var i=0; i < this.length; i++)
  {
    if(p_element.toString() == (this[i].toString())) return new Number(i);
  }
}    


/**
 *create copy of an array
 *@return new Array as copy
 *@type Array
 *@param -
 *@addon 
 */
Array.prototype.copy = function()
{
  return this.slice(0);  
}


/**
 *removes all doubled occurances of elements from an array. For example [1,1,2,3,4,4,5].clearDuplicities => [1,2,3,4,5]
 *@return length of the new array
 *@type Integer
 *@param -
 *@addon 
 */
Array.prototype.clearDuplicities = function()
{
  var a_temp = this.copy();
  this.clear();
  for(var i=0; i < a_temp.length; i++){
    if(!this.contains(a_temp[i])) this.push(a_temp[i])
  }    
  return this.length;      
}

/**
 *removes p_element from an array
 *@return index which had p_element in an array
 *@type Integer
 *@param {-} p_element an element of an array
 */
Array.prototype.remove = function(p_element){
  var index = this.getIndex(p_element);
  this.splice(index,1);    
  return index;
}

/**
 *adds an element (p_element) at the and an array. The difference between method <b>push</b> and <b>add</b> is that method array.add(p_element) checks first if the array doesnt contains the element already and adds the element to an array only in case it will have unique occurance in there.
 *@return new length of the array
 *@type Integer
 *@param {-} p_element an element which is going to be added to an array
 */
Array.prototype.add = function(p_element){
  if(!this.contains(p_element)){
    this.push(p_element);
  }  
return this.length;
}







///////////////////////////////////////////////////////////////////////////////////////////
//functions
///////////////////////////////////////////////////////////////////////////////////////////
  
/**
  *@return  	widh or height of window
  *@type		{int}
  *@param		{string}  type "width" or "height"
  */
function GetWindowSize(type){
var viewportwidth;
var viewportheight;
 
 // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
 
 if (typeof window.innerWidth != 'undefined')
 {
      viewportwidth  = window.innerWidth;
      viewportheight = window.innerHeight;
 }
 
// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)

 else if (typeof document.documentElement != 'undefined'
     && typeof document.documentElement.clientWidth != 'undefined'
	 && document.documentElement.clientWidth != 0)
 {
       viewportwidth = document.documentElement.clientWidth,
       viewportheight = document.documentElement.clientHeight
 }
 
 // older versions of IE
 
 else
 {
       viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
       viewportheight = document.getElementsByTagName('body')[0].clientHeight;
 }
 
 if(type == "width") 	return  viewportwidth;
 if(type == "height") 	return  viewportheight;
}


function Hide(element){
element.style.visibility = "hidden";
}
function Show(element){
element.style.visibility = "visible";
}


function hide(id){
document.getElementById(id).style.visibility = "hidden";
}
function show(id){
document.getElementById(id).style.visibility = "visible";
}


/*
function SetStylePositionFixed(id){
  var browser   	= navigator.appName;
  var version     	= new Number(navigator.appVersion.charAt(0));  
       	
}
*/

function ImageIsLoaded(img) {
// During the onload event, IE correctly identifies
//any images that
// weren’t downloaded as not complete. Others should too. Gecko-based
// browsers act like NS4 in that they report this incorrectly.
if (!img.complete) {
return false;
}

// However, they do have two very useful properties:
//naturalWidth and
// naturalHeight. These give the true size of the image. If it failed
// to load, either of these should be zero.
if (typeof img.naturalWidth!="undefined" && img.naturalWidth== 0) {
return false;
}
// No other way of checking: assume it’s ok.
return true;
}

/**
  * sets position fixed for an element
  * example: document.getElementById("myElement").style.position = documetnElementStylePositionFixed(document.getElementById('myElement'))
  */
function documentElementStylePositionFixed(element){
var patt1 = /MSIE.{1,4}/gi;
var patt2 = /\d/;
var navigatorVersion 	= navigator.appVersion;
    navigatorVersion	= navigatorVersion.toString().match(patt1);
	if(navigatorVersion != null){
		navigatorVersion	= navigatorVersion.toString().match(patt2);
	}

//if browser is kind of IE older then IE7
if(navigatorVersion != null && navigatorVersion < 7){
 return "absolute";
} 
//else
return "fixed";
}

/**
  *description: returns true if current browser is kind of IE with version lower  then  nVersion
  */
function DetectIE(nVersion){
	var patt1 = /MSIE.{1,4}/gi;
	var patt2 = /\d/;	
	var navigatorVersion 	= navigator.appVersion;
		navigatorVersion	= navigatorVersion.toString().match(patt1);
		if(navigatorVersion != null){
			navigatorVersion = navigatorVersion.toString().match(patt2);
		}		
	if(navigatorVersion != null && navigatorVersion < nVersion){
		return true;
	}
	return false;				
}

function MSIEVersion(){
   var arVersion = navigator.appVersion.split("MSIE");
   var version = parseFloat(arVersion[1]);
   return version;
}


function SetStyleTransparency(element,level){ //element = document.getElementById(); level = value <0, 100>
	element.style.opacity		 = level/10; 						//variable opacity/10
	element.style.filter		 = 'alpha(opacity='+level*10+')'; 	//variable opacity*10
	return
}


function pause(millis){
var date = new Date();
var curDate = null;

	while( curDate-date < millis ){
		curDate = new Date();
	}
}	

function makeHttpObject() {
  try {return new XMLHttpRequest();}
  catch (error) {}
  try {return new ActiveXObject("Msxml2.XMLHTTP");}
  catch (error) {}
  try {return new ActiveXObject("Microsoft.XMLHTTP");}
  catch (error) {}

  throw new Error("Could not create HTTP request object.");
}

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1; 
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
return "";
}






