function WallpaperHover(id,color){
	var wpel = document.getElementById(id);
	wpel.style.border = '2px solid '+color;
}
function ImgHover(img,newImg){
	img.src=newImg;
	img.style.cursor = 'pointer';
}
function AdultCoategory(rem_id){	
	/*var parentNode = rem_id.parentNode;
	parentNode.innerHTML = '';
//	document.body.removeChild(parentNode);*/
	$(".adult_content").remove();
}
function ViewWallpapers(cat_id){
	window.onload = function(){
		var w = screen.width;
		var h = screen.height;
		var Aj = new Ajax();
		Aj.SetContainer('right_side');
		if(parseInt(cat_id)>0){
			var and_cat = "/catid/"+cat_id;
		}
		else{
			var and_cat = "";
		}
		Aj.SetPage('/wallpapersbysize/view/w/'+w+'/h/'+h+and_cat);
		Aj.DoXML();
		Aj.Load();		
	}
	setTimeout('ViewWallpapers_a('+cat_id+')',10000);
}
function ViewWallpapers_a(cat_id){	
		var w = screen.width;
		var h = screen.height;
		var Aj = new Ajax();
		Aj.SetContainer('right_side');
		if(parseInt(cat_id)>0){
			var and_cat = "/catid/"+cat_id;
		}
		else{
			var and_cat = "";
		}
		Aj.SetPage('/wallpapersbysize/view/w/'+w+'/h/'+h+and_cat);
		Aj.DoXML();
		Aj.Load();	
	setTimeout('ViewWallpapers_a('+cat_id+')',10000);
}
function ChangeInputBackground(in_id,image){
	var input = document.getElementById(in_id);
	input.style.backgroundImage = 'url(/template/default/images/'+image+')';
}
function CheckRegisterValues(input_id){
	var Aj_check = new Ajax();
	var container = input_id.replace('r_','');
	container+='_result';
	var value = document.getElementById(input_id).value;
	value = escape(value);
	if(value.length>1){
		Aj_check.SetContainer(container);
		Aj_check.SetPage('/checkregisterfields/v/t/'+input_id+'/i/'+value);
		Aj_check.DoXML();
		Aj_check.Load();
	}
}
function ShowCropDownload(id,width,height){
	var background_cover = document.createElement('DIV');
	var crop_div = document.createElement('DIV');
	
	var heightAll = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight;
	var scroll = window.pageYOffset ? window.pageYOffset : (document.body.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop);
	var widthAll = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth;
	var l = (widthAll / 2) - 300;
	var h = (heightAll / 2) + scroll - 200;
	
	background_cover.setAttribute('id','bg_crop_cover');
	
	
	background_cover.style.width = widthAll+'px';
	background_cover.style.height = heightAll + 'px';
	
	background_cover.style.position = 'fixed';
	background_cover.style.top = '0px';
	background_cover.style.left = '0px';
	background_cover.style.background = "#383838";
	background_cover.style.filter = 'alpha(opacity=50)';
	background_cover.style.opacity = '0.50';
	
	crop_div.setAttribute('id','crop_div');
	crop_div.style.position = 'fixed';
	crop_div.style.top = h+'px';
	crop_div.style.left = l+'px';
	crop_div.style.margin = 'auto';
	crop_div.style.backgroundColor = '#eeeeee';
	crop_div.style.width = '600px';
	crop_div.style.height = '400px';
	crop_div.style.padding = '0px';
	crop_div.style.border = '1px solid #000000';
	
	var Aj_C = new Ajax();
	Aj_C.SetContainer('crop_div');
	Aj_C.SetPage('/crop_wallpaper/w/i/' + id + '/wi/' + width + '/hi/' + height);
	Aj_C.DoXML();
	Aj_C.Load();
	
	document.body.appendChild(background_cover);
	document.body.appendChild(crop_div);
	
	return false;
		
}
function CloseCropWindow(){
	var bg_div = document.getElementById('bg_crop_cover');	
	document.body.removeChild(bg_div);
	var crop_div = document.getElementById('crop_div');	
	document.body.removeChild(crop_div);
}
var Drag = {

	obj : null,

	init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
	{
		o.onmousedown	= Drag.start;

		o.hmode			= bSwapHorzRef ? false : true ;
		o.vmode			= bSwapVertRef ? false : true ;

		o.root = oRoot && oRoot != null ? oRoot : o ;

		if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
		if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
		if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
		if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

		o.minX	= typeof minX != 'undefined' ? minX : null;
		o.minY	= typeof minY != 'undefined' ? minY : null;
		o.maxX	= typeof maxX != 'undefined' ? maxX : null;
		o.maxY	= typeof maxY != 'undefined' ? maxY : null;

		o.xMapper = fXMapper ? fXMapper : null;
		o.yMapper = fYMapper ? fYMapper : null;

		o.root.onDragStart	= new Function();
		o.root.onDragEnd	= new Function();
		o.root.onDrag		= new Function();
	},

	start : function(e)
	{
		var o = Drag.obj = this;
		e = Drag.fixE(e);
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		o.root.onDragStart(x, y);

		o.lastMouseX	= e.clientX;
		o.lastMouseY	= e.clientY;

		if (o.hmode) {
			if (o.minX != null)	o.minMouseX	= e.clientX - x + o.minX;
			if (o.maxX != null)	o.maxMouseX	= o.minMouseX + o.maxX - o.minX;
		} else {
			if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
			if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
		}

		if (o.vmode) {
			if (o.minY != null)	o.minMouseY	= e.clientY - y + o.minY;
			if (o.maxY != null)	o.maxMouseY	= o.minMouseY + o.maxY - o.minY;
		} else {
			if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
			if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
		}

		document.onmousemove	= Drag.drag;
		document.onmouseup		= Drag.end;

		return false;
	},

	drag : function(e)
	{
		e = Drag.fixE(e);
		var o = Drag.obj;

		var ey	= e.clientY;
		var ex	= e.clientX;
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		var nx, ny;

		if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
		if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
		if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
		if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);

		nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
		ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));

		if (o.xMapper)		nx = o.xMapper(y)
		else if (o.yMapper)	ny = o.yMapper(x)

		Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
		Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
		Drag.obj.lastMouseX	= ex;
		Drag.obj.lastMouseY	= ey;

		Drag.obj.root.onDrag(nx, ny);
		return false;
	},

	end : function()
	{
		document.onmousemove = null;
		document.onmouseup   = null;
		Drag.obj.root.onDragEnd(	parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]), 
									parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
		Drag.obj = null;
	},

	fixE : function(e)
	{
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		return e;
	}
};
function CheckDragResults(t_w, t_h, wp_id, d_w, d_h, p_w , p_h, n_w, n_h){
	
	var button_c = document.getElementById('download_cropped_button');
	var src_y = document.getElementById('crop_src_y');
	var dr_div = document.getElementById('Drag_dv');
	var dr_top = dr_div.style.top.replace('px','');
	var dr_left = dr_div.style.left.replace('px','');
	if(dr_top>(p_h-d_h)){
		var new_top= p_h-d_h;
		dr_div.style.top = new_top+"px";
		dr_top = new_top;
	}	
	var new_t_width = parseInt((t_w * dr_left)/p_w);
	var new_t_height = parseInt((t_h * dr_top)/p_h);
	
	//button_c.setAttribute('onclick',"CropDownload('/view/as/i/"+wp_id+"/w/"+ n_w +"/h/"+ n_h +"/srcx/"+new_t_width+"/srcy/"+new_t_height+"');");
	button_c.onclick = function(){
		CropDownload('/view/as/i/'+wp_id+'/w/'+ n_w +'/h/'+ n_h +'/srcx/'+new_t_width+'/srcy/'+new_t_height);
	}

}
function ExtCloseCropWindow(){
	var bg_div = parent.document.getElementById('bg_crop_cover');	
	parent.document.body.removeChild(bg_div);
	var crop_div = parent.document.getElementById('crop_div');	
	parent.document.body.removeChild(crop_div);
}
function CropDownload(url){	
	Aj.DownloadParent(url);
	ExtCloseCropWindow();
}
function EditAccountInfo(id){
	var el = document.getElementById(id);
	var pic = document.getElementById(id+'_img');
	var tel = document.getElementsByTagName('div');
	var imgs = document.getElementsByTagName('img');
	for(var a=0;a<tel.length;a++){
		if(tel[a].getAttribute('jv_close')=='true'){
			tel[a].style.display = 'none';
		}
	}
	for(var i=0;i<imgs.length;i++){
		if(imgs[i].getAttribute('jv_close')=='true'){
			imgs[i].src = '/template/default/images/plus.png';
		}
	}
	el.style.display = 'block';
	pic.src = '/template/default/images/minus.png';
}
function UpdateAccountInfo(input_id){
	var Aj_check = new Ajax();
	var container = input_id;
	container+='_result';
	var value = document.getElementById(input_id).value;
	value = escape(value);
	if(value.length>1){
		Aj_check.SetContainer(container);
		Aj_check.SetPage('/updateaccountinfo/v/t/'+input_id+'/i/'+value);
		Aj_check.DoXML();
		Aj_check.Load();
	}
}
function UpdateAvatarInfo(val){
	var input_id = 'avatar_id';
	var Aj_check = new Ajax();
	var container = input_id;
	container+='_result';
	var value = val
	value = escape(value);
	if(value.length>=1){
		Aj_check.SetContainer(container);
		Aj_check.SetPage('/updateaccountinfo/v/t/'+input_id+'/i/'+value);
		Aj_check.DoXML();
		Aj_check.Load();
	}
}
function UploadWallpaperCat(val,uid){	
	if(val!=0){
		var s = document.getElementById('upload_wp');
		s.innerHTML = s.innerHTML.replace(/%%CAT%%/ig,val+'_'+uid);
		s.style.display = 'block';
	}
}
function ProfileWallpapers(id){
	var Aj_check = new Ajax();
	Aj_check.SetContainer('profile_load');
		Aj_check.SetPage('/users/wallpapers/id/'+id);
		Aj_check.DoXML();
		Aj_check.Load();
}
function ProfileInfo(id){
	var Aj_check = new Ajax();
	Aj_check.SetContainer('profile_load');
		Aj_check.SetPage('/users/info/id/'+id);
		Aj_check.DoXML();
		Aj_check.Load();
}
function ProfileMessage(id){
	var Aj_check = new Ajax();
	Aj_check.SetContainer('profile_load');
		Aj_check.SetPage('/users/send/id/'+id);
		Aj_check.DoXML();
		Aj_check.Load();
}
function WriteProfileMessage(){
	
	var Aj_c = new Ajax();
	Aj_c.SetContainer('message_result');
	Aj_c.SetForm('writemessage_form');
	Aj_c.SetMethod('POST');
	Aj_c.SetPage('/users/writemessage');
	Aj_c.DoXML();
	Aj_c.Post();
	/*
	var f = document.getElementById('message');
	f.value = '';*/
}
function AjaxCommand(loadTo,loadUrl,message){
	if(message.length!=0){
		if(confirm(message)){
			AjaxCmd(loadTo,loadUrl);
		}
	}
	else{
		AjaxCmd(loadTo,loadUrl);
	}
}
function AjaxCmd(loadTo,loadUrl){
	var Aj = new Ajax();
	Aj.SetContainer(loadTo);
	Aj.SetPage(loadUrl);
	Aj.DoXML();
	Aj.Load();
}
function AdminLoadWpThumb(wpid,cont){
	var el = document.getElementById(cont);
	el.style.display='block';
	var im = document.getElementById(cont+'_img');
	im.src = "/view/viewthumbsmall/wpid/"+wpid;
}
function AdminUnLoadWpThumb(cont){
	var el = document.getElementById(cont);
	el.style.display='none';
}
function SearchResShow(id){
	var el = document.getElementById(id);
	if(el.style.display == 'block'){
		el.style.display = 'none';
	}
	else{
		el.style.display = 'block';
	}
}
function SelectSearchRes(res_width,res_height,fill_txt,close_sec){
	var el = document.getElementById(close_sec);
	el.style.display = 'none';
	var el_txt = document.getElementById(fill_txt);
	el_txt.innerHTML = res_width+'x'+res_height;
	var imp_w = document.getElementById('search_res_show_width');
	imp_w.value = res_width;
	var imp_h = document.getElementById('search_res_show_height');
	imp_h.value = res_height;
}
function preSearchFill(id){
	var w = screen.width;
	var h = screen.height;
	var el_txt = document.getElementById(id);
	el_txt.innerHTML = w+'x'+h;
	var imp_w = document.getElementById('search_res_show_width');
	imp_w.value = w;
	var imp_h = document.getElementById('search_res_show_height');
	imp_h.value = h;
}
function WallpaperDownload(id,width,height){
	var Ajax_x = new Ajax();
	Ajax_x.Download('/view/pl/i/'+id+'/w/'+width+'/h/'+height);
}
