/*
these vars are set in teh markup and are used to set the accordions to their initial state
navI, subI, subSubI, subSubSubI
*/

if(window.ie6) 
  var heightValue='100%';
else 
  var heightValue='';

var guts1;
var gut1Tween;
var delay = 0;
var initCount = 0;  
var theContent = "";

window.addEvent('domready', function() {
  var clicked = null;

  // hopefully fixes the menu staying open when loading in some cases, see onActive below
  
  var sub = new Accordion("ul.nav li.handle0 a", "ul.sub", { 
  opacity: false,  
  show: navI,
  onComplete: function(e) {
    fixNested(this);
    if (clicked)
      window.location.replace(clicked);
    },
    
  // hopefully fixes the menu staying open when loading in some cases
  onActive: function(t, e)
    {
    $$("ul.sub").each(function(el){
        el.setStyle("display", "block");
        });
    }
  });
    
  var subsub = new Accordion("ul.sub li.handle1 a", "ul.subsub", { 
    opacity: false,  
    show: subI,
    onComplete: function(e) { 
      fixNested(this);
      if (clicked)
        window.location.replace(clicked);
      guts1.set("html", "");
      }
    });

  var subsubsub = new Accordion("ul.subsub li.handle2 a", "ul.subsubsub", { 
    opacity: false,  
    show: subSubSubI,
    onComplete: function() { 
      fixNested(this);
      }
    });
    
  $$("div.column1 a").each(function(link){

    // handle each click accordingly
    link.addEvent("click", function(e) { 
      initCount = 0;
      hideContent();
      var theLink;

      // the scrollover stuff screws up the event handling...
      if (e.target.get("tag") == "span")
        theLink = e.target.getParent().getParent();
      else
        theLink = e.target;

      // this is a hack to allow opened accordions to be clickable
      
      if (theLink.hasClass("active"))
        window.location.replace(theLink.get("href"));
        
      setMenuState(theLink);
    
      // projects get loaded via  ajax
      if (theLink.hasClass("project"))
        {
        var req = new Request({ 
          method: "get", 
          url: TEMPLATE_URL + "/ajax" + theLink.get("rel"),
          onSuccess: function (e) {
            theContent = e;
            window.setTimeout(showContent, 400, e);
            }
          });
        req.send();
        return false;
        }
      // this is a accordion handle that needs to refresh when animation complete
      else if (theLink.getParent().get("class").indexOf("handle") >= 0)
        {
        clicked = theLink.get("href");
        return false;
        }
      // all others, just load the page
      else
        return;
      });
    });

  $$("div.column1 li.ro a").each(function(a){
     if (Browser.Engine.trident)
      return;
    
    var contents = new Element("span", { "html": a.get("html"), "class": "contents" });
    var copy = new Element("span", { "html": a.get("html"), "class": "copy" });
    a.set("html", "");

    var house1 = new Element("span", { "class": "house1" });

    house1.grab(contents);
    house1.grab(copy);

    a.grab(house1);

    var tween1 = new Fx.Tween(contents, { link: "ignore", duration: "short" });
    var tween2 = new Fx.Tween(copy, { link: "ignore", duration: "short" });

    a.addEvent("mouseenter", function(e){
      tween1.start("top", "20px");
      tween2.start("top", "0px");
      });

    a.addEvent("mouseleave", function(e){
      tween1.cancel();
      tween2.cancel();
      tween1.start("top", "0px");
      tween2.start("top", "-20px");
      });

    copy.removeEvents();
    contents.removeEvents();
    house1.removeEvents();
    });

  if($$("a.active")[0])
    setMenuState($$("a.active")[0]);
  
  guts1 = $$("div.column2")[0];

  gut1Tween = new Fx.Tween(guts1, { link: "cancel" });
  guts1.set("opacity", 0);
  guts1.setStyle("display", "block");
  gut1Tween.start('opacity', 0, 1);

  $$("div.column2 .type").each(function(el)  {
      var contents = el.get("text");
      el.set("text", "");
      var space = Browser.Engine.trident ? " " : " ";
      
      contents.split("\n").each(function(line){
        line.split(" ").each(function(word){
          
          var span = new Element("span", { "class" : "word", "html" : word + space, "opacity" : 0 });
          el.grab(span);
        });
        el.grab(new Element("br"));
      });
      
      });

  typewriterIn();
  });  // end domready

function typewriterIn()
  {
  $$(".type span").each(function(span) { 
    showit.delay(delay, span)
    delay += 50;
    });
  }
  
function showit()
  {
  this.set("opacity", 1);
  }
  
function showContent(content)
  {
  guts1.set("html", theContent);
  gut1Tween.start("opacity", 0, 1);
  initGallery();
  }
  
function hideContent()
  {
  gut1Tween.start("opacity", 1, 0);
  }
  
// eek your way up the menu heirarchy and set the right things
function setMenuState(active)
  {
  // wipe out all hightlights
  $$("div.column1 a").each( function(e){ e.removeClass("active"); });

  active.addClass("active");
  
  while (active)
    {
    active = getParentHandle(active)
    if(active != false)
      active.addClass("active");
    }
  }


function getParentHandle(node)
  {
  if (!node.getParent)
    alert(node);
    
  node = node.getParent("li");
  if (!node)
    return false;
    
  node = node.getParent("ul");
  if (!node)
    return false;

  node = node.getParent("li");
  if (!node)
    return false;
      
  node = node.getPrevious("li");
  if (!node)
    return false;

  node = node.getFirst();
  if (!node)
    return false;
  
  return node;
  }


function fixNested(accordion)
  {
  var element=$(accordion.elements[accordion.previous]);
  if(element && element.offsetHeight>0) 
    element.setStyle('height', heightValue);         
  }


// for swf object

var flashvars = {};
var params = {
	menu: "false",
	allowScriptAccess: "always",
	scale: "noscale",
  bgcolor: "#ff0000"	
};
var attributes = {};



var currentImage, lastImage;

function initGallery()
  {
  currentImage = 0;
  var items = 0;
  
  // numbers
  $$("#controls ul.numbers li a").each( function(e) {
    e.addEvent("click", function (e) {
    	showImage((e.target.get("html")-1));
      return false;
      });
    items++;
    });

  if (items > 1)
    {
    // arrows
    $$("ul.arrows li")[0].addEvent("click", function(e){
      showImage(currentImage - 1);
      return false;
      });
    $$("ul.arrows li")[1].addEvent("click", function(e){
      showImage(currentImage + 1);
      return false;
      });
    }
    
    
  // initialze image gallery
  
  $$("div#mediaArea ul li.galleryImage a.image").each( function(a){
    var href = a.get("href");
    
    a.set("href", "#");
    a.addEvent("click", function(e) { 
      showBigImage(href);
//      showBigImage(e.target.getParent().get("href"));
      return false;
      });  
    });

  // init video players
  $$("#mediaArea ul li.video").each(function(li){
    var container = li.getFirst("div");
    var link = li.getElement("a.playButton")

    var flashvars = params =  {videoUrl: link.get("href"), imageUrl: link.get("rel"), volume: 1, autoStart: "true", playerId:  "container.id"};

    swfobject.embedSWF(TEMPLATE_URL + "/f/KastnerVideoTest.swf", container.id, li.get("width"), li.get("height"), "9.0.0", flashvars, params, attributes);

    link.addEvent("click", function(e){
      $(container.id).playVideo();
      return false;
      });
    });
    
  // init swf banners
  $$("#mediaArea ul li.swf").each(function(li){
    var container = li.getFirst("div");
    var a = li.getElement("a.swfBanner");
    var i = a.getElement("img");
    a.addEvent("click", function(e){
      a.dispose();
      swfobject.embedSWF(a.get("href"), container.id, i.get("width"), i.get("height"), "9.0.0", flashvars, params, attributes);
      return false;
      });
      
    var button = li.getElement("a.swfButton");
    button.addEvent("click", function(e){
      a.dispose();
      swfobject.embedSWF(button.get("href"), container.id, i.get("width"), i.get("height"), "9.0.0", flashvars, params, attributes);
      return false;
      });
    });
    
  $$("#mediaArea ul").setStyle("left", 0);
  }
  
function showBigImage(src)
  {
  var image = new Element("img", { "src": src, "title": "Cerrar", "class": "imageFrame" });
  var link = new Element("a", { "href": null, 
                                "events": {
      'click': function(){
        $$("div#canvas").set("html", "");
        $$("div#canvas").set("opacity", "0");
      }}
    });
  link.appendText("Cerrar");
  link.grab(new Element("br"));
  link.adopt(image);
  $("canvas").set("opacity", 0);
  $("canvas").adopt(link);
  $("canvas").tween("opacity", 1);
    
  }
  
function showImage(index)
  {
  if (index == -1 || index == $$("#mediaArea ul li").length)
    return;
   
   if($$("#mediaArea ul li")[currentImage].get("class") == "video")
      $($$("#mediaArea ul li.video")[currentImage].getFirst("object").id).pauseVideo();
    
  var image = $$("#mediaArea ul li")[index];

  var number = $$("#controls ul.numbers li a")[index];

  number.addClass("active");
  
  if(lastImage && lastImage != number)
    lastImage.removeClass("active");

  lastImage = number;

  var next = (currentImage < index) ? true : false;
  
  currentImage = index;

  var xPos = index * 500;
    
	$$("#mediaArea ul").tween("left", -xPos);
  }
  
    
function getAddy(e,u){
location.href = 'mailto:' + e + '@' + u;
}

