﻿
$(function() {
	initLabels();
	initMaps();
});

$(function() {
   $('a.lightbox').lightBox({
	overlayBgColor: '#000',
	overlayOpacity: 0.6,
	fixedNavigation: true,
	imageLoading: '/images/lightbox/lightbox-ico-loading.gif',
	imageBtnClose: '/images/lightbox/lightbox-btn-close.gif',
	imageBtnPrev: '/images/lightbox/lightbox-btn-prev.gif',
	imageBtnNext: '/images/lightbox/lightbox-btn-next.gif',
	imageBlank: '/images/lightbox/lightbox-blank.gif',
	containerResizeSpeed: 350,
	txtImage: 'Image',
	txtOf: 'of'
   });
});



/* labels
===================================================================================== */
function initLabels() {
	$(".moveLbls label").hide();

	$(".moveLbls input:text").each(function() {
		setText($(this));
		$(this).focus(function() { clearText($(this)); });
		$(this).blur(function() { setText($(this)); });
	});
	
	$(".moveLbls select").each(function() {
		setList($(this));
	});
	
	$(".moveLbls input:submit,.moveLbls input:image").click(function() {
		$(".moveLbls input:text").each(function() { clearText($(this)); });
	});
	
	
	function setText(txtBox) {
		if ($(txtBox).val() == '') $(txtBox).val($(txtBox).siblings("label").text());
	}
	function clearText(txtBox) {
		var val = $(txtBox).siblings("label").text();
		if ($(txtBox).val() == val) $(txtBox).attr('value', '');
	}

	function setList(list) {
		var val = $(list).siblings("label").text();
		if ($(list).find("option:first").val() != val) $(list).find("option:first").text(val);
	}
}

/* External Links and PDF Links
===================================================================================== */
function initExternalLinks() {
	var h = window.location.host.toLowerCase();
	$("a[@href^='http']:not([@href^='http://" + h + "']):not([@href^='http://www." + h + "']), a[@href$='.pdf']").attr("target", "_blank");
}


/* Googlemaps
===================================================================================== */
function initMaps() {
   if (!$("div.map")[0]) return;

  var icon = createPushpin();
	  
  if (GBrowserIsCompatible()) {
	$("div.map").each( function() {
	  var pos = new GLatLng($(this).attr("lat"), $(this).attr("lng"));
	  var map = new GMap2(document.getElementById($(this).attr("id")));
	  map.setCenter(pos, 13);
	  map.addControl(new GLargeMapControl());
	  map.addControl(new GMapTypeControl());
	  map.addOverlay(new GMarker(pos, icon));
	});
  }
  
  $("body").unload(GUnload);
}


var markers = new Array();
var map;
var info = new Array();
function initMap() {
   if (!$("#mapWrap")[0]) return;
    
  var i = 0;
  markers = new Array();
  $("ul.mapData li").each( function(i) {
        var ico = createPushpin();
        var m = new GMarker(new GLatLng($(this).attr("lat"), $(this).attr("lng")), ico);
        m.id = i;
        markers.push(m);
   
			var html = createInfoWin($(this).html());
			info.push(html);
        
    });
    
	  setTimeout("createMap();",1000);
	  $("ul.mapData").remove();
}

function createMap(){
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("mapWrap"));
        map.addControl(new GLargeMapControl());
        map.clearOverlays();
        
//        if (markers.length > 0) {
//			map.setCenter(new GLatLng(markers[0].getPoint().lat(), markers[0].getPoint().lng()), 6);
//		} else {
			map.setCenter(new GLatLng("53.956086", "-2.768555"), 5);
//        }
        for (i=0;i<markers.length;i++){
          map.addOverlay(markers[i]);
          GEvent.addListener(markers[i], "click", function() {
            this.openInfoWindowHtml(info[this.id]);
		});
        }        
       //zoomToMarkers();
    }
}
function createPushpin(){
  
  var icon = new GIcon();
  icon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
  icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
  icon.iconSize = new GSize(12, 20);
  icon.shadowSize = new GSize(22, 20);
  icon.iconAnchor = new GPoint(6, 20);
    
    icon.infoWindowAnchor = new GPoint(9,2);
    icon.infoShadowAnchor = new GPoint(18,25);
    
    return icon;    
}

function createInfoWin(tooltip){
    var ret;
        
    ret = '<div class="infowin">';
    ret += tooltip    
    ret += '</div>';

    return ret;
}
function zoomToMarkers(){
    var count = 0;
    var thePoint, x, y, minX, maxX, minY, maxY, span;
    
    for (i = 0; i < markers.length; i++) {
        marker = markers[i];
        
        thePoint = marker.getPoint();
        x = thePoint.lat(); y = thePoint.lng();
        if (count == 0)
        {
	        minX = x; maxX = x; minY = y; maxY = y;
        }
        else
        {
	        if (x < minX) minX = x;
	        if (x > maxX) maxX = x;
	        if (y < minY) minY = y;
	        if (y > maxY) maxY = y;
        }
        count++;
    }
    
    if (count == 1)
        map.setCenter(new GLatLng(x,y), 12);
    else if (count > 1)
    {
        var center = new GLatLng((minX + maxX) / 2, (parseFloat(minY) + parseFloat(maxY)) / 2);
        span = new GSize(Math.abs(maxX - minX), Math.abs(maxY - minY));
        slopWid = 0;
        slopHgt = 0;
        deltaHgt = 0;
        var bounds = new GLatLngBounds(new GLatLng(minX-slopHgt, minY-slopWid), new GLatLng(maxX+slopHgt, maxY+slopWid)); // sw, ne
        var zoom = map.getBoundsZoomLevel(bounds);
        
        map.setCenter(center, zoom);
    }
}
