var processing_atm=0;
var url_prefix="//"; // dont change!

function initCoolStuff () {
  if(do_really_cool_stuff) {
    var change_location=document.location.href.sub(/.*?\#(.*?)/, "");
    if (change_location!=document.location.href) {
      if (change_location.startsWith(url_prefix)) {
        change_location=change_location.sub(/.*?\/\/(.*?)/, "")
        document.location.href=site_url+change_location;
      } else {
        new Effect.ScrollTo(change_location,{duration:1});
      }
    }
    $$("#mainmenu a").invoke("observe", "click", function() {
      if(!processing_atm) {
        initChangeContent(this.parentNode.id,fixUrl(this.href));
      }
      this.href="#"+url_prefix+""+fixUrl(this.href);
    });
    updateLinks();
  }
  applySIFR();
  fix_pattern($("inner-content2"));
  return false;
}
var cache_obj = { 
  fx: function(e) {
    var linkobj=Event.findElement(e, 'A'); ;
    var url=linkobj.href;
    if (url.include(site_url) && !url.include("file_download") && !linkobj.hasClassName("external")) {
      if (url.endsWith(".jpg") || url.endsWith(".png") || url.endsWith(".gif")) {
        showImage(fixUrl(url));
        Event.stop(e);
      }
      else {
        if(!processing_atm) {
          initChangeContent(linkobj.parentNode.id,fixUrl(url));
          linkobj.href="#"+url_prefix+""+fixUrl(url);
        }
      }
    }
  }
};
cache_obj.bfx = cache_obj.fx.bindAsEventListener(cache_obj); 

function updateLinks () {
  $$("#content a,#secondary-content a").invoke("observe","click",cache_obj.bfx);
}

function showImage(url) {
  //alert(url);
  var the_image,dark_bg;
  the_image=new Image();
  the_image.src=site_url+url;
  new Insertion.After("all",
    "<div id=\"image-showcase\">"
   +"<span class=\"dark\" style=\"display:none;\"></span>"
   +"</div>");
  dark_bg=$$("#image-showcase span.dark")[0];
  new Effect.Appear(dark_bg,{from:0.0,to:0.75,duration:0.5,queue:"image_showcase"});
  the_image.onload=function(){
    var x=(document.viewport.getWidth()/2)-(the_image.width/2);
    if (x<12) x=12;
    var y=document.viewport.getScrollOffsets().top+(document.viewport.getHeight()/2)-(the_image.height/2);
    if (y<12) y=12;
    new Insertion.Bottom("image-showcase",
      "<div style=\"top:"+y+"px;left:"+x+"px;width:"+the_image.width+"px;\">"
     +"<span class=\"box top-left\"></span><span class=\"box top\"></span><span class=\"box top-right\"></span>"
     +"<span class=\"box left\"></span><span class=\"box right\"></span>"
     +"<span class=\"box bottom-left\"></span><span class=\"box bottom\"></span><span class=\"box bottom-right\"></span>"
     +"<span class=\"box2 top-left\"></span><span class=\"box2 top-right\"></span><span class=\"box2 bottom-left\"></span><span class=\"box2 bottom-right\"></span>"
     +"<span class=\"box3 top-left\"></span><span class=\"box3 top-right\"></span><span class=\"box3 bottom-left\"></span><span class=\"box3 bottom-right\"></span>"
     +"<a href=\"javascript:myvoid();\" style=\"display:none;width:"+the_image.width+"px;height:"+the_image.height+"px;\"><span></span><img src=\""+the_image.src+"\" /></a>"
     +"</div>");
    new Effect.Appear($$("#image-showcase div")[0],{duration:0.5,queue:"image_showcase"});
    new Effect.BlindDown($$("#image-showcase a")[0],{duration:0.5,queue:"image_showcase"});
  }
    
  $("image-showcase").onclick=function() {
    var showcase=$("image-showcase");
    new Effect.BlindUp(showcase.getElementsByTagName("a")[0],{duration:0.5,queue:"image_showcase"})
    new Effect.Fade(dark_bg,{duration:0.5,queue:{scope:"image_showcase",position:"end"},
      afterFinish:function(){ showcase.remove(); }
    });
  };
}

function fixUrl(url) {
  return url.sub(/.*?(\#.*?)/, "").sub(site_url, "").sub(url_prefix, "");
}

function applySIFR () {
/*
  var familian_elder = {
    src: site_url+"familian_elder.swf"
  };
  sIFR.delayCSS  = true;
  sIFR.activate(familian_elder);
  sIFR.replace(familian_elder, {
     selector: "div.portfolio-showcase.even h2"
    ,css: [
       ".sIFR-root { color:#000000; }"
    ]
    ,wmode: "transparent"
    ,selectable: false
  });
  sIFR.replace(familian_elder, {
     selector: "div.portfolio-showcase.odd h2"
    ,css: [
       ".sIFR-root { color:#200000;text-align:right; }"
    ]
    ,wmode: "transparent"
    ,selectable: false
  });
  sIFR.replace(familian_elder, {
     selector: "#secondary-content h2"
    ,css: [
       ".sIFR-root { color:#ffc664; }"
    ]
    ,wmode: "transparent"
    ,selectable: false
  });
  sIFR.replace(familian_elder, {
     selector: "h1"
    ,css: [
       ".sIFR-root { color:#ffc664; }"
      ,"em { font-style: normal; color: #000000; }"
    ]
    ,wmode: "transparent"
    ,selectable: false
  });
  sIFR.replace(familian_elder, {
     selector: "h2"
    ,css: [
       //".sIFR-root { color:#500000; }"
       ".sIFR-root { color:#000000; }"
    ]
    ,wmode: "transparent"
    ,selectable: false
  });
  sIFR.replace(familian_elder, {
     selector: "div.portfolio-showcase.odd p.category"
    ,css: [
       ".sIFR-root { color:#ad6c00;text-align:right;}"
    ]
    ,wmode: "transparent"
    ,selectable: false
  });
  sIFR.replace(familian_elder, {
     selector: "#content div.box p.category"
    ,css: [
       ".sIFR-root { color:#ad6c00;}"
    ]
    ,wmode: "transparent"
    ,selectable: false
  });
*/
}

function fix_pattern(container) {
  container.setStyle("padding-bottom: 0");
  var old_height=container.getHeight();
  var fix_px=5- (old_height % 5 );
  if (fix_px==5) fix_px=0;
  container.setStyle("padding-bottom: " + fix_px + "px;");
  //alert(container.id+":"+old_height+"=>"+container.getHeight()+"("+old_height%5+")");
}

function myvoid() { }




/*---------------------------------------------------------------*/
/*---------------------------------------------------------------*/

function initChangeContent(id,url) {
  processing_atm=1;
  new Effect.ScrollTo('header',{duration:0.5,
    afterFinish:function() {
      $$("body")[0].id="page_disabled";
      new Effect.Fade("mainmenu",{to:0.5,duration:0.3});
      new Insertion.Top("content", "<p id=\"throbber\" style=\"display:none;\"><img src=\""+site_url+"style/img/throbber.gif\" alt=\"Loading&hellip;\" /></p>"); 
      new Effect.BlindUp("inner-content");
      new Effect.Fade("inner-content",{queue:{scope:"initChange",position:"end"}});
      new Effect.Appear("throbber",{
        queue:{scope:"initChange",position:"end"},
        afterFinish:function() {
          changeContent(site_url+url);
        }
      });
      //$("inner-content2").setStyle("padding-bottom: 0px");
      var second_content_obj=$("secondary-content");
      if(second_content_obj!=null) {
        //second_content_obj.getElementsByTagName("div")[0].getElementsByTagName("div")[0].setStyle("padding-bottom: 0px");
        new Effect.SlideUp("secondary-content",{
          afterFinish:function(){
            second_content_obj.remove();
          }
        });
      }
    }
  });
}

/*---------------------------------------------------------------*/

function changeContent(url) {
  new Ajax.Request(url, { 
    method: "post",
    contentType: "application/x-www-form-urlencoded",
    parameters: {
      ajax: "1"
    },
    onComplete: function(transport) {
      var responseXML=transport.responseXML.documentElement;
      var section=responseXML.getElementsByTagName("section")[0].firstChild.data;
      var title=responseXML.getElementsByTagName("title")[0].firstChild.data;
      var content=responseXML.getElementsByTagName("content")[0].firstChild.data;
      var content2=responseXML.getElementsByTagName("secondarycontent")[0].firstChild.data;

      //alert(content2);
      $("inner-content2").update(content);
      document.title=title;
      if(section=="work" || section=="contact") {
        if(section=="work") $("article_nav").hide();
        new Insertion.Bottom("all", "<div id=\"secondary-content\" style=\"display:none;\"><div class=\"inner\"><div class=\"content\"></div></div></div>");
        $$("#secondary-content div.content")[0].update(content2);
      };
      
      new Effect.Fade("throbber",{
        queue:{scope:"applyChange",position:"end"},
        afterFinish:function() {
          $("throbber").remove();

      $("inner-content2").hide();
      new Effect.Appear("inner-content2");
        } 
      });
      var do_stuff=1;
      new Effect.BlindDown("inner-content",{
        queue:{scope:"applyChange",position:"end"},
        beforeUpdate:function () {
          if(do_stuff) {
            applySIFR();
            updateLinks();
            if(section=="work" || section=="contact") {
              if(section=="work") new Effect.BlindDown("article_nav",{duration:0.3});
              if(!content2.blank()) {
                var do_stuff2=1;

                new Effect.SlideDown("secondary-content",{
                  afterFinish:function() {
                    fix_pattern($$("#secondary-content div.content")[0]);
                  },
                  beforeUpdate:function () {
                    applySIFR();
                    updateLinks();
                  }
                });
              }
            } else {
            }
            do_stuff=0;
            $$("body")[0].id="page_"+section;
            new Effect.Appear("mainmenu",{duration:0.3});
          }
        },
        afterFinish:function(){
          fix_pattern($("inner-content2"));
          processing_atm=0;
        }
      });
    }
  }); 
}
