function wrap(classname, elementwidth) {
	bodyw = docSize("width");
	basew = bodyw > 1000 ? 1000 : bodyw < 760 ? 760 : bodyw;
	// distribuimos los thumbnails de canales uniformemente
	prinw = basew - 280;
	rowthumbs = Math.floor(prinw / elementwidth);
	margi = (prinw - (rowthumbs * elementwidth)) / (rowthumbs + 1);
	canalclass = getStyleClass(classname);
	canalclass.style.marginLeft = margi+"px";
}

// chat.php
function resizeChat() {
	bodyw = docSize("width");
	basew = bodyw > 1000 ? 1000 : bodyw < 760 ? 760 : bodyw;
	basew -= 30;
	divchat = document.getElementById("chat");
	posx = Math.floor((bodyw-basew)/2) + 6;
	if (posx < 21) posx = 21;
	divchat.style.left = posx+"px";
	divchat.style.top = "154px";
	divchat.style.width = basew+"px";
	divchat.style.height = "430px";
}
function thumbRec(step) {
	if (step == "0") {
		document.getElementById("tuvideo").style.display = "block";
	}
	recthumbs = document.getElementById("recthumbs");
	recthumbs.innerHTML += '<a href="/galeria"><img src="http://fotos.sexywebcam.com/recthumb.php?id='+codigo+'&step='+step+'" width="120" height="98" alt=""</a>';
}
function fullscreenSwitch(full) {
	divchat = document.getElementById("chat");
	divpos = document.getElementById("chatpos");
	if (full) {
		// Pantalla completa
		document.getElementById("gradient").style.display = "none";
		divchat = document.getElementById("chat");
		divchat.style.left = "0";
		divchat.style.top = "0";
		divchat.style.width = "100%";
		divchat.style.height = "100%";
		window.onresize = "";
	} else {
		// Cambiamos a tamaño normal
		document.getElementById("gradient").style.display = "block";
		resizeChat();
		window.onresize = resizeChat;
	}
}
// lang
function setLang(idioma) {
	setCookie("idioma", idioma, 365);
	suba = location.hostname.split(".");
	subd = idioma == "es" ? "www" : idioma;
	location.href = "http://" + subd + "." + suba[suba.length-2] + "." + suba[suba.length-1] + location.pathname;
}
function clickLang(idioma, span) {
	cidioma = readCookie('idioma');
	if (cidioma == idioma) {
		cspan = document.getElementById("flags"+span);
		cflag = document.getElementById("flagswitch"+span);
		if (cflag.src.indexOf("left.gif") > 0) {
			cspan.style.width = "auto";
			cflag.src = "/img/flag_right.gif";
		} else {
			cspan.style.width = "27px";
			cflag.src = "/img/flag_left.gif";
		}
	} else {
		setLang(idioma);
	}
}

// comunes
function docSize(value) {
	if (document.documentElement && document.documentElement.clientHeight) {
		width = document.documentElement.clientWidth;
		height = document.documentElement.clientHeight;
		scrolly = document.documentElement.scrollTop;
		scrollx = document.documentElement.scrollLeft;
	} else if (self.innerHeight) {
		width = self.innerWidth;
		height = self.innerHeight;
		scrolly = this.pageYOffset;
		scrollx = this.pageXOffset;
	} else if (document.body) {
		width = document.body.clientWidth;
		height = document.body.clientHeight;
		scrolly = document.body.scrollTop;
		scrollx = document.body.scrollLeft;
	}
	return eval(value);
}

function getStyleClass(className) {
	for (var s = 0; s < document.styleSheets.length; s++) {
		if(document.styleSheets[s].rules){
			for (var r = 0; r < document.styleSheets[s].rules.length; r++) {
				if (document.styleSheets[s].rules[r].selectorText == '.' + className) {
					return document.styleSheets[s].rules[r];
				}
			}
		} else if(document.styleSheets[s].cssRules) {
			for (var r = 0; r < document.styleSheets[s].cssRules.length; r++) {
				if (document.styleSheets[s].cssRules[r].selectorText == '.' + className) {
					return document.styleSheets[s].cssRules[r];
				}
			}
		}
	}	
	return null;
}

function findPos(obj) {
	// http://www.quirksmode.org/js/findpos.html
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	return [curleft,curtop];
}

function createRequest() {
	var xmlHttp;
	if(window.ActiveXObject){
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	} else if(window.XMLHttpRequest){
		xmlHttp = new XMLHttpRequest();
	}
	return xmlHttp;
}

function readCookie(cookieName) {
	var theCookie = "" + document.cookie;
	var ind = theCookie.indexOf(cookieName);
	if (ind == -1 || cookieName == "") return(""); 
	var ind1 = theCookie.indexOf(';', ind);
	if (ind1==-1) ind1 = theCookie.length;
	return decodeURIComponent(theCookie.substring(ind + cookieName.length + 1, ind1));
}

function setCookie(cookieName, cookieValue, nDays) {
	var today = new Date();
	var expire = new Date();
	if (nDays == null || nDays == 0) nDays = 1;
	expire.setTime(today.getTime() + 3600000*24*nDays);
 	document.cookie = cookieName + "=" + encodeURIComponent(cookieValue) + ";expires=" + expire.toGMTString() + ";path=/;domain=.sexywebcam.com";
}

function deleteCookie(cookieName) {
	document.cookie = cookieName + "=null;expires=Thu, 01-Jan-1970 00:00:01 GMT;path=/;domain=.sexywebcam.com";
}

function openBlank(e) {
	if (!e) var e = window.event;
	if (e.target) {
		targ = e.target;
	} else {
		targ = e.srcElement;
	}
	while (targ.nodeName != "A") targ = targ.parentNode;	
	window.open(targ.href);
}