var justJoinedNames = new Array();
var justJoinedCitys = new Array();
var justJoinedImages = new Array();
var justJoinedIds = new Array();
var xmlparsed = false;
//Identifier of new div ID
var newID = 0;
//##
//Identifier of images dir (this will be usefull for fast dir update)
var imagesDir = '../uploaded_images/profile_images/sp/';
//##
//That one it's for FOR cicles
var z = 0;
var lastXMLid = 0;
var gender = 'F';
var request = false;
var objHttpFileDataRequest;
function ReadCookies(cookieName) {
 var theCookie=""+document.cookie;
 var ind=theCookie.indexOf(cookieName);
 if (ind==-1 || cookieName=="") return ""; 
 var ind1=theCookie.indexOf(';',ind);
 if (ind1==-1) ind1=theCookie.length; 
 return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}
function setCookies(c_name,value,expiredays){
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
if(ReadCookies('justjoinedgender') != ""){
	gender = ReadCookies('justjoinedgender');
}
var started = false;
//##

function getXMLData(jjnumber, lastid){
	objHttpFileDataRequest = $.ajax({
		type: "GET",
		url: "justJoined.php",
		dataType: "xml",
		data: "jjnumber="+jjnumber+"&lastid="+lastid+"&gender="+gender,
		success: parseXml
	});
	request = true;
}

function parseXml(xml) {
if(xmlparsed != true){
	z=0;
	$(xml).find("serviceProvider").each(function() {  
	//find each instance of loc in xml file and wrap it in a link 
	lastXMLid=$(this).find("id").text();
	createNew($(this).find("username").text(), $(this).find("city").text(), $(this).find("image").text(), z);
	z+=1;
	}); 
	setTimeout('getXMLData(4,'+lastXMLid+')', 10000);
	xmlparsed = true;
	loadBar(false);
}
};  

//Function to create the new div's and update Array's (it will activate the deleteLast function too!)
function createNew(name, city, image, delay){
	if(started == true && justJoinedIds.length == 4){
		deleteLast(name, city, image, delay);
	}else{
	justJoinedNames.push(name);
	justJoinedCitys.push(city);
	justJoinedImages.push(image);
	
	if(started != false){
		justJoinedIds.push(Math.abs(justJoinedIds[justJoinedIds.length-1])+1);
		newID = justJoinedIds[justJoinedIds.length-1];
	}else{
		started = true;
		justJoinedIds[0] = "0";
		newID = 0;
	}
	$('#imagesContainer').prepend('<div class="spimage" id="sp'+newID+'" style="display:none;"><div id="imageBackground"><a href="http://www.electronicvisits.com/members/'+name+'"><img src="'+imagesDir+name+'/'+image+'" width="130" height="170" border="0" /></a></div><div class="infoContainer" id="infoContainer'+newID+'"><div id="spname">'+name+'</div><div id="spcountry">'+city+'</div></div></div>');
	setTimeout('$("#sp'+newID+'").show("slow"); $("#infoContainer'+newID+'").show("slow");',delay*1000);
	}
}
//##
//Function to delete the oldest div (last one) and delete oldest Array values
function deleteLast(name, city, image, delay){
	$("#infoContainer"+justJoinedIds[0]).hide("fast");
	$("#sp"+justJoinedIds[0]).hide("fast", function(){
		$(this).remove();		
	});
	justJoinedNames.shift ();
	justJoinedCitys.shift ();
	justJoinedImages.shift ();
	justJoinedIds.shift ();
	createNew(name, city, image, delay);
}
//##
//Function to change gender
function changeGender(selectedGender){
	if(gender != selectedGender){
		loadBar(true);
		gender = selectedGender;
		setCookies('justjoinedgender',gender,30);
		xmlparsed = false;
		if(request != false){
			objHttpFileDataRequest.abort();
			request=false;}
			getXMLData(4, 0);
	}
}

function loadBar(active){
	if(active){
		$('#jjidentifier').addClass('loadingFiles');
	}else{
		$('#jjidentifier').removeClass('loadingFiles');
	}
}
//###
//When the document it's loaded - create divs with the Array values. (this will inicializate the justJoined feature)
$(document).ready(function() {
	getXMLData(4, 0);
}); 
//##
