var topics = [];

topics['psychic']='1119';


var CURRENTPAGE = '';
var CURRENTTOPIC ='';
var EXPERTSPERPAGE = 3;
var PROFILEURL = '/profile'; // this is the url for the profile pages
var REVIEWURL = 'expertreviews.html';  //url for the reviews page

//obj is returned json data
function api_expertProfiles(obj){
	var totalpages = Math.ceil( obj.total / EXPERTSPERPAGE );
	// cycle through experts and create their html
	for(var i=0;i<obj.experts.length;i++){
	
		var catID = obj.category.id;
		var expertID = obj.experts[i].id;
		var expertThumbnail = obj.experts[i].thumbnail;
		var expertName = obj.experts[i].name;
		var expertRatingUnrounded = obj.experts[i].averageRating;
		var expertAverageRating = roundStarRating(obj.experts[i].averageRating);
		var expertReviews = obj.experts[i].reviews;
		var expertReviewsCount = obj.experts[i].reviewsCount;
		var expertFee = obj.experts[i].fee;
		var expertDetailedDescriptionWithTags = obj.experts[i].description;
		var expertDetailedDescription = remove_html(expertDetailedDescriptionWithTags);
		var expertContactLive = obj.experts[i].contactLive;
		var expertStatus = obj.experts[i].status;
		var expertContactOffline = obj.experts[i].contactOffline;
		
		var contactLink = expertContactOffline;
        if(expertStatus == 1 || expertStatus == 4)
        {
        contactLink = expertContactLive;
        }		
		
		if(catID == 1119 || catID == 10328 || catID == 1140 || catID == 1120 || catID == 10422 || catID == 1143 || catID == 10011 || catID == 1126 || catID == 1142 || catID == 10552){
		var expertIndex = Math.floor(Math.random()*100)+1;
		}
		else{var expertIndex = Math.floor(Math.random()*20)+1;}
	
		
		//start the expert profile
		var expertString = "<div id='mygrid_Expert_"+i+"'>"+
		"<div class='grid'>"+
		"<a id='profile_url' href='"+PROFILEURL+"?BanID="+BANNERID+"&catid="+catID+"&expid="+expertID+"&count=3&index="+expertIndex+"' class='expname'>"+expertName+"</a>"+
"<div class='profile-info'>"+
		"<div class='profile-photo'><a href='"+PROFILEURL+"?BanID="+BANNERID+"&catid="+catID+"&expid="+expertID+"&count=3&index="+expertIndex+"' ><img class='photo' src='"+expertThumbnail+"' title='"+expertName+"' alt='"+expertName+"' /></a></div>"+
		"<div class='profile-desc'>"+		
		"<p class='description'>"+expertDetailedDescription+"</p>"+
		"</div>"+
		"<span class='rating'><img src='http://kasamba.s3.amazonaws.com/images/all/rate"+expertAverageRating+".png' alt='"+expertRatingUnrounded+"' title='"+expertRatingUnrounded+"' /></span><span class='reviews'><a href=\"javascript:OpenWindow('http://www.liveperson.com/rating/expert-rating.aspx?i_MemID="+expertID+"','450','700')\">"+expertReviewsCount+" reviews</a></span>"+
"</div>"+		
		"<a href=\"javascript:OpenWindow('"+contactLink+"','970','630')\" onclick=\"_gaq.push(['_trackPageview', '/chatlivebutton'])\" title='Contact Live' class='contact-link' >"+expertStatus+"</a>"+
		"<div id='chat_email'><span class='perminute'>$"+expertFee+"<span style=\"color:#666666\"> / minute</span></span></div>"+
"</div></div>";

		$("#top_advisors_grid").append(expertString);	
		var tempURL = $("#mygrid_Expert_"+i+" .contact-link").attr('href').replace('BanID=0','BanID='+BANNERID+'');	
		$("#mygrid_Expert_"+i+" .contact-link").attr('href',tempURL);
	}

	
	// clean up profile descriptions
	loop('mygrid', set_status, ['Chat Live','']); 
    loop('mygrid', remove_tags, []); 
	loop('mygrid', trim_description, 120 ); 
	//remove target blank that is inserted from the trim_description function above
	$('a').attr('target','');
}

//updates the experts displayed to the given category
function changeTo(category){
	var catID = topics[category];
	var offset = ((CURRENTPAGE-1) * EXPERTSPERPAGE) + 1; //offset for retreving experts
	if(undefined == catID){ catid = '1119'; } //this id is the tarot id, used as a backup
	// $("#top_advisors_grid").empty();  might not be needed for this case

	//build GET url and fetch the results
	var url = 'http://apis.liveperson.com/experts.svc/1.1/categories/'+catID+'/jsonp?count='+EXPERTSPERPAGE+'&index='+offset+'&sort=9&callback=api_expertProfiles'
	$.ajax({
		type:'GET',
		url: url,
		dataType: 'script'
	});			
	

	

}

//default to general counseling category
$(document).ready(function () {
	
		//onload read topic / cateogry from url variable s'topic' 
	URLvars = getUrlVars();
	var category = 'psychic'; //default
	if(undefined != topics[URLvars['topic']]) 
	{ category=URLvars['topic']; }

	//read page # from url variable 'page'
	var page = 1;
	if(URLvars.length>0 && undefined!= URLvars['page'] && URLvars['page'].match(/^\d+$/))
	{ page  = URLvars['page'] }
	
	//set global page and categories
	CURRENTPAGE = page;
	CURRENTTOPIC = category;
	
	//update display of experts to the given category and page
	changeTo(category) //change to specified topic	
	
})

