// Creates a advice DIV
// - name: nombre del div que se creará
// - element: before of...
// 

function Advice(name, element, pText, pURL) {
	this._name = name;
	this._element = element;
	this._div = null;
	//podem canviar-lo
	var cn = pText.slice(0,15);
  	var re = new RegExp(" ", "g");
	cn = cn.replace(re,'_');
	this._cookie = 'pr_' + cn;
	//alert(this._cookie);
	this._text = pText;
	this._url = pURL;
	//newdiv.setAttribute('display', 'block'); 
  	//window.alert(getCookie(this._cookie));
}
  
Advice.prototype.hide = function () {
  this._hide();
}
  
Advice.prototype.hide2click = function () { alert('google');
															return this.hide();
														}

Advice.prototype._hide = function () {  
    this._div.style.display = 'none';
	this.setCookieValue(99);
}
  
Advice.prototype.setCookieValue = function (value) {
 var oneMinute = 60 * 1000  // milliseconds in a minute
 var oneHour = oneMinute * 60
 var oneDay = oneHour * 24
 var oneWeek = oneDay * 7
 var today = new Date();
 var dateInMS = today.getTime() + oneDay * 45;
 var targetDate = new Date(dateInMS);
	setCookie(this._cookie, value, targetDate)	
}

Advice.prototype.show = function() {
	var newdiv = document.createElement('div');
	newdiv.setAttribute('id', this._name);

	var img = document.createElement('img');
	img.setAttribute('src', '/images/ic_close.gif');
	img.setAttribute('alt','Cerrar y no volver a mostrar');


	var urlimg = document.createElement('a');
	urlimg.setAttribute('href', 'javascript:;');  
	img.setAttribute('title','Cerrar y no volver a mostrar');

	urlimg.appendChild(img);

	urlimg.p = this;
	urlimg.onclick = function () { 
		//this será el propi objecte en el moment de la funció;
		this.p.hide();
	};
	

	var url = document.createElement('a');
  	url.setAttribute('href', this._url);
  	url.setAttribute('target', '_blank');

	url.p = this;
	url.onclick = function(){ this.p.setCookieValue(99);}

	 var newText = document.createTextNode(this._text);
  	url.appendChild(newText);

	newdiv.appendChild(urlimg);
	newdiv.appendChild(url);
	this._div = newdiv;
	var divs = this._element.getElementsByTagName("div");
	this._element.insertBefore(newdiv,divs[0]);

	this.incCookie();
}

Advice.prototype.getCookieValue = function() {
	if (getCookie(this._cookie) != null)
		return parseInt(getCookie(this._cookie))
	else
		return 0;
}

Advice.prototype.mustShow = function() {
	return (this.getCookieValue() < 10);
}

Advice.prototype.incCookie = function(){
	this.setCookieValue(this.getCookieValue() + 1);
}

