//
// Generic JS that can be used in both the CMS and the public skin.
//

function changeLoc(select) {
    var href = select.options[select.selectedIndex].value;
    if (href != " ") {
        document.location = href;
    }
}

function printPreview() {
  if (top.location != location) {
    var frames = top.frames;
    for(var i=0; i<frames.length; i++) {
      var frame = frames[i];
      if(frame.name == "print_main_frame") {
          frame.setActiveStyleSheet("Print_Preview", null, false);
          return true;
      }
    } 
  }
  return false;
}

function popup(url, width, height) {
  if (url.length > 0) {
    window.open(url, "", "scrollbars=yes,width=" + width + ",height=" + height + ",resizable=yes");
  }
}

/*
 * Constructs a mailto so that spambots won't scrape it.
 *
 * @param mail_id The mail id for the supplied domain.
 * @param domain The domain to mail to. For example yahoo.com.
 */
function mailme(mail_id, domain) {
  window.location = "mailto:" + mail_id + "@" + domain;
}

/*
 * Enables navigation from select/option elements. To use, call this function from the select
 * element's onchange handler, passing this as the only argument.
 *
 * Parameters:
 *   sel - the select element
 *
 * Example:
 * <select name="specialty" id="specialty" onchange="navigateFromSelect(this)">
 *   <option value="">Choose a specialty</option>
 *   <option tal:attributes="value here/BySpecialty/BackSpine/absolute_url">Back & Spine</option>
 *   <option tal:attributes="value here/BySpecialty/Elbow/absolute_url">Elbow</option>
 * </select>
 */
function navigateFromSelect(sel) {
  var url = sel.options[sel.selectedIndex].value;
  if (url) {
    location.href = url;
  }
}

/*
 * image swapping function
 * pass in the 'name' attr on the image to swap, and the name of the var
 * holding the new image source
 */
function changeImage(imageName, reference) {
  document.images[imageName].src = reference.src;
}

LOGGING = false;
logwin = null;

function log(msg) {
  if (LOGGING) {
    if (!logwin) {
      logwin = window.open("", "", "top=0,left=0,scrollbars=yes,width=400,height=500,resizable=yes");
      logwin.document.writeln("<style type='text/css'>* {background-color:black;color:white;font-family:Courier New;font-size:0.9em;}p{margin:0;padding:0.3em 0;}</style>");
    }
    if (logwin) {
      logwin.document.writeln("<p>" + msg + "</p>");
      logwin.scrollBy(0,100);
    }
  }
}

// ----------------------------------------------
// Cookie functions
// ----------------------------------------------

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i=0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}


// --------------------------------------------------------------------
// Style switcher functions. Used for font size switching 
// and print preview.
//
// For the details, see the A List Apart article:
// http://www.alistapart.com/articles/alternate/
// Refactored somewhat by Sam, who was particularly irked by this:
// for(i=0; (a = document.getElementsByTagName("link")[i]); i++)
// Also added the setCookie argument to setActiveStylesheet() 
// so that we can temporarily switch to the print stylesheet
// for print preview without overwriting the font preference cookie.
// --------------------------------------------------------------------

function setActiveStyleSheet(activeTitle, setCookie) {
    // enable stylesheet referred to by title, disable other alternate stylesheets
    var i, a, title;
    var availableSheets = new Array();
    var links = document.getElementsByTagName("link");
    for (i=0; i < links.length; i++) {
        a = links[i];
        if (a.getAttribute("rel").indexOf("style") != -1) {
            title = a.getAttribute("title");
            if(title) {
                availableSheets[availableSheets.length] = title;
                a.disabled = true;
                // Yes, we're disabling then enabling the stylesheet to be used, but IE seems to 
                // be happier this way and the alistapart.com example concurs. C'est la vie.
                if (title == activeTitle) {
                    a.disabled = false;
                }
            }
        }
    }

    // Explicitly set the class name of the (two) inactive font-changing widgets
    var el;
    for (i = 0; i < availableSheets.length; i++) {
        if (availableSheets[i] != activeTitle)
            el = document.getElementById(availableSheets[i]);
            if(el) {
                el.className = "inactive";
            }
    }
    el = document.getElementById(activeTitle);
    if(el) {
        el.className = "active";
    }

    if(setCookie) {
        createCookie("font_size_style", activeTitle, 365);
    }
}

// ----------------------------------------------
// Window events
// ----------------------------------------------

function init_font_settings() {
    var i, a, title, sheet;
    var availableSheets = new Array();
    var preferredSheet = null;
    var links = document.getElementsByTagName("link");
    for (i=0; i < links.length; i++) {
        a = links[i];
        if (a.getAttribute("rel").indexOf("style") != -1) {
            title = a.getAttribute("title");
            if(title) {
                availableSheets[availableSheets.length] = title;
                if (a.getAttribute("rel").indexOf("alt") == -1) {
                    preferredSheet = title;
                }
            }
        }
    }
    title = null;
    var cookie = readCookie("font_size_style");
    if(cookie) {
        for(i=0; i < availableSheets.length; i++) {
            sheet = availableSheets[i];
            if(cookie == sheet) {
                title = cookie;
            }
        }
    }
    if(! title) {
        title = preferredSheet;
    }
    setActiveStyleSheet(title, true);
}

