﻿// 
//
//  SSR Main Javascript file

var SSR = {
  init: function() {

    if ($.browser.opera) { // fix buttons layout in Opera browser
      $('button').addClass('opera_fix');
    }

    $('.popup').click(function(e) { // open links in a new window the class of 'popup'
      window.open(this.href, "_blank");
      return false;
    });

    $('.popclose').click(function(e) { // close a window on click of a link with 'popclose' as the class
      window.close();
      return false;
    });

    $('.expando').each(
		function() {
		  SSR.createPanels(this);
		});

    SSR.itineraryPreview();
    SSR.setPreviewHeight();
    SSR.previewExpand();
  }
}


SSR.createPanels = function(element) {
  var panel = element, titlebar = $('.titlebar', element).get(0), viewport = $('.viewport', element).get(0);
  var cookie_name = $(element).attr('id');
  if ($.cookie(cookie_name) != 'expando') {
    $(element).addClass('closed');
  }


  $(titlebar).click(function() {
    $(viewport).toggle();
    $(element).toggleClass('closed');
    //setting cookie
    $cookie_value = $(element).attr('class');
    $.cookie(cookie_name, $cookie_value, { expires: 7, path: '/' })
  });
}


SSR.itineraryPreview = function() {
  $('#itineraryMap').each(function() {
    if (GBrowserIsCompatible()) {
      var map = new GMap2(this);
      map.setCenter(new GLatLng(1.5, 17.5), 1);
      map.enableContinuousZoom();
      // map.enableScrollWheelZoom();
      map.addControl(new GSmallMapControl());
      map.addMapType(G_PHYSICAL_MAP);
      map.setMapType(G_HYBRID_MAP);
      map.addControl(new GHierarchicalMapTypeControl());
      var markers = new Array();
      var days = $('div.itineraryDay');
      days.each(function(index) {
        var numberedIcon = new GIcon(ewIconSmall);
        numberedIcon.iconSize = new GSize(30, 34);
        numberedIcon.shadowSize = new GSize(48, 34);
        numberedIcon.iconAnchor = new GPoint(15, 34);
        numberedIcon.imageMap = [1, 1, 30, 1, 30, 34, 1, 34];
        numberedIcon.infoWindowAnchor = new GPoint(15, 1);
        numberedIcon.image = '../../images/ItinMarker' + (index + 1) + 'sm.png';
        numberedIcon.shadow = '../../images/ItinMarker-sh-sm.png';
        numberedIcon.transparent = '../../images/ItinMarker-tr-sm.png';
        var lat = $('p.latlng', this).text().split(',')[0];
        var lng = $('p.latlng', this).text().split(',')[1];
        var point = new GLatLng(lat, lng);
        var name = jQuery.trim($('h3', this).text());
        var text = $('p:not(.latlng)', this).get(0);
        // if (index == 0) map.setCenter(point, 10);
        var marker = new GMarker(point, { icon: numberedIcon, title: name });
        /*
        GEvent.addListener(marker, "click", function() {
        if ($(text).text().length > 0) {
        marker.openInfoWindowHtml('hello', { maxWidth: '300px' });
        }
        });
        */
        markers.push(marker);
        map.addOverlay(marker);
      });
      centreOnMarkers(map, markers);
    }
  });
}

SSR.setPreviewHeight = function() {
  if ($('#head_content').get(0) != null) {
    var mainHeight = $('#head_content').height();
    if ($.browser.mozilla) {
      $('#quick_ship_itineraries').height(mainHeight + 14);
    }
    else {
      $('#quick_ship_itineraries').height(mainHeight + 15);
    }
  }
}

SSR.previewExpand = function() {
  //show|hide preview page long description for header
  $('#head_ship_describe .arrow_down').click(function() {
    $('#head_ship_describe .expand').toggle();
    SSR.setPreviewHeight();
    return false;
  });

  //show|hide preview page long description for amenities
  $('.amenities_description .expand').hide();
  $('.amenities_description .arrow_down').click(function() {
    $(this).next().eq(0).toggle();
    return false;
  });
}

$(function() { // equivalent to $(document).ready(...)
  SSR.init();
});

$(function() {
	if ($('div#galleryInFlash').get(0) != null) {

		var server = 'http://iis6.altsys.co.uk';
		if (window.location.hostname.toLowerCase().indexOf("ewaterways1") != -1)
			server = "http://ssr.ewaterways1.com";
		else if (window.location.hostname.toLowerCase().indexOf("altsys") != -1)
			server = "http://iis6.altsys.co.uk";
		else if (window.location.hostname.toLowerCase().indexOf("ewaterways") != -1)
			server = "http://ssr.ewaterways.com";

		var fileName = server + $('#galleryFileName').val();
		swfobject.embedSWF(server+"/Content/flashGallery/gallery.swf?xmlFile=" + fileName, "galleryInFlash", "960", "270", "8", false);
	} // Load flash on preview gallery page
});

// Maps
if (typeof (GBrowserIsCompatible) != 'undefined' && GBrowserIsCompatible()) {
  // Standard icon
  var ewIcon = new GIcon(G_DEFAULT_ICON);
  ewIcon.iconSize = new GSize(39, 51);
  ewIcon.shadowSize = new GSize(65, 51);
  ewIcon.iconAnchor = new GPoint(19, 51);
  ewIcon.imageMap = [0, 0, 39, 0, 39, 51, 0, 51];
  ewIcon.infoWindowAnchor = new GPoint(19, 1);
  ewIcon.image = '../../images/ew-marker.png';
  ewIcon.shadow = '../../images/ew-marker-sh.png';
  ewIcon.transparent = '../../images/ew-marker-tr.png';

  var ewIconSmall = new GIcon(G_DEFAULT_ICON);
  ewIconSmall.iconSize = new GSize(26, 34);
  ewIconSmall.shadowSize = new GSize(48, 34);
  ewIconSmall.iconAnchor = new GPoint(13, 34);
  ewIconSmall.imageMap = [1, 1, 26, 1, 26, 34, 1, 34];
  ewIconSmall.infoWindowAnchor = new GPoint(13, 1);
  ewIconSmall.image = '../../images/ew-marker-sm.png';
  ewIconSmall.shadow = '../../images/ew-marker-sh-sm.png';
  ewIconSmall.transparent = '../../images/ew-marker-tr-sm.png';
  window.onunload = function() { GUnload(); };
  var map; // Global map variable
} 

// Centres the map on the markers in arrMarkers and zooms in as close as possible
// while still showing all the markers.
function centreOnMarkers(map, arrMarkers) {
  var north = -90;
  var south = 90;
  var east = -180;
  var west = 180;
  if (arrMarkers.length > 0) {
    for (m in arrMarkers) // Loop through array of markers
    {
      // Check they have lat and long and are not hidden
      if (arrMarkers[m].getLatLng && !arrMarkers[m].isHidden()) {
        // Get most southerly point
        if (arrMarkers[m].getLatLng().lat() < south) {
          south = arrMarkers[m].getLatLng().lat();
        }
        // Get most northernly point
        if (arrMarkers[m].getLatLng().lat() > north) {
          north = arrMarkers[m].getLatLng().lat();
        }
        // Get most westernly point
        if (arrMarkers[m].getLatLng().lng() < west) {
          west = parseFloat(arrMarkers[m].getLatLng().lng());
        }
        // Get most easternly point
        if (arrMarkers[m].getLatLng().lng() > east) {
          east = parseFloat(arrMarkers[m].getLatLng().lng());
        }
      }
    }
    // Create top right boundary point
    var sw = new GLatLng(south, west);
    // Create bottom left boundary point
    var ne = new GLatLng(north, east);
    // Get the centre point of the boundary
    var centre = new GLatLng(((north + south) / 2), ((east + west) / 2));
    // Get the highest zoom level possible while still showing all markers
    var bounds = new GLatLngBounds(sw, ne);
    var zoom = map.getBoundsZoomLevel(bounds);
    if (zoom <= 1) {
      // Show whole world
      map.setCenter(new GLatLng(1.5, 17.5), 1);
    }
    else {
      // Centre on markers
      map.setCenter(centre, zoom - 1);
    }
  }
  else {
    // Show whole world
    map.setCenter(new GLatLng(1.5, 17.5), 1);
  }
}