/**
 * Clear Channel Admin utilities library.
 *
 * Author: bkennedy
 * Created on: Jan 29, 2009
 */

/**
 * Handles the "Report Offensive Content" link.
 */
function approve_offensive_content(btn, nodeId, wsUrl) {

    YAHOO.util.Connect.asyncRequest('GET', wsUrl + '/' + nodeId, {
        success: function (o) {
            if (o.responseText.indexOf('OK') == 0) {
                alert('The content has been approved.');
            }
            else {
                alert('An error occurred reporting this content!');
            }
        },

        failure: function (o) {
            alert('Ajax request failed!');
        }
    });

    btn.value = 'Approved';

    return false;
}


/**
 * Media library integration. Handles images, video, polls, and flash.
 */
function mediaLibrary()
{

  var customNameCtl = getCustomTagControl('custom_name_source');

  if (!customNameCtl) {
      alert('An error occurred: Could not determine custom tag selected.');
      return;
  }

  var customName = customNameCtl.options[customNameCtl.selectedIndex].value;

  mode = 'ezp';
  var path, width, height;

  if (customName == 'image') {
    path = '/mlib/?type=image';
    width = 800;
    height = 600;
  }
  else if (customName == 'ccomr_video') {
    path = '/mlib/?type=video';
    width = 800;
    height = 600;
  }
  else if (customName == 'ccomr_polling') {
    path = '/admin/ver2/poll2/insertPoll.php';
    width = 700;
    height = 600;
  }
  else if (customName == 'flash') {
    path = '/mlib/?type=flash';
    width = 800;
    height = 600;
  }
  else {
    alert('Only "image", "ccomr_video", "ccomr_polling", and "flash" are available in the media library.');
    return;
  }

  newWindow = window.open(path,'_blank',"width="+width+",height="+height+",,resizable=yes,scrollbars=yes,menu=no,status=no");

  //test page:
  //newWindow = window.open('/mediaLibrary.php','_blank',"width=200,height=200,,resizable=yes,scrollbars=yes,menu=no,status=no");
}

//callback from media/poll library
function insertResource(type, param1, param2, param3)
{
    if (type == 'image') {
        var elem = getCustomTagControl('image_link_source');
        if (elem)
            elem.value = param1;
    }
    else if (type == 'video') {
        var elem = getCustomTagControl('ccomr_video_skin_source');
        if (elem)
            elem.value = param1;
        elem = getCustomTagControl('ccomr_video_link_source');
        if (elem)
            elem.value = param2;
    }
    else if (type == 'poll') {
        var elem = getCustomTagControl('ccomr_polling_id_source');
        if (elem)
            elem.value = param1;
        elem = getCustomTagControl('ccomr_polling_title_source');
        if (elem)
            elem.value = param2;
    }
    else if (type == 'flash') {
        var elem = getCustomTagControl('flash_link_source');
        if (elem)
            elem.value = param1;
        elem = getCustomTagControl('flash_width_source');
        if (elem)
            elem.value = param2;
        elem = getCustomTagControl('flash_height_source');
        if (elem)
            elem.value = param3;
    }
}

//convenience function to find HTML controls within the custom tag IFRAME
function getCustomTagControl(id) {

    for (var i = 0; i < window.frames.length; i++) {
        var currFrame = window.frames[i];
        if (i.id = 'mce_0_ifr') {

            //for some reason, there are two iframes with this ID on the page
            var elem = window.frames[i].document.getElementById(id);

            if (elem) {
                return elem;
            }

        }
    }

    return null;
}