/*  Expects parsed: cookies.js
                    common.js
                    dmenus.js
    Expects set: global 'island' variable with island info.
*/

addLoadEvent(function() {
	if (document.getElementById) {
        newMenu('menu_listing_reginfo', 'root_listing_reginfo', null, 0);
		newMenu('menu_listing_save', 'root_listing_save', null, 0);
        newMenu('menu_listing_send', 'root_listing_send', null, 0);
        newMenu('menu_listing_images', 'root_listing_images', null, 0);
        newMenu('menu_listing_print', 'root_listing_print', null, 0);
		makeSiblings('menu_listing_reginfo',
                     'menu_listing_save',
                     'menu_listing_send',
                     'menu_listing_images',
                     'menu_listing_print');
        // listing footer menu
		newMenu('menu_listing_save2', 'root_listing_save2', null, 0);
        newMenu('menu_listing_send2', 'root_listing_send2', null, 0);
        newMenu('menu_listing_images2', 'root_listing_images2', null, 0);
        newMenu('menu_listing_print2', 'root_listing_print2', null, 0);
		makeSiblings('menu_listing_save2',
                     'menu_listing_send2',
                     'menu_listing_images2',
                     'menu_listing_print2');
	}
});

function popBrochureWin(url) {
    return pioPopup(url, 'brochure', 'width=700,height=550,scrollbars=yes');
}
             
function listingVisit() {
/*  Records this island (as defined by global 'island' variable) into the
    'recently viewed' cookie. Should be called before the cookie is parsed
    into a global variable by js/recent.js. */
             
	// alert('before:\n\n\n'+readCookie('recent').split('|||').join('\n\n'));
	var maxRecent = 5;
	var cookieName = 'recent';
	var cookieExpire = 0; // recent islands cookie expires on browser close
	var majorSplitChar = '|||', minorSplitChar = '||';
	var alreadyThere = false;
	var i;
	// get listings already in the recent cookie, if any
	var recentCookie = readCookie(cookieName);
	if (recentCookie) {
		// check if it's already in there
		var recentIslands = recentCookie.split(majorSplitChar);
		for (i = 0; i < recentIslands.length; i++) {
			if (recentIslands[i].split(minorSplitChar)[1] == island.url) {
				// if so, just bring it to the front
				recentIslands = recentIslands.concat(recentIslands.splice(i, 1));
				alreadyThere = true;
				break;
			}
		}
	} else {
		recentIslands = new Array();
	}
	
	// if either no cookie or it's not already there, add it to the front
	if (!recentCookie || !alreadyThere) {
		recentIslands = recentIslands.concat(
			island.balloon + minorSplitChar +
			island.url + minorSplitChar +
			island.imgsrc + minorSplitChar +
			island.which
		);
		// if there are now more than 5 islands, remove oldest until there
		// are only 5 (should only have to remove 1, but make sure)
		if (recentIslands.length > maxRecent) {
			recentIslands.splice(0, recentIslands.length - maxRecent);
		}
	}
	
	// now, either way, update the cookie
	createCookie(cookieName, recentIslands.join(majorSplitChar), cookieExpire);
	// alert('after:\n\n\n'+readCookie('recent').split('|||').join('\n\n'));
}

listingVisit();

/* returnToImage1 = baseUrl+'images/buttons/returnto-1.gif';
returnToImage2 = baseUrl+'images/buttons/returnto-2.gif';
returnToImageLoops = 3;
returnToImageCounter = 0;

preloadImages(returnToImage1, returnToImage2);

function animateReturnTo() {
    if (!window.returnToLink) returnToLink = document.getElementById('link_returnto');
    switch (returnToLink.style.backgroundImage) {
    case 'url('+returnToImage2+')':
        returnToLink.style.backgroundImage = 'url('+returnToImage1+')';
        var timeToNextFrame = 800;
        break;
    default:
        returnToLink.style.backgroundImage = 'url('+returnToImage2+')';
        var timeToNextFrame = 250;
        break;
    }
    returnToImageCounter++;
    if (returnToImageCounter < returnToImageLoops*2) { // 2 frames per loop
        setTimeout(animateReturnTo, timeToNextFrame);
    }
}

addLoadEvent(function() { setTimeout(animateReturnTo, 1000); }); */