/*Begin: Before opening popup*/
function prepare_for_popup(){
	tb_init('a.thickbox, area.thickbox, input.thickbox, tr.thickbox');//pass where to apply thickbox
	imgLoader = new Image();// preload image
	imgLoader.src = tb_pathToImage;
}
/*End: Before opening popup*/

/*Begin: Auto height - When opening popup*/
function doIframe(){
	o = document.getElementsByTagName('iframe');
	for(i=0;i<o.length;i++){
		//if (/\bautoHeight\b/.test(o[i].className)){
		if (true){
			setHeight(o[i]);
			addEvent(o[i],'load', doIframe);
		}
	}
}

function setHeight(e){
	var height = 300;
	var current_url;
	if(e.contentDocument){
		height = e.contentDocument.body.offsetHeight;
		current_url = e.contentDocument.location.href;
	} else {
		height = e.contentWindow.document.body.scrollHeight;
		current_url = e.contentWindow.document.location.href;
	}
	
	if(height > 0 && height < 100){
		height += 35;
	}
	$('#'+e.id).css('height',height);
	
	// Auto center vertically popup
	if(height > 0){
		TB_HEIGHT = height+40;
		//TODO: Width
		tb_position();
	}
	
	//Truong hop form dang ky (users/register) => Fix loi khi chuyen tu trang Dang nhap sang trang Dang ky
	if(e.src.indexOf('users\/login') != -1 && current_url.indexOf('users\/register') != -1 && height > 0){
		width = user_register_width+29;
		$('#TB_window').css('width',width);
		$('#'+e.id).css('width',width);
	}
	
}

function addEvent(obj, evType, fn){
	if(obj.addEventListener){
		obj.addEventListener(evType, fn,false);
		return true;
	} else if (obj.attachEvent){
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	} else {
		return false;
	}
}

if (document.getElementById && document.createTextNode){
	//addEvent(window,'load', doIframe);	
}
/*End: Auto height - When opening popup*/
