


/**

LADDA OM BAKGRUNDSSIDAN HELA TIDEN?

VALIDERING!

*/


/** General community popin js */

// Creates a -popup- if one is needed
function openCommunityPopup(url)
{
	window.open(url,"communitypopup","height=700,width=700,menubar=no,resizable=no,scrollbars=yes,status=no,toolbar=no");
}

// Creates the community popin container, with GI Direct styles as an option
function createCommunityPopin(isGIDirect)
{
	var cssClass = "";
	if(isGIDirect == 'true')
		cssClass = "gi-direct-popin";

	// Create container
	$(document.body).adopt(
		$$(
			new Element("div", 
				{
					id:"popin-wrapper",
					styles: { "height": window.getScrollSize().y.toInt() }
				}
			)
		),
		popin = 
			new Element("div", { id:"community-popin", "class":cssClass }).adopt(
				$$(	new Element("div", { "style":"float:right;display:block;" }) ).adopt(
					$$(	new Element("a", { id:"community-close", href:"#", "html":"( st&auml;ng f&ouml;nster )" }) )
				)
				,
				$$(	new Element("div", { id:"community-content" }) )				
		)
	);
	
	// Add GI Direct specific elements
	if(isGIDirect == 'true')
	{
		var giDirectEl = 
			$$(	new Element("div", { id:"header" }) ).adopt(
				$$(	new Element("img", { "src":"/images/icons/ico_coach.png" }) )
				,
				$$(	new Element("span", { "html":"Coachen" }) )
				,
				$$(	new Element("img", { "src":"/images/logo-gi-direct-white.png", "style":"float:right;" }) )
			);
		giDirectEl.inject($("community-popin"),'top');
	}
	
	// Button events
	$('community-close').addEvent("click", function(e) {
		e.stop();
		disposePopinElement('community-popin');
		disposePopinElement('popin-wrapper');
	});
		
	rePositionCommunityPopin("community-popin");
}

// Create popin for feedback on close, save and load status
function createAlertPopin()
{	
	$(document.body).adopt(
		$$(	new Element("div", { id:"community-status" }) ).adopt(
			$$(	new Element("img", { id:"status-img" }) )
			,
			$$(	new Element("br", {  }) )
			,			
			$$(	new Element("br", {  }) )
			,
			$$(	new Element("span", { id:"status-desc" }) )
		)
	);
	
	rePositionCommunityPopin("community-status");
}

function rePositionCommunityPopin(popinID) {
	if ($("popin-wrapper"))
		$("popin-wrapper").setStyle("height", window.getScrollSize().y.toInt());
	if ($(popinID))
		$(popinID).setStyle("left", window.getSize().x/2 - $(popinID).getStyle("width").toInt()/2);
}

// General popin element remover
function disposePopinElement(elementID)
{
	if($(elementID))
	{
		$(elementID).fade(0);
		$(elementID).setStyle('display','none');
		$(elementID).dispose();
	}
}


/** Business card specific js */

function openBusinessCardPopin(userID)
{
	var url = "/utils/community/ContentHandler.aspx";
	var bloggURL = "";
	if($('blogg-page-url-for-bc'))
		bloggURL = $('blogg-page-url-for-bc').get('value');
		
	var args = 
		"action=businesscard"
		+ "&userID=" + userID
		+ "&bloggPageURL=" + bloggURL;
		
	new Request({
		"url": url,
		"data": args,
		"headers": {"X-Request": "JSON"},
		"onSuccess": function(response) {
		
			// Create the popin container
			createCommunityPopin();	
			
			// Response from AJAX-request contains the business card content
			var bcArray = response.split('|');
			
			// Fill popin container with business card content
			insertBusinessCardContent(bcArray[0], bcArray[1], bcArray[2], bcArray[3]);
		}
	}).send();
}

function insertBusinessCardContent(userID, alias, content, options)
{	
	$('community-content').empty();
	$('community-content').adopt(
		$$(	new Element("span", { "id":"page-desc", "html":"Tittar p&aring; visitkort" }) )	
		,
		$$(	new Element("br", { }) )			
		,
		$$(	new Element("div", { "id":"subject", "html":alias }) )
		,
		$$(	new Element("div", { "html":content }) )
		,
		$$(	new Element("div", { id:"community-options", "class":"bc-options", "html":options }) )
	);
		
		/**.adopt(
			$$(	new Element("a", { id:"add-remove-friend-link", href:"#" }) ).adopt(
				$$(	new Element("div", { "style":"margin-right:10px;" }) ).adopt(
					$$(	new Element("img", { "src":"/images/icons/ico_addfriend.png" }) )
					,
					$$(	new Element("span", { "html":"L&auml;gg till v&auml;n" }) )
				)
			)
			,
			$$(	new Element("a", { id:"send-mail-link", href:"#" }) ).adopt(
				$$(	new Element("div", { }) ).adopt(
					$$(	new Element("img", { "src":"/images/icons/ico_answer.png" }) )
					,
					$$(	new Element("span", { "html":"Skicka mail" }) )
				)
			)
			,
			$$(	new Element("br", { }) )
			,
			$$(	new Element("br", { }) )
			,
			$$(	new Element("a", { id:"image-link", href:"#" }) ).adopt(
				$$(	new Element("div", { "style":"margin-right:10px;" }) ).adopt(
					$$(	new Element("img", { "src":"/images/icons/ico_gallery.png" }) )
					,
					$$(	new Element("span", { "html":"Se bilder" }) )
				)
			)
			,
			$$(	new Element("a", { id:"blogg-link", href:"#" }) ).adopt(
				$$(	new Element("div", { }) ).adopt(
					$$(	new Element("img", { "src":"/images/icons/ico_blogg.png" }) )
					,
					$$(	new Element("span", { "html":"L&auml;s blogg" }) )
				)
			)
		)*/
	
	// Button events
	if($('add-friend-link'))
		$('add-friend-link').addEvent("click", function(e) {
			e.stop();
			addOrRemoveFriend(true, userID);
		});
	if($('remove-friend-link'))
		$('remove-friend-link').addEvent("click", function(e) {
			e.stop();
			addOrRemoveFriend(false, userID);
		});
	if($('send-mail-link'))
		$('send-mail-link').addEvent("click", function(e) {
			e.stop();
			insertWriteContent("", userID, alias, "", "");
		});	
}

function addOrRemoveFriend(isToBeAdded, userID)
{
	var url = "/utils/community/ContentHandler.aspx";
	var args = 
		"action=addOrRemoveFriend&"
		+ "isToBeAdded=" + isToBeAdded + "&"
		+ "userID=" + userID;
	
	new Request({
		"url": url,
		"data": args,
		"headers": {"X-Request": "JSON"},
		"onSuccess": function(response) {
		
			// reloads business card popin
			disposePopinElement('community-popin');
			disposePopinElement('popin-wrapper');
			openBusinessCardPopin(userID);
		}
	}).send();
}


/** Mail popin specific js */

function createWriteMailPopin(userID, alias, isGIDirect)
{
	createCommunityPopin(isGIDirect);	
	insertWriteContent("", userID, alias, "", "");
}

function createReadMailPopin(mailID, isGIDirect)
{
	var url = "/utils/community/ContentHandler.aspx";
	var args = 
		"action=getMail&"
		+ "mailID=" + mailID;
	
	new Request({
		"url": url,
		"data": args,
		"headers": {"X-Request": "JSON"},
		"onSuccess": function(response) {
		
			var mailArray = response.split('|');
			
			createCommunityPopin(isGIDirect);
			
			insertReadContent(mailArray[0], mailArray[1], mailArray[2], mailArray[3], mailArray[4], mailArray[5]);
		}
	}).send();
}

function insertReadContent(mailID, senderID, senderName, subject, body, date)
{
	// Beautify content
	if(body)
	{
		if(body.indexOf(">>>") >= 0) 
			body = body.replace(/>>>/,"><em>") + "</em>";
	}
	else
		body = "";
	if(!senderName)
		senderName = "";
	if(!subject)
		subject = "";
	if(!date)
		date = "";

	// Replace old popin content with read mail interface
	$('community-content').empty();
	$('community-content').adopt(
		$$(	new Element("span", { "id":"page-desc", "html":"L&auml;ser meddelande" }) )	
		,
		$$(	new Element("br", { }) )			
		,
		$$(	new Element("div", { "id":"subject", "html":subject }) )
		,
		$$(	new Element("span", { id:"mail-id", "html":mailID }) )	
		,
		$$(	new Element("span", { id:"sender-id", "html":senderID }) )	
		,
		$$(	new Element("span", { "class":"header", "html":"Fr&aring;n " }) )	
		,
		$$(	new Element("span", { "html": senderName + "&nbsp; &nbsp; &nbsp;" }) )	
		,
		$$(	new Element("span", { "class":"header", "html":"Datum " }) )	
		,
		$$(	new Element("span", { "html":date }) )	
		,
		$$(	new Element("br", { }) )
		,
		$$(	new Element("div", { id:"mail-body", "html":body }) )
		,
		$$(	new Element("br", { }) )
		,
		$$(	new Element("div", { id:"community-options", "class":"mail-options" }) ).adopt(
			$$(	new Element("a", { id:"mail-abort-link", href:"#" }) ).adopt(
				$$(	new Element("div", { "style":"margin-right:10px;" }) ).adopt(
					$$(	new Element("img", { "src":"/images/icons/ico_back.png" }) )
					,
					$$(	new Element("span", { "html":"St&auml;ng" }) )
				)
			)
			,
			$$(	new Element("a", { id:"mail-answer-link", href:"#" }) ).adopt(
				$$(	new Element("div", { }) ).adopt(
					$$(	new Element("img", { "src":"/images/icons/ico_answer.png" }) )
					,
					$$(	new Element("span", { "html":"Svara" }) )
				)
			)
		)
	)
	
	// Button events
	$('mail-abort-link').addEvent("click", function(e) {
		e.stop();
		if($('community-popin')){
			disposePopinElement('community-popin');
			disposePopinElement('popin-wrapper');
		}
	});
	
	$('mail-answer-link').addEvent("click", function(e) {
		e.stop();
		insertWriteContent(mailID, senderID, senderName, subject, body);
	});
}

function insertWriteContent(mailID, receiverID, receiverName, subject, body)
{
	// Beautify content
	if(subject && subject != "")	subject = "SV: " + subject;
	else							subject = "";
	if(body && body.indexOf("<em>") >= 0) 
	{
		body = ">>" + body.replace("<em>",">>");
		body = body.replace("</em>","");
	}
	if(body && body != "")		
	{
	    body = "\n\n\n\n>>" + body.replace(/<br \/>/g,"\n");	
	    body = body.replace("&nbsp;", " ");
	    body = body.replace("&", "o");
	}

	// Replace old popin content with write-mail interface
	$('community-content').empty();
	$('community-content').adopt(
		$$(	new Element("span", { "id":"page-desc", "html":"Skriver meddelande" }) )	
		,
		$$(	new Element("br", { }) )			
		,
		$$(	new Element("input", { "id":"subject", "type":"text", "value":subject }) )
		,	
		$$(	new Element("br", { }) )
		,
		$$(	new Element("span", { id:"mail-id", "html":mailID }) )
		,
		$$(	new Element("span", { id:"receiver-id", "html":receiverID }) )	
		,
		$$(	new Element("span", { "class":"header", "html":"Till "}) )	
		,
		$$(	new Element("span", { "html": receiverName }) )	
		,	
		$$(	new Element("br", { }) )
		,
		$$(	new Element("textarea", { id:"mail-body", "value":body }) )
		,
		$$(	new Element("br", { }) )
		,
		$$(	new Element("div", { id:"community-options", "class":"mail-options" }) ).adopt(
			$$(	new Element("a", { id:"mail-abort-link", href:"#" }) ).adopt(
				$$(	new Element("div", { "style":"margin-right:10px;" }) ).adopt(
					$$(	new Element("img", { "src":"/images/icons/ico_back.png" }) )
					,
					$$(	new Element("span", { "html":"St&auml;ng" }) )
				)
			)
			,
			$$(	new Element("a", { id:"mail-send-link", href:"#" }) ).adopt(
				$$(	new Element("div", { }) ).adopt(
					$$(	new Element("img", { "src":"/images/icons/ico_answer.png" }) )
					,
					$$(	new Element("span", { "html":"Skicka" }) )
				)
			)
		)
	)
	
	// Button events
	$('mail-abort-link').addEvent("click", function(e) {
		e.stop();
		disposePopinElement('community-popin');
		disposePopinElement('popin-wrapper');
	});
	
	$('mail-send-link').addEvent("click", function(e) {
		e.stop();
		sendMail(
			mailID,
			receiverID, 
			$('subject').get('value'), 
			$('mail-body').get('value')
		);
	});
}

// Action : Sends mail, closes popin and reloads parent page
function sendMail(mailID, receiverID, subject, body)
{		
	disposePopinElement('community-popin');
	createAlertPopin();
	showLoadingFeedback();

	var url = "/utils/community/ContentHandler.aspx";
	var args = 
		"action=sendMail&"
		+ "mailID="		+ mailID		+ "&"
		+ "receiverID=" + receiverID	+ "&"
		+ "subject="	+ subject		+ "&"
		+ "body="		+ body;
	
	new Request({
		"url": url,
		"data": args,
		"headers": {"X-Request": "JSON"},
		"onSuccess": function(response) {
			showSuccessFeedback();
			var delayedFunction = function(){ disposePopinElement('community-status'); disposePopinElement('popin-wrapper'); if($$('.Inbox')[0]) {window.location.reload();} };
			var delayedFunction = function(){ disposePopinElement('community-status'); disposePopinElement('popin-wrapper'); if($$('.Inbox')[0]) {window.location.reload();} };
			delayedFunction.delay(1000);
		}
	}).send();
}

function showLoadingFeedback()
{	
	$('status-img').set("src","/images/icons/ajax-loader.gif");
	$('status-desc').set("html","Meddelandet skickas..");	
}

function showSuccessFeedback()
{	
	$('status-img').set("src","/images/icons/ico_success.png");
	$('status-desc').set("html","Meddelandet har skickats!");	
}