/*  Any page might need functions from this file,
    so it should always be included before any
    other JS files.
    
    Exceptions (for now):
        - listing slideshow
        - interactive maps
        
    HOWEVER, basic site params should be defined before inclusion of this
    or any other JS file: baseUrl, cookieDomain, which.
*/

/* var alertStr = window.name + '\nopened by\n' + window.opener;
if (window.opener && !window.opener.closed) alertStr += ' named ' + window.opener.name;
alert(alertStr); */

// Identify this window as part of our site.
var privateislandsonline = true;

// used to open a various types of windows with the right dimensions/attribs.
function popMultishowWin(url) {
    return pioPopup(url, 'multishow', 'width=870,height=500');
}
function popSlideshowWin(url) {
    return pioPopup(url, 'slideshow', 'width=870,height=500');
}
function popMapWin(url) {
    return pioPopup(url, 'map', 'width=870,height=500');
}
function popGmapWin(url) {
    return pioPopup(url, 'gmap', 'width=870,height=500');
}
function popInquireWin(url) { // deprecated
    return pioPopup(url, 'inquire', 'width=500,height=400,scrollbars=yes');
}
function popTellFriendWin(url) { // deprecated
    return pioPopup(url, 'tellafriend', 'width=500,height=400,scrollbars=yes');
}
// opens the manage favourites window
function popFavsWin() {
	return pioPopup(baseUrl+'favourites', 'managefavs', 'width=500,height=550,scrollbars=yes');
}

/*  Find the highest-level (i.e. "most parent") window still open, starting from
    this one, and from there, pop up a new window with given URL/attribs, and
    assign it a name that indicates it came from our site, and what type of
    window it is. */
function pioPopup(url,
                  winNameSuffix, // should not contain underscores
                  opts) {
                      
    // first see if there's a parent window to open it from (this will recursively
    // go back through the window hierarchy and open the popup from the highest-
    // level window).
    if (window.opener && !opener.closed && opener.pioPopup) {
        return opener.pioPopup(url, winNameSuffix, opts);
        
    // if called on a window with no opener or a non-PIO opener, pop the
    // new window from here.
    } else {
        
        // if the URL is relative and a baseUrl was set, prepend it.
        if (!url.match(/^http\:\/\//) && window.baseUrl) {
            url = baseUrl + url;
        }
        
        var uniqueNum = (new Date()).getTime() + '' + Math.round(Math.random() * 10);
        var theWin = open(url, 'privateislandsonline_'+uniqueNum+'_'+winNameSuffix, opts);
        if (window.focus) {
            theWin.focus();
        }
        return theWin;
    }
}

// returns true if the current window (or the window object passed to it)
// has a name that indicates it's a PIO popup window.
function isPioPopup() { // * see bottom
    var theWin;
    if (arguments.length == 1) {
        theWin = arguments[0];
    } else {
        theWin = window;
    }
    return theWin.name.split('_')[0] == 'privateislandsonline';
}

// closes the current window if it's a PIO pop-up.
function closeIfPopup() {
    if (isPioPopup()) {
        close();
    }
}

/*  Will attempt to open the given URL in the background window,
    or if this window has no PIO background window:
        - opens in a new window if this one's a popup
        - opens in this window otherwise. */
function openInBg(url) { // * see bottom
    if (hasPioBgWin()) {
        window.opener.location = url;
    } else if (isPioPopup()) {
        window.open(url);
        window.close();
    } else {
        window.location = url;
    }
}

/*  Returns true if the background window (this window's opener) exists
    and identifies as PIO. */
function hasPioBgWin() { // * see bottom
    return window.opener && !window.opener.closed &&
        window.opener.privateislandsonline;
}

function addToIeFavs() {
    if (window.external && (typeof window.external.addFavorite != 'undefined')) {
        window.external.addFavorite(window.location, window.document.title);
    } else {
        alert('Sorry, this feature requires Internet Explorer. Try pressing Ctrl+D.');
    }
}

/*  Preloads image files, using any argument(s) given as filename(s). */
function preloadImages() {
	var images = new Array(arguments.length);
	for (var i = 0; i < arguments.length; i++) {
		images[i] = new Image();
		images[i].src = arguments[i];
	}
}

/*	Stack up window.onload events using this function from Simon Willison:
	http://www.sitepoint.com/blog-post-view.php?id=171578 */
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

/*  Write a Flash embed object dynamically so as to bypass the IE activation
    box.  Optional args for no-Flash alternate image:
        altSrc, altHref, altAltText */
function writeFlashEmbed(src, id, width, height) {
    
    // optional args
    if (arguments.length >= 7) {
        altSrc = arguments[4];
        altHref = arguments[5];
        altAltText = arguments[6];
    }
    
    var MM_contentVersion = 6;
    var plugin = (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"]) ? navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin : 0;
    if ( plugin ) {
            var words = navigator.plugins["Shockwave Flash"].description.split(" ");
            for (var i = 0; i < words.length; ++i)
            {
            if (isNaN(parseInt(words[i])))
            continue;
            var MM_PluginVersion = words[i]; 
            }
        var MM_FlashCanPlay = MM_PluginVersion >= MM_contentVersion;
    }
    else if (navigator.userAgent && navigator.userAgent.indexOf("MSIE")>=0 
       && (navigator.appVersion.indexOf("Win") != -1)) {
        document.write('<SCR' + 'IPT LANGUAGE=VBScript\> \n'); //FS hide this from IE4.5 Mac by splitting the tag
        document.write('on error resume next \n');
        document.write('MM_FlashCanPlay = ( IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash." & MM_contentVersion)))\n');
        document.write('</SCR' + 'IPT\> \n');
    }
    if ( MM_FlashCanPlay ) {
        document.write('<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"');
        document.write('  codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" ');
        document.write(' ID="'+id+'" WIDTH="'+width+'" HEIGHT="'+height+'" ALIGN="">');
        document.write(' <PARAM NAME=movie VALUE="'+src+'"> <PARAM NAME=quality VALUE=high> <PARAM NAME=bgcolor VALUE=#FFFFFF> <param name="wmode" value="opaque">  '); 
        document.write(' <EMBED src="'+src+'" quality=high bgcolor=#FFFFFF  ');
        document.write(' swLiveConnect=FALSE WIDTH="'+width+'" HEIGHT="'+height+'" NAME="'+id+'" ALIGN=""');
        document.write(' TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer" wmode="opaque">');
        document.write(' </EMBED>');
        document.write(' </OBJECT>');
    } else if (arguments.length >= 7) {
        document.write('<a href="'+altHref+'" target="_blank"><img src="'+altSrc+'" width="250" height="250" alt="'+altAltText+'" border="0"></a>');
    }
}

/*
  Returns a custom-structured page object reference, so obj/style
  attributes can be accessed in a browser-blind way. Thanks to Peter-Paul Koch:
  http://www.quirksmode.org/js/dhtmloptions.html.
  
  Call like: var foo = new getObj('fooId');
  Use like: foo.obj.src = 'etc.gif';
            foo.style.top = '100px';
*/
function getObj(name)
{
  // alert('getting '+name)
  if (document.getElementById)
  {
    this.obj = document.getElementById(name);
    this.style = document.getElementById(name).style;
  }
  else if (document.all)
  {
    this.obj = document.all[name];
    this.style = document.all[name].style;
  }
  else if (document.layers)
  {
    this.obj = document.layers[name];
    this.style = document.layers[name];
  }
}
