
function salonGetElementsByClassName(oElm, strTagName, strClassName){
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }   
    }
    return (arrReturnElements)
}

function shareRotate(rotater_id, on_position) {
	var rotater = document.getElementById(rotater_id);
	if (!rotater) return;

	var buttons = salonGetElementsByClassName(rotater, 'li', 'button');
	if (!buttons) return;

	for (var i = 0; i < buttons.length; i++) {
		if (i == on_position) {
			buttons[i].style.display = 'block';
		} else {
			buttons[i].style.display = 'none';
		}
	}

	if (on_position + 1 == buttons.length) on_position = 0;
	else on_position++;
	var refreshTimeout = setTimeout('shareRotate("'+rotater_id+'",'+on_position+')', 5*1000);
}

function doShareRotate() {
	var rotaters = salonGetElementsByClassName(document, 'li', 'buzz rotate');
	for (var i = 0; i < rotaters.length; i++) {
		shareRotate(rotaters[i].id, 0);
	}
}

function shareClick(link, url, label) {
	var s = s_gi('salondev'); 
	s.eVar10 = label; 
	s.eVar16 = url; 
	s.events = 'event7'; 
	s.linkTrackVars = 'eVar10,eVar16,events'; 
	s.linkTrackEvents = 'event7'; 
	s.tl(link, 'o', label);
	return false;
}



// Popups via class="popup"
// Source: http://adactio.com/atmedia2005/
function doPopups() {
  if (!document.getElementsByTagName)
    return false;
  var links = document.getElementsByTagName("a");
  for (var i=0; i < links.length; i++) {
    if (links[i].className.match("tc_popup")) {
      links[i].onclick = function() {
        var win = window.open(this.href, "tc_popup", "width=350,height=452,scrollbars=no,resizable=no");
        if (window.focus)
          win.focus();
        return false;
      }
    }
    /*
    else if (links[i].className.match("facebook_link")) {
      links[i].onclick = function() {
        return fbs_click();
      }
    }
    */
    else if (links[i].className.match("popup")) {
      links[i].onclick = function() {
        var win = window.open(this.href, "popup", "width=600,height=515,scrollbars=yes,resizable=yes");
        if (window.focus)
          win.focus();
        return false;
      }
    }
  }
}

// dropdown digg, etc widgets via class="share", cw 2006-06-21
function initShare() {
  if (!document.getElementsByTagName)
    return false;
  var lis = document.getElementsByTagName("li");
  for (var i=0; i < lis.length; i++) {
    if (lis[i].className.match("share")) {
	  lis[i].onmouseover = function() {
	    this.className += " sfhover";
	  }
	  lis[i].onmouseout = function() {
        this.className = this.className.replace(new RegExp(" sfhover\\b"), "");
	  }
	}
  }
}

// Facebook - http://www.facebook.com/share_partners.php
function fbs_click() {u=location.href;t=document.title;window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');return false;}

// Font size switching via S/S+/S++ widget
function fontsizeup() {
  active = getActiveStyleSheet();
  switch (active) {
    case 'S' : 
      setActiveStyleSheet('S+');
      break;
    case 'S+' : 
      setActiveStyleSheet('S++');
      break;
    case 'S++' : 
      break;
    default :
      setActiveStyleSheet('S');
      break;
  }
}

function fontsizedown() {
  active = getActiveStyleSheet();
  switch (active) {

    case 'S+' : 
      setActiveStyleSheet('S');
      break;
    case 'S++' : 
      setActiveStyleSheet('S+');
      break;
    case 'S' : 
      break;
    default :
      setActiveStyleSheet('S');
      break;
  }
}

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");

  }
  return null;
}

function getPreferredStyleSheet() {
  return ('S');
}

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+"; domain=.salon.com; 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;
}

var cookie = readCookie("SalonFontSize");
var title = cookie ? cookie : getPreferredStyleSheet();
if (title == 'null') {
  title = getPreferredStyleSheet();
}

setActiveStyleSheet(title);


/**
 * This function reads all thinks from a container and sets
 * their targets to open in a new window if the domain is non-salon.
 *
 * This behavior can be forced if you use the following class names on a link:
 *   <a class="target_self"></a> or <a class="target_new"></a>
 */

function setLinkTargets(container_name) {

        // make sure we have ability to move forward
        if (!document.getElementById || !document.getElementsByTagName) return true;
        if (!document.getElementById(container_name)) return true;


        // we will only re-target links in outer_container (i.e. excludes header & footer)
        var container = document.getElementById(container_name);

        // get all links
        var links = container.getElementsByTagName('a');

        var ct = links.length;
        for (var i=0; i<ct; i++) {
                // skip link with target attribute set
                if (links[i].target) {
                	if (links[i].target == 'new') {
                        	// change 'new' to '_blank'
                        	links[i].target = '_blank';
                	}
                }

                // check for manual over-ride
                else if (/\s?target_new\s?/.test(links[i].className)) {
                        links[i].target = '_blank';
                }
                else if (/\s?target_self\s?/.test(links[i].className)) {
                        // do nothing
                }

                // otherwise process href and choose target
                else {
                        // if url doesn't contain :// or url contains salon.com in domain name. fantastic
                        if (/^\w+:\/\//.test(links[i].href)==false || /^\w+:\/\/(\w+\.)*salon.com\//i.test(links[i].href)) {
                                // salon, do nothing
                        } else {
                                links[i].target = '_blank';
                        }
                }
        }
}


window.onload = function(e) {
  var cookie = readCookie("SalonFontSize");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
  doPopups();
  initShare();
  setLinkTargets('outer_container');
  setLinkTargets('yui-main');
  setLinkTargets('cols');
  doShareRotate();
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("SalonFontSize", title, 365);
}


