/* This JavaScript file accompanies Famiva.com
 * (c) Famiva LLC
 */
function removeIfExists(id) {
    var elem = $(id)
    if (elem) Element.remove(id)
}

function showSubMenu(id) {
    var show = $(id).style.display=="none";
    $$('.inactivePopdown').each(
        function(elm) {
            elm.style.display = 'none';
        }
        );
    if (show)
        $(id).style.display="block";
}

function validateEmail(element_id, message) {
    var reFormat=/^.+@.+\..{2,4}$/;
    var reInvalidChars=/[\(\)\<\>\,\;\:\\\/\"\[\]]/;
    val = $(element_id).value;
    if (reFormat.test(val) && !(reInvalidChars.test(val)))
    {
        return true;
    }
    else {
        alert(message);
        return false;
    }
}

function confirmIfEmail(element_id, no_email_message, valid_email_message) {
    // return if no element
    elm = $(element_id)
    if (!elm) return true;
    if (elm.value=='') { // empty
    /*if (!confirm(no_email_message))
   {
     elm.focus();
     return false;
   }*/
    } else { // has value, must be valid
        return validateEmail(element_id, valid_email_message);
    }
    return true;
}

function enableFck(fck_id) {
    var editor = FCKeditorAPI.GetInstance(fck_id);
    editor.EditorDocument.designMode = "on";
}

function toggleAllBoxes(checkAll, selecter) {
    var mark = (checkAll.checked) ? true : false;
    $$(selecter).each(
        function(elm) {
            elm.checked = mark;
        }
        );
}

function checkSelected(selector, nothingSelectedMessage) {
    // anything checke;
    trace(selector);
    var hasChecked = false
    $$(selector).each(
        function(box) {
            if (box.checked==true) hasChecked = true
        }
        );
    if (!hasChecked) {
        alert(nothingSelectedMessage);
        return false;
    } else return true;
}

var ClipsOrganizer = {
    add: function(confirmationMessage, nothingSelectedMessage) {
        dest = $('move_to_destination');
        dest_index = dest.selectedIndex;
        if (dest_index==0) return;
        ClipsOrganizer.organize('add', confirmationMessage, nothingSelectedMessage);
    },
  
    organize: function(action, confirmationMessage, nothingSelectedMessage) {
        if (!ClipsOrganizer.checkSelected(nothingSelectedMessage)) return;
        if (confirm(confirmationMessage)) {
            $('organize_action').value = action;
            Element.show('spinner_organizing')
            new Ajax.Request('/clips/organize', {
                asynchronous:true,
                evalScripts:true,
                onComplete:function(request) {
                    ClipsOrganizer.reset();
                    Element.hide('spinner_organizing');
                },
                parameters:Form.serialize($('FormOrganizeClips'))
            });
        }
    },
  
    toggle: function(guid) {
        cbox = $('check_' + guid)
        if (cbox.checked) {
            ClipsOrganizer.mark(guid, false);
        } else {
            ClipsOrganizer.mark(guid, true);
        }
    },
  
    mark: function(guid, checked) {
        clip = $(guid);
        cbox = $('check_' + guid);
        if (checked) {
            cbox.checked = true;
            clip.removeClassName("thumbnailNotSelected")
            clip.addClassName("thumbnailSelected");
        } else {
            cbox.checked = false;
            clip.removeClassName("thumbnailSelected")
            clip.addClassName("thumbnailNotSelected");
        }
    },
  
    markAll: function(checked) {
        $$('IMG.draggableThumbnail').each(
            function(elm) {
                ClipsOrganizer.mark(elm.id, checked);
            }
            );
    },
  
    reset: function() {
        ClipsOrganizer.markAll(false);
        $('move_to_destination').selectedIndex = 0;
    },
  
    checkSelected: function(nothingSelectedMessage) {
        // anything checked?
        var hasChecked = false
        $$('#FormOrganizeClips input.checkbox').each(
            function(box) {
                if (box.checked==true) hasChecked = true
            }
            );
        if (!hasChecked) {
            alert(nothingSelectedMessage);
            ClipsOrganizer.reset();
            return false;
        } else return true;
    }
}

function slideshow(name, start_with)
{
  PicLensLite.start({
    feedUrl: '/clips/slideshow/' + name + '.rss',
    maxScale: '0',
    guid: start_with
  })
}