function Acurra(){};

Acurra.prototype.getElm = function(elm) {
  return document.getElementById(elm) ? document.getElementById(elm) : null;
}

Acurra.prototype.getStyle = function(el, prop) {
  if (window.getComputedStyle) {
    return getComputedStyle(el, null)[prop];
  } else if (el.currentStyle) {
    return el.currentStyle[prop];
  } else {
    return el.style[prop];
  }
}

Acurra.prototype.hasClass = function(element, className) {
  var elementClassName = element.className;
  return (elementClassName.length > 0 && (elementClassName == className ||
    new RegExp("(^|\\s)" + className + "(\\s|$)").test(elementClassName)));
}

Acurra.prototype.elementsToReplace = function(tagName, className) {
  var found = new Array();
  var elements = document.getElementsByTagName(tagName);
  for (var i = 0; i < elements.length; i++) {
    if (this.hasClass(elements[i], className)) {
      found[found.length] = elements[i];
    }
  }
  return found;
}

Acurra.prototype.colorFromClass = function(elm, defaultColor) {
  if (this.hasClass(elm, "people")) {
    return "ff3673"; //ff3366
  }
  else if (this.hasClass(elm, "practices")) {
    return "d079ff"; // cc66ff
  }
  else if (this.hasClass(elm, "story")) {
    return "88EEFF"; //"cccccc";
  }
  else if (this.hasClass(elm, "careers")) {
    return "61ff95"; //61ff95
  }
  return defaultColor;
}

Acurra.prototype.coloredHeadersToSifr = function() {
  var headers = this.elementsToReplace("h1", "image");
  if (headers.length > 0) {
    var color = this.colorFromClass(headers[0], "66CCFF");
    sIFR.replace(cellinimedium, {
      selector: 'h1.image'
      ,css: [
        '.sIFR-root { color: #' + color + '; }'
        ,'a { text-decoration: none; }'
        ,'a:link { color: #' + color + '; }'
        ,'a:hover { color: #' + color + '; }'
      ]
      ,wmode: 'transparent'
    });
  }
}

Acurra.prototype.otherHeadersToSifr = function() {
  sIFR.replace(cellini, {
    selector: 'h2.image'
    ,css: [
      '.sIFR-root { text-transform: uppercase; }'
      ,'a { text-decoration: none; }'
      ,'a:link { color: #000000; }'
      ,'a:hover { color: #000000; }'
    ]
    ,wmode: 'transparent'
  });
  
  sIFR.replace(cellini, {
    selector: 'h3.image'
    ,css: [
      '.sIFR-root { text-transform: uppercase; }'
      ,'a { text-decoration: none; }'
      ,'a:link { color: #000000; }'
      ,'a:hover { color: #000000; }'
    ]
    ,wmode: 'transparent'
  });
  
  sIFR.replace(cellinimedium, {
    selector: 'h4.people'
    ,css: [
      '.sIFR-root {}'
      ,'a { text-decoration: none; }'
      ,'a:link { color: #FF3366; }'
      ,'a:hover { color: #FF3366; }'
    ]
    ,wmode: 'transparent'
  });
  
  sIFR.replace(cellinimedium, {
    selector: 'h4.practices'
    ,css: [
      '.sIFR-root {}'
      ,'a { text-decoration: none; }'
      ,'a:link { color: #CC66FF; }'
      ,'a:hover { color: #CC66FF; }'
    ]
    ,wmode: 'transparent'
  });
  
  sIFR.replace(cellinimedium, {
    selector: 'h4.story'
    ,css: [
      '.sIFR-root {}'
      ,'a { text-decoration: none; }'
      ,'a:link { color: #88EEFF; }'
      ,'a:hover { color: #88EEFF; }'
    ]
    ,wmode: 'transparent'
  });
  
  sIFR.replace(cellinimedium, {
    selector: 'h4.careers'
    ,css: [
      '.sIFR-root {}'
      ,'a { text-decoration: none; }'
      ,'a:link { color: #66FF99; }'
      ,'a:hover { color: #66FF99; }'
    ]
    ,wmode: 'transparent'
  });
  
  sIFR.replace(cellinimedium, {
    selector: '#slogan1'
    ,css: [
      '.sIFR-root { color: #FFFFFF; }'
      ,'a { text-decoration: none; }'
      ,'a:link { color: #FFFFFF; }'
      ,'a:hover { color: #FFFFFF; }'
    ]
    ,wmode: 'transparent'
  });
  
  sIFR.replace(cellinimedium, {
    selector: '#slogan2'
    ,css: [
      '.sIFR-root { color: #FFFFFF; }'
      ,'a { text-decoration: none; }'
      ,'a:link { color: #FFFFFF; }'
      ,'a:hover { color: #FFFFFF; }'
    ]
    ,wmode: 'transparent'
  });
}

Acurra.prototype.getText = function(elm) {
  var text;
  if (elm.getElementsByTagName("a").length > 0) {
    text = elm.getElementsByTagName("a")[0].innerHTML;
  }
  else {
    text = elm.innerHTML;
  }
  text = text.replace(/"/g, '\'');
  return text;
}

Acurra.prototype.getLink = function(elm) {
  if (elm.getElementsByTagName("a").length > 0) {
    return elm.getElementsByTagName("a")[0].href;
  }
  else {
    return "";
  }
}

Acurra.prototype.hasLink = function(elm) {
  return (elm.getElementsByTagName("a").length > 0);
}

Acurra.prototype.replaceHeaders = function(type, defaultColor, size) {
  var headers = this.elementsToReplace(type, "image");
  var elm, color, text, link;
  for (var i = 0; i < headers.length; i++) {
    elm = headers[i];
    color = this.colorFromClass(elm, defaultColor);
    text = this.getText(elm);
var textcoded = text.replace( new RegExp( "&", "g" ), "%26" );
//alert(text + "/" + textcoded);
    if (this.hasLink(elm)) {
      link = this.getLink(elm);
      
      elm.innerHTML = '<span><a href="' + link + '"><img src="/umbraco/imagegen.ashx?text=' + textcoded +'&font=/fonts/CIMD.ttf&fontsize=' + size +'&fontcolor=' + color +'" alt="' + text +'" /></a></span>';
    }
    else { 
      elm.innerHTML = '<span><img src="/umbraco/imagegen.ashx?text=' + textcoded +'&font=/fonts/CIMD.ttf&fontsize=' + size +'&fontcolor=' + color +'" alt="' + text +'" /></span>';
    }
  }
}

Acurra.prototype.replaceSlogans = function(defaultColor, size) {
  var elm1 = this.getElm("slogan1");
  var elm2 = this.getElm("slogan2");
  var color, text;
  if (elm1) {
    color = this.colorFromClass(elm1, defaultColor);
    text = this.getText(elm1);
    elm1.innerHTML = '<span><img src="/umbraco/imagegen.ashx?text=' + text +'&font=/fonts/CIMD.ttf&fontsize=' + size +'&fontcolor=' + color +'" alt="' + text +'" /></span>';
  }
  if (elm2) {
    color = this.colorFromClass(elm2, defaultColor);
    text = this.getText(elm2);
    elm2.innerHTML = '<span><img src="/umbraco/imagegen.ashx?text=' + text +'&font=/fonts/CIMD.ttf&fontsize=' + size +'&fontcolor=' + color +'" alt="' + text +'" /></span>';
  }
}

function addcookie(pageid) {
   var COOKIE_NAME = 'accurafavorites';
   var temp = $.cookie(COOKIE_NAME);
   	// debug alert(temp);		 
   if (pageid != '' ) {
      if (temp == null)
         temp = "favs";
      if( (temp + ',').indexOf(','+pageid+',') == -1 ) {

         $.cookie(COOKIE_NAME, temp + ','+ pageid, { path: '/', expires: 100 });

}
    }


}

Acurra.prototype.createComponentLinks = function() {
  var components = ['.cp_news_item', '.cp_search_result', '.cp_person_teaser', '.cp_section_teaser'];
  for (var i = 0; i < components.length; i++) {
    $(components[i]).css('cursor', 'hand');
    $(components[i]).css('cursor', 'pointer');
    $(components[i]).bind('click', function() {
      var link = $(this).find('a')[0].href;
      location.href = link;
    });
  }
}

Acurra.prototype.makeListsEqualHeight = function() {
  var lists = $("#content .col_50 .cp_link_list");
  var max = 0;
  
  // Find max height
  for (var i = 0; i < lists.length; i++) {
    if ($(lists[i]).height() > max) {
      max = $(lists[i]).height();
    }
  }
  
  // Set height
  for (var i = 0; i < lists.length; i++) {
    if ($(lists[i]).height() != max) {
      $(lists[i]).height(max + "px");
    }
  }
}

function init() {
  app = new Acurra();
  app.coloredHeadersToSifr();
  app.otherHeadersToSifr();
  app.createComponentLinks();
  app.makeListsEqualHeight();
}

var app;

var cellini = {
  src: '/swf/cellini.swf'
  ,ratios: [6, 1.41, 9, 1.35, 15, 1.29, 21, 1.25, 22, 1.22, 27, 1.24, 29, 1.21, 34, 1.22, 41, 1.21, 45, 1.2, 46, 1.21, 59, 1.2, 68, 1.19, 69, 1.2, 96, 1.19, 97, 1.18, 102, 1.19, 103, 1.18, 107, 1.19, 108, 1.18, 112, 1.19, 114, 1.18, 116, 1.19, 120, 1.18, 121, 1.19, 1.18]
};

var cellinimedium = {
  src: '/swf/cellini_medium.swf'
  ,ratios: [6, 1.41, 9, 1.35, 15, 1.29, 21, 1.25, 22, 1.22, 27, 1.24, 29, 1.21, 34, 1.22, 41, 1.21, 45, 1.2, 46, 1.21, 59, 1.2, 68, 1.19, 69, 1.2, 96, 1.19, 97, 1.18, 102, 1.19, 103, 1.18, 107, 1.19, 108, 1.18, 112, 1.19, 114, 1.18, 116, 1.19, 120, 1.18, 121, 1.19, 1.18]
};

// You probably want to switch this on, but read <http://wiki.novemberborn.net/sifr3/DetectingCSSLoad> first.
// sIFR.useStyleCheck = true;
sIFR.activate(cellini);

window.onload = init;