//Primeira fun玢o executada pelo site
function _load() {
	//Calcula tamanhos e posicoes
	_calcDIM();
	
	//Seta variaveis
	WAIT = document.getElementById("wait");
	WAIT.style.display = "none";
	SEL = document.getElementById("sel");
	SEL.style.display = "none";
	SEL.style.backgroundImage = "url('./img/sel" + ic + ".png')";
	txtUrl = document.getElementById("txtUrl");
	txtBusca = document.getElementById("sbi");
	dFav = document.getElementById("dFav");

	//Ve a ultima cor selecionada
	if (Get_Cookie("c") == null) {
		ic = 0;
	} else {
		ic = parseInt(Get_Cookie("c"));
	}	
	SEL.style.backgroundImage = "url('./img/sel" + ic + ".png')";
	_criaSelCores();
	_criaSelModo();
	_criaPop();

	
	//Aplica regras de CSS no JS
	Behaviour.register(myrules);
	Behaviour.apply();
	
	for (var i = 0; i < 12; i++) {
		if ((Get_Cookie("u" + i) != null)
		&&(Get_Cookie("u" + i) != "")) {
			imgBack[i].src = thumbSite + escape(Get_Cookie("u" + i));				
			//opera.postError(imgBack[i].src);
		}	
	}
	window.onresize = _resize;
	txtBusca.focus();
}

//Cria as opcoes dos sites mais populares
function _criaPop() {
	dFav.innerHTML = "";
	for (var i = 0; i < favUrl.length ; i++) {
		d = document.createElement("div");
		d.className = "item";
		d._url = favUrl[i];
		d.innerHTML = favSites[i];
		
		s = document.createElement("span");
		s.className = "link";
		s.innerHTML = favUrl[i];
		d.appendChild(s);		
		
		dFav.appendChild(d);
	}
}

//funcao disparada quando o form redimenciona
function _resize() {
	_calcDIM();
	Behaviour.apply();
	for (var i = 0; i < 12; i++) {
		if ((Get_Cookie("u" + i) != null)
		&&(Get_Cookie("u" + i) != "")) {
			imgBack[i].src = thumbSite + escape(Get_Cookie("u" + i));				
			//opera.postError(imgBack[i].src);
		}	 else {
			_criaOpt(imgBack[i]._pai);
		}
	}
	btnModo.style.left	=document.body.clientWidth - (5) * 25;
	for (var i = 0; i < 5; i++) {
		btnCores[i].style.left =  document.body.clientWidth - (i + 1) * 25;
	}
	txtBusca.focus();
}

	
//Cria botao para escolher se abre na mesma janela, ou em uma nova;
function _criaSelModo() {
	bot = document.createElement("div");
	btnModo = bot;
	bot.type = "button";
	bot.style.position = "absolute";
	bot.style.top = "30px";
	bot.style.width = "120px";
	bot.style.height = "20px";
	bot.style.left	=document.body.clientWidth - (6) * 25;
	bot.align = "Center";
	bot.style.borderWidth = "1px 1px 1px 1px";
	bot.style.borderStyle = "solid";
	bot.style.backgroundColor = "#ADCD3C";
	bot.style.borderColor = "#F2FDDB";
	bot.style.lineHeight = "17px";
	bot.style.cursor = "hand";
	if (Get_Cookie("l") == "T") {
		bot.innerHTML = "当前窗口打开";		
	} else {
		bot.innerHTML = "新的窗口打开";		
		Set_Cookie("l", "N");
	}	
	bot.onmouseout = function() {
		bot.style.backgroundColor = "#96C2F1";
		bot.style.borderColor = "#F0FBEB";
	}
	bot.onmouseover = function() {
		bot.style.backgroundColor = "#FAFCFD";
		bot.style.borderColor = "#92B0DD";
	}
	bot.onclick = function() {
		if (this.innerHTML == "当前窗口打开") {
			this.innerHTML = "新的窗口打开";
			Set_Cookie("l", "N");
		} else {
			this.innerHTML = "当前窗口打开";
			Set_Cookie("l", "T");
		}
	}
	document.body.appendChild(bot);
}
//Cria botoes para selecionar
function _criaSelCores() {
	for (var i = 0; i < 7; i++) {
		d = document.createElement("div");
		btnCores[i] = d;
		d.style.height = 20;
		d.style.width = 20;
		d.style.borderWidth = "1px 1px 1px 1px";
		d.style.borderStyle = "solid";
		d.style.backgroundColor = CorPreen[i];
		d.style.borderColor = CorLin[i];
		d.style.position = "absolute";
		d.style.top = "5px";
		d.style.left =  document.body.clientWidth - (i + 1) * 25;
		d.style.cursor = "hand";
		d._idx = i;
		d.onclick = function() {
			ic = this._idx;
			Set_Cookie("c", ic);
			SEL.style.backgroundImage = "url('./img/sel" + ic + ".png')";
			Behaviour.apply();
		}
		d.onmouseover = function() {
			this.style.backgroundColor = CorPreenO[this._idx];
			this.style.borderColor = CorLinO[this._idx];	
		}
		d.onmouseout = function() {
			this.style.backgroundColor = CorPreen[this._idx];
			this.style.borderColor = CorLin[this._idx];	
		}		
		document.body.appendChild(d);
	}
}

//Calcula tamanhos e posicioes
function _calcDIM() {
	h1 =	((document.body.clientHeight - MARGIN - RODAPE)/ 13) * 4;
	w1 =		(h1 / 3) * 4;

	w2 =	((document.body.clientWidth - MARLEFT * 2)/17) * 4 ;
	h2 =	(w2 / 4) * 3;
	
	if (w2 < w1) {
		height = h2;
		width = w2;	
	} else {
		height = h1;
		width = w1;	
	}
	H_ESP = (document.body.clientWidth - MARLEFT - width*4)/4;
	V_ESP = (document.body.clientHeight - MARGIN - RODAPE- height*3)/2;
	
	PAD =	height / 8;
	CENTER =	(document.body.clientWidth - (width + H_ESP)*4 + H_ESP)/2;
}

//Mostra janela para escolher link
function _escLink(d) {
	WAIT.style.display = "block";	
	SEL.style.display = "block";
	txtUrl.value = d._site;
	txtUrl.focus();
	tmpD = d;
}

//"Limpa" uma janela
function _limpaLink(d) {
	Set_Cookie("u" + d._nro, "");
	d._site = "";
	d._title.innerHTML = "";		
	_criaOpt(d);	
}

//Cria a msg de janela vazia dentro da janela
function _criaOpt(d) {
	if (d._site == "") {
		d._filho.innerHTML = 
			"<div align='center' style='position:absolute;width:" + (width-PAD*2) + "px'><h1 style='position:relative;top:" + (height - 60 - 40)/2				
			+ "px'>" + (d._nro + 1) + "</h1>" 
			+ "<span style='position:relative;top:" + (height - 60 - 40)/2				
			+ "px'>单击添加网址</span></div>"
		;
	} else {
	}
}

//Click de cancelar na selecao de url
function _selCancClick() {
	WAIT.style.display = "none";	
	SEL.style.display = "none";	
	txtBusca.focus();
}

//Click de ok na selecao de url
function _selOKClick() {	
	if (txtUrl.value == "") {
		_selCancClick();
		return;
	}

	tmpD._filho.innerHTML = "<img style='position:relative;top:" + 
													((height - PAD*2 - 31)/2)+ "px;left:" + 
													((width - PAD*2 - 31)/2) + "px' src='./img/load.gif' />";
	tmpD._site = txtUrl.value;
	if (txtUrl.value.length > Math.round((width - PAD) / 6)) {
		txtUrl.value = txtUrl.value.substr(0,Math.round((width - PAD) / 6)-3) + "...";
	}
	tmpD._title.innerHTML = txtUrl.value;	
	WAIT.style.display = "none";	
	SEL.style.display = "none";		
	Set_Cookie("u" + tmpD._nro,tmpD._site);
	tmpD._back.src = thumbSite +  escape(txtUrl.value);
	//opera.postError(tmpD._back.src);
	txtBusca.focus();
	
	//tmpD._back.src = "http://images.websnapr.com/?url=" + txtUrl.value;
	

	/*var xml = _xml();
	xmlhttp.open("GET", "./serv/img.aspx?url=" + txtUrl.value,true);
	//xmlhttp.open("GET", "http://images.websnapr.com/?url=" + txtUrl.value,true);	
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4){			
			tmpD._filho.innerHTML = "";
			i = document.createElement("img");
			//i.src = xmlhttp.responseText;
			i.src = "http://images.websnapr.com/?url=" + txtUrl.value;
			i.width = (width - PAD*2) ;
			i.height = (height - PAD*2);
			tmpD._filho.appendChild(i);
		}
	}
	xmlhttp.send(null);*/
}

//Evento de keydown da caixa de busca
function _txtBusca_KeyDown(event) {
	if (event.keyCode == "13") {
		if (Get_Cookie("l") == "N") {
			window.open("http://www.google.cn/search?q=" + txtBusca.value, "_blank");
		} else {
			location.href = "http://www.google.cn/search?q=" + txtBusca.value;
		}		
	}
}

//Evento de focus da caixa de busca
function _txtBusca_Focus() {
	txtBusca.select();
}