/**
 * all.js - Swipht Technologies
 *
 * This file is to be included in ALL NYLS pages
**/

 // -- Global Scope

// -- Sets window.location.params property to an array of hash objects of the get request
function setUrlParams(){
    var request_params = new Hash();;
    var href = window.location.href;
    var q_index = href.indexOf("?");
    if(q_index > -1){
        var param_groups = href.substr(q_index+1);
        if(param_groups.indexOf('&') > -1)
            param_groups = param_groups.split('&');
        else
            param_groups = [param_groups];
        param_groups.each(function(pg){
           var param = pg.split('=');
           request_params.set(param[0], param[1]);
        });
    }
    window.location.params = request_params.toObject();
}

// autocompleter_link_loader is designed to override the normal text completion if the selection is a link
var autocompleter_link_loader = function(text, selected_li){
    var link = selected_li.down('a');
    if(link && link.href.length > 0){
        window.location = link.href;
    }
    return false;
}

// The following slideshow_lw is used by the display of the lightwindow block type
var slideshow_lw_interval = null;
var slideshow_lw = null;
var slideshow_lw_first_asset = null;

var slideshow_lw_set_interval = function(first_asset, seconds, lightwindow) {
  slideshow_lw_first_asset = first_asset;
  slideshow_lw = lightwindow;
  slideshow_lw_interval = setInterval('slideshow_lw_show_next();', seconds * 1000);
}
var slideshow_lw_clear_interval = function() {
  clearInterval(slideshow_lw_interval);
}
var slideshow_lw_show_next = function() {
  if(slideshow_lw.activeGallery) {
    if(slideshow_lw.galleryLocation.current != slideshow_lw.galleryLocation.total) {
      slideshow_lw.navigateWindow('next');
    } else {
      slideshow_lw.galleryLocation.current = 1;
      slideshow_lw.openWindow(slideshow_lw_first_asset);
    }
  } else {
    slideshow_lw_interval = clearInterval(slideshow_lw_interval);
  }
}

// If the lightwindow is animating nasty errors can happen if it is disabled while animating. The following function addresses this
close_lightwindow = function() {
  var lw = (slideshow_lw != null) ? slideshow_lw : myLightWindow;
  if(lw.animating == true) {
    setTimeout('close_lightwindow()', 250);
  } else {
    lw.deactivate();
  }
}

var myLightWindow = null; // Global

document.observe('dom:loaded', function() {
    // -- Set window.location.params to url get params
    setUrlParams();
    
    // -- Fire PNG Hack for IE 6
    if(Prototype.Browser.IE && navigator.appVersion.indexOf('MSIE 6') >= 0) {
        $$('img[src$=.png]').invoke('pngHack');
    }
    
    // -- Set Autocompleter for the main search
    if($('cKeywords')){
        var search_auto_completer = new Ajax.Autocompleter('cKeywords', 'search_auto_complete', '/keyword_search.php', {paramName: "keywords", afterUpdateElement: autocompleter_link_loader});
    }
    
    // --------------------  FIRE DOM:LOADED FUNCTIONS --- START
    //try{ homePageOnDomLoaded(); } catch(e){};  // fire home page dom:loaded script
    // --------------------  FIRE DOM:LOADED FUNCTIONS --- END
    
    var exec_lightwindow = false;
    try{ if(lightwindow != undefined) exec_lightwindow = true; } catch(e){};
    
    if(exec_lightwindow){
        myLightWindow = new lightwindow();
        
        // -- Open lightbox automatically if a lightbox url parameter is set for window.location.params.lightbox
    
        var lightwindow_url = window.location.params.lightwindow_url;
        if(lightwindow_url != undefined && lightwindow_url.length > 0){
            var open_link = $$('a.lightwindow').find(function(link){
                return encodeURIComponent(link.readAttribute('href')) == lightwindow_url;
            });
            // myLightWindow defined in lightwindow.js
            if(open_link != undefined)
                myLightWindow.activate(null, open_link);
        }
    }
    if ($$('div.nyls-lightwindow')) {
      $('lightwindow').addClassName('nyls-lightwindow');
    }
}); // -- dom:loaded -- End
