zoo.dom.hijax = function(elems, callBack, bindTo) {
    elems.each(function(anchor) {
        anchor.onclick = function() {
            new Ajax.Request(this.href, {
                method: 'get', 
                onSuccess: callBack.bindAsEventListener(bindTo)
            });     
            return false;
        }
    });
    return true;
}

/**
 * dlgParams
 *  title - text for titlebar
 *  pos   - either string like - center, cursor or actual coordinates as an object {x, y}
 *  loadingTxt - txt to display while loading
 */
zoo.Dialog = function(dlgParams) {
    this.id           = 'dlg-' + zoo.Math.random(1000,9999);
    this.title        = dlgParams.title;
    this.name         = dlgParams.name
    this.pos          = dlgParams.pos;
    this.offset       = dlgParams.offset || { x: 20, y: 30 };
    this.loadingTxt   = dlgParams.loadingTxt || 'Loading... please be patient.';
    this.nodes        = {};
    this.hidden       = true;
    this.updateCursor = true;
    
    // everytime show is called will recalculate pos if set to cursor
    this.resetCursorOnShow = dlgParams.resetCursorOnShow || true
}  
zoo.Dialog.prototype = function() {
   
    var _createElements = function() {
        var dlg          = this.dlg     = new Element('div', {'class': 'dlg hidden', 'id': this.id});
        var title        = new Element('h1',  {'class': 'dlg-title-bar'});
        var titleContent = new Element('span', {'class': 'title'}).update('x');
        var titleClose   = new Element('span', {'class': 'close'}).update('x');
        var dlgContent   = new Element('div', {'class': 'content'});
        
        if (this.name) {
            dlg.addClassName(this.name);
        }
        
        titleClose.observe('click', this.hide.bindAsEventListener(this));
        
        this.nodes['title']        = title;
        this.nodes['titleContent'] = titleContent;
        this.nodes['titleClose']   = titleClose;        
        this.nodes['content']      = dlgContent;
        
        if (this.title) {
            titleContent.innerHTML = this.title;
        }
        title.appendChild(titleContent);
        title.appendChild(titleClose);        
        
        dlg.appendChild(title);
        dlg.appendChild(dlgContent);
        document.body.appendChild(dlg);
        return this;
    }
    
    var _initDrag = function() {
        new Draggable(this.dlg, {
            handle      : this.title,
            starteffect : function(element) {
                new Effect.Opacity(element, {from: 1, to: .9, duration: 0 });
            },
            endeffect   : function(element) {
                new Effect.Opacity(element, {from: .9, to: 1, duration: .3 }); 
            }
        });        
    }
    
    var _setPosBasedOnCursor = function(clickEvent) {
        // set dimensions, cater for being close to the edge of the window
        var x = clickEvent.pointerX() + this.offset.x;
        
        // watch for displaying off screen .. need to remove hardcoded values
        if (x + 400 > document.viewport.getWidth()) {
            x = document.viewport.getWidth() - 450;
        }
        this.dlg.style.left = x + 'px';
        
        var y = clickEvent.pointerY() + this.offset.y;
        // watch for displaying off screen .. need to remove hardcoded values
        if (y + 210 > document.viewport.getHeight()) {
            y -= 220;
        }
        this.dlg.style.top = y -20 + 'px';
        
        this.updateCursor = this.resetCursorOnShow;        
    }
    
    return {
        init: function() {
            _createElements.call(this);
            _initDrag.call(this);
            
        },
        show_onclick: function(event, dlgParams) {
            // check if pos is cursor and if flag for updateCursor is set
            if (this.pos == 'cursor' && this.updateCursor) {
                _setPosBasedOnCursor.call(this, event);
            }
            
            this.show(dlgParams);
            return true;
        },
        /**
         * dlgParams - same as contructor
         */
        show: function(dlgParams) {
            if (this.hidden) {
                this.dlg.removeClassName('hidden');
                this.hidden = false;
            }
            if (dlgParams.title) {
                this.nodes.titleContent.innerHTML = dlgParams.title;
            }            
        },
        hide: function() {
            if (!this.hidden) {
                this.dlg.addClassName('hidden');
                this.hidden = true;
            }            
        },
        update_onclick: function(event, content, dlgParams) {
            this.update(content, null, false);
            this.show_onclick(event, dlgParams);
            return true;
        },
        update: function(content, dlgParams, showOnUpdate) {
            this.nodes['content'].update(content);
            showOnUpdate = typeof showOnUpdate == 'undefined' ? true : showOnUpdate;
            if (showOnUpdate) {
                this.show();
            }
        },
        kill: function() {
            
        }     
    }
}()


zoo.LocationPopUp = function() {
    this.popUp = new zoo.Dialog({
        'title' : 'Dr. Usal Locations ',
        'pos'   : 'cursor',
        'name'  : 'location-popup'
    });
    this.popUp.init();
    return true;
}
zoo.LocationPopUp.prototype = function() {
    
    var _titles = {
        'ny': 'New York Locations',
        'nj': 'New Jersey Locations',
        'in': 'International Location'
    }
    
    var _hijax = function() {
        var self = this;
        this.locations.each(function(anchor) {
            anchor.observe('click', function(event) {
                var element = Event.element(event);
                self.clickEvent = event;
                
                var l = element.href.match(/\?l=([a-z]{2})/)[1];
                self.popUpTitle = _titles[l];
                
                new Ajax.Request(element.href, {
                    method: 'get', 
                    onSuccess: self.show.bindAsEventListener(self)
                });
                event.stop();
                return false;
            });
        });
    }
   
    return {
        init: function() {
            this.locations = $$('a.location');
            _hijax.call(this);
            return true;
        },
        show: function(res) {
            this.popUp.update_onclick(this.clickEvent, res.responseText, {title: this.popUpTitle });
            return true;           
        }
    }    
}();

zoo.dom.widgets.LocationPopUp = function() {
    var popUpWgt = new zoo.LocationPopUp();
    popUpWgt.init();
    return true;
}

zoo.dom.disableRightClick = function() {
    //Disable right click script
    //visit http://www.rainbow.arch.scriptmania.com/scripts/
    var message="Sorry, right-click has been disabled";
    ///////////////////////////////////
    function clickIE() {if (document.all) { alert(message);return false;}}
    function clickNS(e) {if
    (document.layers||(document.getElementById&&!document.all)) {
    if (e.which==2||e.which==3) {alert(message);return false;}}}
    if (document.layers)
    {document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS;}
    else{document.onmouseup=clickNS;document.oncontextmenu=clickIE;}
    document.oncontextmenu= function() { return false } 
}

Event.observe(window, 'load', zoo.dom.widgets.LocationPopUp);