/*  Main Javascript-File powered by JQuery for the Jacobsen Rechtsanwaelte  
      url: http://www.jacobsen-legal.com  
      author: Robert Weber for closingtag 2009  
      email: info@closingtag.de
*/


var stages = new Array();
stages[0] = new Object();
stages[0]["src"] = "./img/stage.jpg";
stages[0]["alt"] = "Wellington bei Waterloo";
stages[1] = new Object();
stages[1]["src"] = "./img/stage2.jpg";
stages[1]["alt"] = "The Capture of the Royal Prince, 13 June 1666";
stages[2] = new Object();
stages[2]["src"] = "./img/stage3.jpg";
stages[2]["alt"] = "";
stages[3] = new Object();
stages[3]["src"] = "./img/stage4.jpg";
stages[3]["alt"] = "";
stages[4] = new Object();
stages[4]["src"] = "./img/stage5.jpg";
stages[4]["alt"] = "";
stages[5] = new Object();
stages[5]["src"] = "./img/stage6.jpg";
stages[5]["alt"] = "";
stages[6] = new Object();
stages[6]["src"] = "./img/stage7.jpg";
stages[6]["alt"] = "";
stages[7] = new Object();
stages[7]["src"] = "./img/stage8.jpg";
stages[7]["alt"] = "";

function cloneThis() {
      if($("#jacobsen #content img").length){
        var images = new Array();
            var i = 0;
            $("#jacobsen #content img").each(function(){
                  images[i] = this.offsetTop;
                  i++;
            });
            i = 0;
            $("#jacobsen #content").clone().appendTo("#jacobsen").addClass("clone");
            $("#jacobsen #content.clone img").addClass("clone");
            $("#jacobsen #content.clone img").each(function(){
                  $(this).css("top",images[i]+"px");
                  i++;
            });
            $("#jacobsen #content.clone h4").remove();
            $("#jacobsen #content.clone p").remove();
            $("#jacobsen #content.clone").not("#jacobsen #content.mandates").find(".column + .column img").css("left","353px");
            
            $("#jacobsen #content img").each(function(){
                  if(this.className != "clone"){
                        var height = this.offsetHeight;
                    if(!$("#jacobsen #content.mandates").length) height = height + 14;
                    if($("#jacobsen #content.mandates").length) height = height + 10;
                    if($(this).prev("p").length) $(this).prev("p").css("padding-bottom",height+"px");
                    else if($(this).next("p").length) $(this).next("p").css("padding-top",height+"px");
                        $(this).remove();
                  }
            });
      }
}


function randomImage() {  
  var imageIndex; 
  
  if (navigator.cookieEnabled == true && document.cookie) {               
    var pattern = /(.*)(image=)([0-9]{1})(.*)/;   
    var result = pattern.exec(document.cookie);   
    if(result != null){                       
      imageIndex = result[3];         
    } else {
      var randomnumber = Math.round(Math.random() * 7);
      
      if (navigator.cookieEnabled == true) {        
        var now = new Date();       
        later = new Date(now.getTime() + 180000);        
        document.cookie = "image="+randomnumber+"; domain="+location.hostname+"; path=/; expires="+later.toGMTString();     
      }
      
      imageIndex = randomnumber;  
    }
  
  } else {
    var randomnumber = Math.round(Math.random() * 7);
    
    if (navigator.cookieEnabled == true) {
      var now = new Date();
      later = new Date(now.getTime() + 180000);
      document.cookie = "image="+randomnumber+"; domain="+location.hostname+"; path=/; expires="+later.toGMTString();
    }
    imageIndex = randomnumber;
  }

  if(navigator.appVersion.indexOf("MSIE 6.") == -1) {
    $("#jacobsen").append("<div id=\"stage\"><img src=\"\" alt=\"\" /></div>");
    var object = $("#jacobsen #stage");
    var image = object.find("img:first");
    image.attr("src",stages[imageIndex]["src"]);
    if($("#jacobsen.en").length || $("#jacobsen.jobs").length) image.attr("src","."+stages[imageIndex]["src"]);
    if($("#jacobsen.en.jobs").length) image.attr("src","../."+stages[imageIndex]["src"]);
    image.attr("alt",stages[imageIndex]["alt"]);
  } else if(navigator.appVersion.indexOf("MSIE 6.") != -1) {
    $("#jacobsen").attr("style","background-image:url("+stages[imageIndex]["src"]+")");
    if($("#jacobsen.en").length) $("#jacobsen").attr("style","background-image:url("+stages[imageIndex]["src"]+")");
  }

  $(window).bind('resize', set_keyvisual);
  set_keyvisual();
}





function getNavigationPosition() {  
  if (navigator.cookieEnabled == true && document.cookie) {
    var pattern = /(.*)(top=)(.+?)(px)(.*)/;    
    var result = pattern.exec(document.cookie);   

    if(result != null){     
      if($("#navigation").length) {       
        $("#navigation").css("top",result[3]+"px");       
      }
    }   

    pattern = /(.*)(left=)(.+?)(px)(.*)/;   
    result = pattern.exec(document.cookie);   

    if(result != null){     
      if($("#navigation").length) {       
        $("#navigation").css("left",result[3]+"px");        
      }   
    } 
  }
}



function setPositionCookie(top,left) {  
  if (navigator.cookieEnabled == true) {    
    var now = new Date();   
    later = new Date(now.getTime() + 3600000);
    document.cookie = "top="+top+"; domain="+location.hostname+"; path=/;  expires="+later.toGMTString();   
    document.cookie = "left="+left+"; domain="+location.hostname+"; path=/;  expires="+later.toGMTString(); 
      }
}

function animateNavi() {
  $("#navigation").addClass("start");
  $("#navigation li").hide();
  $("#navigation").append("<li style=\"display:none;\"><a>Drag</a></li>");
  $("#navigation li:last").fadeIn(500,function(){
    $("#navigation li:last").append("<a style=\"display:none;\">&nbsp;and</a>");
    $("#navigation li:last a:last").fadeIn(500);
    $("#navigation").append("<li style=\"display:none;\"><a>Drop</a></li>");
    $("#navigation li:last").fadeIn(500,function(){
      $("#navigation").append("<li style=\"display:none;\"><a>Navigation</a></li>");
      $("#navigation li:last").fadeIn(500,function(){
        var left = $("#navigation")[0].offsetLeft - 185;
        var top = $("#navigation")[0].offsetTop - 185;  
        $("#jacobsen #navigation").css("top",$("#navigation")[0].offsetTop+"px");
        $("#jacobsen #navigation").css("left",$("#navigation")[0].offsetLeft+"px");
        $("#jacobsen").append("<div id=\"arrow\"></div>");
        $("#jacobsen #arrow").css("top",top+"px");
        $("#jacobsen #arrow").css("left",left+"px");
        $("#jacobsen #arrow").animate({width: "269px", height: "269px"}, 1500 , function(){
          left = $("#navigation")[0].offsetLeft - 169 - $("#navigation")[0].clientWidth;
          top = $("#navigation")[0].offsetTop - 169 - $("#navigation")[0].clientHeight;
          $("#jacobsen #navigation").animate({left: left+"px", top: top+"px"}, 1500, function(){
            $("#jacobsen #arrow").remove();
            $("#navigation li a").each(function(){
              if(this.href == "") $(this).parent("li").remove();
            });
            $("#navigation li").each(function(){
              $(this).show();
            });
          });
        });
      });   
    });   
  });
}


function set_keyvisual() {
  var $img = $('#jacobsen #stage img:first');
  var ratio = 1218 / 1624; 
  // Get browser window size
  var browserwidth = $(window).width();
  var browserheight = $(window).height();
  // Scale the image
  if ((browserheight/browserwidth) > ratio){
      $img.height(browserheight);
      $img.width(browserheight / ratio);
  } else {
      $img.width(browserwidth);
      $img.height(browserwidth * ratio);
  }
}


$(document).ready(function() {
      $(window).load(function(){
            var contentheight = $("#jacobsen #content")[0].clientHeight - 200;
        $("#jacobsen.start h2").hide();
        $("#jacobsen.start h3").hide();
        if(navigator.appVersion.indexOf("MSIE") != -1)     {
          $("#jacobsen #content .column #subnavigation li:last-child").addClass("last-child");
          $("#jacobsen #content #language li:last-child").addClass("last-child");
        }
        
        randomImage();
        
        if(navigator.appVersion.indexOf("MSIE 6.") == -1) {
          if($("#jacobsen #content").length) getNavigationPosition();
        };

        $("#jacobsen.start #content").hide();
      
        if(navigator.appVersion.indexOf("MSIE 6.") == -1)     {
          $("#jacobsen").append("<div id=\"body\">&nbsp;</div>");
          $("#jacobsen #body").hide();
        }
        
        if($("#jacobsen").length) {
             $("a.mail").each(function(){
                   if($(this).attr("href") != "") {
                             var text = $(this).attr("href");
                         text = text.replace("$$","@");
                         text = text.replace("#",".");
                         $(this).attr("href","mailto:"+text);
                       } else {
                              
                             var text = $(this).text();
                         text = text.replace("$$","@");
                         text = text.replace("#",".");
                         $(this).attr("href","mailto:"+text);
                             $(this).text(text);
                       }
                       
                 });
        }
      
        if($("#jacobsen #content").length && !$("#jacobsen.start").length) {
          if(navigator.appVersion.indexOf("MSIE 6.") == -1)     {
            $("#jacobsen #content").addClass("transparent");
                        cloneThis();
            $("#jacobsen #body").css("height",contentheight+"px");
            $("#jacobsen #body").show();
            $("#jacobsen #navigation").removeClass("white");
            $("#jacobsen #navigation").draggable({
              stop: function(){
                                    setPositionCookie($(this).css("top"),$(this).css("left"));
            }});
          } else {
                        $("#jacobsen #content #language").css("display","block");
          }
        } else {
                  $("#jacobsen #navigation").addClass("white");
          $("#jacobsen.start #navigation").hide();
          var h1Text = $("#jacobsen.start h1 a").text();
          var h1Link = $("#jacobsen.start h1 a");
          $("#jacobsen.start h1 a").remove();
          $("#jacobsen.start h1").text(h1Text);
          var pattern = /(.*)(intro=)(.+)/;
          var result = pattern.exec(window.location.href);
          if(result != null) {
            if(result[3] == "false") {
              $("#jacobsen.start h1").text("");
              $("#jacobsen.start h1").append(h1Link);
              if(navigator.appVersion.indexOf("MSIE 6.") == -1)     {
                $("#jacobsen #content").addClass("transparent");
                $("#jacobsen.start #content").show();
                                    cloneThis();
                $("#jacobsen #body").css("height",contentheight+"px");
                $("#jacobsen.start #content").hide();
                $("#jacobsen #body").show();
                $("#jacobsen.start #content").show();
                                    $("#jacobsen #navigation").removeClass("white");
                $("#jacobsen #navigation").show();
                                    $("#jacobsen #navigation").draggable({
                                          stop: function(){setPositionCookie($(this).css("top"),$(this).css("left"));}
                                    });
              } else {
                $("#jacobsen.start h1").show();
                $("#jacobsen.start #content").show();
                                    $("#jacobsen #navigation").removeClass("white");
                                    $("#jacobsen.start #navigation").show();
                $("#jacobsen #content #language").css("display","block");
              }
            } else {
              $("#jacobsen.start h1").bind("click",function(){
                $("#jacobsen.start #content").hide();
                $("#jacobsen #navigation").addClass("white");
                                    $("#jacobsen.start #navigation").hide();
                $("#jacobsen #body").hide();
                $("#jacobsen.start h1").fadeOut("slow");
                $("#jacobsen.start h1").text("");
                $("#jacobsen.start h1").append(h1Link);
                $("#jacobsen.start h1 + h2").css("color","#fff");
                $("#jacobsen.start h1 + h2").fadeIn("slow");
                $("#jacobsen.start h1 + h2").bind("click",function(){
                  $("#jacobsen.start h1 + h2").fadeOut("slow");
              $("#jacobsen.start h1").fadeIn("slow");
              if(navigator.appVersion.indexOf("MSIE 6.") == -1)     {
                $("#jacobsen.start #content").addClass("transparent");
                $("#jacobsen.start #content").show();
                                                cloneThis();
                var contentheight = $("#jacobsen.start #content")[0].scrollHeight + 353;
                $("#jacobsen #body").css("height",contentheight+"px");
                $("#jacobsen.start #content").hide();
                $("#jacobsen #body").fadeIn("slow", function(){
                  $("#jacobsen.start #content").show();
                });
                $("#jacobsen #navigation").removeClass("white");
                                                $("#jacobsen #navigation").show();
                                                //animateNavi();
                                                $("#jacobsen #navigation").draggable({
                                                      stop: function(){setPositionCookie($(this).css("top"),$(this).css("left"));}
                                                });                                         
                $("#jacobsen.start h1").unbind("click");
              } else {
                $("#jacobsen.start h1").fadeIn("slow");
                $("#jacobsen.start #content").fadeIn('slow');
                $("#jacobsen #navigation").removeClass("white");
                                                $("#jacobsen.start #navigation").fadeIn('slow');
                $("#jacobsen #content #language").css("display","block");
              }
                });
              });
            }
          } else {
            $("#jacobsen.start h1").bind("click",function(){
              $("#jacobsen.start #content").hide();
              $("#jacobsen.start #navigation").hide();
                              $("#jacobsen #navigation").addClass("white");
                              $("#jacobsen #body").hide();      
              $("#jacobsen.start h1").fadeOut("slow");
              $("#jacobsen.start h1").text("");
              $("#jacobsen.start h1").append(h1Link);
              $("#jacobsen.start h1 + h2").css("color","#fff");
              $("#jacobsen.start h1 + h2").fadeIn("slow");
              $("#jacobsen.start h1 + h2").bind("click",function(){
                $("#jacobsen.start h1 + h2").fadeOut("slow");
                $("#jacobsen.start h2 + h3").fadeOut("slow");
                                    $("#jacobsen.start h1").fadeIn("slow");
                                    if(navigator.appVersion.indexOf("MSIE 6.") == -1)     {
                                          $("#jacobsen.start #content").addClass("transparent");
                                          $("#jacobsen.start #content").show();
                                          cloneThis();
              var contentheight = $("#jacobsen.start #content")[0].scrollHeight + 353;
                                          $("#jacobsen #body").css("height",contentheight+"px");
                                          $("#jacobsen.start #content").hide();
                                          $("#jacobsen #body").fadeIn("slow", function(){
                                                $("#jacobsen.start #content").show();
                                          });
                                          $("#jacobsen #navigation").removeClass("white");
                                          $("#jacobsen #navigation").show();
                                          //animateNavi();
                                          $("#jacobsen #navigation").draggable({
                                                stop: function(){setPositionCookie($(this).css("top"),$(this).css("left"));}
                                          }); 
                                          $("#jacobsen.start h1").unbind("click");
      
                } else {
                                          $("#jacobsen.start h1").fadeIn("slow");
                  $("#jacobsen.start #content").fadeIn('slow');
                                          $("#jacobsen.start #navigation").removeClass("white");
                  $("#jacobsen.start #navigation").fadeIn('slow');
                                          $("#jacobsen #content #language").css("display","block");
      
                }       
              });
            });
          }
        }
      });
});

