// iPad/iPhone viewport/page bug fix
if(navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod')
    {
     $("#globalfooter").css("position", "static");
    };





// Infopanel Showcase show/hide

function test (){
    var hoppewe = $('h1').width();
  alert (hoppewe);
    }

function showPanel () {
  $(".info-wrapper").animate({width: '+=500px', height: '+=100px'}, "slow");
  $(".content-wrapper").animate({width: '+=500px', height: '+=100px'}, "slow");
  $(".content-wrapper .left").animate({width: '+=250px', height: '+=140px'}, "slow");
  $(".teaser-wrapper").animate({width: '+=500px', top: '+=100px'}, "slow");
  $(".description-wrapper").animate({left: '-=500px', width: '+=180', top: '+=100px'}, "slow");
  //$(".shareCase").animate({left: '-=500px', width: '+=100'}, "slow");
  $(".nav-wrapper .hide").animate({height: '+=100px'}, "slow");
  $(".nav-wrapper .show").animate({height: '+=100px'}, "slow");
  $(".tx-ttnewstags-pi1").animate({right: '+=400px', width: '+=100px', bottom: '+=100px'}, "slow");
  $('.description-wrapper').css('display','block');
  $('.links-wrapper').css('display','block');
  $('.related-wrapper').css('display','block');
  $('.show').css('display','none');
  $('.hide').css('display','block');
  $('.teaser-wrapper').css('display','none');
  $('.tx-ttnewstags-pi1').css('display','block');
  $('.content-wrapper .left').css('border-right','1px solid #3f3e3b');
}

function hidePanel () {
  $(".info-wrapper").animate({width: '-=500px', height: '-=100px'}, "slow");
  $(".content-wrapper").animate({width: '-=500px', height: '-=100px'}, "slow");
  $(".content-wrapper .left").animate({width: '-=250px', height: '-=140px'}, "slow");
  $(".teaser-wrapper").animate({width: '-=500px', top: '-=100px'}, "slow");
  $(".description-wrapper").animate({left: '+=500px', width: '-=180', top: '-=100px'}, "slow");
  $(".nav-wrapper .hide").animate({height: '-=100px'}, "slow");
  $(".nav-wrapper .show").animate({height: '-=100px'}, "slow");
  $(".tx-ttnewstags-pi1").animate({right: '-=400px', width: '-=100px', bottom: '-=100px'}, "slow");
  $('.description-wrapper').css('display','none');
  $('.links-wrapper').css('display','none');
  $('.related-wrapper').css('display','none');
  $('.show').css('display','block');
  $('.hide').css('display','none');
  $('.teaser-wrapper').css('display','block');
  $('.tx-ttnewstags-pi1').css('display','none');
  $('.content-wrapper .left').css('border-right','none');
}

function showglobalnav () {
  $("#globalheader_flex").animate({top: '+=100px', opacity: '1'}, "slow");
  $('#globalheader_flex #shownav a').css('display','none');
  $('#globalheader_flex #hidenav a').css('display','block');
  $("#globalheader_flex #hidenav a").animate({top: '+=100px'}, "slow");
  $("#globalheader_flex #shownav a").animate({top: '+=100px'}, "slow");
  $("#outer_container").animate({top: '+=100px'}, "slow");
}
function hideglobalnav () {
  $("#globalheader_flex").animate({top: '-=100px', opacity: '0.8'}, "slow");
  $('#globalheader_flex #shownav a').css('display','block');
  $('#globalheader_flex #hidenav a').css('display','none');
  $("#globalheader_flex #hidenav a").animate({top: '-=100px'}, "slow");
  $("#globalheader_flex #shownav a").animate({top: '-=100px'}, "slow");
  $("#outer_container").animate({top: '-=100px'}, "slow");
}


//Imagegallery WEADYOU Showcase
$(document).ready(function(){
	$('#showcases .case a').hover(function(){
      $('.rollover-info', this).stop().animate({ opacity: '0.9' },{queue:false,duration:250});
    } , function(){
        $('.rollover-info', this).stop().animate({ opacity: '0px' },{queue:false,duration:250});
    });
});


  var img_W = 2560;
  var img_H = 1440;
  var thb_W = 240;
  var thb_H = 150;
  var menu_W = 300;
  var gap = 20;
  var space_L = 30;
  var space_R = 50;
  var fontSize = 24;
  var right_col = 225+50;
  var border_col = 225+50;



  var MIN_COLS = 1;
  var started=false;
  var offx = 0;
  var offy = 0;
  maxy = new Array();
  var space = 10;
  
  
  // on site load (DOM READY)
  $(function() { 
    arrangeCases(); arrangeFrontImg();
  });
  
  // on window resize, call again
  $(window).resize( function() { arrangeCases(); arrangeFrontImg(); started=true; } );
  arrangeCases();
  
//---------------------------------------------------------------------------------------------------//
// Showcases Anordnung
//---------------------------------------------------------------------------------------------------//
  
  function arrangeCases() {
    var z=0;
    
    //get the width for showcases
    var all_W = parseInt($('body').innerWidth() - menu_W - (space_L + space_R));
    var globalnav_W = parseInt($('body').innerWidth() - (space_L + space_R));
    //how many cols fits here?
    var cols = Math.round (all_W / (thb_W + gap));
    //get width for cols
    var col_W = Math.round ((all_W / cols) - gap);
    // get new showcase width
    var showases_W = cols * (col_W + gap);
    //get scale factor for images
    var scaleFactor = col_W / thb_W;
    var scaleFont = Math.round (fontSize * parseInt($('body').innerWidth()) / img_W);
    // get new image size
    var new_thb_W = Math.round (thb_W * scaleFactor);
    var new_thb_H = Math.round (thb_H * scaleFactor);
    var rollover_H = new_thb_H + 5;
	// get window middle
	var xCenter = parseInt($('body').innerWidth() / 2) - 66;

   

    
  $('#showcases').css('width',showases_W + 'px');
  $('.case').css('width',col_W + 'px');
  $('.case').css('height',new_thb_H + 'px');
  $('.case').css('margin-right',gap);
  $('#showcases .case a .rollover-info').css('width',new_thb_W + 'px');
  $('#showcases .case a .rollover-info').css('height',new_thb_H + 'px');
  $('#showcases .case a .rollover-info').css('top', -new_thb_H-5 + 'px');
  $('#globalnav').css('width',globalnav_W + 'px');
  $('#main-container').css('width',globalnav_W + 'px');
  $('#main-container #content').css('width',showases_W + 'px');
  $('#hr .hr-left').css('width',showases_W-20 + 'px');
  $('#preloader').css('left',xCenter + 'px');





    //alert(space);
    for (x=0; x < cols; x++) {
      maxy[x] = 0;
    }
    
    // lets iterate over all posts
    $('.case').each(function(i) {
    
              
      var pos, cursor, w , altura= 0;  
  
      w = (Math.floor($(this).outerWidth() / col_W));
      cursor = 0;
      
      if (w>1) {
        for (x=0; x < cols-(w-1); x++) {
          cursor = maxy[x] < maxy[cursor] ? x : cursor;
        }
        pos = cursor;
        
        for (var x=0; x<w; x++) {
          altura = Math.max(altura, maxy[pos+x]);
        }
        for (var x=0; x<w; x++) 
          maxy[pos+x] = parseInt($(this).outerHeight()) + GAP + altura;
          
        $(this).css('left', pos*(col_W) + offx).css('top',altura + offy);

      }
      else {
        
          for (x=0; x < cols; x++) {
            cursor = maxy[x] < maxy[cursor] ? x : cursor;
          }
          //*from left to right
          //cursor=z%columns;
          //z++;
          
        
          $(this).css('left', cursor*(col_W) + offx).css('top',maxy[cursor] + offy);
          
          if(started){
            $(this).stop().animate({
              left:cursor*(col_W) + offx+space/2,
              top:maxy[cursor] + offy},200+i*20);
              $('.caseimg').css('width',new_thb_W);
              $('.caseimg').css('height',new_thb_H);
          }else{
            $(this).css('left', 100).css('top',maxy[cursor] + offy);
            $('.caseimg').css('width',new_thb_W);
            $('.caseimg').css('height',new_thb_H);
            
          }
          maxy[cursor] += $(this).outerHeight() + GAP;


      }
    
    });

      $('#showcases').css('width',all_W);
    

      
      
    var maxH=0;
    for(i=0;i<maxy.length;i++){
      if(maxy[i]>maxH){
        maxH=maxy[i];
      }
      
    }

  
  }
  



//---------------------------------------------------------------------------------------------------//
// Single Image Scale
//---------------------------------------------------------------------------------------------------//

function arrangeFrontImg() {
  
  //get the width for image
  var all_W = window.innerWidth;
  var all_H = window.innerHeight;
  //get scale factor for images
  var scaleFactor = all_W / img_W;
  var scaleFactor2 = all_H / img_H;
  // get new image size
  var new_img_W = Math.round (img_W * scaleFactor);
  var new_img_W_2 = Math.round (img_W * scaleFactor2);
  var new_img_H = Math.round (img_H * scaleFactor);
  
  
  $('#bgimg').css('width',new_img_W + 'px');
  $('#bgimg').css('height',new_img_H + 'px'); 
  $('#showcase').css('width',all_W + 'px'); 

  
  
  if(new_img_H < all_H) {
    $('.caseimg').css('height',all_H + 'px'); 
    $('.caseimg').css('width',new_img_W_2 + 'px');
  }
}
 
 
 
//---------------------------------------------------------------------------------------------------//
// Showcase Single Gallery
//---------------------------------------------------------------------------------------------------//
 
 function weadyou_showcase(){
  //set default view mode
  $defaultViewMode="full"; //full (fullscreen background), fit (fit to window), original (no scale)
  //cache vars
  $bg=$("#bg");
  $bgimg=$("#bg #bgimg");
  $preloader=$("#preloader");
  $outer_container=$("#outer_container");
  $outer_container_a=$("#outer_container a.thumb_link");
  $toolbar=$("#toolbar");
  $nextimage_tip=$("#nextimage_tip");
  
$(window).load(function() {
  $toolbar.data("imageViewMode",$defaultViewMode); //default view mode
  ImageViewMode($toolbar.data("imageViewMode"));
  //cache vars
  $customScrollBox=$("#customScrollBox");
  $customScrollBox_container=$("#customScrollBox .container");
  $customScrollBox_content=$("#customScrollBox .content");
  $dragger_container=$("#dragger_container");
  $dragger=$("#dragger");
  $outer_container_a.attr("class", "hoppe"); //switch class
  
  CustomScroller();
  
  function CustomScroller(){
    outerMargin=0;
    innerMargin=20;
    $customScrollBox.height($(window).height()-outerMargin);
    $dragger_container.height($(window).height()-innerMargin);
    visibleHeight=$(window).height()-outerMargin;
    if($customScrollBox_container.height()>visibleHeight){ //custom scroll depends on content height
      $dragger_container,$dragger.css("display","block");
      totalContent=$customScrollBox_content.height();
      draggerContainerHeight=$(window).height()-innerMargin;
      animSpeed=400; //animation speed
      easeType="easeOutCirc"; //easing type
      bottomSpace=1.05; //bottom scrolling space
      targY=0;
      draggerHeight=$dragger.height();
      $dragger.draggable({ 
        axis: "y", 
        containment: "parent", 
        drag: function(event, ui) {
          Scroll();
        }, 
        stop: function(event, ui) {
          DraggerOut();
        }
      });

      //scrollbar click
      $dragger_container.click(function(e) {
        var mouseCoord=(e.pageY - $(this).offset().top);
        var targetPos=mouseCoord+$dragger.height();
        if(targetPos<draggerContainerHeight){
          $dragger.css("top",mouseCoord);
          Scroll();
        } else {
          $dragger.css("top",draggerContainerHeight-$dragger.height());
          Scroll();
        }
      });

      //mousewheel
      $(function($) {
        $customScrollBox.bind("mousewheel", function(event, delta) {
          vel = Math.abs(delta*10);
          $dragger.css("top", $dragger.position().top-(delta*vel));
          Scroll();
          if($dragger.position().top<0){
            $dragger.css("top", 0);
            $customScrollBox_container.stop();
            Scroll();
          }
          if($dragger.position().top>draggerContainerHeight-$dragger.height()){
            $dragger.css("top", draggerContainerHeight-$dragger.height());
            $customScrollBox_container.stop();
            Scroll();
          }
          return false;
        });
      });

      function Scroll(){
        var scrollAmount=(totalContent-(visibleHeight/bottomSpace))/(draggerContainerHeight-draggerHeight);
        var draggerY=$dragger.position().top;
        targY=-draggerY*scrollAmount;
        var thePos=$customScrollBox_container.position().top-targY;
        $customScrollBox_container.stop().animate({top: "-="+thePos}, animSpeed, easeType); //with easing
      }

      //dragger hover
      $dragger.mouseup(function(){
        DraggerOut();
      }).mousedown(function(){
        DraggerOver();
      });

      function DraggerOver(){
        $dragger.css("background", "url(round_custom_scrollbar_bg_over.png)");
      }

      function DraggerOut(){
        $dragger.css("background", "url(round_custom_scrollbar_bg.png)");
      }
    } else { //hide custom scrollbar if content is short
      $dragger,$dragger_container.css("display","none");
    }
  }

  //resize browser window functions
  $(window).resize(function() {
    FullScreenBackground("#bgimg"); //scale bg image
    $dragger.css("top",0); //reset content scroll
    $customScrollBox_container.css("top",0);
    $customScrollBox.unbind("mousewheel");
    CustomScroller();
  });
  
  LargeImageLoad($bgimg);
});
  
  //loading bg image
  $bgimg.load(function() {
    LargeImageLoad($(this));
  });
  
  function LargeImageLoad($this){
    $preloader.fadeOut("fast"); //hide preloader
    $this.removeAttr("width").removeAttr("height").css({ width: "", height: "" }); //lose all previous dimensions in order to rescale new image data
    $bg.data("originalImageWidth",$this.width()).data("originalImageHeight",$this.height());
    if($bg.data("newTitle")){
      $this.attr("title",$bg.data("newTitle")); //set new image title attribute
    }
    FullScreenBackground($this); //scale new image
    $bg.data("nextImage",$($outer_container.data("selectedThumb")).next().attr("href")); //get and store next image
    if(typeof itemIndex!="undefined"){
      if(itemIndex==lastItemIndex){ //check if it is the last image
        $bg.data("lastImageReached","Y");
        $bg.data("nextImage",$outer_container_a.first().attr("href")); //get and store next image
      } else {
        $bg.data("lastImageReached","N");
      }
    } else {
      $bg.data("lastImageReached","N");
    }
    $this.fadeIn("slow"); //fadein background image
    if($bg.data("nextImage") || $bg.data("lastImageReached")=="Y"){ //don't close thumbs pane on 1st load
      SlidePanels("close"); //close the left pane
    }
    NextImageTip();
  }

  //slide in/out left pane
  $outer_container.hover(
    function(){ //mouse over
      SlidePanels("open");
    },
    function(){ //mouse out
      SlidePanels("close");
    }
  );
  
  //Clicking on thumbnail changes the background image
  $outer_container_a.click(function(event){
    event.preventDefault();
    var $this=this;
    $bgimg.css("display","none");
    $preloader.fadeIn("fast"); //show preloader
    //style clicked thumbnail
    $outer_container_a.each(function() {
        $(this).children(".selected").css("display","none");
      $(this).attr("class", "thumb_link");
      });
    $(this).children(".selected").css("display","block");
    //get and store next image and selected thumb 
    $outer_container.data("selectedThumb",$this); 
    $bg.data("nextImage",$(this).next().attr("href"));   
    $bg.data("newTitle",$(this).children("img").attr("title")); //get and store new image title attribute
    itemIndex=getIndex($this); //get clicked item index
    lastItemIndex=($outer_container_a.length)-1; //get last item index
    $bgimg.attr("src", "").attr("src", $this); //switch image
    $(this).attr("class", "hoppe"); //switch class
  }); 

  //clicking on large image loads the next one
  $bgimg.click(function(event){
    var $this=$(this);
    if($bg.data("nextImage")){ //if next image data is stored
      $this.css("display","none");
      $preloader.fadeIn("fast"); //show preloader
      $($outer_container.data("selectedThumb")).children(".selected").css("display","none"); //deselect thumb
      $($outer_container.data("selectedThumb")).attr("class", "thumb_link");
      if($bg.data("lastImageReached")!="Y"){
        $($outer_container.data("selectedThumb")).next().children(".selected").css("display","block"); //select new thumb
        $($outer_container.data("selectedThumb")).next().attr("class", "hoppe"); //select new thumb
      } else {
        $outer_container_a.first().children(".selected").css("display","block"); //select new thumb - first
        $outer_container_a.first().attr("class", "hoppe"); //select new thumb - first
      }
      //store new selected thumb
      var selThumb=$outer_container.data("selectedThumb");
      if($bg.data("lastImageReached")!="Y"){
        $outer_container.data("selectedThumb",$(selThumb).next()); 
      } else {
        $outer_container.data("selectedThumb",$outer_container_a.first()); 
      }
      $bg.data("newTitle",$($outer_container.data("selectedThumb")).children("img").attr("title")); //get and store new image title attribute
      if($bg.data("lastImageReached")!="Y"){
        itemIndex++;
      } else {
        itemIndex=0;
      }
      $this.attr("src", "").attr("src", $bg.data("nextImage")); //switch image
    }
  });
  
  //function to get element index (fuck you IE!)
  function getIndex(theItem){
    for ( var i = 0, length = $outer_container_a.length; i < length; i++ ) {
      if ( $outer_container_a[i] === theItem ) {
        return i;
      }
    }
  }
  
  //toolbar (image view mode button) hover
  $toolbar.hover(
    function(){ //mouse over
      $(this).stop().fadeTo("fast",1);
    },
    function(){ //mouse out
      $(this).stop().fadeTo("fast",0.8);
    }
  ); 
  $toolbar.stop().fadeTo("fast",0.8); //set its original state
  
  //Clicking on toolbar changes the image view mode
  $toolbar.click(function(event){
    if($toolbar.data("imageViewMode")=="full"){
      ImageViewMode("fit");
    } else if($toolbar.data("imageViewMode")=="fit") {
      ImageViewMode("original");
    } else if($toolbar.data("imageViewMode")=="original"){
      ImageViewMode("full");
    }
  });

  //next image balloon tip
  function NextImageTip(){
    if($bg.data("nextImage")){ //check if this is the first image
      $nextimage_tip.stop().css("right",20).fadeIn("fast").fadeOut(2000,"easeInExpo",function(){$nextimage_tip.css("right",$(window).width());});
    }
  }

  //slide in/out left pane function
  function SlidePanels13456(action){
    var speed=900;
    var easing="easeInOutExpo";
    if(action=="open"){
      $("#arrow_indicator").fadeTo("fast",0);
      $outer_container.stop().animate({left: 0}, speed,easing);
      $bg.stop().animate({left: 585}, speed,easing);
    } else {
      $outer_container.stop().animate({left: -710}, speed,easing);
      $bg.stop().animate({left: 0}, speed,easing,function(){$("#arrow_indicator").fadeTo("fast",1);});
    }
  }

//Image scale function
function FullScreenBackground(theItem){
  var winWidth=$(window).width();
  var winHeight=$(window).height();
  var imageWidth=$(theItem).width();
  var imageHeight=$(theItem).height();
  if($toolbar.data("imageViewMode")!="original"){ //scale
    $(theItem).removeClass("with_border").removeClass("with_shadow"); //remove extra styles of orininal view mode
    var picHeight = imageHeight / imageWidth;
    var picWidth = imageWidth / imageHeight;
    if($toolbar.data("imageViewMode")!="fit"){ //image view mode: full
      if ((winHeight / winWidth) < picHeight) {
        $(theItem).css("width",winWidth).css("height",picHeight*winWidth);
      } else {
        $(theItem).css("height",winHeight).css("width",picWidth*winHeight);
      };
    } else { //image view mode: fit
      if ((winHeight / winWidth) > picHeight) {
        $(theItem).css("width",winWidth).css("height",picHeight*winWidth);
      } else {
        $(theItem).css("height",winHeight).css("width",picWidth*winHeight);
      };
    }
    //center it
    $(theItem).css("margin-left",((winWidth - $(theItem).width())/2)).css("margin-top",((winHeight - $(theItem).height())/2));
  } else { //no scale
    //add extra styles for orininal view mode
    $(theItem).addClass("with_border").addClass("with_shadow");
    //set original dimensions
    $(theItem).css("width",$bg.data("originalImageWidth")).css("height",$bg.data("originalImageHeight"));
    //center it
    $(theItem).css("margin-left",((winWidth-$(theItem).outerWidth())/2)).css("margin-top",((winHeight-$(theItem).outerHeight())/2));
  }
}

//image view mode function - full or fit
function ImageViewMode(theMode){
  $toolbar.data("imageViewMode", theMode); //store new mode
  FullScreenBackground($bgimg); //scale bg image
  //re-style button
  if(theMode=="full"){
    $toolbar.html("<span class='lightgrey'>IMAGE VIEW MODE &rsaquo;</span> FULL");
  } else if(theMode=="fit") {
    $toolbar.html("<span class='lightgrey'>IMAGE VIEW MODE &rsaquo;</span> FIT");
  } else {
    $toolbar.html("<span class='lightgrey'>IMAGE VIEW MODE &rsaquo;</span> ORIGINAL");
  }
}

//preload script images
var images=["ajax-loader_dark.gif","round_custom_scrollbar_bg_over.png"];
$.each(images, function(i) {
  images[i] = new Image();
  images[i].src = this;
});
}
