// CONFIRM PROMPT
/* <a href="http://www.google.com/" onclick="return confirmPrompt(this.href, 'Are you sure you want to?');">Test</a> */
function confirmPrompt(url, msg) {
    if (confirm(msg)) window.location = url;
    return false;
}

// POPUP
/* <a href="http://www.google.com/" onclick="return doPopup(this.href, {location:'yes'});">Test</a> */
function doPopup(url, options) {
    var Popup = {
        open: function(url, options) {
            this.options = {
                width: 600,
                height: 500,
                name: '_blank',
                location: 'no',
                menubar: 'no',
                toolbar: 'no',
                status: 'yes',
                scrollbars: 'yes',
                resizable: 'yes',
                top: '',
                left: '',
                center: true
            }
            Object.extend(this.options, options || {});
            this.options.width = this.options.width < screen.availWidth ? this.options.width : screen.availWidth;
            this.options.height = this.options.height < screen.availHeight ? this.options.height : screen.availHeight;
            if (this.options.center) {
                this.options.top = (screen.height - this.options.height) / 2 ;
                this.options.left = (screen.width - this.options.width) / 2;
            }
            var openoptions = 'width=' +this.options.width+ ',height=' +this.options.height+ ',location=' +this.options.location+ ',menubar=' +this.options.menubar+ ',toolbar=' +this.options.toolbar+ ',scrollbars=' +this.options.scrollbars+ ',resizable=' +this.options.resizable+ ',status=' +this.options.status
            if (this.options.top != '') openoptions += ',top=' +this.options.top;
            if (this.options.left != '') openoptions += ',left=' +this.options.left;
            window.open(url, this.options.name, openoptions);
            return false;
        }
    }
    return Popup.open(url, options);
}

// HANDLE AJAX JSON RESPONSE
function handleResponse(transport) {
    var content = (transport.responseText).evalJSON();
    Object.keys(content.elements).each(function(item) {
        $(item).update(content['elements'][item]);
    });
    Object.keys(content.javascript).each(function(item) {
        eval( content['javascript'][item]);
    });
}

// SET COOKIE
function setCookie(c_name, value, expiredays, path) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie = c_name+ "=" +escape(value) + ((expiredays==null) ? "" : ";expires=" +exdate.toGMTString()) + ";path=" +path;
}

// GET COOKIE
function getCookie(name) {
    var search = name + "="
    var returnvalue = "";
    if (document.cookie.length > 0) {
        offset = document.cookie.indexOf(search)
        // COOKIE EXISTS
        if (offset != -1) {
            offset += search.length
            // SET INDEX OF BEGINNING OF VALUE
            end = document.cookie.indexOf(";", offset);
            // SET INDEX OF END OF COOKIE VALUE
            if (end == -1) end = document.cookie.length;
            returnvalue=unescape(document.cookie.substring(offset, end))
        }
    }
    return returnvalue;
}

// PRELOAD IMAGE
function preloadImage(img) {
    var preload = new Image();
    preload.src = img;
}


/*** CUSTOM FUNCTIONS ***/

var num_quotes = 0;

// ROTATE QUOTES
var rQ = Class.create({

    initialize: function(init) {
        if (num_quotes > 0) {
            this.img_dir = '/images/layout/quotes/';
            this.img_ext = '.png';
            this.el = $('quote').select('img')[0];
            if (init) {
                this.preload(this.next(this.current()));
                this.timer();
            } else {
                this.change();
            }
        }
    },

    change: function() {
        thisObj = this;
        var newimg = this.next(this.current());
        Effect.Fade(thisObj['el'], {
            duration: 1,
            afterFinish: function() {
                thisObj['el'].writeAttribute('src', thisObj['img_dir'] + newimg + thisObj['img_ext']);
                Effect.Appear(thisObj['el'], {
                    duration: 1,
                    afterFinish: function() {
                        thisObj.preload(thisObj.next(newimg));
                        thisObj.timer();
                    }
                });
            }
        });
    },

    current: function() {
        return parseInt(this['el'].readAttribute('src').replace(this['img_dir'], '').replace(this['img_ext'], ''));
    },

    next: function(num) {
        if (num < num_quotes) {
            return num + 1;
        } else {
            return 1;
        }
    },

    preload: function(img) {
        preloadImage(this['img_dir'] + img + this['img_ext']);
    },

    timer: function() {
        thisObj = this;
        setTimeout(function() {
            thisObj.change();
        }, 8000);
    }

});

// CUSTOM IMAGE CHANGER
var cIC = Class.create({

    initialize: function(cel, options) {
        var timer;
        this.cel = cel;
        this.changing = false;
        this.options = {
            startNum: 1,
            width: 0,
            height: 0,
            duration: .5,
            rotate: false,
            rotateStartDelay: 0,
            rotatePause: 6,
            mi_duration: .4,
            mi_text: 'more info',
            mi_ltext: 'less info'
        }
        Object.extend(this.options, options || {});
        this.options['startNum'] = parseInt(this.options['startNum']);
        this.options['width'] = parseInt(this.options['width']);
        this.options['height'] = parseInt(this.options['height']);
        this.options['duration'] = parseFloat(this.options['duration']);
        this.options['rotateStartDelay'] = parseFloat(this.options['rotateStartDelay']);
        this.options['rotatePause'] = parseFloat(this.options['rotatePause']);
        this.options['mi_duration'] = parseFloat(this.options['mi_duration']);
        this.curnum = this.options['startNum'];
        this.createLinks();
        this.change(this.curnum, true);
        this.moreInfo();
    },

    createLinks: function() {
        var numi = $(this.cel).select('.img_container img').length;
        if (numi > 1) {
            var pe = $(this.cel).select('.link_container')[0];
            for (i=1; i<=numi; i++) {
                var newA = new Element('a', {
                    href: '#',
                    onclick: 'return false;',
                    onmousedown: 'void(0); return false;',
                    onmouseup: 'void(0); return false;',
                    title: i
                }).update(i);
                pe.insert({ 'bottom': newA });
            }
            this.attachClick();
        } else {
            this.options['rotate'] = false;
        }
    },

    attachClick: function() {
        var thisObj = this;
        $(this.cel).select('.link_container a').each(function(item) {
            Event.observe(item, 'click', function() {
                thisObj.change(parseInt(item.readAttribute('title')));
            });
        });
    },

    change: function(num, init) {
        var thisObj = this;
        if (!this.changing) {
            this.changing = true;
            if ((this.timer) && (this.options['rotate'])) clearTimeout(thisObj.timer);
            var dur = (init) ? 0 : this.options['duration'];
            var scroller = $(this.cel).select('.img_scroller')[0];
            if (!$(this.cel).select('.link_container a[title="' +num+ '"]')[0]) num = 1;
            if (this.options['height'] == 0) {
                this.options['height'] = $(this.cel).select('.img_container')[0].getHeight();
            }
            var ypos = ((num * this.options['height']) - this.options['height']) * -1;
            new Effect.Move(scroller, {
                x: 0,
                y: ypos,
                mode: 'absolute',
                duration: dur,
                transition: Effect.Transitions.SwingFromTo,
                afterFinish: function() {
                    $(thisObj.cel).select('.link_container a').each(function(item) {
                        item.removeClassName('selected');
                    });
                    if ($(thisObj.cel).select('.link_container a[title="' +num+ '"]')[0]) {
                        $(thisObj.cel).select('.link_container a[title="' +num+ '"]')[0].addClassName('selected');
                    }
                    thisObj.changing = false;
                    if (thisObj.options['rotate']) {
                        if ((init) && (thisObj.options['rotateStartDelay'] > 0)) {
                            var rp = (thisObj.options['rotateStartDelay'] * 1000);
                        } else {
                            var rp = (thisObj.options['rotatePause'] * 1000);
                        }
                        thisObj.timer = setTimeout(function() {
                            thisObj.change(thisObj.curnum + 1);
                        }, rp);
                    }
                }
            });
        }
        this.curnum = num;
    },

    moreInfo: function() {
        var thisObj = this;
        var miel = $(this.cel).select('.more_info')[0];
        if (miel) {
            miel.hide();
            this.moreInfo_LinkText(miel);
            if (miel.innerHTML.replace(/^\s+|\s+$/g,"").length > 0) {
                var lel = $(this.cel).select('.more_info-link')[0];
                lel.down('a').writeAttribute('href', '#');
                lel.down('a').writeAttribute('onclick', 'return false;');
                lel.down('a').writeAttribute('onmousedown', 'void(0); return false;');
                lel.down('a').writeAttribute('onmouseup', 'void(0); return false;');
                lel.show();
                Event.observe(lel, 'click', function() {
                    Effect.toggle(miel, 'slide', {
                        duration: thisObj.options['mi_duration'],
                        afterFinish: function() {
                            thisObj.moreInfo_LinkText(miel);
                        }
                    });
                });
            }
        }
    },

    moreInfo_LinkText: function() {
        var lel = $(this.cel).select('.more_info-link')[0];
        var miel = $(this.cel).select('.more_info')[0];
        if (miel.visible()) {
            var text = this.options['mi_ltext'];
        } else {
            var text = this.options['mi_text'];
        }
        lel.down('a').update(text);
    }

});

// ON PAGE LOAD
Event.observe(window, 'load', function() {

    new rQ(true);

});