// JavaScript Document


var homepage = false;
var resourcePage = false;
var currentSortType = null;
var lang = "en";

function  getAssetComments(node) {
		
		if($(node).attr('fullComments') == 'false'){
			$.ajax({
				url: "asset.do",
				type: "POST",
				data: ({
					"actionMethod": "getAssetComments",
					"asset.id":$(node).attr('id')
				}),
				dataType: "json",
				success: function (data, textStatus) {
					node.fullComments = "true";
					telusFilterTool.updateComments(node,data);
					$(node).attr('fullComments','true');
					//node.html($.toJSON(data));
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					$("#errorMessages").html("There was an error loading the requested asset");
				}
			});
		}
	}





buildDate = function(){
	for(var i=0;i<dataSet.length;i++){
		dataSet[i].date = new Date(dataSet[i].year,dataSet[i].month,dataSet[i].day);
	}
}

telusFilterTool = new telusFilterTool(null);
function telusFilterTool(options) {
    target = this;
	 this.criteriaCount = 0;
   this.criteria = new Object();
   this.filteredDataSet = [];
    this.contentListing = '#contentItems';
	this.listingFromTag = false;
	this.productAndSolutionsId = '#productAndSolutions';
	 this.setFilter = function(obj,category) {
		 
		if(homepage){
			//set cookie and refresh
			if(obj.value == undefined){
				this.setFilterCookie(obj,"filter",true);
			}else{
				this.setFilterCookie(obj.value,"filter",obj.checked);
			}
		}else{
			
			//alert(obj.value+"---"+filterNames[obj.value])
			if(this.criteria[category] == undefined){
				this.criteria[category] = {};	
			}
			this.criteria[category][filterNames[obj.value]] = obj.checked;
			if(obj.checked){
				this.criteriaCount++;
			}else{
				this.criteriaCount--;
			}
		  	//alert(this.criteria[filterNames[obj.value]])
		   //alert('here')
		   //alert(obj.value)
		   submitFilter(obj.value,obj.checked);
		   this.updateListings(obj.checked);
		   this.updateFeatures();
		}
    }
	
	this.initFilters = function(){
		//return;
		if(userFilterSet != undefined){
			//var filterSet = [];
			for (var a in userFilterSet){
			
				for(var i = 0;i<userFilterSet[a].length;i++){
					//var name = this.filterTrans[userFilterSet[i]];
					var name = userFilterSet[a][i];
					if(this.criteria[a] == undefined){
						this.criteria[a] = {};	
					}
					
					this.criteria[a][name] = true;	
					
					for(var b in filterNames){
						if(filterNames[b] == name)	{
							var inputName = b;	
							break;
						}
					}
					
					
					this.criteriaCount++;					
					$('input[value='+inputName+']').eq(0).attr('checked', true);				
				}
			}
		//alert($.toJSON(this.criteria))
			
		}		
		
	}
	this.updateFeatures = function(){
		// get largest business size
		var lbs = userType;
		var selected = [];
		$("input[name='bus']:checked").each(function() {
			selected.push(this.value);
        });
		if (selected.length == 0)
			lbs = "generic";
		else if ($.inArray("large", selected) >= 0)
			lbs = "large";
		else if ($.inArray("medium", selected) >= 0)
			lbs = "medium";
		else if ($.inArray("small", selected) >= 0)
			lbs = "small";
		else if ($.inArray("verySmall", selected) >= 0)
			lbs = "verySmall";
		
		// construct feature iframe url
		var isCollapsed = $("#flashFeatureIframe").attr("height") < 100;
		loadFlashFeature(filterNames[lbs], resourcePage && isCollapsed);
	}
	 this.updateListings = function(bool,showFiltersBool){
		 
		 if(showFiltersBool == "true"){
				$('#accordion .head').next().show();
				$('#accordion .head').toggleClass("current");
				$('#tagCloud').next().show();
				$('#tagCloud').toggleClass("current");
		}
		 
		$('#backToFilters').css('display','none');
		/* if(bool &&  this.filteredDataSet.length > 0 && this.listingFromTag != true){
			//added a filter so just use subset			
			var currentDataSet = this.filteredDataSet;
			this.filteredDataSet = [];
		}else{*/
			//removed a filter so start from scratch
			this.filteredDataSet = [];
			var currentDataSet = dataSet;
		//}
		
		 
		/*if(bool &&  this.filteredProductDataSet.length > 0 && this.listingFromTag != true){
			//added a filter so just use subset					
			var currentProductDataSet = this.filteredProductDataSet;
			this.filteredProductDataSet = [];
		}else{*/
			//removed a filter so start from scratch			
			this.filteredProductDataSet = [];
			var currentProductDataSet = dataSet_products;
		//}
		
		var flag = true;
		var found = false;
		var hasFilter = false;
		
	/*for(var a in this.criteria){
		for(var b in this.criteria[a]){
			alert(b)		
		}
	}*/
		
		if(!homepage){		
			if(this.criteriaCount > 0){
			   for(var i=0;i<currentDataSet.length;i++){
					flag = false;
					
					for(var a in this.criteria){					
						hasFilter = false;
						found = false;
						
						for(var b in this.criteria[a]){
							if(this.criteria[a][b] != false){
								hasFilter = true;
								if(this.criteria[a][b] == true && currentDataSet[i].categories[String(b)] == "true"){
									flag = true;
									found = true;
									break;					
								}
							}
						}	
						if(!found && hasFilter){
							flag = false;
							break;	
						}					
					}
					if(flag){
						this.filteredDataSet.push(currentDataSet[i]);
						
					}
				
				}
				//alert(this.filteredDataSet.length);
				if(currentProductDataSet != undefined){
				
					 for(var i=0;i<currentProductDataSet.length;i++){
						flag = false;
							
						for(var a in this.criteria){					
							hasFilter = false;
							found = false;
						
								for(var b in this.criteria[a]){
									
									if(this.criteria[a][b] != false){
										hasFilter = true;
										if(this.criteria[a][b] == true && currentProductDataSet[i].categories[String(b)] == "true"){
											flag = true;
											found = true;
											break;					
										}else if(a == 5){
											flag = true;
											found = true;
											break;	
										}
									}
								}	
								if(!found && hasFilter){
									flag = false;
									break;	
								}	
							
						}
						if(flag){
							this.filteredProductDataSet.push(currentProductDataSet[i]);
							
						}
						
						
					}
					
					//this.showProductsAndListings();
					
				/*	
					 for(var i=0;i<currentProductDataSet.length;i++){
						flag = true;
						for(var a in this.criteria){
							if(this.criteria[a] == true && (currentProductDataSet[i].categories[a] == undefined || currentProductDataSet[i].categories[a] == false)){
								flag = false;
								break;					
							}
						}
						if(flag){
							this.filteredProductDataSet.push(currentProductDataSet[i]);
							
						}
					
					}
					this.showProductsAndListings();*/
				}
			}else{
				this.filteredDataSet = currentDataSet;
				this.filteredProductDataSet = currentProductDataSet;
				
			}
		}else{
			this.filteredDataSet = currentDataSet;	
			this.filteredProductDataSet = currentProductDataSet;
			
			//this.filteredProductDataSet = currentProductDataSet;
		}
		
		if(currentProductDataSet != undefined){
			this.showProductsAndListings();
		}
		if(currentSortType!= null){
			this.doSort(currentSortType);	
		}else{
			this.showListings();
		}
	}
	 this.updateListingsFromTag = function(tag,favHistBool){
		 
		 if(homepage){
			 //set cookie and refresh
			 if(favHistBool){
				 this.setFilterCookie(tag,"favHist");
			 }else{
			 	this.setFilterCookie(tag,"tag");
			 }
			 
			return; 
		 }
		 //track Search
		 if(favHistBool == undefined){
			 var trackingObj = {};
			trackingObj.pageName= currentPage+'/Search/Search Title:'+tag;
			trackingObj.events='event19';
			//trackingObj.heir1=currentPage+',Submit,Search Title:'+tag;
			trackingObj.prop37= tag;
			trackItem(trackingObj);
		 }
		 $('#backToFilters').css('display','');
		 
		 $('#accordion .head').next().hide();
		 $('#accordion .head').toggleClass("current",false);
		  $('#tagCloud').next().show();
		 $('#tagCloud').toggleClass("current");
		 
		this.listingFromTag = true;
		this.filteredDataSet = [];
		var currentDataSet = dataSet;
		for(var i=0;i<currentDataSet.length;i++){
			if(favHistBool){
				if(currentDataSet[i][tag] != undefined){
					this.filteredDataSet.push(currentDataSet[i]);								
				}
			}else{
				
				if(currentDataSet[i].tags != undefined && currentDataSet[i].tags[tag] != undefined){
					this.filteredDataSet.push(currentDataSet[i]);								
				}
			}
		}
		
		this.showListings();		 
	}
	
	this.showListings = function(dir){
	 var en_homepage = homepage && typeof(user_locale) != "undefined" && user_locale == "en_CA";
		if (en_homepage){
			$("#innerColNoCommunity").hide();
			$(".communityResourcesWrap").remove();
		}
		else{
			$(this.contentListing).insertAfter("#innerColNoCommunity .innerColTitle");
			$("#innerColCommunity").hide();
			$(this.contentListing).html('');
		}
		
		if(dir == "desc"){
			for(var i = (this.filteredDataSet.length-1);i>=0;i--){
				var temp = this.buildHtml(this.filteredDataSet[i],i);
				if (en_homepage)
					$(temp).insertBefore(this.contentListing);
				else
					$(this.contentListing).append(temp);			
			}
  		}else{
			for(var i = 0;i<this.filteredDataSet.length;i++){
				var temp = this.buildHtml(this.filteredDataSet[i],i);
				if (en_homepage)
					$(temp).insertBefore(this.contentListing);
				else
					$(this.contentListing).append(temp);			
			}
		}

		if (en_homepage){
			$('#community-acc .head').click(function() {
				$(this).next().toggle();
				$(this).toggleClass("current");
				getAssetComments($(this));
				return false;
			}).next().hide();
		}
		else{
			$('#accordion-mid .head').click(function() {
				$(this).next().toggle();
				$(this).toggleClass("current");
				getAssetComments($(this));
				//alert('here')
				return false;
			}).next().hide();
		}
		
		this.updateCountOnPage(this.filteredDataSet.length);
	}
	
	this.updateCountOnPage = function(total){
		if (typeof(user_locale) != "undefined" && user_locale == "fr_CA"){
			if(total == 1){
				var start = total+" item correspond";
			}else{
				var start = total+" items correspondent";
			}
			$('#numOfAssets').html(start + ' à votre sélection. Pour en générer davantage, veuillez modifier vos critères de recherche.');
		}
		else {
			if(total == 1){
				var start = total+" resource";
			}else{
				var start = total+" resources";
			}
			$('#numOfAssets').html(start + ' match your selection(s). To view more, please adjust your selection criteria or search phrase.');
		}
	}
	 
	this.buildHtml = function(dataObj,pos){
		/*{day:6,month:3,year:2010,rating:4,title:"Lorem Ipsum Delor Sit Amet5",description:"Etiam dapibus placerat magna, eu placerat augue placerat a. Fusce pharetra suscipit eleifend. In sodales ultricies nunc, nec ornare est scelerisque in. Donec eget nisi lectus.",commentNum:30,comment:"I thought this was really interesting - Paul from Calgary",categories:{verySmall:false,small:false,large:false,busManage:false,busGrowth:false,risk:false,empEmpow:true,busFoc:true,tech:true,field:true,office:true,team:true}}*/
		

			var commentsLabel = "comment";
			
			if(dataObj.commentNum <1){
				var fullComments = true;
			}else{
				var fullComments = false;	
			}
			
			if(dataObj.commentNum > 1){
				commentsLabel += "s";	
			}
				
		
		
		if (homepage && (typeof(user_locale) != "undefined" && user_locale == "en_CA")){
			
			var lightBox="#yesno";
			
			var typeIcon="";
			switch(dataObj.type){
				case "document":
					typeIcon="images/cr-icon-document.jpg";
					break;
				case "tool":
					typeIcon="images/tools_off.gif";	
					break;
				case "video":
					typeIcon="images/cr-icon-video.jpg";
					break;
				case "promotion":
					typeIcon="images/cr-icon-promotion.jpg"
			}
			typeIcon = '<img src="'+typeIcon+'" align="absmiddle" />'
			
			var htmlStr = '<div class="communityResourcesWrap">';  
            htmlStr += '<div class="communityResources head" id="'+dataObj.id+'" fullComments="'+fullComments+'">';
            htmlStr += '<p class="crDate">'+formatDate(dataObj.day,dataObj.month,dataObj.year)+'</p>';
            htmlStr += '<p class="crTitle" style="cursor:pointer" onclick="showLightBox(\''+lightBox+'\',\''+dataObj.type+'\',\''+dataObj.id+'\',\'undefined\',\''+pos+'\');">'+dataObj.title+'</p>';
            htmlStr += '<p class="crDesc clearfix">';

			if(dataObj.thumb != undefined){
				htmlStr += '<img src="'+dataObj.thumb+'" />';
			}
			
				htmlStr += dataObj.subtitle+'</p>';
			if (dataObj.type != "promotion") {
	            htmlStr += '<div class="crComments">';
	            htmlStr += '<div class="crCommentsIcon">'+typeIcon;
				
				if(dataObj.commentNum != 0){
					htmlStr += '<a href="#">('+dataObj.commentNum+' '+commentsLabel+')</a>';
				}
	
				htmlStr += '</div>'; //class="crCommentsIcon"
	            htmlStr += '<div class="crCommentsRate"><img src="images/cr-'+dataObj.communityRating+'-star.jpg" /></div>';
	            htmlStr += '</div>'; //class="crComments"
			}
            htmlStr += '</div>'; //class="communityResources head"

			if(dataObj.commentNum > 0){
				htmlStr += '<div class="pane"></div>'
			}

			htmlStr += '</div>'; //class="communityResourcesWrap"
			return htmlStr;			
		}
		else{
			var htmlStr = '<div class="content clearfix">';
			htmlStr += ' <table class="contentTbl">';
			htmlStr += '<tr>';
			htmlStr += '<td colspan="2">';
			htmlStr += '<p class="date">'+formatDate(dataObj.day,dataObj.month,dataObj.year)+'</p>';
			
			//if(dataObj.type !="tool"){
			var lightBox="#yesno";
			if(dataObj.type == "document"){
				var typeIcon="images/paper_off.gif";
			}else if(dataObj.type =="tool"){
				var typeIcon="images/tools_off.gif";			
			}else if(dataObj.type =="promotion"){
				var typeIcon="images/promo_off.gif";
			}else{
				var typeIcon="images/video_off.gif";
			}			
			
			//rel="'+lightBox+'"
			htmlStr += '<div class="resTitle"><p class="title"><a href="javascript:showLightBox(\''+lightBox+'\',\''+dataObj.type+'\',\''+dataObj.id+'\',\'undefined\',\''+pos+'\');"  class="modalInput" >'+dataObj.title+'</a></p></div><div class="resTitleIcon"><img src="'+typeIcon+'" /></div><div class="resRating"><img src="images/rating-'+dataObj.communityRating+'.jpg" alt="" title="" /></div>';
			htmlStr += '</td>';
			htmlStr += '</tr>';
			htmlStr += '</table>';
			htmlStr += '<table cellpadding="0" cellspacing="0" width="100%" style="margin:0;padding:0">';
			htmlStr += '<tr>';
			htmlStr += ' <td valign="top"><p>'+dataObj.subtitle+'</p></td>';
			htmlStr += '<td valign="top" style="text-align:right">';
			if(dataObj.thumb != undefined){
				htmlStr += '<img src="'+dataObj.thumb+'" />';
			}
			htmlStr += '</td>';
			htmlStr += ' </tr>';
			htmlStr += ' </table>';
			htmlStr += ' <div id="accordion-mid" > ';
			if(dataObj.commentNum == 0){
				htmlStr += '<div class="comment head headNoComment clearfix" id="'+dataObj.id+'" fullComments="'+fullComments+'">';
	
			}else{
				htmlStr += '<div class="comment head clearfix" id="'+dataObj.id+'" fullComments="'+fullComments+'">';
			}
			htmlStr += '  <div class="commentLeft">';
			if(dataObj.commentNum != 0){
				//figure out comment Length
				var commentTitle = dataObj.comments[0].title;
				var commentDesc = dataObj.comments[0].comment;
				//var titleLength = dataObj.comments[0].title.length;
				//alert(commentTitle.length)
				var commentTextLength = 0;
				if(commentTitle.length > 50){
					commentTitle = commentTitle.slice(0,50);
					commentDesc = "";
					commentTextLength = 50;
				}else{
					commentTitle = commentTitle+", ";
					
					commentDesc = commentDesc.slice(0,Math.min(commentDesc.length,50-commentTitle.length));
					commentTextLength = commentTitle.length+commentDesc.length;
				}
				
				htmlStr += '<span class="greenTxt">('+dataObj.commentNum+' '+commentsLabel+')</span> '+commentTitle+commentDesc;
				if (dataObj.comments[0].commentName != undefined && 60 > (commentTextLength+dataObj.comments[0].commentName.length)) {
					htmlStr += ", "+dataObj.comments[0].commentName;
				}
			}
			htmlStr += '</div>';
			if(dataObj.commentNum > 0){
				htmlStr += ' <div class="commentRight"><a href="#" class="more">More</a></div>';
			}
			
			htmlStr += '   </div>';
			if(dataObj.commentNum > 0){
				htmlStr += '<div class="pane"></div>'
			}
			htmlStr += '   </div>';
			htmlStr += '   </div>';
			return htmlStr
		}
			//javascript:getAssetComments(2)
	} 
	
	this.updateComments = function(node,dataArr){
		var comment = '';
		
		for(var i = 0;i<dataArr.length;i++){
			comment = dataArr[i].title+"<br/>"+dataArr[i].comment;
			if (dataArr[i].commentName != undefined) {
				comment += "<br/>"+dataArr[i].commentName;
			}
			node.next().append("<p>"+comment+"</p>")
		}
	
	}

	
	
	this.doSort = function(type){
		if(type == "null"){
			currentSortType = null;
			return;	
		}
		currentSortType = type;
		var dataArray = this.filteredDataSet;
		this.filteredDataSet = quickSort(dataArray, type);
		this.showListings("desc");
		
	}
	
	this.setFilterCookie = function(name,type,value){
		//alert(value+"--"+type);	
		
		if(value == undefined){
			if(type == "favHist"){
				window.location = "resources.do?searchType="+name;
			}else{
				window.location = "resources.do?search="+name;
			}
		}else{
			submitFilter(name,value,true);
			
		}
	
	}
	this.setFavHis = function(pos,type,bool,assetID){
	
		if(resourcePage){
			if(isNaN(pos)){
				this.setFavHistInFullDataset(type,bool,assetID);
			}else{
				if(bool == "true" || bool == undefined){
					this.filteredDataSet[pos][type]=true;
				}else{
					this.filteredDataSet[pos][type]=undefined;
				}
			}
		}
		//}
	}
	this.setFavHistInFullDataset = function(type,bool,assetID){
		for(var i = 0;i<dataSet.length;i++){
			if(dataSet[i].id == assetID){
				if(bool == "true" || bool == undefined){
					dataSet[i][type]=true;
				}else{
					dataSet[i][type]=undefined;
				}
				//return i;
				break;	
				
			}
		}	
		
	}
	
	this.getDataPosFromFullData = function(assetID){
		for(var i = 0;i<dataSet.length;i++){
			if(dataSet[i].id == assetID){				
				return dataSet[i].title;
				break;	
				
			}
		}		
	}
	
	//productAndSolutions
	this.buildProductAndSolutionsHtml = function(dataObj){
		
		var htmlStr = '<tr>';
		htmlStr+=  '<td><a href="'+dataObj.productUrl+'" target="_blank">'+dataObj.headline+'</a></td>';
		//htmlStr+=     '<td style="text-align:right;"><img src="images/icon-youtube.gif" alt="Youtube" title="Youtube" /></td>';
		htmlStr+='</tr>';
		return htmlStr;
	}
	
	this.showProductsAndListings = function(){
		//$(this.productAndSolutionsId).html('<table>');
		//alert(this.filteredProductDataSet.length)
		var dataStr ="<table>";		
		for(var i = 0;i<Math.min(this.filteredProductDataSet.length,5);i++){
			var temp = this.buildProductAndSolutionsHtml(this.filteredProductDataSet[i],i);
			dataStr+=temp;			
		}
		dataStr+='</table>';	
		$(this.productAndSolutionsId).html(dataStr);		
		
		
		/*$('#accordion-mid .head').click(function() {
			$(this).next().toggle();
			$(this).toggleClass("current");
			getAssetComments($(this));
			//alert('here')
			return false;
		}).next().hide();*/
		
		
	}

}

submitFilter = function(name,value,fromHome){
		//send to server	
		//alert(name+" -- "+filterNames[name])
		 var nameId =   filterNames[name];    
		if (fromHome)
		{
		  $('.resourcesShield').show();			
		}
		//$("#errorMessages").hide();
		$.ajax({
			url: "user.do",
			type: "POST",
			data: ({
					"actionMethod": "setFilter",
					"id": nameId,
					"value": value
			}),
			dataType: "json",
			success: function (data, textStatus) {
				if(fromHome){
					window.location = "resources.do";
				}
				
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				//will always through error from home as we are not waiting for this to come back
				//alert("error");
				
			}
			
		});
		
	
		/*Used to track when filters are checked. If new ones get added then they will need to be added to the appropriate category they are in
		via the case statement, or they will be added to the 'UndefinedCategory'*/	
		
		if(value == true){
			var tracked = "On";
		}else{
			var tracked = "Off";	
		}
			var trackingObj = {};
			
			switch(nameId){
				case 10: case 11: case 12: case 13: case 18:
					var catName = "BusSize";
					break;
				case 3: case 4: case 5: case 8: case 9:
					var catName = "BusNeed";
					break;
				case 1: case 2: 
					var catName = "Role";
					break;
				case 14: case 15: case 16:
					var catName = "WorkEnviro";
					break;
				default:
					var catName = "UndefinedCategory";
					break;
			}
			
		/*	if (fromHome){
				trackingObj.pageName= "Home";	
			}else{
				trackingObj.pageName= currentPage;
			}*/
			
			trackingObj.pageName= currentPage+'/'+catName+'/'+name+'/'+tracked;
			
			trackItem(trackingObj);
      // alert(trackingObj.pageName)
	
}

function updateFavouriteFromFlash(dataSetPos,bool,assetID){
	//alert(dataSetPos+"--"+bool+" -- "+assetID);
	//alert('here')
	telusFilterTool.setFavHis(Number(dataSetPos),"favourite",bool,Number(assetID));
}
function updateHistoryFromFlash(dataSetPos,bool,assetID){
	//alert(dataSetPos+"--"+bool);
	telusFilterTool.setFavHis(Number(dataSetPos),"history",bool,Number(assetID));
}

function initHomeFilter(type){
	submitFilter(type,true);
	$('input[value='+type+']').eq(0).attr('checked', true);
	var api = $("#lightbox3").overlay();			
	api.close();	
	//reset home page with this type	
	
	var dataSetName = "dataSet_"+type;
	//dataSet = null;	
	dataSet = dataSetLib[filterNames[type]];
	dataSet.unshift(this[dataSetName][0]);	
	buildDate();
	//var dataSetName = "dataSet_"+type;
	//dataSet.splice(0,1,this[dataSetName][0])

	telusFilterTool.updateListings(false);		

	//send userType into flash feature
	loadFlashFeature(filterNames[type]);
	//loadFlashFeature(this[dataSetName]);
	userType = type;
	//tracking
	var trackingObj = {};
	trackingObj.pageName= 'Welcome/'+userType;
	trackingObj.prop1=userType;
	trackingObj.channel=userType;
	trackingObj.events='Complete';
	//trackingObj.heir1='Welcome,'+userType;
	trackItem(trackingObj);
	
}

function removeWelcome(){
	
	//tracking
	var trackingObj = {};
	trackingObj.pageName= 'Welcome/Close';
	trackingObj.events= 'event9';
	//trackingObj.heir1='Welcome,Close';
	trackItem(trackingObj);
}

function getFavHist(tag){	
	telusFilterTool.updateListingsFromTag(tag,true);		
}


var aDay	= new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var aMonth	= new Array("January","February","March","April","May","June","July","August","September","October","November","December")
var aDayFr	= new Array("dimanche","lundi","mardi","mercredi","jeudi","vendredi","samedi");
var aMonthFr= new Array("janvier","février","mars","avril","mai","juin","juillet","août","septembre","octobre","novembre","décembre")



function formatDate(day,month,year){
	if (typeof(user_locale) != "undefined" && user_locale == "fr_CA"){		
		return 'le ' + day + ' ' + aMonthFr[month] + ' ' + year;
	}
	else{	
		return aMonth[month]+" "+day+", "+year;
	}
	
}



// STABLE implementation of quick sort to replace unstable Array.sort method in Firefox
// if sorting an array of objects, key = name of object property to compare
// otherwise leave key undefined
quickSort = function(arr, key) {

  // return if array is unsortable
  if (arr.length <= 1){
    return arr;
  }

  var less = Array(), greater = Array();

  // select and remove a pivot value pivot from array
  // a pivot value closer to median of the dataset may result in better performance
  var pivotIndex = Math.floor(arr.length / 2);
  var pivot = arr.splice(pivotIndex, 1)[0];

  // step through all array elements
  for (var x = 0; x < arr.length; x++){

    // if (current value is less than pivot),
    // OR if (current value is the same as pivot AND this index is less than the index of the pivot in the original array)
    // then push onto end of less array
    if (
      (
        !key  // no object property name passed
        &&
        (
          (arr[x] < pivot)
          ||
          (arr[x] == pivot && x < pivotIndex)  // this maintains the original order of values equal to the pivot
        )
      )
      ||
      (
        key  // object property name passed
        &&
        (
          (arr[x][key] < pivot[key])
          ||
          (arr[x][key] == pivot[key] && x < pivotIndex)  // this maintains the original order of values equal to the pivot
        )
      )
    ){
      less.push(arr[x]);
    }

    // if (current value is greater than pivot),
    // OR if (current value is the same as pivot AND this index is greater than or equal to the index of the pivot in the original array)
    // then push onto end of greater array
    else {
      greater.push(arr[x]);
    }
  }

  // concatenate less+pivot+greater arrays
  return quickSort(less, key).concat([pivot], quickSort(greater, key));
};




