$j(document).ready(function(){
	if(Ka.Info.PAGE=='common/userTermsOfService.jsp'){
		$j('a:contains(" Back to previous page")').replaceWith('<a href="javascript: window.close();" style="float:right"> Close</a>');
	}
	
	
	if(Ka.Info.PAGETYPE=='Kickplace'&& Ka.Info.PAGE=='pages/kickPlaceServerSide.jsp'){
		var url = baseResource+'index.php?option=com_scholasticajax&format=raw&ka_action=userLocation&username='+encodeURIComponent(Ka.Info.PROFILENAME)+'&callback=gotLocationInfo';
		aObj = new JSONscriptRequest(url);
		aObj.buildScriptTag();
		aObj.addScriptTag();
	}
	
	if(Ka.Info.PAGETYPE=='MKP'&& Ka.Info.PAGE=='pages/myPlace.jsp'){
		var url = baseResource+'index.php?option=com_scholasticajax&format=raw&ka_action=userLocation&username='+encodeURIComponent(Ka.Info.USERNAME)+'&callback=gotLocationInfoMyHome';
		aObj = new JSONscriptRequest(url);
		aObj.buildScriptTag();
		aObj.addScriptTag();
	}
	if (Ka.Info.PAGE == "pages/myPlace.jsp") {
	//group
	  	var url = 'http://clientapps.kickapps.com/scholastic/getUserGroups.php?as='+Ka.Info.AFFILIATESITEID+'&userId='+Ka.Info.USERID+'&callback=gotClubs';
		aObj = new JSONscriptRequest(url);
		aObj.buildScriptTag();
		aObj.addScriptTag();
		//media
		var mediaURL2 = encodeURIComponent('http://serve.a-feed.com/service/getFeed.kickAction?as='+Ka.Info.AFFILIATESITEID+'&mediaType=video,photo,blog,audio&members='+encodeURIComponent(Ka.Info.USERNAME));
		$j('#ka_newPhoto_header').after('<div id="mod2"></div>');
		var flashvars2 = {  
		affiliateSiteId:104512,
		widgetId:251907,
		width:420,
		height:100,
		revision:3,
		mediaURL:mediaURL2
		};
		var params2 = {  
			allowFullScreen:true,
			allowScriptAccess:"always",
			wmode:"transparent",
			movie:"http://serve.a-widget.com/service/getWidgetSwf.kickAction"
		};
		var attributes2 = {  
			id: "kickWidget_104512_251907",  
			name: "kickWidget_104512_251907"
		};
		swfobject.embedSWF("http://serve.a-widget.com/service/getWidgetSwf.kickAction", "mod2", "420", "100", "9.0.0","http://serve.a-widget.com/service/getWidgetSwf.kickAction", flashvars2, params2, attributes2);
		
		
	
	}
	if (Ka.Info.PAGE == "pages/kickPlaceServerSide.jsp") {
		if (Ka.Info.USERID == Ka.Info.PROFILEID) {
	        $j('#ka_profileRight').prepend('<div id="status"><div id="statusTop"></div><div id="statusContent"><p>Tell everybody about a success or challenge in your classroom.</p><input type="text" value="Enjoying the new READ 180 site!" onFocus="if(this.value==\'Enjoying the new READ 180 site!\')this.value=\'\';" id="statusInput" /><input value="UPDATE" id="submitStatus" type="submit" onClick="javascript:addStatusUpdate()"/></div><span id="limitCount"></span><div id="statusBottom"></div></div>');
	
	    } else {
	    	$j('#ka_profileRight').prepend('<div id="status"></div>');
	    }
	    updateStatusList();
    
	}

});
var updateFormVar=null;
function updateUserMail(formVar){
		updateFormVar = formVar;
		var email = $j('#email').val();
		var username = escape(Ka.Info.USERNAME);
		var url = baseResource+'index.php?callback=?';
		$j.ajax({
		 dataType: 'jsonp',
		 data: {option: 'com_scholasticajax',format:'raw',ka_action:'updateUserEmail',username:username,newemail:email },
		 jsonp: 'updatedUserEmail',
		 url: url,
		 success: updatedUserEmail
		});
}

function updatedUserEmail(json){
	if(json.status==1){
		updateFormVar.submit();
	} else {
		$j('#email').before('<div id="emailError">Unable to update your email address.</div>');
	}
}

function addStatusUpdate(){
	var update = $j('#statusInput').val();
	$j('#statusInput').val('');
	if(update!="" && update!="Enjoying the new Read180 site!"){
		//looks like we have a real update
		var url = baseResource+'index.php?option=com_scholasticajax&format=raw&ka_action=addUpdate&callback=addedStatusUpdate&username='+escape(Ka.Info.USERNAME)+'&kaUid='+Ka.Info.USERID+'&text='+encodeURIComponent(update);
		aObj = new JSONscriptRequest(url);
		aObj.buildScriptTag();
		aObj.addScriptTag();
	}
}

function removeStatus(updateId){
	if(Ka.Info.USERID==Ka.Info.PROFILEID){
		var url = baseResource+'index.php?option=com_scholasticajax&format=raw&ka_action=deleteUpdate&callback=deletedUpdate&username='+escape(Ka.Info.USERNAME)+'&updId='+updateId;
		aObj = new JSONscriptRequest(url);
		aObj.buildScriptTag();
		aObj.addScriptTag();
	} else {
		alert("unable to remove status update.  Please try again later.");
	}
}

function deletedUpdate(json){
	updateStatusList();
}


function addedStatusUpdate(json){
	if(json.status==1){
		updateStatusList();
	} else {
		alert("unable to store status update at this time.  Please try again later.");
	}
}

function updateStatusList(){
		var url = baseResource+'index.php?option=com_scholasticajax&format=raw&ka_action=getUpdates&callback=renderStatusUpdates&username='+escape(Ka.Info.PROFILENAME);
		aObj = new JSONscriptRequest(url);
		aObj.buildScriptTag();
		aObj.addScriptTag();
}

function renderStatusUpdates(json){
	if(json.status==1){
		$j('#currentStatus').remove();
		var statusList = json.statusList;
		var outputStr = '<div id="currentStatus"><h5>Current Status</h5>';
		if(statusList.length>0){
			for (var i in statusList){
				if(typeof(statusList[i].text)=='string'){
					if(Ka.Info.USERID==Ka.Info.PROFILEID){
						outputStr = outputStr+'<div class="statusContainer">  <p class="status"><strong class="statusUN">'+Ka.Info.PROFILENAME+'</strong>&nbsp;'+statusList[i].text+'</p>  <p class="statusTime"><strong>Posted:</strong> '+statusList[i].created+' - <span><a href="javascript:removeStatus('+statusList[i].id+')">Delete</a></span> </p> </div>';
					} else {
						outputStr = outputStr+'<div class="statusContainer">  <p class="status"><strong class="statusUN">'+Ka.Info.PROFILENAME+'</strong>&nbsp;'+statusList[i].text+'</p>  <p class="statusTime"><strong>Posted:</strong> '+statusList[i].created+' </p> </div>';
					}
		
				}
				
			}
		} else {
			outputStr = outputStr+'<p>No Status</p>';
		}
		outputStr = outputStr+'</div>';
		$j('#status').after(outputStr);
	}
}


function gotClubs(json){
	var groupsList = json.groups;
	var groups = "";
	 $j('#ka_newestModule_header').after('<div id="mod1"></div>');
	if(groupsList.length>0){
		for (var i in groupsList){
			var item = groupsList[i];
			if(typeof(item.id)!='undefined'){
				if(groups==""){
					groups = item.id;
				} else {
					groups = groups+","+item.id;
				}
			}
		}
		var mediaURL1 = encodeURIComponent("http://serve.a-feed.com/service/getFeed.kickAction?as="+Ka.Info.AFFILIATESITEID+"&mediaType=group&mediaId="+groups);
		//alert("mediaURL:"+mediaURL1);
		
			var flashvars = {  
			affiliateSiteId:104512,
			widgetId:251909,
			width:420,
			height:100,
			revision:4,
			mediaURL:mediaURL1
			};
			var params = {  
				allowFullScreen:true,
				allowScriptAccess:"always",
				wmode:"transparent",
				movie:"http://serve.a-widget.com/service/getWidgetSwf.kickAction"
			};
			var attributes = {  
				id: "kickWidget_104512_251909",  
				name: "kickWidget_104512_251909"
			};
			swfobject.embedSWF("http://serve.a-widget.com/service/getWidgetSwf.kickAction", "mod1", "420", "100", "9.0.0","http://serve.a-widget.com/service/getWidgetSwf.kickAction", flashvars, params, attributes);
	} else {
		$j('#mod1').html('<a href="/service/searchEverything.kickAction?as='+Ka.Info.AFFILIATESITEID+'&mediaType=group&sortType=recent&includeGroups=on&d-7095067-p=1">join a group</a>');
	}
		
}

function gotLocationInfo(json){
	if(json.status==1){
		//htmlStr = '<div id="scholastic_school"><strong>School:</strong><a href="/service/searchEverything.kickAction?as='+Ka.Info.AFFILIATESITEID+'&mediaType=user&sortType=recent&includeUser=on&d-7095067-p=1&tab=memberSearchResults&keywords='+encodeURIComponent(json.school.toLowerCase())+'^^^">'+json.school+'</a></div>';
		//htmlStr = htmlStr+'<div id="scholastic_district"><strong>District:</strong><a href="/service/searchEverything.kickAction?as='+Ka.Info.AFFILIATESITEID+'&mediaType=user&sortType=recent&includeUser=on&d-7095067-p=1&tab=memberSearchResults&keywords='+encodeURIComponent(json.district.toLowerCase())+'^^^">'+json.district+'</a></div>';
		//htmlStr = htmlStr+'<div id="scholastic_location"><strong>Location:</strong><a href="/service/searchEverything.kickAction?as='+Ka.Info.AFFILIATESITEID+'&mediaType=user&sortType=recent&includeUser=on&d-7095067-p=1&tab=memberSearchResults&keywords='+encodeURIComponent(json.city.toLowerCase()+','+json.state.toLowerCase())+'^^^">'+json.city+', '+json.state+'</a></div>';
	
		htmlStr = '<div id="scholastic_school"><strong>School:</strong><a href="/service/searchEverything.kickAction?as='+Ka.Info.AFFILIATESITEID+'&mediaType=user&sortType=recent&includeUser=on&d-7095067-p=1&tab=memberSearchResults&meta='+encodeURIComponent('school='+json.school.toLowerCase())+'">'+json.school+'</a></div>';
		htmlStr = htmlStr+'<div id="scholastic_district"><strong>District:</strong><a href="/service/searchEverything.kickAction?as='+Ka.Info.AFFILIATESITEID+'&mediaType=user&sortType=recent&includeUser=on&d-7095067-p=1&tab=memberSearchResults&meta='+encodeURIComponent('district='+json.district.toLowerCase())+'">'+json.district+'</a></div>';
		htmlStr = htmlStr+'<div id="scholastic_location"><strong>Location:</strong><a href="/service/searchEverything.kickAction?as='+Ka.Info.AFFILIATESITEID+'&mediaType=user&sortType=recent&includeUser=on&d-7095067-p=1&tab=memberSearchResults&meta='+encodeURIComponent('city='+json.city.toLowerCase())+'">'+json.city+'</a>, <a href="/service/searchEverything.kickAction?as='+Ka.Info.AFFILIATESITEID+'&mediaType=user&sortType=recent&includeUser=on&d-7095067-p=1&tab=memberSearchResults&meta='+encodeURIComponent('state='+json.state.toLowerCase())+'">'+json.state+'</a></div>';
		$j('#ka_profileLocItem').html(htmlStr);
	}
}

function gotLocationInfoMyHome(json){
	if(json.status==1){
		$j('#ka_myHomePointsLevels').append('<div id="userRegInfo"><ul><li id="userSchool"><strong>School:</strong>  <a href="#"></a></li><li id="userDistrict"><strong>District:</strong><a href="#"></a></li><li id="userLocation"><strong>Location:</strong>  <a href="#"></a></li></ul></div>');
	
		//$j('#userSchool').html('<strong>School:&nbsp;</strong><a href="/service/searchEverything.kickAction?as='+Ka.Info.AFFILIATESITEID+'&mediaType=user&sortType=recent&includeUser=on&d-7095067-p=1&tab=memberSearchResults&keywords='+encodeURIComponent(json.school.toLowerCase())+'^^^">'+json.school+'</a>');
		//$j('#userDistrict').html('<strong>District:&nbsp;</strong><a href="/service/searchEverything.kickAction?as='+Ka.Info.AFFILIATESITEID+'&mediaType=user&sortType=recent&includeUser=on&d-7095067-p=1&tab=memberSearchResults&keywords='+encodeURIComponent(json.district.toLowerCase())+'^^^">'+json.district+'</a>');
		//$j('#userLocation').html('<strong>Location:&nbsp;</strong><a href="/service/searchEverything.kickAction?as='+Ka.Info.AFFILIATESITEID+'&mediaType=user&sortType=recent&includeUser=on&d-7095067-p=1&tab=memberSearchResults&keywords='+encodeURIComponent(json.city.toLowerCase()+','+json.state.toLowerCase())+'^^^">'+json.city+', '+json.state+'</a>');
		
		$j('#userSchool').html('<strong>School:&nbsp;</strong><a href="/service/searchEverything.kickAction?as='+Ka.Info.AFFILIATESITEID+'&mediaType=user&sortType=recent&includeUser=on&d-7095067-p=1&tab=memberSearchResults&meta='+encodeURIComponent('school='+json.school.toLowerCase())+'">'+json.school+'</a>');
		$j('#userDistrict').html('<strong>District:&nbsp;</strong><a href="/service/searchEverything.kickAction?as='+Ka.Info.AFFILIATESITEID+'&mediaType=user&sortType=recent&includeUser=on&d-7095067-p=1&tab=memberSearchResults&meta='+encodeURIComponent('district='+json.district.toLowerCase())+'">'+json.district+'</a>');
		$j('#userLocation').html('<strong>Location:&nbsp;</strong><a href="/service/searchEverything.kickAction?as='+Ka.Info.AFFILIATESITEID+'&mediaType=user&sortType=recent&includeUser=on&d-7095067-p=1&tab=memberSearchResults&meta='+encodeURIComponent('city='+json.city.toLowerCase())+'">'+json.city+'</a>, <a href="/service/searchEverything.kickAction?as='+Ka.Info.AFFILIATESITEID+'&mediaType=user&sortType=recent&includeUser=on&d-7095067-p=1&tab=memberSearchResults&meta='+encodeURIComponent('state='+json.state.toLowerCase())+'">'+json.state+'</a>');
		
	}
}


function showEditorsPicks(){
		$j('#resultsDetailView').html('');
		$j('#resourceEditorsPicks').css('background-position','0 -58px');
		$j('#resourceNewest').css('background-position','-110px 0px');
		$j('#resourceHighestRated').css('background-position','-180px 0px');
		$j('#resourceMostCommented').css('background-position','-280px 0px');
		var url = 'http://clientapps.kickapps.com/scholastic/homePageResource.php?as='+Ka.Info.AFFILIATESITEID+'&divTag=resultsDetailView&sortType=picks';
		aObj = new JSONscriptRequest(url);
		aObj.buildScriptTag();
		aObj.addScriptTag();
}
function showRecent(){
		$j('#resultsDetailView').html('');
		$j('#resourceEditorsPicks').css('background-position','0 0px');
		$j('#resourceNewest').css('background-position','-110px -58px');
		$j('#resourceHighestRated').css('background-position','-180px 0px');
		$j('#resourceMostCommented').css('background-position','-280px 0px');
		var url = 'http://clientapps.kickapps.com/scholastic/homePageResource.php?as='+Ka.Info.AFFILIATESITEID+'&divTag=resultsDetailView&sortType=recent';
		aObj = new JSONscriptRequest(url);
		aObj.buildScriptTag();
		aObj.addScriptTag();
}
function showHighestRating(){
		$j('#resultsDetailView').html('');
		$j('#resourceEditorsPicks').css('background-position','0 0px');
		$j('#resourceNewest').css('background-position','-110px 0px');
		$j('#resourceHighestRated').css('background-position','-180px -58px');
		$j('#resourceMostCommented').css('background-position','-280px 0px');
		var url = 'http://clientapps.kickapps.com/scholastic/homePageResource.php?as='+Ka.Info.AFFILIATESITEID+'&divTag=resultsDetailView&sortType=rating';
		aObj = new JSONscriptRequest(url);
		aObj.buildScriptTag();
		aObj.addScriptTag();
		//$j('#resultsDetailView').html('<p>Sorry, this function is not currently available.  Check back soon!</p><p>Thank you for your patience as we work on enhancements to the Community Web Site (BETA)!</p>');
		
}
function showMostComments(){
		$j('#resultsDetailView').html('');
		$j('#resourceEditorsPicks').css('background-position','0 0px');
		$j('#resourceNewest').css('background-position','-110px 0px');
		$j('#resourceHighestRated').css('background-position','-180px 0px');
		$j('#resourceMostCommented').css('background-position','-280px -58px');
		var url = 'http://clientapps.kickapps.com/scholastic/homePageResource.php?as='+Ka.Info.AFFILIATESITEID+'&divTag=resultsDetailView&sortType=commented';
		aObj = new JSONscriptRequest(url);
		aObj.buildScriptTag();
		aObj.addScriptTag();
		//$j('#resultsDetailView').html('<p>Sorry, this function is not currently available.  Check back soon!</p><p>Thank you for your patience as we work on enhancements to the Community Web Site (BETA)!</p>');
}

function showEditorsPicksGroup(){
		$j('#resultsDetailView').html('');
		$j('#resourceEditorsPick').css('background-position','0 -58px');
		$j('#resourceNewest').css('background-position','-110px 0px');
		$j('#resourceHighestRated').css('background-position','-180px 0px');
		$j('#resourceMostCommented').css('background-position','-280px 0px');
		var url = 'http://clientapps.kickapps.com/scholastic/groupDetailResource.php?as='+Ka.Info.AFFILIATESITEID+'&divTag=resultsDetailView&sortType=picks&groupName='+encodeURIComponent(Ka.Info.CLUBNAME);
		aObj = new JSONscriptRequest(url);
		aObj.buildScriptTag();
		aObj.addScriptTag();
}

function showRecentGroup(){
		$j('#resultsDetailView').html('');
		$j('#resourceEditorsPick').css('background-position','0 0px');
		$j('#resourceNewest').css('background-position','-110px -58px');
		$j('#resourceHighestRated').css('background-position','-180px 0px');
		$j('#resourceMostCommented').css('background-position','-280px 0px');
		var url = 'http://clientapps.kickapps.com/scholastic/groupDetailResource.php?as='+Ka.Info.AFFILIATESITEID+'&divTag=resultsDetailView&sortType=recent&groupName='+encodeURIComponent(Ka.Info.CLUBNAME);
		aObj = new JSONscriptRequest(url);
		aObj.buildScriptTag();
		aObj.addScriptTag();
}
function showHighestRatingGroup(){
		$j('#resultsDetailView').html('');
		$j('#resourceEditorsPick').css('background-position','0 0px');
		$j('#resourceNewest').css('background-position','-110px 0px');
		$j('#resourceHighestRated').css('background-position','-180px -58px');
		$j('#resourceMostCommented').css('background-position','-280px 0px');
		var url = 'http://clientapps.kickapps.com/scholastic/groupDetailResource.php?as='+Ka.Info.AFFILIATESITEID+'&divTag=resultsDetailView&sortType=rating&groupName='+encodeURIComponent(Ka.Info.CLUBNAME);
		aObj = new JSONscriptRequest(url);
		aObj.buildScriptTag();
		aObj.addScriptTag();
		//$j('#resultsDetailView').html('<p>Sorry, this function is not currently available.  Check back soon!</p><p>Thank you for your patience as we work on enhancements to the Community Web Site (BETA)!</p>');
}
function showMostCommentsGroup(){
		$j('#resultsDetailView').html('');
		$j('#resourceEditorsPick').css('background-position','0 0px');
		$j('#resourceNewest').css('background-position','-110px 0px');
		$j('#resourceHighestRated').css('background-position','-180px 0px');
		$j('#resourceMostCommented').css('background-position','-280px -58px');
		var url = 'http://clientapps.kickapps.com/scholastic/groupDetailResource.php?as='+Ka.Info.AFFILIATESITEID+'&divTag=resultsDetailView&sortType=commented&groupName='+encodeURIComponent(Ka.Info.CLUBNAME);
		aObj = new JSONscriptRequest(url);
		aObj.buildScriptTag();
		aObj.addScriptTag();
		//$j('#resultsDetailView').html('<p>Sorry, this function is not currently available.  Check back soon!</p><p>Thank you for your patience as we work on enhancements to the Community Web Site (BETA)!</p>');
}

String.prototype.toTitleCase = function () {
	var A = this.split(' '), B = [];
	for (var i = 0; A[i] !== undefined; i++) {
		B[B.length] = A[i].substr(0, 1).toUpperCase() + A[i].substr(1);
	}
	return B.join(' ');
}


