
SchedAPI = {
   errmsg: {
        'USER_NOT_EXISTS': 'Username or Password is incorrect.',
        'AUTH_FAILED': 'Username or Password is incorrect.',
        'LOGGED_IN': 'A logged in user cannot sign-up.',
        'INVITATION_CODE': 'Your invitation code is invalid or expired.',
        'HOSTNAME': 'You must enter a username(alphabets + numerics, 4-20 chars).',
        'PASSWORD': 'You must enter a password(more than 4 chars).',
        'RESET_KEY': 'Your password reset key is invalid.',
        'EMAIL': 'You must enter a E-Mail.',
        'HOSTNAME_EXISTS': 'The username you entered already exists.',
        'EMAIL_EXISTS': 'Your E-Mail is already registered.',
        'URL': 'Your URL is invalid format.'
    }

    , updateProfile: function(form) {
        var self = this;
        new Ajax.Request('/gateway/in/api/update_profile',
            {
                method: 'post',
                parameters: form.serialize(true),

                onComplete: function(req) {
                    try {
                        var res = eval('(' + req.responseText + ')');
                        if (res.success) {
                            window.alert('Your profile is updated.');
                        }
                        else {
                            var msg = self.errmsg[res.error] || res.error;
                            window.alert(msg);
                        }
                    }
                    catch (e) {
                        window.alert(e.message);
                    }
                },

                onFailure: function(req) {
                    window.alert('connection failed.');
                }
            }
        );
    }

    , signin: function(form, href) {
        var self = this;
        new Ajax.Request('/account/login',
            {
                method: 'post',
                parameters: form.serialize(true),

                onComplete: function(req) {
                    try {
                        var res = eval('(' + req.responseText + ')');
                        if (res.success) {
                            if (href == null) {
                                location.reload();
                            }
                            else {
                                if (!href.match(/^\//)) {
                                    href = '/';
                                }

                                location.href = href;
                            }
                        }
                        else {
                            var msg = self.errmsg[res.error] || res.error;
                            window.alert(msg);
                        }
                    }
                    catch (e) {
                        window.alert(e.message);
                    }
                },

                onFailure: function(req) {
                    window.alert('connection failed.');
                }
            }
        );
    }

    , signout: function() {
        new Ajax.Request('/account/logout',
            {
                method: 'post',

                onComplete: function(req) {
                    location.href = '/';
                },

                onFailure: function(req) {
                    window.alert('connection failed.');
                }
            }
        );
    }

    , setPassword: function(form) {
        var self = this;
        new Ajax.Request('/gateway/in/api/set_password',
            {
                method: 'post',
                parameters: form.serialize(true),

                onComplete: function(req) {
                    try {
                        var res = eval('(' + req.responseText + ')');
                        if (res.success) {
                            window.alert('Your new password set successfully.');
                            location.href = '/';
                        }
                        else {
                            var msg = self.errmsg[res.error] || res.error;
                            window.alert(msg);
                        }
                    }
                    catch (e) {
                        window.alert(e.message);
                    }
                },

                onFailure: function(req) {
                    window.alert('connection failed.');
                }
            }
        );
    }
    
    , remove: function(id) {

       	var params = {};
        params.artist_id = id;

        var self = this;
        new Ajax.Request('/account/remove',
            {
                method: 'post',
                parameters: params,

                onComplete: function(req) {
                    try {
		
											$("favorite_bands").innerHTML=req.responseText;
											new Effect.Highlight('favorite_bands');		
											
                    }
                    catch (e) {
                        window.alert(e.message);
                    }
                },

                onFailure: function(req) {
                    window.alert('connection failed.');
                }
            }
        );
    }

    

}

function getWindowBounds() {
    var w, h, x, y;

    if (Prototype.Browser.Gecko || Prototype.Browser.WebKit) {
        w = window.innerWidth;
        h = window.innerHeight;
        x = window.scrollX;
        y = window.scrollY;
    }
    else if (Prototype.Browser.Opera) {
        w = window.innerWidth;
        h = window.innerHeight;
        x = window.pageXOffset;
        y = window.pageYOffset;
    }
    else {
        var d = document.documentElement;
        var b = document.body;
        w = d.clientWidth  ? d.clientWidth  : b.clientWidth  ? b.clientWidth  : 0;
        h = d.clientHeight ? d.clientHeight : b.clientHeight ? b.clientHeight : 0;
        x = d.scrollLeft   ? d.scrollLeft   : b.scrollLeft   ? b.scrollLeft   : 0;
        y = d.scrollTop    ? d.scrollTop    : b.scrollTop    ? b.scrollTop    : 0;
    }

    /*
    if (w == 0 && h == 0 && x == 0 && y == 0) {
        window.alert('failed to get window bounds.');
    }
    */

    return {
        'w': w,
        'h': h,
        'x': x,
        'y': y
    };
}

function getScrollLeft() {
    return window.pageXOffset
        || document.documentElement.scrollLeft
        || document.body.scrollLeft
        || 0;
}

function getScrollTop() {
    return window.pageYOffset
        || document.documentElement.scrollTop
        || document.body.scrollTop
        || 0;
}

function button(img) {
    var s = img.src;
    if (s.match(/_01/)) {
        img.src = s.replace('_01', '_02');
    }
    else if (s.match(/_02/)) {
        img.src = s.replace('_02', '_01');
    }
}

// IE won't set document.referrer when location.href is called, so we use normal anchor tag to change location.
function redirect(href) {
    if (Prototype.Browser.IE) {
        var a = document.createElement('a');
        a.style.display = 'none';
        a.href = href;
        document.body.appendChild(a);
        a.click();
    }
    else {
        location.href = href;
    }
}


// thanks goes to ma.la.
// http://la.ma.la/blog/diary_200508141140.htm
Function.prototype.toSingleton = function(){
    var ctor = this;
    var Dummy = function(){};
    Dummy.prototype = ctor.prototype;
    var new_func = function(){
        if(new_func.__allow_create__ == null){
            throw "this is SingletonClass. use getInstance()";
            return null;
        }
        return ctor.apply(this,arguments)
    };
    new_func.getInstance = function(){
        if(new_func.instance == null){
            var tmp = new Dummy;
            var res = ctor.apply(tmp,arguments);
            new_func.instance = (typeof res == "undefined")?tmp:res;
        }
        return new_func.instance;
    };
    new_func.prototype = ctor.prototype;
    return new_func;
};


//
//
var PopupMenu = Class.create({
    d: null,
    te: null,
    cb_show: null,
    cb_hide: null,

    initialize: function(id) {
        Event.observe(window, 'load', function() {
            this.d = $(id);
            this.d.setStyle({
                display: 'none',
                zIndex: 1000,
                position: 'absolute',
                margin: 0,
                padding: 0,
                // border: '4px solid #ffff00'
                border: '4px solid #ffff00'
            });

            this.d.onmouseout = function (e) {
                var x = Event.pointerX(e || window.event);
                var y = Event.pointerY(e || window.event);

                if (!Position.within(this.d, x, y)) {
                    this.hide();
                }
            }.bind(this);
        }.bind(this));

        Event.observe(window, 'resize', function() {
            this.recalcPosition();
        }.bind(this));
    }

    , isDisplay: function() {
        return this.d.getStyle('display') != 'none';
    }

    , setElement: function(e) {
        this.te = e;
        this.recalcPosition();
    }

    , onClick: function(func) {
        this.d.onclick = func;
    }
    , setContent: function(html) {
        this.d.update(html);
    }

    , recalcPosition: function() {
        if (!this.te) {
            return;
        }

        var offset = Position.cumulativeOffset(this.te);
        var d = this.te.getDimensions();
        var p = {};

        p.left = (offset[0] + 0) + 'px';
        p.top = (offset[1] + 0) + 'px';

        if (Prototype.Browser.IE) {
            p.width = (d.width - 0) + 'px';
            p.height = (d.height - 0) + 'px';
        }
        else {
            p.width = (d.width - 8) + 'px';
            p.height = (d.height - 8) + 'px';
        }

        this.d.setStyle(p);
    }

    , setStyle: function(style) {
        this.d.setStyle(style);
    }

    , setShowCallback: function(func) {
        this.cb_show = func;
    }

    , setHideCallback: function(func) {
        this.cb_hide = func;
    }

    , show: function() {
        this.cb_show && this.cb_show();
        this.d.show();
    }

    , hide: function() {
        this.cb_hide && this.cb_hide();
        this.d.hide();
    }
});

var AlphaScreenClass = Class.create({
    s: null,
    c: null,

    initialize: function() {
        this.s = $(document.createElement('div'));
        this.s.id = '__screen';
        this.s.setStyle({
            display: 'none',
            position: 'absolute',
            backgroundColor: '#ffffff',
            zIndex: 10000
        });

        Event.observe(window, 'resize', this._resize.bind(this));
        Event.observe(window, 'scroll', this._resize.bind(this));
    }

    , init: function() {
        /*
        Event.observe(window, 'load', function() {
        }.bind(this));
        */

        document.body.appendChild(this.s);
        this._resize();
    }

    , _resize: function() {
        var b = getWindowBounds();

        this.s.setStyle({
            top: '0px',
            left: '0px',
            width: (b.w + b.x) + 'px',
            height: (b.h + b.y) + 'px'
        });
    }

    , setAlpha: function(opacity) {
        this.s.setStyle({
            'opacity': opacity
        });
    }

    , onClick: function(func) {
        this.s.onclick = func;
        if (this.c) {
            this.c.onclick = func;
        }
    }

    , onClickScreen: function(func) {
        this.s.onclick = func;
    }

    , onClickContainer: function(func) {
        if (this.c) {
            this.c.onclick = func;
        }
    }

    , setContainer: function(e) {
        this.c = e;
    }

    , setStyle: function(style) {
        this.s.setStyle(style);
    }

    , centerize: function() {
        if (!this.c) {
            return;
        }

        var b = getWindowBounds();

        var d = this.c.getDimensions();

        this.c.setStyle({
            top: (b.h / 2 - d.height / 2) + 'px',
            left: (b.w / 2 - d.width / 2) + 'px'
        });
    }

    , show: function() {
        [this.s, this.c].each(function(e) {
            if (e) {
                e.show();
            }
        });
    }

    , hide: function() {
        [this.s, this.c].each(function(e) {
            if (e) {
                e.hide();
            }
        });
    }
});

AlphaScreenClass = AlphaScreenClass.toSingleton();
var AlphaScreen = AlphaScreenClass.getInstance();


//
//
var g_kb = false;
try {
    g_kb = new HotKey();
    g_kb.add('r', function() { location.reload(); });
    g_kb.add('e', function() { location.href = '/'; });
    g_kb.add('m', function() { location.href = '/home/'; });
}
catch (e) {
}

function keyboard_scroll(dx, dy) {
    var x = getScrollLeft() + dx;
    var y = getScrollTop() + dy;
    window.scrollTo(x, y);
}


/*
 * Orginal: http://adomas.org/javascript-mouse-wheel/
 * prototype extension by "Frank Monnerjahn" <themonnie@gmail.com>
 */
Object.extend(Event, {
    wheel:function (event){
        var delta = 0;
        if (!event) event = window.event;
        if (event.wheelDelta) {
            delta = event.wheelDelta/120; 
            if (window.opera) delta = -delta;
        } else if (event.detail) { delta = -event.detail/3; }
        return Math.round(delta); //Safari Round
    }
});


// ui click track
function ga_uct(path) {
    if (g_ga_enabled && window.urchinTracker) {
        urchinTracker('/@ui/click' + path);
    }
}

// ui time track
function ga_utt(path, time) {
    if (g_ga_enabled && window.urchinTracker) {
        var t = Math.round(time / 1000);
        urchinTracker('/@ui/time' + path + '/' + t);
    }
}

