﻿Array.prototype.$ = function(x) { this.push(x); return this };

var NWTR = {
    loaded:false /*,
    GoogleSearcher:new GwebSearch(),
    Search:function() {
        var results = NWTR.GoogleSearcher.results,
            text = [];
        if(results.length>0) {
            $('page').update(results.collect(function(result) {
                return text.$('<div>').$('<a href="javascript:load(\'').$(result.url).$('\')">').$(result.title).$('</a>'
                    ).$('<div class="searchResultContent">').$(result.content).$('</div>').$('</div>');
            }).flatten().join(''));
            new Effect.Fade('loading');
            new Effect.BlindDown('page');    
        } else {
            $('loading').hide();
            $('page').show();    
            load('searcherror.htm');
        }
    }*/
};

function init() {
    if(NWTR.loaded==false) {
        NWTR.loadSite();
    }
}

if(Prototype.Browser.Gecko) {
    document.addEventListener("DOMContentLoaded", init, false);
} else if(!Prototype.Browser.IE) {
    Event.observe(window,'load',init);
} else {
    window.onload = init;
}

NWTR.loadSite = function() {
    if(NWTR.loaded==false) { 
        $$('#navigation a').each(function(item) {
            item.onclick = function(e) {
                //alert('click!!!');
                load(this.readAttribute('link'));
            }.bind(item); //.bindAsEventListener(item);
            item.onmouseover = function(e) {
                //alert('mouseover');
                new Effect.Morph(this,{
                    style: {
                        marginLeft:'10px',
                        color:'#ffffff'
                    },
                    duration:0.5
                });
            }.bind(item); //.bindAsEventListener(item);
            item.onmouseout = function(e) {
                //alert('mouseout');
                new Effect.Morph(this,{
                    style: {
                        marginLeft:'0px',
                        color:'#292071'
                    },
                    duration:0.5
                });
            }.bind(item); //.bindAsEventListener(item);
        });
        NWTR.resize();
        dhtmlHistory.initialize();
	    dhtmlHistory.addListener(RSHListener);
        
        load('home.htm');
        
    }
};



function load(url) {
    if((['note_1','note_2','note_3','note_4']).include(url)) {
        return;
    }
    new Effect.Parallel([
        new Effect.Appear('loading'),
        new Effect.BlindUp('page')
    ],{
        afterFinish:function() {
            dhtmlHistory.add(url,{});
            new Ajax.Updater($('page'),url,{
                onSuccess:function(transport) {
                    new Effect.Fade('loading');
                    new Effect.BlindDown('page',{
                        afterFinish:function() {
                            NWTR.resize();
                        }
                    });
                    var t = setTimeout(function() {
                        NWTR.tabs();
                        NWTR.tables();   
                        NWTR.slideshows();
                        NWTR.disclosures();
                        NWTR.lightboxes();
                    },0);                 
                },
                evalScripts:true,
                method:'get'
            });
            
        }
    });
    
}


NWTR.resize = function() {
    var x,y;
    if (self.innerHeight) // all except Explorer
    {
	    x = self.innerWidth;
	    y = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight)
	    // Explorer 6 Strict Mode
    {
	    x = document.documentElement.clientWidth;
	    y = document.documentElement.clientHeight;
    }
    else if (document.body) // other Explorers
    {
	    x = document.body.clientWidth;
	    y = document.body.clientHeight;
    }
    
    $('main').setStyle({
        width: (x - 360) + 'px',
        height: (y - 80) + 'px'
    });
    $('content').setStyle({
        width: (x - 360) + 'px',
        height: (y - 80) + 'px'
    });
    $('page').setStyle({
        width: (x - 380) + 'px',
        height: 'auto'
    });

};

NWTR.tabs = function() {
    $$('.tabSet').each(function(tabSet) {
        var strip = tabSet.down('.tabStrip'),
            setID = tabSet.identify();
        $$('#'+strip.identify()+'>a.tab').each(function(tab) {
            tab.stopObserving('click');
            tab.observe('click',function() {
                $$('#'+setID+' .tabActive').invoke('removeClassName','tabActive');
                $$('#'+setID+' #'+$(this).readAttribute('rel')).concat([this]).invoke('addClassName','tabActive');
            });
        });
    });
    
    $$('a[rel=tab]').each(function(a) {
        a.observe('click',function() {
            var tab = $($(this).readAttribute('tab')),
                pane = $($(this).readAttribute('pane'));
            if(tab && pane) {
                var tabSet = tab.up('.tabSet');
                $$('#'+tabSet.identify()+' .tabActive').invoke('removeClassName','tabActive');
                [tab,pane].invoke('addClassName','tabActive');
            }
        });
    });
};

NWTR.tables = function() {
    TableKit.load();
};

NWTR.slideshows = function() {
    $$('.slideshow').each(function(container) {
        container = $(container);
        var path = (container.readAttribute('path') || '');
        var images = $w(container.readAttribute('images')).compact().uniq().map(function(x) {
            return path+x;
        });
        new Widget.Fader(container.identify(),images,{autosize:true});  
    });
};

NWTR.disclosures = function() {
    $$('.disclosure').each(function(container) {
        (function(container) {
            var discContents = $($(container).readAttribute('content')).addClassName('disclosureContent').hide().identify();
            $($(container).readAttribute('trigger')).addClassName('trigger').observe('click',function() {
                Effect.toggle($(discContents),'blind');
                /*
                if ($(discContents).visible()) {
                    $(discContents).hide();
                } else {
                    $(discContents).show();
                }
                */
            });
        }(container));
    });
};

NWTR.lightboxes = function() {
    $$('a.lightbox').each(function(a) {
        var o = { 
                options: {
                    autosize:true,
                    topclose:false
                }
            },
            a = $(a);
        ['href','rel','title','caption'].each(function(attr) { 
            o[attr] = (a.readAttribute(attr) || '');
            a.writeAttribute(attr,"");
        });
        a.observe('click',(function(conf) {
            return function(event) {
                Event.stop(event);
                Lightview.show(conf);
            }
        })(o).bindAsEventListener());
    });
    
}

Event.observe(window,'resize',NWTR.resize.bind(NWTR));