var paddingWidth = 16;

function launchDocument(url, options) {
    if(typeof options == "undefined") options = {};
    if(typeof url == "object") url = url.getAttribute('url');
    if(options['newWindow']) {
		window.open(url, '_blank');
    } else {
        window.location.href = url;
    }
}

function launchReload(e, options) {
    launchDownload(e, {reuseWindow:true});
}

function launchDownload(e, options) {
    if(typeof options == "undefined") options = {};
    var type = (typeof e == 'string') ? 'FILES' : e.getAttribute('type').toUpperCase();
    var url = (typeof e == 'string') ? e : e.getAttribute('url');

    switch(type) {
        case 'FILES':
            url = '/files/downloads/' + url;
            break;
        case 'MEDIA':
            url = '/files/media/' + url;
            break;
        case 'EXTERNAL':
            break;
        default:
            launchOverlay(e, options);
            return; break;
    }

    if(options['reuseWindow']) {
        window.location.reload(url);
    } else {
        $('_DOWNLOAD_').href = url;
        $('_DOWNLOAD_').click();
        $('_DOWNLOAD_').href = '';
    }
}

function launchOverlay(e, options) {
    if (typeof options == "undefined") options = {};
    options['fullScreen'] = true;
    launchPanel(e, options);
}

function launchPanel(e, options) {
    if (typeof options == "undefined") options = {};
    var loaderHTML = '<IMG id="LOADER" src="/img/loader.gif" />';

    if (options["fullScreen"] || false) ezyOverlay();
    Effect.Fade("CONTENT", {duration:0.75});

    $("PANEL").style.display = "block";
    $("PANEL").getElementsByTagName("DIV")[0].innerHTML = loaderHTML;

    var type = (typeof e == 'string') ? 'HTML' : (e.getAttribute('type') || 'html').toUpperCase();
    var url = (typeof e == 'string') ? e : e.getAttribute('url');
    switch(type) {
        case 'FILES':
            url = 'inline?data=title[]type[files]style[]link[' + url + ']body[]param[]';
            break;
        case 'MEDIA':
            url = 'inline?data=title[]type[media]style[]link[' + url + ']body[]param[]';
            break;
    }

    if(typeof e == 'string') {
        window.setTimeout('getContent("PANEL", {}, "' + e + '", "")', 1250);
    } else {
        if (options["fullScreen"] || false)
            window.setTimeout('getContent("PANEL", ' +
                              '{width:' + ((options['width'] || '640') + paddingWidth) + ',height:' + ((options['height'] || '480') + paddingWidth) + ',fullScreen:true,close:true},' +
                              '"' + url + '", ' +
                              '"' + (e.getAttribute('callback') || '') + '")', 1250);
        else
            window.setTimeout('getContent("PANEL", {}, ' +
                              '"' + url + '", ' +
                              '"' + (e.getAttribute('callback') || '') + '")', 1250);
    }
}

function getContent(id, options, url, callback) {
	oAjax = new Ajax.Request("/content/" + url + ((url.indexOf("?")==-1) ? "?" : "&") + (new Date()).getTime(),
	{
		method: 'get',
		onComplete: function(oAjax) {
            $(id).getElementsByTagName('DIV')[0].innerHTML = oAjax.responseText;
            var styleID = $(id).getElementsByTagName('DIV')[0].getElementsByTagName('FIELDSET')[0].getAttribute('styleID') || 'aquaStyle';
            if (options['fullScreen'] || false) ezyContainer(id, styleID, options);
            else ezyContainer(id, styleID, {width:535,close:true});
            if (callback != "") eval(callback);
            window.setTimeout('ezyRSS_Scan("PANEL")', 500);
        }
	});
}

var container = {
    show : function(id) { $(id + '-cp').style.visibility = 'visible'; },
    hide : function(id) { $(id + '-cp').style.visibility = 'hidden'; },
    close: function(id) {
        id = id || 'PANEL';
        if ($('BACKDROP')) ezyOverlay();
        $(id).getElementsByTagName("DIV")[0].innerHTML = "";
        $(id).style.display = "none";
        Effect.Appear("CONTENT", {duration:0.5});
        window.setTimeout('ezyRSS_Scan("CONTENT", true)', 500);
    }
}

var rss = {
    playlist: function(id) {
            launchPanel($('RSS').getAttribute('inline'));
            rss.scroller(id);
    },
    scroller: function(id) {
            if(typeof $('SCROLL') == 'undefined') window.setTimeout('rss.scroller("' + id + '")', 500);
            else ezyScroller('SCROLL', {interval:10});
    },
    play : function(id) {
        if($(id + '-cp').getElementsByTagName('DIV')[1].className == 'rss-Pause') {
            var customControl = $('RSS').getAttribute('customControl');
            $(id + '-cp').innerHTML = customControl.replace('rss-Pause', 'rss-Play');
            putCookie('RSS', 'command', 2);
        } else {
            $(id + '-cp').innerHTML = $('RSS').getAttribute('customControl');
            putCookie('RSS', 'command', 0);
            eval('ezyRSS("RSS", ' + getCookie('RSS', 'options') + ')');
        }
    },
    previous : function(id) {
        $(id + '-cp').innerHTML = $('RSS').getAttribute('customControl');
        putCookie('RSS', 'command', 1);
        eval('ezyRSS("RSS", ' + getCookie('RSS', 'options') + ')');
    },
    next: function(id) {
        $(id + '-cp').innerHTML = $('RSS').getAttribute('customControl');
        putCookie('RSS', 'command', 3);
        eval('ezyRSS("RSS", ' + getCookie('RSS', 'options') + ')');
    }
}

var scroller = {
    play : function(id) {
        if($(id + '-cp').getElementsByTagName('DIV')[1].className == 'rss-Pause') {
            var customControl = $('SCROLL').getAttribute('customControl');
            $(id + '-cp').innerHTML = customControl.replace('rss-Pause', 'rss-Play');
            putCookie('SCROLL', 'command', 2);
        } else {
            $(id + '-cp').innerHTML = $('SCROLL').getAttribute('customControl');
            putCookie('SCROLL', 'command', 0);
            eval('ezyScroller("SCROLL", ' + getCookie('SCROLL', 'options') + ')');
        }
    },
    previous : function(id) {
        var customControl = $('SCROLL').getAttribute('customControl');
        $(id + '-cp').innerHTML = customControl.replace('rss-Pause', 'rss-Play');
        putCookie('SCROLL', 'command', 1);
        eval('ezyScroller("SCROLL", ' + getCookie('SCROLL', 'options') + ')');
    },
    next: function(id) {
        var customControl = $('SCROLL').getAttribute('customControl');
        $(id + '-cp').innerHTML = customControl.replace('rss-Pause', 'rss-Play');
        putCookie('SCROLL', 'command', 3);
        eval('ezyScroller("SCROLL", ' + getCookie('SCROLL', 'options') + ')');
    }
}
