var hasAlertBox = typeof SexyAlertBox != "undefined";

/**
 * Loads a javascript file into the current document.  This would be the equivalent
 * of using include() in php
 * @param filename A string that represents the path to the javascript file you want to load.
 */
function loadJS(filename){
    var fileref=document.createElement('script');
    fileref.setAttribute("type","text/javascript");
    fileref.setAttribute("src", filename);
    if (typeof fileref!="undefined")
        document.getElementsByTagName("head")[0].appendChild(fileref);
}
/**
 * Loads a css stylesheet into the current document.  This would be the equivalent
 * of using include() in php
 * @param filename A string that represents the path to the css file you want to load.
 */
function loadCSS(filename){
    var fileref=document.createElement("link");
    fileref.setAttribute("rel", "stylesheet");
    fileref.setAttribute("type", "text/css");
    fileref.setAttribute("href", filename);
    if (typeof fileref!="undefined")
        document.getElementsByTagName("head")[0].appendChild(fileref);
} 

/**
 * Creates a cookie
 * @param name The name of the cookie
 * @param value The value of the cookie
 * @param days The number of days this cookie should be stored
 */
function createCookie(name,value,days) {
	Cookie.write(name, value, {duration: days, path:'/'});
}

/**
 * Reads a cookie
 * @param name The name of the cookie
 */
function readCookie(name) {
	return Cookie.read(name);
}

/**
 * Hides an announcement an updates the user's cookies to reflect this
 * @param announcementId The id of the announcement being hidden
 */
function hideAnnouncement(announcementId){
    $("announcement"+announcementId).destroy();
    createCookie("latestAnnouncement",announcementId,365);
}

/**
 * Loads all the required AlertBox files if necessary
 */
function loadAlertBox(){
    if(!hasAlertBox){
        loadJS('/js/sexyalert.js');
        loadCSS('/css/sexyalertbox.css');
        hasAlertBox = true;
        return false;
    }
    else{
        return true;
    }
}

function loadCaptcha(){
    loadJS('http://api.recaptcha.net/js/recaptcha_ajax.js');
}

/**
 * Shows an "info" dialog box
 * @param message The string that should be displayed.  If you need to display
 *  an entire element, use Element.innerHTML
 * @param returnFunction A function that should be executed once the user hits ok
 */
function generateDialog(message,type,returnFunction){
    var masterDiv = new Element('div',{'id':'SexyAlertBox-Box','style':'display: block; z-index: 65557; position: absolute; width: 500px;'});
    var styleClass = "";
    if(type == 'confirm'){
        styleClass = 'BoxConfirm';
    }
    else{
        styleClass = "BoxAlert";
    }
    var container = new Element('div',{'id':'SexyAlertBox-BoxContenedor','class':styleClass});
    masterDiv.adopt(
        new Element('div',{'id':'SexyAlertBox-InBox'}).adopt(
        new Element('div',{'id':'SexyAlertBox-BoxContent'}).adopt(container)
    ));
    container.adopt(message);
    sizes = window.getSize();
    scrollito = window.getScroll();
    masterDiv.injectInside(document.body);
    masterDiv.setStyle('left',(scrollito.x + (sizes.x - 500) / 2).toInt());
    masterDiv.setStyle('top',(scrollito.y + (sizes.y - masterDiv.offsetHeight) / 2).toInt());
    window.addEvent('scroll', replaceBox.bind($('SexyAlertBox-Box')));
    var buttons = new Element('div',{'id':'SexyAlertBox-Buttons'});
    var okButton = new Element('input',{'type':'button','value':'OK','style':'width: 70px;'});
    okButton.addEvent('click', function() {
        if(returnFunction != null){
            returnFunction();
        }
        $('BoxOverlay').destroy();
        $('SexyAlertBox-Box').destroy();
      }.bind(this));
    buttons.adopt(okButton);
    if(type == 'confirm'){
        var cancelButton = new Element('input',{'type':'button','value':'Cancel','style':'width: 70px;'});
        cancelButton.addEvent('click', function() {
            $('BoxOverlay').destroy();
            $('SexyAlertBox-Box').destroy();
          }.bind(this));
        buttons.adopt(cancelButton);
    }
    container.adopt(buttons);
    masterDiv.fade('in');
    var overlay = new Element('div', {
            'id': 'BoxOverlay',
            'styles': {
                    'display': 'block',
                    'z-index': 65555,
                    'position': 'absolute',
                    'top': '0',
                    'left': '0',
                    'background': '#000',
                    'visibility': 'visible',
                    'opacity': 0,
                    'height': window.getScrollHeight() + 'px',
                    'width': window.getScrollWidth() + 'px'
            }
    });
    overlay.setStyle('opacity','0.7');
    overlay.injectInside(document.body);
}
/**
 * Shows an "info" dialog box
 * @param message The string that should be displayed.  If you need to display
 *  an entire element, use Element.innerHTML
 * @param returnFunction A function that should be executed once the user hits ok
 */
function showInfoDialog(message,returnFunction){
    generateDialog(message,'alert',returnFunction);
}
function replaceBox() {
    sizes = window.getSize();
    scrollito = window.getScroll();
    var MoveBox = new Fx.Morph($('SexyAlertBox-Box'), {
                duration: 500,
                transition: Fx.Transitions.Back.easeOut
        }).start({
                'left': (scrollito.x + (sizes.x - 500) / 2).toInt(),
                'top': (scrollito.y + (sizes.y - $('SexyAlertBox-Box').offsetHeight) / 2).toInt()
        });

}
function showConfirmDialog(message,returnFunction){
    generateDialog(message,'confirm',returnFunction);
}

function register(target){
    var request = new Request.HTML({
        url:'/members/register.php?ajax=true',
        onSuccess:function(responseTree, responseElements, responseHTML, responseJavaScript){
            var box = new Element('div');
            box.innerHTML = responseHTML;
            showConfirmDialog(box,function(){$('registrationForm').submit()});
            Recaptcha.create("6LdzHwcAAAAAAIOX4R_JykzswNRbGwRPWdzO2oqt",
                "captcha", {
                theme: "red",
                callback: Recaptcha.focus_response_field
        });
        $('registerButton').destroy();
        $('registrationForm').setAttribute("onsubmit", "return false;");
        if(!target){
            target = window.location;
        }
        $('registrationTarget').setAttribute('value', target);
    }});
    request.send();
    return false;
}

