// ------------------------------------------------------
// |													|
// |	Site-wide Scripts Loading						|
// |	Copyright (c) 2010 O'oui.ca						|
// |													|
// ------------------------------------------------------

// ------------------------------------------------------
// |													|
// |	Load everything as soon as the DOM is loaded	|
// |	and before the page contents are loaded			|
// |													|
// ------------------------------------------------------
$(document).ready(function(){

	// Homepage Showcase
	$("#background .images ul li img").css('display', 'block')

	$(function() {
		$("#captions").jCarouselLite({
			visible: 1,
			start: 0,
			vertical: true,
			auto: 6000,
			speed: 1000
		});
	});

	$(function() {
		$("#background .images").jCarouselLite({
			visible: 1,
			start: 0,
			auto: 6000,
			speed: 1000
		});
	});

	// Disable Right Mouse Click
	$('img').bind("contextmenu",function(e){
		return false;
	});

	// Homepage Newsletter
	var newsletterDefaultValue = $("#txtSearch").attr('value');
	$("#txtSearch").focus(function()
	{
		if($(this).val() == newsletterDefaultValue)
			$(this).val('');
	});
	$("#txtSearch").blur(function()
	{
		if($(this).val() == '')
			$(this).val(newsletterDefaultValue);
	});

	// Open chat in new window
	$('a[href$=/chat/]').click(function()
	{
		window.open($(this).attr('href'),"mywindow","directories=0,height=665,location=0,menubar=0,resizable=1,scrollbars=1,status=1,toolbar=0,width=750");
		return false;
	});

	// User Settings
	$("#settingsMenu").change(function()
	{
		document.location.href = $("#settingsMenu").val();
	});

	// LightBox Effect<br />
	$("a[rel^='prettyPhoto']").prettyPhoto({animationSpeed:'slow',theme:'light_rounded',slideshow:5000});
});

// ------------------------------------------------------
// |													|
// |	Custom Javascript Functions						|
// |													|
// |													|
// ------------------------------------------------------
// ---------------------------------------------------
// 			USER Interraction
// ---------------------------------------------------
var isUserActionConfirmed = false;

function ConfirmUserAction(msg)
{
	var btn = confirm(msg);
	if(btn)
		return true;
	else
		return false;
}

// --------------------------
// Checkbox Manipulation
function ToggleSelectAll(srcChkObj,dstChkName)
{
	if(srcChkObj.checked)
		SetCheckbox = true;

	if(document.getElementsByName(dstChkName))
	{
		var elems = document.getElementsByName(dstChkName);
		numElems = elems.length;

		for(i = 0; i < numElems; i++)
		{
			elems[i].checked = srcChkObj.checked;
		}
	}
}

function GetNumChkSelected(chkName)
{
	var numChkSelected = 0;

	if(document.getElementsByName(chkName))
	{
		var elems = document.getElementsByName(chkName);
		numElems = elems.length;

		for(i = 0; i < numElems; i++)
		{
			if(elems[i].checked)
				numChkSelected++;
		}
	}
	return numChkSelected;
}

// ---------------------------------------------------
// SECTION:		USER Picture EDIT
// ---------------------------------------------------
var formImgIndex = -1;

function SubmitPicEdit(objForm)
{
	//alert ("formImgIndex = " + formImgIndex);
	if(formImgIndex == -1)
	{
		alert ("Vous devez choisir une image à manipuler.");
		return false;
	}
	else
	{
		objForm.imgIndex.value = formImgIndex;
		return true;
	}
}

function KeepOneInputFileImg (selectedId)
{
	// Test Javascript compatibility
	if(document.getElementById('jsPic_'+ selectedId) || formImgIndex == -1)
	{
		// SET working image index for FORM Submit
		formImgIndex = selectedId;

		// 0 = pic_default, [1-4] = Extra Pics
		for(i = 0; i <= 4; i++)
		{
			if(i != selectedId)
			{
					//alert('Clear input = ' + i);
					document.getElementById('jsPic_'+ i).innerHTML = '<input id="pic-upload" type="file" name="pic_' + i + '" value="" />';
			}
		}
	}
}

function ClearAllInputFileImg (exceptionId)
{
	//alert('ClearAllInputFileImg is running');
	// SET working image index for FORM Submit
	KeepOneInputFileImg (-1);
	formImgIndex = exceptionId;
}

// ---------------------------------------------------
// SECTION:		USER Favoris
// ---------------------------------------------------
function SubmitDeleteFriend(aForm, chkName)
{
	var isValidForm = true;

	if(!GetNumChkSelected(chkName))
	{
		alert("Aucun favori n'a été sélectionné. Suppression annulée.");
		isValidForm = false;
	}
	else
		isValidForm = ConfirmUserAction("Supprimer les favoris sélectionnés ?");

	return isValidForm;
}

// ---------------------------------------------------
// SECTION:		USER Favoris
// ---------------------------------------------------
function SubmitDeleteUserPicsAuth(aForm, chkName)
{
	var isValidForm = true;

	if(!GetNumChkSelected(chkName))
	{
		alert("Aucun membre n'a été sélectionné. Suppression annulée.");
		isValidForm = false;
	}
	else
		isValidForm = ConfirmUserAction("Supprimer les membres sélectionnés ?");

	return isValidForm;
}
// ---------------------------------------------------
// SECTION:		USER Messenger
// ---------------------------------------------------
function SubmitDeleteMsg(aForm, chkName)
{
	var isValidForm = true;

	if(!GetNumChkSelected(chkName))
	{
		alert("Aucun message n'a été sélectionné. Suppression annulée.");
		isValidForm = false;
	}
	else
		isValidForm = ConfirmUserAction("Supprimer les messages sélectionnés ?");

	return isValidForm;
}

// ---------------------------------------------------
// SECTION:		NIGHTS: jQuery Comments
// ---------------------------------------------------
var oldCommentId 				= -1;
var oldCommentHtmlContent 		= '';
var oldCommentTextContent 		= '';
var markerHtmlPrefix 			= '#jMarkerHtmlId_';
var markerTextPrefix 			= '#jMarkerTextId_';
var markerFormHtmlContainerId 	= '#jMarkerFormHtmlContainerId';
var markerFormTextContainerId 	= '#jMarkerFormTextContainerId';

function InlineCommentEdit(currentCommentId)
{
	// Deactivate previous inline editing
	InlineCommentCancel();

	// COPY new to old inline editing
	oldCommentId 			= currentCommentId;
	oldCommentHtmlContent 	= $(markerHtmlPrefix + currentCommentId ).html();
	oldCommentTextContent 	= $(markerTextPrefix + currentCommentId ).text();

	// SET new inline editing FORM
	$(markerFormTextContainerId ).text(oldCommentTextContent);
	$(markerHtmlPrefix + oldCommentId ).html( $(markerFormHtmlContainerId ).html() );
}

function InlineCommentCancel()
{
	// IF at least one inline editing has been initiated
	if(oldCommentId != -1)
	{
		// Restore comment content <div> HTML + <p> Text
		$(markerHtmlPrefix + oldCommentId ).html(oldCommentHtmlContent);
		$(markerFormTextContainerId ).text('');

		// Restore FORM editing template

		// Reset jQuery Comment variables
		oldCommentId = -1;
		oldCommentHtmlContent 	= '';
		oldCommentTxtContent 	= '';
	}
}

// Avoid adding multiple concatenation
var SubmitCommentUrl = 'none'

function SubmitComment(aForm)
{
	var isCommentValid = true;

	if(aForm.comment)
	{
		if(aForm.comment.value.length < 2)
		{
			alert("Veuillez entrer un commentaire afin de continuer votre ajout.");
			isCommentValid = false;
		}
	}
	if(isCommentValid && aForm.submit.value == 'Modifier')
	{
		if(SubmitCommentUrl == "none")
			SubmitCommentUrl = aForm.action;

		aForm.action = SubmitCommentUrl + "&commentId=" + oldCommentId + "#marker_comment_" + oldCommentId;
	}
	//return false;
	return isCommentValid;
}

// ---------------------------------------------------
// 			MISC Functions
// ---------------------------------------------------

// --------------------------
// Show / Hide DIV
function ShowDiv(id)
{
   document.getElementById(id).style.visibility = "visible";
}
function HideDiv(id)
{
   document.getElementById(id).style.visibility = "hidden";
}
function Nl2br(objText)
{
	objText.value = (objText.value).replace(/\n/g, "{n}");
	//alert("New value = " + objText.value);
	return true;
}
