
/* Queue the Site Load Events
===================================================================================== */

$(function() {
	initMenu();
	initHomeAnim();
	initBookmark();
	initPrint();
	initMaps();
	initMap();
	initForm();
	initPopUp();
});

function initPopUp() {
  $(".panel a.close").click( function() {
	$(this).parent().parent().hide();
	return false;
  });

}

function initForm() {

  // hide multiple fieldsets
  $(".bespokeForm fieldset").find("fieldset:gt(1)").each(function() {	
	$(this).slideUp(100);
	var parent  = $(this).parent();
	// add button
	if (!$(parent).find("p.expand")[0]) {
	  if ($(parent).find("legend:contains(Education)")[0]) {
		$(parent).append('<p class="expand">Add another place of study</p>');	
	  } else if ($(parent).find("legend:contains(Employment)")[0]) {	
		$(parent).append('<p class="expand">Add another employer</p>');
	  } else {	  
		$(parent).append('<p class="expand">Add another</p>');	
	  }
	}		
  });
  
  $(".bespokeForm p.expand").click(function() {
	$(this).parent().find("fieldset:hidden:first").slideDown(400);
    
	// remove button
	if (!$(this).parent().find("fieldset:hidden")[0]) {
	  $(this).fadeOut(400);
	}
  });
  
  // add required *
  $(".bespokeForm label[@title='(required)']").append(" <strong>*</strong>");
}


function initBookmark() { 
  $(".contentNormal, .contentWide, .contentWider").append('<h6 class="bookmark">Bookmark this page</h6>');
  $("h6.bookmark").click( function() { 
	var url = document.location;
	var title = "LifeWays";
	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	} else if( window.external ) { // IE Favorite
		window.external.AddFavorite( url, title);
	}
  });
  
}
function initPrint() { 
  $(".contentNormal, .contentWide, .contentWider").append('<h6 class="print">Print this page</h6>');
  $("h6.print").click( function() { window.print(); });  
}

//function initMenu() {
//	$(".nav>ul>li.parent>a").click (
//		function() {
//		
//			if ($(this).next("ul:hidden")[0]) {
//				// send to page if no sub menu
//				if (!$(this).next("ul")[0]) {
//					return true;				
//				}
//				
//				// hide others
//				$(".nav ul>li").removeClass("current");
//				$(".nav ul>li>ul").hide();
//				
//				$(this).parent().addClass("current");
//				$(this).next("ul").slideDown(400, "easeIn");
//				
//			
//			} else {
//				$(this).parent().removeClass("current");
//				$(this).next("ul").slideUp(300, "easeOut");
//			}
//			
//			return false;
//		}
//	);
//}

function initMenu() {
	// bind hide click
	$("div.wrap *:not(.nav a)").bind("click", function() {
	  if ($(this).parent(".nav")[0]) return;
	  if ($(this).is("a")) return true;
	  //alert(this);
	  hideAll();
	});
  
	$(".nav>ul>li.parent>a").click (function() {
	
		  if (!$(this).next("ul")[0]) return true;
		  
		  //alert($(this).next("ul:visible")[0])
		  //alert($(this).next("ul").is(":visible"))
		  if ($(this).next("ul").is(":visible")) {
			$(this).parent().removeClass("current");
			$(this).next("ul:visible").slideUp(200, "easeIn");
		  } else {
			hideAll();
			$(this).parent().addClass("current");
			$(this).next("ul:hidden").slideDown(200, "easeIn");
		  }
		  return false;
	});
	
	function hideAll() {
		$(".nav ul>li>ul:visible").hide().parent().removeClass("current");
	}
}
 
 
 
/* Googlemaps
===================================================================================== */
function initMaps() {
   if (!$("div.location div.map")[0]) return;

  var icon = createPushpin();
	  
  if (GBrowserIsCompatible()) {
	$("div.location 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);
    }
}


    
        
/* Home animation
===================================================================================== */
// Nb speeds are actually durations of transitions
var pause = 4000;
var speedOut = 3000;
var speedIn = 5000;

function initHomeAnim() {
	if (!$(".anim")[0]) return;
	
	$(".anim div:first").fadeIn(speedIn);
	$(".anim div").show();
	$(".anim div:not(:first)").css( { opacity: 0 } );
	$(".anim div:first").addClass("cur");
	
	// wait... then call anim
	$(".anim div:first").animate( { opacity: 1 }, pause, "swing", anim );		
}

function anim() {
	var n, c;	
	
	// find next (either next, or first)
	c = $(".anim div.cur");
	n = ( $(c).next("div")[0] ? $(c).next("div") : $(".anim div:first") );
	
	// wait...
	$(n).animate( { opacity: 1 }, pause );
	
	// fade current out		
	$(c).animate( { opacity: 0 }, speedOut, "easeOutQuad" );
	
	//fade next in
	$(n).animate( { opacity: 1 }, speedIn, "easeInQuad",
		function() {			
			$(n).addClass("cur");
			$(c).removeClass("cur");
			anim();
		}
	);		
}
 