/* ganzo js library */
/* depends on jQuery */

/* avoid firebug errors in ie and chrome */
if ("undefined" == typeof(console)) {
console = {};
console.debug = function() {};
}
console.debug('loading gzo.js ' + Date());

/* Namespace for lib functions */
var gzo = function () {};

gzo.timeStamp = function() { return +new Date(); };

/**
 * Call from a link with onclick="gzo.toggleHidden(this)"
 *
 * steps through parents looking for a container with class .hideable
 * and then hides all the children elements with a class of .cantoggle
 */

gzo.toggleHidden = function(control)
{
    var hidden = control.parentNode;
    while (hidden.className != 'hideable') { 
        hidden = hidden.parentNode; 
    } 
    var rows = hidden.getElementsByTagName('div');
    for (i=0; i < rows.length; i++) {
        if('cantoggle' == rows[i].className) {
            if ('inline' == rows[i].style.display) {
               rows[i].style.display = 'none';
               control.innerHTML = 'Show room details';
            } else {
               rows[i].style.display = 'inline';
               control.innerHTML = 'Hide room details';
            }
        }
    }
};

/* requires jQuery and google search api: 
 *  eg. google.load('search', '1', {'nocss':true});
 *
 *  @param string Url of site to search www.foobar.com
 *  @param callback(result) function to handle each individual result
 *  @param node empty container for google branding
 *
 *  gs = new gzo.gSearch('www.foo.com', function(result) { blah... });
 *  gs.execute('skiing');
 */
gzo.gSearch = function(siteUrl,resultCb,brandingNode)
{
    google.search.Search.getBranding(brandingNode);

    this.execute = function(q) { webSearch.execute(q); };

    var searchComplete = function()
    {
        if(webSearch.results && webSearch.results.length) {
            $.map(webSearch.results, resultCb);
            var multipage = false; 
            if(multipage) {
                webSearch.gotoPage(1 + webSearch.cursor.currentPageIndex);
            } else {
                console.debug('gzo.gSearch: multipage off');
            }
        }
    };

    var webSearch = new google.search.WebSearch();
    webSearch.setSiteRestriction(siteUrl);
    webSearch.setResultSetSize(google.search.Search.LARGE_RESULTSET);
    webSearch.setNoHtmlGeneration();
    webSearch.setSearchCompleteCallback(this, searchComplete);
};

/* revert to popups for IE6 rather than go into workaround hell */
gzo.isIE6 = function(){ return jQuery.browser && jQuery.browser.msie && (jQuery.browser.version.substring(0,1)==6);};
gzo.fixJumpLinksIE6 = function() {
    jQuery('a.jump').click(function(){ 
        var reviews = window.open(this.href, 'reviews', 'height=500,width=700,resizeable=1,scrollbars=1'); 
        reviews.moveTo(20,20);
        jQuery(reviews).blur(function(){reviews.close();});
        return false;
    }); 
};

/* handle behaviour of "jump" popups */
gzo.fixJumpLinks = function()
{
    if(gzo.isIE6()) {
        gzo.fixJumpLinksIE6();
        return;
    }

    $body = $(document.body);

    var $container = jQuery('<div id="container"/>')
        .css({position:'fixed',left:'15%',top:'10%',width:'70%',height:'70%'})
        .css({zIndex:'99',background:'white',border:'1px solid #c2c4cc',padding:'1px'})
        .appendTo($body)
        .hide();

    var $jumpFrame = jQuery('<iframe id="jumpframe"/>')
        .css({width:'100%',height:'100%', border:'1px solid #c2c4cc',background:'#E4EAFA'})
        .appendTo($container);

    var overlay_css = {
        zIndex:'100',position:'absolute',bottom:'0',right:'0',
        fontSize:'20px',background:'#F7BA0C',color:'#533',
        padding:'5px',border:'1px solid red',textDecoration:'none'
    };
    jQuery('<a id="overlay">&bull; Close reviews</a>')
        .css(overlay_css)
        .attr('href', '#top')
        .appendTo($container);

    jQuery('a.jump').click(function(){
        $jumpFrame
            .attr('src',this.href)
            .load(function(){ $container.show();});
        return false;
    });    
    
    /* make any other elements (apart from external iframe) in container clickable */ 
    $body.click(function(){$container.hide();});
};


/* store extra options for use with the hidden dialogs */
gzo.hiddenDialogOptions = {};
gzo.setHiddenDialogOptions = function(name, val)
{
    gzo.hiddenDialogOptions[name] = val;
};

/**
 * convert .hidden-dialog elements into jquery-ui dialogs
 *
 * eg 
 *  <div class="hidden-dialog" id="shark_warning">Shark! Shark!</div>
 *  <a class="shark-warning" href="#">Swimming?</a>
 *
 */
gzo.prepareHiddenDialogs = function()
{
    jQuery('.hidden-dialog').each(function(){
        /* prepare dialog */
        var dialog = $(this);
        var options = {
            width: 400,
            autoOpen:false,
            modal: true,
            buttons: { 
                'Close': function() { 
                            /* brief timeout because resizables not shutting properly */
                            me = $(this); 
                            setTimeout(function(){me.dialog('close');}, 50); 
                         }
            }
        };
       
        /* add any extra options - height, width etc */ 
        if(gzo.hiddenDialogOptions[this.id]) {
            jQuery.extend(options, gzo.hiddenDialogOptions[this.id]);
        }

        /* pass extra classes to dialog */
        dialog.removeClass('hidden-dialog');
        var extraClass = dialog.attr('class');
        if(extraClass) { options.dialogClass = extraClass; }
        dialog.addClass('hidden-dialog');

        dialog.dialog(options); 

        /* find elements with class = dialog.id and make them into triggers */
        jQuery('.'+this.id).click(function() {
            dialog.dialog('open'); return false;
        });
    });
};

