function checkAll(){
    var x = false;
    if (document.frm.elements.check_all.checked) {
        x = true;
    }
    var y = document.frm.elements['box[]'];
    var i = 0;
    for (i = 0; i < y.length; i++) {
        y[i].checked = x;
    }
}

function atleastone2(){
    var y = false;
    for (var x = 0; x < document.frm.length; x++) {
        if ((document.frm.elements[x].name === 'xmlbox[]') && (document.frm.elements[x].checked === true)) {
            y = true;
        }
    }
    if (y !== true) {
        Dialog.alert('<img src=\'images/exclamation.png\'> Please select at least one playlist!', {
            width: 250,
            className: 'alphacube',
            okLabel: 'OK'
        });
        return false;
    }
}

function atleastone(){
    var y = false;
    for (var x = 0; x < document.frm.length; x++) {
        if (document.frm.elements[x].checked === true) {
            y = true;
        }
    }
    if (y !== true) {
        Dialog.alert('<img src=\'images/exclamation.png\'> Please select at least one file!', {
            width: 250,
            className: 'alphacube',
            okLabel: 'OK'
        });
        return false;
    }
}

function KeepCount(){
    var i = 0;
    var len = document.getElementById('xmlname').options.length;
    var xml = document.frm.elements['xmlbox[]'];
    document.frm.elements.playlist_input.value = document.frm.elements['xmlbox[]'].value;
    for (i = 0; i < len; i++) {
        if (document.getElementById('xmlname').options[i].value === document.frm.elements.playlist_input.value) {
            document.getElementById('xmlname').options[i].selected = true;
        }
    }
    for (i = 0; i < xml.length; i++) {
        if (xml[i].checked === true) {
            document.frm.elements.playlist_input.value = xml[i].value;
            var x = document.frm.elements.playlist_input.value;
            for (i = 0; i < len; i++) {
                if (document.getElementById('xmlname').options[i].value === x) {
                    document.getElementById('xmlname').options[i].selected = true;
                }
            }
        }
    }
}

function clearsel(){
    var xml = document.frm.elements['xmlbox[]'];
    var x = document.getElementById('xmlname');
    xml.checked = false;
    if (x.options[x.selectedIndex].value != document.frm.elements.playlist_input.value) {
        document.frm.elements.playlist_input.value = x.options[x.selectedIndex].value;
        for (var i = 0; i < xml.length; i++) {
            if (xml[i].value == x.options[x.selectedIndex].value) {
                xml[i].checked = true;
            }
            else {
                xml[i].checked = false;
            }
        }
    }
}

function createchk(){
    var w = document.getElementById('xmltype');
    var y = document.frm.elements.playlist_input.value;
    var x = document.frm.elements['box[]'];
    var z = document.frm.elements.url_file.value;
    var xS = false;
    var wS = false;
    if (w.options[w.selectedIndex].value === 'subtitle') {
        wS = true;
    }
    if (y === '') {
        Dialog.alert('<img src=\'images/exclamation.png\'> insert or select a name for your playlist', {
            width: 250,
            className: 'alphacube',
            okLabel: 'OK'
        });
        return false;
    }
    else 
        if ((!x) && (z === '') && (!wS)) {
            Dialog.alert('<img src=\'images/exclamation.png\'> no file to add! upload some file, OR add a Remote Media!', {
                width: 250,
                className: 'alphacube',
                okLabel: 'OK'
            });
            return false;
        }
    if (x != null) {
        if (x.checked) {
            xS = true;
        }
        var i = 0;
        for (i = 0; i < x.length; i++) {
            if (x[i].checked) {
                xS = true;
            }
        }
    }
    if ((z === '') && (!xS) && (!wS)) {
        Dialog.alert('<img src=\'images/exclamation.png\'> You must select a least one file OR add a Remote Media!', {
            width: 250,
            className: 'alphacube',
            okLabel: 'OK'
        });
        return false;
    }
}

function checkinp(){
    var z = document.getElementById('xmlname');
    var y = document.frm.elements.playlist_input.value;
    var x = document.frm.elements['xmlbox[]'];
    if ((y === '') && (x != null)) {
        x.checked = false;
        z.value = '';
        var i = 0;
        for (i = 0; i < x.length; i++) {
            if (x[i].checked) {
                x[i].checked = false;
            }
        }
    }
}

function nomatch(){
    Dialog.alert('<img src=\'images/exclamation.png\'> Playlist formats doesn\' t match!', {
        width: 250,
        className: 'alphacube',
        okLabel: 'OK'
    });
    return false;
}

function success(){
    Dialog.alert('<img src=\'images/accept.png\'> playlist has been created!', {
        width: 250,
        className: 'alphacube',
        okLabel: 'OK'
    });
    return false;
}

function err_msg1(){
    new Effect.Pulsate('err1', {
        pulses: 5,
        duration: 1.5
    });
}

function err_msg2(){
    new Effect.Pulsate('err2', {
        pulses: 5,
        duration: 1.5
    });
}

var tooltip = function(){
    var id = 'tt';
    var top = 3;
    var left = 3;
    var maxw = 300;
    var speed = 10;
    var timer = 20;
    var endalpha = 95;
    var alpha = 0;
    var tt, t, c, b, h;
    var ie = document.all ? true : false;
    return {
        show: function(v, w){
            if (tt == null) {
                tt = document.createElement('div');
                tt.setAttribute('id', id);
                t = document.createElement('div');
                t.setAttribute('id', id + 'top');
                c = document.createElement('div');
                c.setAttribute('id', id + 'cont');
                b = document.createElement('div');
                b.setAttribute('id', id + 'bot');
                tt.appendChild(t);
                tt.appendChild(c);
                tt.appendChild(b);
                document.body.appendChild(tt);
                tt.style.opacity = 0;
                tt.style.filter = 'alpha(opacity=0)';
                document.onmousemove = this.pos;
            }
            tt.style.display = 'block';
            c.innerHTML = v;
            tt.style.width = w ? w + 'px' : 'auto';
            if (!w && ie) {
                t.style.display = 'none';
                b.style.display = 'none';
                tt.style.width = tt.offsetWidth;
                t.style.display = 'block';
                b.style.display = 'block';
            }
            if (tt.offsetWidth > maxw) {
                tt.style.width = maxw + 'px'
            }
            h = parseInt(tt.offsetHeight) + top;
            clearInterval(tt.timer);
            tt.timer = setInterval(function(){
                tooltip.fade(1)
            }, timer);
        },
        pos: function(e){
            var u = ie ? event.clientY + document.documentElement.scrollTop : e.pageY;
            var l = ie ? event.clientX + document.documentElement.scrollLeft : e.pageX;
            tt.style.top = (u - h) + 'px';
            tt.style.left = (l + left) + 'px';
        },
        fade: function(d){
            var a = alpha;
            if ((a != endalpha && d == 1) || (a != 0 && d == -1)) {
                var i = speed;
                if (endalpha - a < speed && d == 1) {
                    i = endalpha - a;
                }
                else 
                    if (alpha < speed && d == -1) {
                        i = a;
                    }
                alpha = a + (i * d);
                tt.style.opacity = alpha * .01;
                tt.style.filter = 'alpha(opacity=' + alpha + ')';
            }
            else {
                clearInterval(tt.timer);
                if (d == -1) {
                    tt.style.display = 'none'
                }
            }
        },
        hide: function(){
            clearInterval(tt.timer);
            tt.timer = setInterval(function(){
                tooltip.fade(-1)
            }, timer);
        }
    };
}();

function addtips(e){

        t = e.getAttribute('title');
        tooltip.show(t);
        e.removeAttribute('title');
   
}

function deltips(e){

        e.writeAttribute('title', t);
        tooltip.hide();

}

/**
 * Toggle Function -  aseptik@gmail.com (c) '09
 *
 */
 
function tog(e){

    var myArray = ['properties', 'colors', 'layout', 'playback', 'communication', 'accessibility', 'embedparams'];
          
        t = e.className;

        myArray.splice(myArray.indexOf(t),1);
        
        for( var i=0; i < myArray.length; i++ )
     { 
        Effect.Fade(myArray[i], {
                duration: 0.2
            });
      } 
                Effect.Appear(t, {
                duration: 0.2
            });
            
            return false;
   
}

function gid(name){
    return document.getElementById(name);
}

var perline = 9;
var divSet = false;
var curId;
var colorLevels = Array('0', '3', '6', '9', 'C', 'F');
var colorArray = Array();
var ie = false;
var nocolor = 'none';
if (document.all) {
    ie = true;
    nocolor = '';
}
function addColor(r, g, b){
    var red = colorLevels[r];
    var green = colorLevels[g];
    var blue = colorLevels[b];
    addColorValue(red, green, blue);
}

function addColorValue(r, g, b){
    colorArray[colorArray.length] = r + r + g + g + b + b;
}

function setColor(color){
    var link = gid(curId);
    var field = gid(curId);
    var prev = gid(curId + 'pick');
    var picker = gid('colorpicker');
    field.value = color;
    prev.style.backgroundColor = '#' + color;
    if (color == '') {
        color = nocolor;
    }
    picker.style.display = 'none';
    eval(gid(curId).title);
}

function setDiv(){
    if (!document.createElement) {
        return;
    }
    var elemDiv = document.createElement('div');
    if (typeof(elemDiv.innerHTML) != 'string') {
        return;
    }
    genColors();
    elemDiv.id = 'colorpicker';
    elemDiv.innerHTML = getColorTable();
    document.body.appendChild(elemDiv);
    divSet = true;
}

function pickColor(id){
    if (!divSet) {
        setDiv();
    }
    var picker = gid('colorpicker');
    if (id == curId && picker.style.display == 'block') {
        picker.style.display = 'none';
        return;
    }
    curId = id;
    var thelink = gid(id);
    picker.style.top = getAbsoluteOffsetTop(thelink) + "px";
    picker.style.left = getAbsoluteOffsetLeft(thelink) + "px";
    picker.style.display = 'block';
}

function genColors(){
    addColorValue('0', '0', '0');
    addColorValue('3', '3', '3');
    addColorValue('6', '6', '6');
    addColorValue('8', '8', '8');
    addColorValue('9', '9', '9');
    addColorValue('A', 'A', 'A');
    addColorValue('C', 'C', 'C');
    addColorValue('E', 'E', 'E');
    addColorValue('F', 'F', 'F');
    for (a = 1; a < colorLevels.length; a++) 
        addColor(0, 0, a);
    for (a = 1; a < colorLevels.length - 1; a++) 
        addColor(a, a, 5);
    for (a = 1; a < colorLevels.length; a++) 
        addColor(0, a, 0);
    for (a = 1; a < colorLevels.length - 1; a++) 
        addColor(a, 5, a);
    for (a = 1; a < colorLevels.length; a++) 
        addColor(a, 0, 0);
    for (a = 1; a < colorLevels.length - 1; a++) 
        addColor(5, a, a);
    for (a = 1; a < colorLevels.length; a++) 
        addColor(a, a, 0);
    for (a = 1; a < colorLevels.length - 1; a++) 
        addColor(5, 5, a);
    for (a = 1; a < colorLevels.length; a++) 
        addColor(0, a, a);
    for (a = 1; a < colorLevels.length - 1; a++) 
        addColor(a, 5, 5);
    for (a = 1; a < colorLevels.length; a++) 
        addColor(a, 0, a);
    for (a = 1; a < colorLevels.length - 1; a++) 
        addColor(5, a, 5);
    return colorArray;
}

function getColorTable(){
    var colors = colorArray;
    var tableCode = '';
    tableCode += '<div id="copick" class=""><table>';
    for (i = 0; i < colors.length; i++) {
        if (i % perline == 0) {
            tableCode += '<tr>';
        }
        tableCode += '<td><a style="color:#' + colors[i] + '; background:#' + colors[i] + ';" href="javascript:setColor(\'' + colors[i] + '\');">&nbsp;</a></td>';
        if (i % perline == perline - 1) {
            tableCode += '</tr>';
        }
    }
    if (i % perline != 0) {
        tableCode += '</tr>';
    }
    tableCode += '</table></div>';
    return tableCode;
}

function getAbsoluteOffsetTop(obj){
    var top = obj.offsetTop;
    var parent = obj.offsetParent;
    while (parent != document.body) {
        top += parent.offsetTop;
        parent = parent.offsetParent;
    }
    return top;
}

function getAbsoluteOffsetLeft(obj){
    var left = obj.offsetLeft;
    var parent = obj.offsetParent;
    while (parent != document.body) {
        left += parent.offsetLeft;
        parent = parent.offsetParent;
    }
    return left;
}

function addmmfp(mod){
    var file = $('mmfp_file').value ? '&file=' + $('mmfp_file').value : '';
    var width = $('mmfp_width').value ? '&width=' + $('mmfp_width').value : '';
    var height = $('mmfp_height').value ? '&height=' + $('mmfp_height').value : '';
    var author = $('mmfp_author').value ? '&author=' + $('mmfp_author').value : '';
    var description = $('mmfp_description').value ? '&description=' + $('mmfp_description').value : '';
    var captions = $('mmfp_captions').value ? '&captions.file=' + $('mmfp_captions').value : '';
    var duration = $('mmfp_duration').value ? '&duration=' + $('mmfp_duration').value : '';
    var image = $('mmfp_image').value ? 'I=' + $('mmfp_image').value : '';
    var link = $('mmfp_link').value ? '&link=' + $('mmfp_link').value : '';
    var startz = $('mmfp_start').value ? '&start=' + $('mmfp_start').value : '';
    var title = $('mmfp_title').value ? '&title=' + $('mmfp_title').value : '';
    var type = $('mmfp_type').value ? '&type=' + $('mmfp_type').value : '';
    var backcolor = $('mmfp_backcolor').value ? '&backcolor=' + $('mmfp_backcolor').value : '';
    var frontcolor = $('mmfp_frontcolor').value ? '&frontcolor=' + $('mmfp_frontcolor').value : '';
    var lightcolor = $('mmfp_lightcolor').value ? '&lightcolor=' + $('mmfp_lightcolor').value : '';
    var screencolor = $('mmfp_screencolor').value ? '&screencolor=' + $('mmfp_screencolor').value : '';
    var controlbar = $('mmfp_controlbar').value ? '&controlbar=' + $('mmfp_controlbar').value : '';
    var logo = $('mmfp_logo').value ? '&logo=' + $('mmfp_logo').value : '';
    var playlist = $('mmfp_playlist').value ? '&playlist=' + $('mmfp_playlist').value : '';
    var playlistsize = $('mmfp_playlistsize').value ? '&playlistsize=' + $('mmfp_playlistsize').value : '';
    var skin = $('mmfp_skin').value ? '&skin=' + $('mmfp_skin').value : '';
    var autostart = $('mmfp_autostart').value ? '&autostart=' + $('mmfp_autostart').value : '';
    var bufferlength = $('mmfp_bufferlength').value ? '&bufferlength=' + $('mmfp_bufferlength').value : '';
    var displayclick = $('mmfp_displayclick').value ? '&displayclick=' + $('mmfp_displayclick').value : '';
    var icons = $('mmfp_icons').value ? '&icons=' + $('mmfp_icons').value : '';
    var item = $('mmfp_item').value ? '&item=' + $('mmfp_item').value : '';
    var mute = $('mmfp_mute').value ? '&mute=' + $('mmfp_mute').value : '';
    var quality = $('mmfp_quality').value ? '&quality=' + $('mmfp_quality').value : '';
    var repeat = $('mmfp_repeat').value ? '&repeat=' + $('mmfp_repeat').value : '';
    var shuffle = $('mmfp_shuffle').value ? '&shuffle=' + $('mmfp_shuffle').value : '';
    var stretching = $('mmfp_stretching').value ? '&stretching=' + $('mmfp_stretching').value : '';
    var volume = $('mmfp_volume').value ? '&volume=' + $('mmfp_volume').value : '';
    var linktarget = $('mmfp_linktarget').value ? '&linktarget=' + $('mmfp_linktarget').value : '';
    var plugins = $('mmfp_plugins').value ? '&plugins=' + $('mmfp_plugins').value : '';
    var streamer = $('mmfp_streamer').value ? '&streamer=' + $('mmfp_streamer').value : '';
    var abouttext = $('mmfp_abouttext').value ? '&abouttext=' + $('mmfp_abouttext').value : '';
    var aboutlink = $('mmfp_aboutlink').value ? '&aboutlink=' + $('mmfp_aboutlink').value : '';
    var fontsize = $('mmfp_captionsfontsize').value ? '&captions.fontsize=' + $('mmfp_captionsfontsize').value : '';
    var captionsback = $('mmfp_captionsback').value ? '&captions.back=' + $('mmfp_captionsback').value : '';
    var captionsstate = $('mmfp_captionsstate').value ? '&captions.state=' + $('mmfp_captionsstate').value : '';
    var audiodesc = $('mmfp_audiodescription').value ? '&audiodescription.file=' + $('mmfp_audiodescription').value : '';
    var audiovolume = $('mmfp_audiodescriptionvolume').value ? '&audiodescription.volume=' + $('mmfp_audiodescriptionvolume').value : '';
    var audiostate = $('mmfp_audiodescriptionstate').value ? '&audiodescription.state=' + $('mmfp_audiodescriptionstate').value : '';
    if (!file) {
        mmfp_alert('', 'Source: is needed!', 350);
        return false;
    }
    if ((!width) || (!height)) {
        mmfp_alert('', 'Width:&Height: are needed!', 350);
        return false;
    }
    if ((!backcolor) || (!lightcolor) || (!screencolor) || (!frontcolor)) {
        mmfp_alert('', 'Colors: are needed!', 350);
        return false;
    }
     
     $('data').focus();
     
    addtext('[play]' + file + width + height + backcolor + frontcolor + lightcolor + screencolor + author + description + captions + duration + image + link + startz + type + controlbar + logo + playlist + playlistsize + skin + autostart + bufferlength + displayclick + icons + item + mute + quality + repeat + shuffle + stretching + volume + linktarget + plugins + streamer + abouttext + aboutlink + fontsize + captionsback + captionsstate + audiodesc + audiovolume + audiostate + '[/play]');
    return true;
}

function Pager(tableName, itemsPerPage){
    this.tableName = tableName;
    this.itemsPerPage = itemsPerPage;
    this.currentPage = 1;
    this.pages = 0;
    this.inited = false;
    this.showRecords = function(from, to){
        var rows = document.getElementById(tableName).rows;
        for (var i = 1; i < rows.length; i++) {
            if (i < from || i > to) {
                rows[i].style.display = 'none';
            }
            else {
                rows[i].style.display = '';
            }
        }
    };
    this.showPage = function(pageNumber){
        if (!this.inited) {
            alert("not inited");
            return;
        }
        var oldPageAnchor = document.getElementById('pg' + this.currentPage);
        oldPageAnchor.className = 'pg-normal';
        this.currentPage = pageNumber;
        var newPageAnchor = document.getElementById('pg' + this.currentPage);
        newPageAnchor.className = 'pg-selected';
        var from = (pageNumber - 1) * itemsPerPage + 1;
        var to = from + itemsPerPage - 1;
        this.showRecords(from, to);
    };
    this.prev = function(){
        if (this.currentPage > 1) {
            this.showPage(this.currentPage - 1);
        }
    };
    this.next = function(){
        if (this.currentPage < this.pages) {
            this.showPage(this.currentPage + 1);
        }
    };
    this.init = function(){
        var rows = document.getElementById(tableName).rows;
        var records = (rows.length - 1);
        this.pages = Math.ceil(records / itemsPerPage);
        this.inited = true;
    };
    this.showPageNav = function(pagerName, positionId){
        if (!this.inited) {
            alert("not inited");
            return;
        }
        var element = document.getElementById(positionId);
        var pagerHtml = '<span onclick="' + pagerName + '.prev();" class="pg-normal"> &#171 Prev </span>  ';
        for (var page = 1; page <= this.pages; page++) 
            pagerHtml += '<span id="pg' + page + '" class="pg-normal" onclick="' + pagerName + '.showPage(' + page + ');">' + page + '</span>  ';
        pagerHtml += '<span onclick="' + pagerName + '.next();" class="pg-normal"> Next &#187;</span>';
        element.innerHTML = pagerHtml;
    };
}

function gcolors(e){
    var go = true;
    var w = '';
    var b = $('colorbackcolor');
    var f = $('colorfrontcolor');
    var s = $('colorscreencolor');
    var l = $('colorlightcolor');
    if ((e === 'backcolor') && (!b) && (!f) && (!s) && (!l)) {
        w = 'backcolor';
    }
    else 
        if ((e === 'frontcolor') && (!b) && (!f) && (!s) && (!l)) {
            w = 'frontcolor';
        }
        else 
            if ((e === 'screencolor') && (!b) && (!f) && (!s) && (!l)) {
                w = 'screencolor';
            }
            else 
                if ((e === 'lightcolor') && (!b) && (!f) && (!s) && (!l)) {
                    w = 'lightcolor';
                }
                else {
                    go = false;
                }
    if (go) {
        w = new Window('color' + w, {
            className: 'alphacube',
            width: 170,
            height: 140,
            title: w,
            maximizable: false,
            minimizable: false
        });
        pickColor('mmfp_' + e);
        var p = $('colorpicker');
        $('copick').addClassName('copick');
        w.setContent('copick');
        p.fade({
            duration: 0.1
        });
        w.setDestroyOnClose();
        w.showCenter();
    }
    else {
        $('colorpicker').fade({
            duration: 0.1
        });
        w.destroy();
    }
}

