function getSubNodeText(node, tagname){
  var returnValue;
  subNode = node.getElementsByTagName(tagname);
	if (subNode[0] != null) {
    returnValue = subNode[0].textContent || subNode[0].text;
    if(typeof returnValue == 'undefined' && subNode[0].firstChild){
      returnValue = subNode[0].firstChild.nodeValue;
    }
	}
  if(!returnValue){
    returnValue = "";
  }
	return returnValue;
}
function AddClass(aElement,aClass) {
  aElement.className = aElement.className.addClass(aClass);
  return true;
}
function RemoveClass(aElement,aClass) {
  aElement.className = aElement.className.removeClass(aClass);
  return true;
}
String.prototype.addClass = function(aClass) {
  if (this != "") {
    if (!this.classExists(aClass)) {
      return this + " " + aClass;
    }
  }
  else {
    return aClass;
  }
  return this;
}
String.prototype.Trim = function(){
  return this.replace(/^\s*|\s*$/g,'');
}
String.prototype.classExists = function(aClass) {
  var zRegExp = new RegExp('(^|\\s)' + aClass + '\\b');
  return zRegExp.test(this);
}
String.prototype.removeClass = function(aClass) {
  var zRegExp = new RegExp('(^|\\s)' + aClass + '\\b');
  return this.replace(zRegExp, '').Trim();
}
function UniqueVar(){
	return 'u'+ ((new Date().valueOf()) + Math.round(Math.random()*1000000));
}
Array.prototype.append = function () {
  var i;
  for (i=0; i < arguments.length; i++) {
    this[this.length] = arguments[i];
  }
}
if(!Array.push){
  Array.prototype.push = Array.prototype.append;
}
if(!Array.forEach){
  Array.prototype.forEach = function (fnExec , oThis ) {
    var i,l;
    var zProperty = '__forEachFunc__'+ UniqueVar();
    oThis = oThis || window;
    oThis[zProperty] = fnExec;
    for (i=0, l=this.length; i < l; i++) {
      oThis[zProperty](this[i], i, this);
    }
    oThis[zProperty] = null;
  }
}
if(!Array.indexOf){
  Array.prototype.indexOf = function (vItem , iStart ) {
    var i,l;
    if (iStart == null) {
      iStart = 0;
    }
    for (i=iStart, l=this.length; i < l; i++) {
      if (this[i] == vItem) {
        return i;
      }
    }
    return -1;
  }
}
function getAbsolutePosition(aElement){
  var zReturn = { x:aElement.offsetLeft, y:aElement.offsetTop };
  if(aElement.offsetParent) {
    var tmp = getAbsolutePosition(aElement.offsetParent);
    zReturn.x += tmp.x;
    zReturn.y += tmp.y;
  }
  return zReturn;
}
function $(){
  if (arguments.length == 1){ return get$(arguments[0]); }
  var i,zElements = [];
  for(i=0;i< arguments.length;i++){
    zElements.push(get$(arguments[i]));
  }
  function get$(el){
    return (typeof el == 'string' ? document.getElementById(el):el);
  }
  return zElements;
}
var Tween = {
  Run:function(aParams){
    aParams.Duration           = (typeof aParams.Duration != 'undefined' ? aParams.Duration:400);
    aParams.StartValue         = (typeof aParams.StartValue != 'undefined' ? aParams.StartValue:0);
    aParams.Increment          = (typeof aParams.Increment != 'undefined' ? aParams.Increment:16);
    aParams.EndValue           = (typeof aParams.EndValue != 'undefined' ? aParams.EndValue:1);
    aParams.Method             = aParams.Method || "linear";
    aParams.OnCompleteFunction = aParams.OnCompleteFunction || null;
    aParams.CurrentValue       = aParams.StartValue;
    aParams.StartTime          = new Date().getTime();
    aParams.EndTime            = aParams.StartTime + aParams.Duration;
    aParams.Transition         = Tween.Transition[aParams.Method];
    aParams.PercentComplete    = 0;
    aParams.Timer = setInterval(function(){
      Tween.Step(aParams);
    },aParams.Increment);
    return aParams;
  },
  Step:function(aParams){
    var zNow  = new Date().getTime();
    if (zNow >= aParams.StartTime+aParams.Duration) {
      clearInterval (aParams.Timer);
      aParams.Timer = null;
      aParams.CurrentValue = aParams.EndValue;
      aParams.PercentComplete = 1;
      if (aParams.OnCompleteFunction) {
        aParams.OnCompleteFunction(aParams);
      }
    } else {
      aParams.PercentComplete = (zNow - aParams.StartTime) / (aParams.Duration);
      aParams.CurrentValue = aParams.Transition(aParams.PercentComplete) * (aParams.EndValue - aParams.StartValue) + aParams.StartValue;
    }
    aParams.Function(aParams);
  },
  To:function(aElement,aCoords){
  },
  FadeIn:function(aElement,aParams){
    aParams = aParams || {};
    aParams.StartValue = (typeof(aParams.StartValue)=='undefined' ? 0:aParams.StartValue);
    aParams.EndValue = (typeof(aParams.EndValue)=='undefined' ? 1:aParams.EndValue);
    Tween.Common.SetTween(aElement,aParams);
  },
  FadeOut:function(aElement,aParams){
    aParams = aParams || {};
    aParams.StartValue = (typeof(aParams.StartValue)=='undefined' ? 1:aParams.StartValue);
    aParams.EndValue = (typeof(aParams.EndValue)=='undefined' ? 0:aParams.EndValue);
    Tween.Common.SetTween(aElement,aParams);
  },
  Common:{
    SetOpacity:function(aElement,aValue){
      aElement.style.opacity = aValue;
      aElement.style.filter = 'alpha(opacity=' + aValue*100 + ')';
    },
    SetTween:function(aElement,aParams){
      aParams.Element  = $(aElement);
      aParams.Function = function(aParams){
        Tween.Common.SetOpacity(aParams.Element,aParams.CurrentValue);
      }
      Tween.Common.SetOpacity(aParams.Element,aParams.StartValue);
      Tween.Run(aParams);
    }
  },
  Transition:{
    sinoidal:function(pos){
      return ((-Math.cos(pos*Math.PI)/2) + 0.5);
    },
    linear:function(pos){
      return pos;
    },
    cubic:function(pos){
      return Math.pow(pos, 3);
    },
    circ:function(pos){
      return Math.sqrt(pos);
    },
    expoIn:function(pos){
      return Math.pow(2, 10 * (pos - 1));
    },
    expoOut:function(pos){
      return (-Math.pow(2, -10 * pos) + 1);
    },
    quadIn:function(pos){
      return Math.pow(pos, 2);
    },
    quadOut:function(pos){
      return -(pos)*(pos-2);
    },
    circOut:function(pos){
      return Math.sqrt(1 - Math.pow(pos-1,2));
    },
    circIn:function(pos){
      return -(Math.sqrt(1 - Math.pow(pos, 2)) - 1);
    },
    backIn:function(pos){
      return (pos)*pos*((2.7)*pos - 1.7);
    },
    backOut:function(pos){
      return ((pos-1)*(pos-1)*((2.7)*(pos-1) + 1.7) + 1);
    },
    sineOut:function(pos){
      return Math.sin(pos * (Math.PI/2));
    },
    sineIn:function(pos){
      return -Math.cos(pos * (Math.PI/2)) + 1;
    },
    sineInOut:function(pos){
      return -(Math.cos(Math.PI*pos) - 1)/2;
    }
  }
};
// written by Dean Edwards, 2005
// with input from Tino Zijdel, Matthias Miller, Diego Perini

// http://dean.edwards.name/weblog/2005/10/add-event/

function addEvent(element, type, handler) {
  if (element.addEventListener) {
    element.addEventListener(type, handler, false);
  } else {
    // assign each event handler a unique ID
    if (!handler.$$guid) handler.$$guid = addEvent.guid++;
    // create a hash table of event types for the element
    if (!element.events) element.events = {};
    // create a hash table of event handlers for each element/event pair
    var handlers = element.events[type];
    if (!handlers) {
      handlers = element.events[type] = {};
      // store the existing event handler (if there is one)
      if (element["on" + type]) {
        handlers[0] = element["on" + type];
      }
    }
    // store the event handler in the hash table
    handlers[handler.$$guid] = handler;
    // assign a global event handler to do all the work
    element["on" + type] = handleEvent;
  }
};
// a counter used to create unique IDs
addEvent.guid = 1;

function removeEvent(element, type, handler) {
  if (element.removeEventListener) {
    element.removeEventListener(type, handler, false);
  } else {
    // delete the event handler from the hash table
    if (element.events && element.events[type]) {
      delete element.events[type][handler.$$guid];
    }
  }
};

function handleEvent(event) {
  var returnValue = true;
  // grab the event object (IE uses a global event object)
  event = event || fixEvent(((this.ownerDocument || this.document || this).parentWindow || window).event);
  // get a reference to the hash table of event handlers
  var handlers = this.events[event.type];
  // execute each event handler
  for (var i in handlers) {
    this.$$handleEvent = handlers[i];
    if (this.$$handleEvent(event) === false) {
      returnValue = false;
    }
  }
  return returnValue;
};

function fixEvent(event) {
  // add W3C standard event methods
  event.preventDefault = fixEvent.preventDefault;
  event.stopPropagation = fixEvent.stopPropagation;
  return event;
};
fixEvent.preventDefault = function() {
  this.returnValue = false;
};
fixEvent.stopPropagation = function() {
  this.cancelBubble = true;
};

var successStory = {
  visible:false,
  dimensions:{
    left:0,
    top:0,
    width:641,
    height:343
  },
  currentIndex:0,
  storyArray:[],
  durationIn:300,
  durationOut:200,
  fullStory:null,
  previewStory:null,
  previewContent:null,
  ajaxCount:0,
  setData:function(storyUrlArray){
    storyUrlArray.forEach(function(storyURL){
      successStory.storyArray.push({
        url:storyURL,
        title:null,
        shortContent:null,
        longContent:null,
        loaded:false
      });
    });
  },
  init:function(){
    successStory.previewStory = $("SuccessStorySmall");
    if(successStory.previewStory&&successStory.storyArray.length>0){
      successStory.createPreview();
      successStory.createFull();
      successStory.requestStory(true);
    }
  },
  createPreview:function(){
    var heading = document.createElement('h3');
    heading.id = "SuccessStoryPreviewHeading";
    successStory.previewContent = document.createElement('div');
    heading.innerHTML = successStory.text.SuccessStory;
    successStory.previewContent.id = "SuccessStoryPreviewContent";
    successStory.previewStory.appendChild(heading);
    successStory.previewStory.appendChild(successStory.previewContent);
  },
  createFull:function(){
    // get position of story
    // create elements
    var fullStoryContainer   = document.createElement('div');
    var fullStory   = document.createElement('div');
    var header      = document.createElement('div');
    var controls    = document.createElement('div');
    var linkPrev    = document.createElement('a');
    var linkNext    = document.createElement('a');
    var pageNumber  = document.createElement('span');
    var buttonClose = document.createElement('a');
    var contentHeader = document.createElement('h3');
    var content     = document.createElement('div');
    var contentBody = document.createElement('div');
    successStory.fullStory = fullStory;
    // set element details
    fullStoryContainer.id = "SuccessStoryContainer";
    fullStory.id = 'SuccessStory';
    fullStoryContainer.style.width = "0";
    fullStory.style.width = successStory.dimensions.width + "px";
    fullStory.style.height = successStory.dimensions.height + "px";
    fullStoryContainer.style.height = successStory.dimensions.height + "px";
    linkPrev.id = "SuccessStoryLinkPrevious";
    linkPrev.className = "SuccessStoryLink";
    linkPrev.innerHTML = "&lt;"+ successStory.text.previous;
	  linkPrev.href = "void(0);";
    pageNumber.id = "SuccessStoryPageNumber";
    pageNumber.innerHTML = " | 0 "+ successStory.text.of +" 0 | ";
    linkNext.id = "SuccessStoryLinkNext";
    linkNext.className = "SuccessStoryLink";
    linkNext.innerHTML = successStory.text.next + " &gt;";
	  linkNext.href = "#";
	  buttonClose.href = "#";

    buttonClose.id = "SuccessStoryCloseButton";
    buttonClose.innerHTML = successStory.text.close;
	  content.id = "SuccessStoryContent";
    contentBody.id = "SuccessStoryContentBody";
    contentHeader.id = "SuccessStoryContentHeader";

	  header.id = "SuccessStoryControlsContainer";
	  controls.id = "SuccessStoryControls";

    // append elements to document
    fullStoryContainer.appendChild(fullStory);
    fullStory.appendChild(header);
    fullStory.appendChild(content);
    header.appendChild(buttonClose);
    header.appendChild(controls);
    controls.appendChild(linkPrev);
    controls.appendChild(pageNumber);
    controls.appendChild(linkNext);
    content.appendChild(contentHeader);
    content.appendChild(contentBody);
    successStory.previewStory.appendChild(fullStoryContainer);
    // setup event handlers
    addEvent(fullStoryContainer,"click",function(e){
	    e.stopPropagation();
	  });
    addEvent(document.getElementsByTagName("body")[0],"click",function(e){
      successStory.hide(e,true);
    });
    addEvent(buttonClose,"click",successStory.hide);
    addEvent(linkPrev,"click",successStory.previous);
    addEvent(linkNext,"click",successStory.next);
  },
  displayPreview:function(story){
    var htmlArray = [];
    htmlArray.push(story.shortContent);
    htmlArray.push('<a href="void(0);" id="SuccessStoryTriggerShow">'+ successStory.text.more +'</a>');
    $("SuccessStoryPreviewContent").innerHTML = htmlArray.join('\n');
    addEvent($("SuccessStoryTriggerShow"),"click",successStory.show);
  },
  displayFull:function(story){
  	successStory.setLinkStatus();
    $("SuccessStoryPageNumber").innerHTML = " | "+ (successStory.storyArray.indexOf(story)+1) +" "+ successStory.text.of +" "+ successStory.storyArray.length +" | ";
    $("SuccessStoryContentHeader").innerHTML = story.title;
    $("SuccessStoryContentBody").innerHTML = story.longContent;
  },
  show:function(e){
    if(!successStory.visible){
      successStory.visible = true;
      $("SuccessStoryContainer").style.display = "block";
      Tween.Run({
        Element: $("SuccessStoryContainer"),
        Function: successStory.moveElement,
        OnCompleteFunction:function(aParams){
          AddClass(aParams.Element,"Open");
        },
        StartValue: 0,
        EndValue: 1,
        Method:"sineIn",
        Duration: successStory.durationIn
      });
    }
    e.preventDefault();
    e.stopPropagation();
  },
  hide:function(e,isBodyClick){
    if(successStory.visible){
      RemoveClass($("SuccessStoryContainer"),"Open");
      successStory.visible = false;
      Tween.Run({
        Element: $("SuccessStoryContainer"),
        Function: successStory.moveElement,
        OnCompleteFunction:function(aParams){
          aParams.Element.style.display = "none";
        },
        StartValue: 1,
        EndValue: 0,
        Method:"circIn",
        Duration: successStory.durationOut
      });
    }
	if(!isBodyClick){
      e.preventDefault();
	}
  },
  requestStory:function(displayPreview){
    var story = successStory.storyArray[successStory.currentIndex];
    if(story.loaded){
      successStory.displayFull(story);
    }else{
      var storyLoader = new ajax();
      storyLoader.displayPreview = displayPreview;
      storyLoader.ajaxCount = ++successStory.ajaxCount;
      storyLoader.index = successStory.currentIndex;
      storyLoader.setURL(successStory.storyArray[successStory.currentIndex].url);
      storyLoader.setSuccessHandler(function(req){
        var loader = storyLoader;
        successStory.receiveStory(req,loader);
      });
      storyLoader.execute();
    }
  },
  receiveStory:function(storyRequest,loader){
    successStory.setStory(storyRequest.responseXML,loader.index);
    var story = successStory.storyArray[loader.index];
    if(loader.index == successStory.currentIndex){
      if(loader.displayPreview){
        successStory.displayPreview(story);
      }
      successStory.displayFull(story);
    }
  },
  setStory:function(storyXml,index){
    var story = successStory.storyArray[index];
    story.title = getSubNodeText(storyXml,"storyTitle");
    story.shortContent = getSubNodeText(storyXml,"storyShortDesc");
    story.longContent = getSubNodeText(storyXml,"storyMainContent");
    story.loaded = true;

  },
  setLinkStatus:function(){
    if(successStory.currentIndex === 0){
		AddClass($("SuccessStoryLinkPrevious"),"Inactive");
      }else{
		  RemoveClass($("SuccessStoryLinkPrevious"),"Inactive");
	  }
    if(successStory.currentIndex == (successStory.storyArray.length-1)){
		  AddClass($("SuccessStoryLinkNext"),"Inactive");
    }else{
		  RemoveClass($("SuccessStoryLinkNext"),"Inactive");
	  }
  },
  previous:function(e){
  	successStory.currentIndex--;
    if(successStory.currentIndex<0){
      successStory.currentIndex = 0;
    }else{
      successStory.requestStory();
    }
    e.preventDefault();
  },
  next:function(e){
  	successStory.currentIndex++;
    if(successStory.currentIndex >= successStory.storyArray.length){
      successStory.currentIndex = successStory.storyArray.length-1;
    }else{
      successStory.requestStory();
    }
    e.preventDefault();
  },
  moveElement:function(aParams){
    aParams.Element.style.width = (aParams.CurrentValue*successStory.dimensions.width) +'px';
  }
};

//addEvent(window,'load',successStory.init);