var win= null;
function popupLauncher(title,link,w,h){
	var winl = (screen.width-w)/2;
	var wint = (screen.height-h)/2;
	var settings ='height='+h+',';
	settings +='width='+w+',';
	settings +='top='+wint+',';
	settings +='left='+winl+',';
	settings +='scrollbars=no,';
	settings +='resizable=no';
	win=window.open(link,title,settings);
	if(parseInt(navigator.appVersion) >= 4){win.window.focus();}
}


var pictureController = _picC = new Object();
_picC.timerLeft = "";
_picC.timerRight=""


_picC.getInstance = function() {
}

_picC.vote = function(pic,user,vote) {
	dojo.xhrPost({
		url: base_url+"/service/profile/picture-vote",
		content: {'pictureId':pic,'userId':user,'vote':vote},
		handleAs:"json",
		load: function(data){
			console.log(data);
			alert(data.message);
			dojo.byId('picPopul').className = data.classname;
			if(data.links==1) {
				dojo.query("#picPopul a").forEach(function(node, index, arr){
					node.onclick = '';
				});
			}
			if(data.total > 0) {
				dojo.byId('totalVotes').innerHTML = data.total;
			}
			if(data.average > 0) {
				dojo.byId('avgVotes').innerHTML = data.average;
			}
		}
	});
}
_picC.addFav = function(id) {
	var hash = dojo.byId("secureHash").value;
	dojo.xhrGet({
	    url: base_url+"/profile/pictures/add-favourite/?picId="+id+"&secureHash="+hash,
	    handleAs: "text",
	    load: function(data,args){
			alert(data);
	    },
	    error: function(error,args){
			console.warn("error!",error);
	    }
	});
}

_picC.launchFotoEditor = function(user) {
	popupLauncher('FotoEdytor','http://upload.epuls.pl/profile_pictures_manager2.php?lngUID='+user,820,640);
}


_picC.showDarkMap = function(id) {
	return true;
	var img = dojo.byId("fullImg");
	if((img.height>0) && (img.width>0)) { 
		c = dojo.byId('cloud_init_' + id);
		var x = (document.body.clientWidth-img.width-10)/2;
		var img_pos = realPos(img);
		var top_pos = img_pos[1];

		dml = dojo.byId('darkMapLeft');
		dml.style.visibility = "visible";
		dml.style.left = x - 84 + "px"; 
		dml.style.width = parseInt(c.style.left)  - parseInt(c.style.width) + "px";
		dml.style.height = _imageHeight + "px";
		dml.style.top = top_pos + 2
		dml.style.fontSize = '1px';

		dmt = dojo.byId('darkMapTop');
		dmt.style.visibility = "visible";
		dmt.style.left = x + parseInt(c.style.left) - parseInt(c.style.width) - 84 + "px";
		dmt.style.width = parseInt(c.style.width) + "px";
		dmt.style.height =  parseInt(c.style.top)  + 40 + "px";
		dmt.style.top = top_pos + 2;
		dmt.style.fontSize = '1px';
		dmt.style.zIndex = 1;

		dmr = dojo.byId('darkMapRight');
		dmr.style.visibility = "visible";
		dmr.style.left = x + parseInt(c.style.left) - 84 + "px";
		dmr.style.height = _imageHeight + "px";
		dmr.style.width = x + _imageWidth - parseInt(dmr.style.left) -84 + "px";
		dmr.style.top = top_pos + 2

		dmr.style.fontSize = '1px';

		dmb = dojo.byId('darkMapBottom');
		dmb.style.visibility = "visible";
		dmb.style.top = parseInt(dmt.style.height) + parseInt(c.style.height) + top_pos + 2 + "px";
		dmb.style.left =  x + parseInt(c.style.left) - parseInt(c.style.width) -84 + "px";
		dmb.style.width = parseInt(c.style.width) + "px";
		dmb.style.height = _imageHeight - parseInt(c.style.height) - parseInt(dmt.style.height);

		dmb.style.fontSize = '0px';
		dmb.style.overflow = 'hidden';

		d = dojo.byId('cloud_'+ id);
		d.style.display = 'block';
		d.style.zIndex = 999;



	}
}

_picC.hideDarkMap = function() {
	return true;

	dml = dojo.byId('darkMapLeft');
	dml.style.visibility = "hidden";

	dmt = dojo.byId('darkMapTop');
	dmt.style.visibility = "hidden";

	dmr = dojo.byId('darkMapRight');
	dmr.style.visibility = "hidden";

	dmb = dojo.byId('darkMapBottom');
	dmb.style.visibility = "hidden";
}

_picC.toRight = function() {
	var sc = dojo.byId('picScroller');
	clearTimeout(_picC.timerLeft)
	sc.scrollLeft += 3;
	_picC.timerLeft = setTimeout("_picC.toRight()",10);
	
}

_picC.toLeft = function() {
	var sc = dojo.byId('picScroller');
	clearTimeout(_picC.timerRight);
	sc.scrollLeft -= 3;
	_picC.timerRight = setTimeout("_picC.toLeft()",10);
}
_picC.stop = function() {
	clearTimeout(_picC.timerRight);
	clearTimeout(_picC.timerLeft);
}
_picC.resize = function() {
	if(dojo.byId("fullImg")) {
		var image = dojo.byId("fullImg");
		if(image.width>740) {
			var oldw = image.width;
			var oldh = image.height;
			var newh = (740 * oldh) / oldw;
			newh = Math.round(newh);
			image.width = 740;
			image.height = newh;
		}
	}
}
dojo.addOnLoad(function(){_picC.resize();});