window.onload = ag_init;
window.onresize = art_gallery_reposition;

function ag_init() {
	var div_ag_pop = document.createElement("div");
	div_ag_pop.setAttribute("id","ag_pop");
	document.getElementsByTagName('body')[0].appendChild(div_ag_pop);
}

var ag_width = 0;
function art_gallery(user,md5,ext,w,h,title,more,rate,comments) {
	var inner_size = get_inner_size();
	var x_pos = Math.round((inner_size[0]/2) - (w/2));
	var scrollpos = get_scroll_pos();
	var e = document.getElementById('ag_pop');
	e.style.left = x_pos+'px';
	e.style.top = scrollpos[1]+'px';
	e.style.width = w+'px';
	e.innerHTML = '<a onclick="return art_gallery_close()" href="/art-gallery" onfocus="this.blur()"><img src="/img/art-gallery/'+md5+'.'+ext+'" width="'+w+'" height="'+h+'" /></a>';
	if (user != 0) {
		e.innerHTML += '<div style="float:left"><a href="/profile/'+user+'">'+user+'</a></div>';
	}
	if (title != '') {
		e.innerHTML += '<div style="text-align:center"><small><strong>'+title+'</strong></small></div>';
	}
	if (more == 1) {
		e.innerHTML += '<div style="float:left;margin-right:2em"><a href="/art-gallery/'+md5+'" onfocus="this.blur()">Info</a></div>';
	}
	if (rate != 0) {
		e.innerHTML += '<div id="ratings" style="float:left"></div>';
		ajx('/public/ajax.php?c1=specials&c2=rate&c3='+rate+'&c5=ag','ratings');
	}
	e.innerHTML += '<div style="text-align:right"><small><a onclick="return art_gallery_close()" href="/art-gallery" class="mubutt">X</a></small></div>';
	e.innerHTML += '<div id="comments" style="max-width:35em;margin:0 auto"></div>';
	if (rate != 0) {
		setTimeout("ajx('/public/ajax.php?c1=art-gallery&c2="+md5+"','comments')",1000);
	}
	else {
		ajx('/public/ajax.php?c1=art-gallery&c2='+md5,'comments');
	}
	e.style.display = 'block';
	changeOpac(5,'main');
	z_index_up(e);
	var ag_width = w;
	return false;
}
function art_gallery_close() {
	var e = document.getElementById('ag_pop');
	e.style.display = 'none';
	changeOpac(100,'main');
	return false;
}
function art_gallery_reposition() {
	var e = document.getElementById('ag_pop'); 
	if (ag_width > 0) {
		var x_pos = Math.round((window.innerWidth/2) - (ag_width/2));
		e.style.left = x_pos+'px'; 
	}
}

var http_request = false;
function art_gallery_ajx_send_comment(myurl,mycontainer) {
	http_request = false;
	if (window.XMLHttpRequest) { http_request = new XMLHttpRequest(); if (http_request.overrideMimeType) { http_request.overrideMimeType('text/xml'); } }
	else if (window.ActiveXObject) { try { http_request = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { http_request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} } }
	if (http_request) {
		my_ajax_container = mycontainer; http_request.onreadystatechange = ajax_fill_container;
		http_request.open("POST", myurl);
		http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
		http_request.send("c1=art-gallery&c2="+document.ag_comment.c2.value+"&uid_comment="+document.ag_comment.uid_comment.value+"&comment="+document.ag_comment.comment.value);
	}
	else { document.ag_comment.submit(); }
}



function get_scroll_pos() {
	var x,y;
	if (self.pageYOffset) { x = self.pageXOffset; y = self.pageYOffset; } // all except Explorer
	else if (document.documentElement && document.documentElement.scrollTop) { x = document.documentElement.scrollLeft; y = document.documentElement.scrollTop; } // Explorer 6 Strict
	else if (document.body) { x = document.body.scrollLeft; y = document.body.scrollTop; } // all other Explorers
	return arr = new Array(x,y);
}
function get_inner_size() {
	var x,y;
	if (self.innerHeight) { x = self.innerWidth; y = self.innerHeight; } // all except Explorer
	else if (document.documentElement && document.documentElement.clientHeight) { x = document.documentElement.clientWidth; y = document.documentElement.clientHeight; } // Explorer 6 Strict Mode
	else if (document.body) { x = document.body.clientWidth; y = document.body.clientHeight; } // other Explorers
	return arr = new Array(x,y);
}