/*
hubs - behavior for FMC hub pages

Created by John Hunter on 2009-04-07.
	
Functionality:
1. linked panel - swaps from image to copy, has an initial timed swap
2. menu panel - shows a fixed number of links from a list, these are sequenced through on a timer, with manual controls to go fwd, back

*/

// Fix to stop HTML mode displaying while Flash loads for H-Scroller and V-Scroller
if (window.swfobject && swfobject.hasFlashPlayerVersion('9.0.0')) {
    if (shouldDisplayFlash() === true) {
        document.writeln('<style type="text/css">#Start_page .content, .home_hub_vertical #altContent, #new-jeweller-homepage #content-wrapper .content { display: none; }</style>');
    }
}

$(document).ready(function() {

    linkedItemPanel.init('#link-panel-1');

    $('.home_hub_vertical .content_overlay').css('opacity', 0.9);

    if (typeof flashObj == 'object') {
        // Set-up flash/html toggle link
        if (window.swfobject && swfobject.hasFlashPlayerVersion('9.0.0')) {
            if (flashObj.setToggleLink && flashObj.setToggleLink == true) {
                var subnavList = $('#sub-nav ul:first');
                subnavList.prepend('<li><a class="first" id="flash-link" href="."></a></li>');
                flashObj.setToggleLink = false;
                var flashLink = $('#flash-link');
                initMenu(flashLink.parent());
            }

            if (flashLink) {
                var viewMode = shouldDisplayFlash();
                if (flashObj.localisedStrings === undefined) {
                    flashObj.localisedStrings = { flashText: 'View Flash version', htmlText: 'View HTML version' }; // Fallback toggle text
                }
                flashLink.text(flashObj.localisedStrings[(viewMode ? 'htmlText' : 'flashText')]);
                flashLink.click(function(e) {
                    e.preventDefault();
                    shouldDisplayFlash(!viewMode);
                    window.location = this.href;
                });
            }
            // TODO: Copy functions from base.js in template folder
            if (shouldDisplayFlash()) {
                var flashvars = flashObj.flashvars || {
                    xml_loc: flashObj.xmlPath
                };
                var params = {
                    menu: 'false',
                    scale: 'noScale',
                    allowscriptaccess: 'always',
                    wmode: 'opaque'
                };
                var objWidth = flashObj.objWidth || '980';
                swfobject.embedSWF(flashObj.swfPath, flashObj.elemId, objWidth, "385", "9.0.0", "/swf/expressInstall.swf", flashvars, params);
            }
        }
    }
});

// view html action for flash movie
function doViewHtmlVersion() {
    shouldDisplayFlash(false);
    window.location = window.location;
}

// assuming a single panel
var linkedItemPanel = function() {
    var that = {};
    var panelElems,
		panelWidth,
		timer,
		initDuration = 800,
		duration = 300,
		initDelay = 3000;

    function togglePanel(elem, overrideDuration) {
        clearTimeout(timer);
        var leftPos = elem.hasClass('active') ? -panelWidth : 0;
        elem.parent().animate({ left: leftPos }, (overrideDuration || duration), function() {
            elem.toggleClass('active');
        });
    }

    //public
    that.init = function(cssSelector) {

        // temp for testing:
        var s = location.search;
        var delay = s.substring(s.indexOf('delay=') + 6, s.length);
        initDelay = delay || initDelay;
        // end temp

        panelElems = $(cssSelector);
        panelWidth = parseInt(panelElems.css('width'), 10) - 15; // width of a.control-show

        var body = $('div.body', panelElems).
			css({ 'opacity': 0.9, left: -panelWidth }); // could add png background with opacity

        body.prepend('<a class="control-show" href="#show">show</a>');
        var control = $('a.control-show', body).click(function(e) {
            e.preventDefault();
            togglePanel($(this));
        });

        // initial timed display
        timer = setTimeout(function() { togglePanel(control, initDuration); }, initDelay);
    };
    return that;
} ();

var linkMenuPanel = function() {
    var that = {};
    //public
    return that;
} ();

// Helpers:

Object.beget = function(obj) {
    var F = function() { };
    F.prototype = obj;
    return new F();
};
// use: myThing = Object.beget(thing);
