// $Id: imedia.js,v 1.0 2008/02/08 15:47:21 $

/**
  * @file
  * General javascript functions used by imedia modules
  * 
  * There is a document.ready at the end of this file. If you have any general thing, add it 
  * there.
  * 
  * @version
  * 1.0
  */
    
Drupal.ushBehaviors = {};
    
/**
 * This function is usefull for inserting a piece of javascript to the page with ajax call.
 * The url should return your javascript codes without the <script> tag.
 * 
 * If you don't want to use scriptid, pass ""
 */
function loadScript(url, scriptid) {
  var script;
  if (scriptid != "") {
    script = document.getElementById(scriptid);
    if (script != null) {
      script.parentNode.removeChild(script);
    }
  }
  script = document.createElement('script');
  if (scriptid != "") {
    script.id = scriptid;
  }
  script.type = 'text/javascript';
  script.src = url;
  $('head', document).append(script);
}

function injectScript(code, scriptid) {
  if (!code) return;
  if (scriptid != "") {
    script = document.getElementById(scriptid);
    if (script != null) {
      script.parentNode.removeChild(script);
    }
  }
  script = '<script type="text/javascript"';
  script += (scriptid ? ' id="'+scriptid+'">' : '>');
  script += code;
  script += '</script>';
  $('head', document).append(script);
}

function loadStyle(url, styleid) {
  var style;
  if (styleid != "") {
    style = document.getElementById(styleid);
    if (style != null) {
      style.parentNode.removeChild(style);
    }
  }
  style = document.createElement('link');
  if (styleid != "") {
    style.id = styleid;
  }
  style.type = 'text/css';
  style.rel = 'stylesheet';
  style.href = url;  
  $('head', document).append(style);
}

function showWaitDialog() {
  if (!YAHOO) {
    return false;
  }
  showYuiLoading();  
}

function hideWaitDialog() {
  if (!YAHOO) {
    return false;
  }
  hideYuiLoading();  
}

/**
 * Adding Array.indexOf support for IE
 */
if (!Array.indexOf) {
  Array.prototype.indexOf = function (obj, start) {
    for (var i = (start || 0); i < this.length; i++) {
      if (this[i] == obj) {
        return i;
      }
    }
    return -1;
  }
}

/**
 * Helper functions to manager ajax returned data from drupal json callbacks. Usually the returned data contains css, js and some messages
 * as well as data itself
 */
function imediaAjaxProcessReturn(response, context) {
  //add messages
  if (response.messages) {
    $(".messages").empty();
    if (response.messages != null && response.messages != "") {        
      $(".messages").append(response.messages).show();
    }
    else {
      $(".messages").hide();
    }     
  }
  //add css files
  if (response.cssfiles && response.cssfiles != "") {
    var css_name = "";        
    for (var k = 0; k < response.cssfiles.length; k++) {
      var css = response.cssfiles[k];
      var href_pos = css.indexOf("href=\"");      
      var pos2 = css.indexOf(".css")+4;
      if (href_pos != -1) {
        if (pos2 != -1) {
          css_name = css.substring(href_pos+6, pos2);
        }
        else {
          css_name = css.substring(href_pos+6);
        }
        //css_name.indexOf("theme")== -1 && 
        if ($('head', document).find("link[href*="+css_name+"]").size() == 0) {
          $('head', document).append(css);
        }
      }                     
    }
  }
  //load any javascript file attached to returned content
  if (response.scripts && response.scripts != "") {
    for (var i = 0; i < response.scripts.length; i++) {          
      var script_name = response.scripts[i];
      var js_name = "";          
      var p = script_name.indexOf("?"); 
      if (p != -1) {
        js_name = script_name.substring(0, p); 
      }
      else {
        js_name = script_name;
      }
      if (script_name.indexOf("awareness") !== -1) {
        loadScript(script_name, '');
      }
      else {
        if ($('head', document).find("script[src*="+js_name+"]").size() == 0) {
          if ($('body', document).find("script[src*="+js_name+"]").size() == 0) {                                                           
            loadScript(script_name, '');                       
          }
        }
      }
    }
  }
      
  //Inject inline scripts
  if (response.inlineScripts && response.inlineScripts != "") {
    injectScript(response.inlineScripts);
  }
  
  if (context) {
    YAHOO.util.Event.onDOMReady(function(ev){
      Drupal.attachBehaviors(context);
    });
  }
}

 