var yuiBodyClass = "yui-skin-sam";
var loginPanel;
 
function showLoginForm() {	
	loginPanel = new YAHOO.widget.Panel("loginFormContent", {
		width:"400px", 
		fixedcenter: true,
		zIndex: 1000, 
		constraintoviewport: true, 
		underlay:"shadow",
		modal:true, 
		close:true, 
		visible:true, 
		draggable:false
	} );
	
	if (document.body.className.indexOf(yuiBodyClass) == -1) { 
		document.body.className=document.body.className + " " + yuiBodyClass;
	}
	loginPanel.render(document.getElementById("loginForm"));
	
	return false;
}

function doLogin(form) {
	if (form && form.email && form.password) {
		if (!form.email.value || !form.password.value) {
			errh("Please fill both email and password.");						
		}  else {
			dwr.engine.setErrorHandler(errh);
			YRTVDWRProxy.ajaxLogin(form.email.value, form.password.value, function 
				handleDoLogin(user) {
					if (user) {
						succeedLogin(user);
					}
				})
		}
	}
	return false;
}

function succeedLogin(user) {
	if (loginPanel) {
		loginPanel.destroy();
		document.body.className = document.body.className.replace(yuiBodyClass, "");
	}
	
	jQuery("#loginLinks").hide();
	jQuery("#logedinLinks").show();		
	if (user) {
		jQuery("#userName")[0].innerHTML = (user.name ? user.name + " " : "") + (user.surname ? user.surname : "");
	}	
}

function errh(msg) {
	if (jQuery("#loginErrors")[0]) {
		jQuery("#loginErrors")[0].innerHTML = '<p>' + msg + '</p>';
	} else {
		alert(msg);
	}
}		

var tipOverlay;
var storyOverlay;

var currentReporterId = null;
var cachedReporter = {"id": null, "obj": null};

function renderReporterPopup(user) {
	if (user && user.userid == currentReporterId) {
		tipOverlay = new YAHOO.widget.Overlay("tipOverlay", { context:["mr" + user.userid,"br","tl"], visible:true, width:"250px" } );			
		tipOverlay.setBody(
			"<div class='profilePopup'>" +
	            "<div class='photo'>" +
					(user.photo ? "<img src='/" + user.photo + "' />" : "<img src='/images/no_photo.gif' />") +
				"</div>" +				
				"<div class='persData'>" +
	                  	"<dl>" +
	                      	"<dt>Name:</dt>" +
	                      	"<dd>" + user.fullname + "</dd>" +
	                      	"<dt>Area:</dt>" +
	                      	"<dd>" + user.city  + "</dd>" +
	                  	"</dl>" +
				"</div>" +
			"</div>");
									
		tipOverlay.render(document.body);
	}
}

function showReporterPopup(id) {
	currentReporterId = id;
	
	if (id == cachedReporter.id) {
		renderReporterPopup(cachedReporter.obj);		
	} else {
		YRTVDWRProxy.getCuttedUserModel(id, function(user) {
			cachedReporter.id = user.userid;
			cachedReporter.obj = user;
			renderReporterPopup(user);
		});
	}		
}

function hideReporterPopup() {
	currentReporterId = null;
	if (tipOverlay) {
		tipOverlay.hide();
	}
}

var currentStoryParam = {"id": null, "x": null, "y": null}; //stores current cursor paramters 
var cachedStory = {"id": null, "obj": null}; //stores last selected object
	
function renderStoryPopup(report, floated) {
	if (report && report.reportid == currentStoryParam.id) {
		if (floated) {
			storyOverlay = new YAHOO.widget.Overlay("storyOverlay", { xy:[currentStoryParam.x,currentStoryParam.y], visible:true, width:"250px" } );
		} else {
			storyOverlay = new YAHOO.widget.Overlay("storyOverlay", { context:["ur" + report.reportid,"bl","tr"], visible:true, width:"250px" } );;
		}
		storyOverlay.setBody(
			"<div style='position: relative;'><img src='/images/popup_arrow.gif' style='position: absolute; top: 10px; left: -57px; z-index:10;'></img>" +			
			"<div class='reportPopup'>" +
				(report.userPhoto ? "<img src='/" + report.userPhoto + "' />" : "") +
				"<span>" + report.shortText + "</span>" +
			"</div></div>");
			
		storyOverlay.render(document.body);
	}
}


function showStoryPopup(id, e, floated) {
	currentStoryParam.id = id;
	
	if (e.pageX || e.pageY) {
		currentStoryParam.x = e.pageX;
		currentStoryParam.y = e.pageY;		
	} else if (e.clientX || e.clientY) { 
		currentStoryParam.x = e.clientX + document.body.scrollLeft + (document.documentElement ? document.documentElement.scrollLeft : 0);
		currentStoryParam.y = e.clientY + document.body.scrollTop + (document.documentElement ? document.documentElement.scrollTop : 0);		
	}
	
	cachedStory.rendered = false;
	if (id == cachedStory.id) {
		renderStoryPopup(cachedStory.obj, floated);
	} else {
		cachedStory.id = id; 					
		YRTVDWRProxy.getUserReportShortDetails(id, {callback: function(report) {cachedStory.obj = report; renderStoryPopup(report, floated);}});
	}
}

function hideStoryPopup() {
	currentStoryParam.id = null;
	currentStoryParam.x = null;
	currentStoryParam.y = null;
	if (storyOverlay) {
		storyOverlay.hide();
	}
}