/*
----------------------------------------------------------------------------------
common functions
----------------------------------------------------------------------------------
*/

/* addLoadEvent */
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

/* bookmark & homepage */
addLoadEvent(initBookmarkLinks);
function bookmarkThisSite(anchor, title, url) {
    if (window.sidebar) { // Mozilla Firefox Bookmark
        window.sidebar.addPanel(title, url,"");
        return false;
    } else if( window.external ) { // IE Favorite
        window.external.AddFavorite( url, title); 
        return false;
    } else if(window.opera && window.print) { // Opera Hotlist
        anchor.rel = 'sidebar';
		return true; 
    }
}
function homepageThisSite(anchor, url) {
	if(navigator.userAgent.indexOf('MSIE') > -1) {
        anchor.style.behavior='url(#default#homepage)'; 
        anchor.setHomePage(url); 
	}
	else if (window.opera && window.print) {
		alert('Twoja przeglądarka nie pozwala na zmianę strony domowej z poziomu witryny WWW.');
	}
	else {
		alert('Twoja przeglądarka nie pozwala na zmianę strony domowej z poziomu witryny WWW.');
	}
    return false;
}
function initBookmarkLinks() {
    var bookmarkLink = document.getElementById('addFavorite');
    var homepageLink = document.getElementById('addStartPage');
    if (bookmarkLink) {
        bookmarkLink.onclick = function() {
            return bookmarkThisSite(this,'Rynek Zdrowia - Serwis Branży Medycznej','http://www.rynekzdrowia.pl/');
        }
    }
    if (homepageLink) {
        homepageLink.onclick = function() {
            return homepageThisSite(this,'http://www.rynekzdrowia.pl/');
        }
    }
}

/* by Scott Andrew (http://www.scottandrew.com/) */
function createCookie(name, value, days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1, c.length);
    if (c.indexOf(nameEQ) == 0) 
    	return c.substring(nameEQ.length, c.length);
  }
  return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

/* by Dustin Diaz */
function getElementsByClass(searchClass,node,tag) 
{
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

/* by ethanak */
function remClass(el,x)
{
	if (!el.className) return;
	if (!arguments.callee.r) arguments.callee.r=new Array();
	var r;
	if (!(r=arguments.callee.r[x])) r=arguments.callee.r[x]=
		new RegExp('\\b'+x+'\\b\\s*|\\s*'+x+'\\b','g');
	var c=el.className.replace(r,'');
	if (c != el.className) el.className=c;
}

function addClass(el,x)
{
	if (!el.className) {
		el.className=x;
		return;
	}
	if (!arguments.callee.r) arguments.callee.r=new Array();
	var r;
	if (!(r=arguments.callee.r[x])) r=arguments.callee.r[x]=
		new RegExp('\\b'+x+'\\b');
	if (!r.test(el.className)) el.className+=' '+x;
}

function mouseBlur() 
{
 var anchors = document.getElementsByTagName('a');
 for (var i = 0; i < anchors.length; i++) {
 	anchors[i].onmousedown = function() {
 		this.className += ' mouse-active';
 	}
 }
}

/* moveElement by Jeremy Keith */ 
function moveElement(elem, final_x, final_y, interval) {
  if (!document.getElementById) return false;
  if (elem.movement) {
    clearTimeout(elem.movement);
  }
  if (!elem.style.left) {
    elem.style.left = "0px";
  }
  if (!elem.style.top) {
    elem.style.top = "0px";
  }
  var xpos = parseInt(elem.style.left);
  var ypos = parseInt(elem.style.top);
  if (xpos == final_x && ypos == final_y) {
    return true;
  }
  if (xpos < final_x) {
    var dist = Math.ceil((final_x - xpos)/4);
    xpos = xpos + dist;
  }
  if (xpos > final_x) {
    var dist = Math.ceil((xpos - final_x)/4);
    xpos = xpos - dist;
  }
  if (ypos < final_y) {
    var dist = Math.ceil((final_y - ypos)/4);
    ypos = ypos + dist;
  }
  if (ypos > final_y) {
    var dist = Math.ceil((ypos - final_y)/4);
    ypos = ypos - dist;
  }
  elem.style.left = xpos + "px";
  elem.style.top = ypos + "px";
  //var repeat = "moveElement(elem,"+final_x+","+final_y+","+interval+")";
  //elem.movement = setTimeout(repeat,interval);
  
  elem.movement = setInterval((
		function(a, b, c, d, e) { 
			return function() { 
				moveElement(a, b, c, d, e) 
			}
		}
	)(elem, final_x, final_y, interval), interval);
  
  return true;
}

