// preload menu images
var preLoadMenuPics = new Array('aboutus.png','aboutus-over.png','docks.png','docks-over.png','boatlifts.png','boatlifts-over.png','stairselevators.png','stairselevators-over.png','marina.png','marina-over.png','rampsrailings.png','rampsrailings-over.png','gangwaysbridges.png','gangwaysbridges-over.png','waterfront.png','waterfront-over.png','boathouses.png','boathouses-over.png','guide.png','guide-over.png');
var preLoad = new Array();
for (i in preLoadMenuPics)
{
  preLoad[i] = new Image();
  preLoad[i].src = 'menu/'+preLoadMenuPics[i];
}

// pre-load the control button slideshows
var preLoadButtonPics = new Array('slideshow-thumb-prev.png','slideshow-thumb-next.png','slideshow-thumb.png','slideshow-prev.png','slideshow-pause.png','slideshow-next.png','slideshow-play.png');
var preLoadButtons = new Array();
for (i in preLoadButtonPics)
{
  preLoadButtons[i] = new Image();
  preLoadButtons[i].src = 'images/'+preLoadButtonPics[i];
}

// js for IE, modified from drupal nice_menus module

// We need to do some browser sniffing to weed out IE
if (document.all && !window.opera && $.browser.msie) {
  function IEHoverPseudo() {
      $("ul.nice-menu li.menuparent").hover(function(){
          $(this).addClass("over").find("> ul").show().addShim();
        },function(){
          $(this).removeClass("over").find("> ul").removeShim().hide();
        }
      );
      // Add a hover class to all li for CSS styling. Silly naming is done
      // so we don't break CSS compatibility for .over class already in use
      // and due to the fact that IE6 doesn't understand multiple selectors.
      $("ul.nice-menu li").hover(function(){
          $(this).addClass("ie-over");
        },function(){
          $(this).removeClass("ie-over");
        }
      );
    }
}

$.fn.addShim = function() {
  return this.each(function(){
	  if(document.all && $("select").size() > 0) {
	    var ifShim = document.createElement('iframe');
	    ifShim.src = "javascript:false";
			ifShim.style.width=$(this).width()+1+"px";
      ifShim.style.height=$(this).find("> li").size()*23+20+"px";
			ifShim.style.filter="progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)";
		  ifShim.style.zIndex="0";
    $(this).prepend(ifShim);
      $(this).css("zIndex","99");
		}
	});
};

$.fn.removeShim = function() {
  return this.each(function(){
	  if (document.all) $("iframe", this).remove();
	});
};

/**
 * scripts for making slideshows work
 */

slideshowPaused = false;

function slideshowSwitch() {
	if (slideshowPaused == true) { return; }
	var $active = $('#slideshow-picture div.active');
	
	if ( $active.length == 0 ) $active = $('#slideshow-picture div:last');
	
	var $next = $active.next().length ? $active.next()
      : $('#slideshow-picture div:first');
    
    $active.addClass('last-active');

	$next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });	
		
	var slideshowTitle = $('#slideshow-picture div.active img').attr("alt");
  	$('#slideshow-title').html(slideshowTitle);
}

function slideshowNext() {
  var $active = $('#slideshow-picture div.active');
  if ( $active.length == 0 ) $active = $('#slideshow-picture div:last');
  var $next =  $active.next().length ? $active.next()
      : $('#slideshow-picture div:first');
  $next.addClass('active');
  $active.removeClass('active');
  var slideshowTitle = $('#slideshow-picture div.active img').attr("alt");
  $('#slideshow-title').html(slideshowTitle);
  if (slideshowPaused == false) { slideshowPause(); }
}

function slideshowPrevious() {
  var $active = $('#slideshow-picture div.active');
  if ( $active.length == 0 ) $active = $('#slideshow-picture div:last');
  var $prev =  $active.prev().length ? $active.prev()
      : $('#slideshow-picture div:last');
  $prev.addClass('active');
  $active.removeClass('active');
  var slideshowTitle = $('#slideshow-picture div.active img').attr("alt");
  $('#slideshow-title').html(slideshowTitle);
  if (slideshowPaused == false) { slideshowPause(); }
}

function slideshowPause() {
  if (slideshowPaused) {
    slideshowPaused = false;
    $('#slideshow-pause img').attr('src','images/slideshow-pause.png');
    slideshowSwitch();
    slideshowInterval = setInterval( "slideshowSwitch()", 3000 );
  } else {
    slideshowPaused = true;
    $('#slideshow-pause img').attr('src','images/slideshow-play.png');
    clearInterval(slideshowInterval);
  }
}

function slideshowThumbnails() {
  $('#slideshow-thumbnails').toggle();
}

function slideshowThumbnailMovePrev() {
  $('#slideshow-thumbnail-strip img:last').prependTo('#slideshow-thumbnail-strip');
}

function slideshowThumbnailMoveNext() {
  $('#slideshow-thumbnail-strip img:first').appendTo('#slideshow-thumbnail-strip');
}

function slideshowSelectByThumb() {
  var $clicked = $(this);
  var slideNum = $clicked.attr('slideshowOrder');
  var $active = $('#slideshow-picture div.active');
  if ( $active.length == 0 ) $active = $('#slideshow-picture div:last');
  var activeNum = $active.attr('slideshowOrder');
  if (activeNum != slideNum) {
    var $next = $('#slideshow-picture div[slideshowOrder='+slideNum+']');
    $next.addClass('active');
    $active.removeClass('active');
    var slideshowTitle = $('#slideshow-picture div.active img').attr("alt");
    $('#slideshow-title').html(slideshowTitle);
  }
  if (slideshowPaused == false) { slideshowPause(); }
}


/**
 * Pop-up jquery borrowed from
 * http://www.djsipe.com/2008/07/04/doing-pop-ups-the-right-way-with-jquery/
 */

// Create a namespace for our utilities  
var UTIL = UTIL || {};  
UTIL.popup = UTIL.popup || {};  
  
/** 
 * Open popup window 
 * 
 * Opens a popup window using as little as a URL. An optional params object can 
 * be passed. 
 * 
 * @param {String} href 
 * @param {Object} params 
 * @return {WindowObjectReference} 
 */  
UTIL.popup.open = function (href, params)  
{  
    // Defaults (don't leave it to the browser)  
    var defaultParams = {  
        "width":       "400",   // Window width  
        "height":      "278",   // Window height  
        "top":         "250",     // Y offset (in pixels) from top of screen  
        "left":        "400",     // X offset (in pixels) from left side of screen  
        "directories": "no",    // Show directories/Links bar?  
        "location":    "no",    // Show location/address bar?  
        "resizeable":  "yes",   // Make the window resizable?  
        "menubar":     "no",    // Show the menu bar?  
        "toolbar":     "no",    // Show the tool (Back button etc.) bar?  
        "scrollbars":  "no",   // Show scrollbars?  
        "status":      "no"     // Show the status bar?  
    };

    // centers the pop-up over the parent window
    var width = params["width"] || defaultParams["width"];
    var parentX = window.screenLeft || window.screenX;
    var parentWidth = window.outerWidth || document.body.clientWidth;
    params["left"] = ((parentWidth - width) / 2) + parentX;
  
    var windowName = params["windowName"] || "new_window";  
  
    var i, useParams = "";  
  
    // Override defaults with custom values while we construct the params string  
    for (i in defaultParams)  
    {  
        useParams += (useParams === "") ? "" : ",";  
        useParams += i + "=";  
        useParams += params[i] || defaultParams[i];  
    }  
  
    return window.open(href, windowName, useParams);  
};

// Add custom pop-up properties to links by giving them id's
// and inserting javascript after them in the document
// which assigns custom params like so:
//
// $("a#custom-popup").data("popup", {width:400,height:400, top:200, left:200});  

// script to execute when page is done loading
$(document).ready(function(){

  // slideshow scripts

  $("#slideshow-picture div").each(function(i) {
     $(this).attr('slideshowOrder',i);
     $(this).css('visibility','visible');
  });
  $("#slideshow-thumbnail-strip img").each(function(i) {
     $(this).attr('slideshowOrder',i);
  });
  $('#slideshow-thumb-control').click(slideshowThumbnails);
  $('#slideshow-previous').click(slideshowPrevious);
  $("#slideshow-pause").click(slideshowPause);
  $('#slideshow-next').click(slideshowNext);
  $('#slideshow-thumbnail-prev').click(slideshowThumbnailMovePrev);
  $('#slideshow-thumbnail-next').click(slideshowThumbnailMoveNext);
  $('#slideshow-thumbnail-strip img').click(slideshowSelectByThumb);

  // pop-ups

    // Apply this code to each link with class="popup"  
    $("a.popup").each(function (i){  
  
        // Add an onClick behavior to this link  
        $(this).click(function(event) {  
  
            // Prevent the browser's default onClick handler  
            event.preventDefault();  
  
            // Grab parameters using jQuery's data() method  
            var params = $(this).data("popup") || {};              
  
            // Use the target attribute as the window name  
            if ($(this).attr("target"))  
            {  
                params.windowName = $(this).attr("target");  
            }  
  
            // Pop up the window  
            var windowObject = UTIL.popup.open(this.href, params);  
  
            // Save the window object for other code to use  
            $(this).data("windowObject", windowObject);  
        });  
    });  

  // custom pop-ups
  $("a#pricing-vertical-boatlifts").data("popup", {width:961,height:631});
  $("a#pricing-ultimate-vl").data("popup", {width:548,height:371});
  $("a#pricing-rgc").data("popup", {width:950,height:230});
  $("a#pricing-ultimate-hl").data("popup", {width:997,height:630});
  $("a#pricing-pwc-small-craft").data("popup", {width:671,height:436});
  $("a#photo-gallery-trolley").data("popup", {width:300,height:428});

  // IE fixes for menus
  if (document.all && !window.opera && $.browser.msie) {
    IEHoverPseudo();
  }

});

// start the slideshow once all images have been loaded!
$(window).load(function(){
  slideshowInterval = setInterval( "slideshowSwitch()", 3000 );
});

