﻿var addressObj = null;

$jQ( function() {


	//2009-09-23-IF Tweak for merging seperate locations to single dot
	try {

		$jQ("#zurich > A",$jQ("#world")).attr("onclick","").click( function() {toggleAddresses('switzerland',0);return false;});
		$jQ("#zurich",$jQ("#world")).attr("id","switzerland").attr("title","Geneva,Zurich");
		$jQ("#geneva",$jQ("#world")).remove(); //remove so we dont get overlay dots

		$jQ("#city-geneva > A",$jQ("#citylist")).attr("onclick","").click( function() {toggleAddresses('switzerland',0);return false;});
		$jQ("#city-zurich > A",$jQ("#citylist")).attr("onclick","").click( function() {toggleAddresses('switzerland',1);return false;});

	} catch(e) {;}



	try{
	var qs = new QueryString();
	qs.Read();
	var region = qs.GetValue("region");
	var panel = qs.GetValue("panel");


	if( region != null)
	{
		toggleAddresses(region, panel);
	}
	} catch(e) {;}




});

function toggleAddresses(region, panel) 
{

    var defpanel = 0;
    if( panel != undefined )
    {
    	defpanel = parseInt(panel);
    }


    //2009-09-23-IF Tweak for merging seperate locations to single dot
    //combine multiple city definitions - like used for switzerland
    //This will need tidying up into a hash to make more generic
    if( region == 'switzerland')
    {
	try {
		var mergemaster = "geneva";
		var mergeslave = "zurich";
		var $orig = $jQ("#addressBank-" + mergemaster);
		var $old = $jQ("#addressBank-" + mergeslave);
		if( $old.length > 0)
		{
			$orig[0].id = "addressBank-" + region;
			$origtabs = $jQ("#tabBank-" + mergemaster, $orig[0] ).attr("id","tabBank-" + region);
			var $banks = $jQ("#addressBank-" + mergeslave);
			for(i=0;i<$banks.length;i++)
			{
				$banks[i].id = "oldaddressBank-" + mergeslave;
				$origtabs.append( $jQ("#tabBank-" + mergeslave + " > LI",$banks[i]) );
				$orig.append( $jQ( ".addressPanel", $banks[i]) );
			}
			$orig[0].id = "addressBank-" + region;
		}


	} catch(e) {;}

    }

  
    if( addressObj != null ) { closeAddress(null); addressObj = null; }

    try {

	$bank = $jQ("#addressBank-" + region);
        $bank.show();

        $jQ("#addressBank-" + region).bind('tabsshow', function(event, ui) {
		addressOpen( jQuery("#" + ui.panel.id) );

	});

        var $tabs = $jQ("#addressBank-" + region).tabs();
	if( $jQ("#maptabclose-" + region).length < 1)
	{
		var cl = $jQ($tabs).prepend("<div class='maptabclose' id='maptabclose-" + region + "'></div>");
		$jQ("#maptabclose-" + region).bind("click", function() {$jQ("#addressBank-" + region).hide();});
	}
	
	$tabs.tabs('select', defpanel);
	
	addressObj = $tabs;


    } catch (e) { alert("We are unable to supply an address at the moment: " + region); }
    
}

function addressOpen(panel)
{
	panelid = panel.attr("id");
	var id = panelid.substr(panelid.lastIndexOf("-")+1);

	if( GMapData["id"+id] )
	{	
		if( GMapData["id"+id][3] == false ) 
		{
			loadGMap(GMapData["id"+id],id);
			GMapData["id"+id][3] = true;
		} else {

		}

	} else {
		if( YMapUrl["id"+id] )
		{
			if( YMapUrl["id"+id][2] == false)
			{
				loadYMap( YMapUrl["id"+id], id );
				YMapUrl["id"+id][2] = true;
			}
		}

	}
}

function closeAddress(obj)
{
    if( addressObj != null )
    {   
        addressObj.hide();
    } else {
        try {
            $jQ(obj).parent().parent().hide();
            } catch(e) {}
    }

}

function loadYMap(yid,cid)
{
	$jQ("#mapimg-"+cid).attr("src",yid[0]);

}


var map = null;
var pt = null;
function loadGMap(gid,cid) //glatlng, map containerid
{


		if (GBrowserIsCompatible()) {        
			map = new GMap2(document.getElementById("mapele-" + cid));        
			map.disableDragging();
			//alert(gid[0]+' '+gid[1]+' '+gid[2]);
			pt = new GLatLng(gid[0],gid[1]);
			map.setCenter(pt, gid[2]);      



			GEvent.addListener(map,"click", function(marker,point) { 
											var ll = map.getCenter(); 
											var z = map.getZoom();
											var w = window.open("http://maps.google.com/maps?ie=UTF&hl=en&q=&ll=" + ll.toUrlValue() + "&z=" + z);
										} );

			//map.addOverlay(new GMarker(pt));
		        //window.setTimeout(setMarker, 100);
			setMarker();

		}
}	


function setMarker()
{

			var icon = null;
          		icon = new GIcon();
 		        icon.image = "/skins/images/" + "smarker1.png";
          		//icon.shadow = "/skins/images/" + "shadow-smarker1.png";
		        icon.iconSize = new GSize(25.0, 35.0);
		        //icon.shadowSize = new GSize(43.0, 35.0);
		        icon.iconAnchor = new GPoint(8.0, 34.0);
	                //icon.transparent = "/skins/images/" + "smarker1.png";
		        //icon.infoWindowAnchor = new GPoint(12.0, 17.0);

			//map.checkResize();
			//var marker = new BpMarkerLight(pt,{icon: icon});
			//var marker = new RMarker(pt);
			//marker.setIcon(icon);
			//marker.setOpacity(30);
			//map.addOverlay(marker); //new GMarker(pt, icon)
			map.addOverlay(new GMarker(pt, icon)); //

}




var YMapUrl = {
			id00: ["/default.img?yapi=yappid_img&latitude=51.5121124&longitude=-0.089338&image_width=190&image_height=215&zoom=3",,false]
		};

var GMapData = { 



			id1001: [0.000000,0.000000,15, false ], //
			id1002: [0.000000,0.000000,16,false] //

		};

function RMarker(pt) 
{  
}

RMarker.prototype = new GOverlay();

RMarker.prototype.initialize = function(map) {  
// Create the DIV representing our 
var img = document.createElement("img");  
img.src = "/skins/images/smarker1.gif";
// Our rectangle is flat against the map, so we add our selves to the  
// MAP_PANE pane, which is at the same z-index as the map itself (i.e.,  
// below the marker shadows)  
map.getPane(G_MAP_MARKER_PANE).appendChild(img);  
this.map_ = map;  
this.img_ = img;
}
RMarker.prototype.remove = function() 
{  
this.img_.parentNode.removeChild(this.img_);
}

RMarker.prototype.copy = function() 
{  
}

RMarker.prototype.redraw = function(force) 
{  
}

