<?php
if(!defined('IN_MYBB')){die();}

function umap_info(){
	return array(
		'name'			=> 'XThreads Usermap Addons',
		'description'	=> 'Additional features for XThreads Usermap',
		'website'		=> 'http://mybbhacks.zingaburga.com',
		'author'		=> 'XThreads Mania',
		'authorsite'	=> 'http://mybbhacks.zingaburga.com',
		'version'		=> '1.0',
		'compatibility' => '*',
		'guid'        	=> ''
	);
}

function umap_activate(){
	$GLOBALS['db']->insert_query('templates', array(
		'title'		=> 'umap_profile',
		'template'	=> $GLOBALS['db']->escape_string('<br />
<table border="0" cellspacing="{$theme[\'borderwidth\']}" cellpadding="{$theme[\'tablespace\']}" class="tborder">
	<tr><td class="thead"><strong>My Location</strong></td></tr>
	<tr>
		<td class="trow1" style="text-align: center;">
			<div id="umap_profile" style="width: 100%; height: 200px;"></div>
		</td>
	</tr>
</table>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
	var umap_profile;
	function initialize() {
		var myLatlng = new google.maps.LatLng{$umap_prof_loc};
		umap_profile = new google.maps.Map(document.getElementById(\'umap_profile\'), {
			zoom: 14,
			center: myLatlng,
			disableDefaultUI: true,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		});
		var marker = new google.maps.Marker({
			position: myLatlng, 
			map: umap_profile,
			title: \'Click to go to the discussion page\'
		});
		google.maps.event.addListener(marker, \'click\', function() {
			window.location=\'{$umap_prof_id}\';
		});
	}
	google.maps.event.addDomListener(window, \'load\', initialize);
</script>'),
		'sid'		=> -1
	));
	require_once MYBB_ROOT.'/inc/adminfunctions_templates.php';
	find_replace_templatesets('member_profile', '#\{\$signature\}#', '{$signature}{$umap_profile}');
}

function umap_deactivate(){
	$GLOBALS['db']->delete_query('templates', 'title="umap_profile"');
	require_once MYBB_ROOT.'/inc/adminfunctions_templates.php';
	find_replace_templatesets('member_profile', '#\{\$umap_profile\}#', '', 0);
}

$plugins->add_hook('newthread_start', 'umap_nt');
$plugins->add_hook('newthread_do_newthread_start', 'umap_nt');
function umap_nt(){
	$umap_fid = 0;
	if($umap_fid != 0){
		if($GLOBALS['mybb']->input['fid'] == intval($umap_fid)){
			$ctid = $GLOBALS['db']->fetch_field($GLOBALS['db']->simple_select('threads','tid','fid='.intval($umap_fid).' AND uid='.intval($GLOBALS['mybb']->user['uid']).' AND visible=1', array('limit'=>1)),'tid');
			if($ctid){
				error('You already have a pinned in the Usermap. Please <a href="'.get_thread_link(intval($ctid)).'">click here</a> to manage your pin.');
			} 
		}
	}
}

$plugins->add_hook('global_start', 'umap_tc');
function umap_tc(){
	if(THIS_SCRIPT == 'member.php'){
		if(isset($GLOBALS['templatelist'])){
			$GLOBALS['templatelist'] .= ',umap_profile';
		}
	}
}

$plugins->add_hook('member_profile_end', 'umap_run');
function umap_run(){
	$umap_fid = 0;
	if($umap_fid != 0){
		$fp = forum_permissions();
		$fperm = $fp[$umap_fid];
		if($fperm['canview'] == 1){
			global $db, $memprofile, $theme, $umap_profile;
			$query = $db->query('
				SELECT tf.tid,tf.umap_geol
				FROM '.TABLE_PREFIX.'threads t
				LEFT JOIN '.TABLE_PREFIX.'threadfields_data tf ON(tf.tid=t.tid)
				WHERE t.uid='.intval($memprofile['uid']).' AND t.fid='.intval($umap_fid).'
				LIMIT 0,1
			');
			if($db->num_rows($query)){
				$umap_prof = $db->fetch_array($query);
				$umap_prof_id = get_thread_link(intval($umap_prof['tid']));
				$umap_prof_loc = htmlspecialchars_uni($umap_prof['umap_geol']);
				eval('$umap_profile = "'.$GLOBALS['templates']->get('umap_profile').'";');
			}
		}
	}
}
?>