/*
	6/2/10 holzinger
	removed from jabout template and put into separate file after becoming too unwieldy to edit in
	the wiki template. Ugh. 

*/


var RFTWabout = new RFTWdekiInit();
var getAboutData = {};
getAboutData.team_name_link = RFTWabout.getUrlRev()[0];

$wpjQ(document).ready(function(){
	$wpjQ(".modified").empty();
	$wpjQ("#about").append("<div id='rftwmsg'></div>");
	var rcData={};
	rcData.pg = window.location.pathname.replace(/\//g,'!');
	rcData.pg=rcData.pg.replace(/^!/,'');
	if (Deki.UserName != "Anonymous"){
		rcData.u =Deki.UserName;
	}
	$wpjQ.getJSON("http://"+ RFTWabout.jsonurl + "/wp-content/themes/rftw/json.php?recentchanges&callback=?",rcData,
			function(data){
			make_ticker(data);			
			});
	$wpjQ.getJSON("http://"+ RFTWabout.jsonurl + "/wp-content/themes/rftw/json.php?seasons&callback=?",{},
			function(data){
				RFTWabout.objSeasons=data;
				var lt = getLeagueType();
				getAboutData.season=RFTWabout.objSeasons[lt][0];
				$wpjQ.getJSON("http://" + RFTWabout.jsonurl + "/wp-content/themes/rftw/json.php?team_info&callback=?",getAboutData,function(data){ processAbout(data); });
	});

});
function getLeagueType(){
	// need league type to get the right season, get it from the url 
	// and THEN figure out the current season... 
	var leeg=RFTWabout.getUrlRev()[2];
	for (var j in RFTWabout.pagelinks){
		if (RFTWabout.pagelinks[j] == leeg){
			return j;
		}
	}
	return RFTWabout.lt;
}
function processAbout(data) {
		
		var about = data.teamabout[0];
		var websiteStr = about.website.substr(0,20) + "...";
		$wpjQ("#about #address").html('<a href="http://maps.google.com?q=' + escape(about.team_address).replace(/ /g,"+") + '" title="Google Maps">' + about.team_address + '</a>');
		$wpjQ("#about #teamWeb").html("<a href='"+about.website+"'>"+websiteStr+"</a>");
		$wpjQ("#about #contactInfo").html(about.contact_info);
		$wpjQ("#about #league").html(about.league_name);
		if(about.team_address) {
			makeMap(about.team_address);
		}
}
function makeMap(address){
    var map = new GMap2(document.getElementById("googleMap"));
    var geocoder = new GClientGeocoder();
    $wpjQ("body").unload(GUnload());
    
    showAddress(address);
    function showAddress(address) {
      geocoder.getLatLng(
        address,
        function(point) {
          if (!point) {
            $wpjQ("#googleMap").html('<em>Address not found</em>');
          } else {
            map.setCenter(point, 14);
            var marker = new GMarker(point);
            map.addOverlay(marker);
          }
        }
      );
    }
}
function make_ticker(rows){
	var output ="";
	var watched = false;

	for (var j in rows.pages){
		output += "Page updated " + rows.pages[j].changetime + " by " + rows.pages[j].rc_user_text + ", " + rows.pages[j].rc_comment + ".&nbsp;&nbsp; ";
	}
	for (j in rows.results){
		output += "Results updated " + rows.results[j].changedate + " for "  + rows.results[j].meet_type.toLowerCase() + " against <a href='http://wiki.reachforthewall.com/Results_Statistics/Meet_Results?meetId=" + rows.results[j].meet_id +"'>" + rows.results[j].opponent + "</a> on " + rows.results[j].meetdate +  ".&nbsp;&nbsp;";
	}
	if (output.length < 5){
			output = " No changes in the last 14 days ";
	}
	if (rows.watchedpages){
		for (j in rows.watchedpages){
			if ( rows.watchedpages[j].page == window.location.pathname.replace(/^\//,'') ){
				watched = true;
			}
		}
	}
	var action;
	var btnlabel;
	if (Deki.UserName == 'Anonymous'){
		btnlabel = 'Log in to follow this team';
		action = "login";
	}
	else if (watched){
		btnlabel = 'Stop following this team';
		action="unfollow";
	}	
	else{
		btnlabel = 'Follow this team';
		action="follow";
	}
//	$wpjQ(".modified").css("display","");
	$wpjQ(".modified").html("<span id='rftwticker'></span><button id='rftwfollow' style='float:left;margin-left:10px;'>" + btnlabel + "</button><br>");
	$wpjQ("#rftwticker").attr("behavior","scroll").attr("direction","left").attr("scrollamount","2").attr("width","500");
	$wpjQ("#rftwticker").html(output);
	$wpjQ("#rftwticker").marquee('rftwscroller');
	$wpjQ("#rftwfollow").click(function(){
		follow_button_pressed(action);
	});

}
function follow_button_pressed(action){
	switch(action){
		case "follow":
			$wpjQ.get("http://wiki.reachforthewall.com/Special:FollowAjax",{id: Deki.PageId,status:1},function(data){
				if (data.result == "success"){
					$wpjQ("#rftwfollow").text("Stop following this team");
					$wpjQ("#rftwfollow").unbind('click');
					$wpjQ("#rftwfollow").click(function(){
						follow_button_pressed("unfollow");
					});
					showmessage("You're now following this team");
				}
			},"json");
		break;
		case "login":
			// simply redirect ... 	Deki.BaseHref	
			window.location = Deki.BaseHref + '/index.php?title=Special:Userlogin&returntotitle='+ Deki.PageTitle.replace(/ /g,'+')
		break;
		case "unfollow":
			$wpjQ.get("http://wiki.reachforthewall.com/Special:FollowAjax",{id: Deki.PageId,status:0},function(data){
				if (data.result == "success"){			
					$wpjQ("#rftwfollow").text("Follow this team");
					$wpjQ("#rftwfollow").unbind('click');
					$wpjQ("#rftwfollow").click(function(){
						follow_button_pressed("follow");
					});
					showmessage("You've stopped following this team");
				}
			},"json");
		break;
	}

}



