//close button
function close_the_box() {
  $('#box .view ').fadeOut('100', function() {
    $("#box").empty();
    $('#box').fadeOut('50', function() {
      $("#box").css({
        'z-index' : '0'
      })
    });
  });
}

$(document).ready(function() {

  //SHOWREEL LAYOUT FIX!!!!
  if($.browser.msie) {
    if(parseInt(jQuery.browser.version) == 8) {

      $('html').css({
        'overflow' : 'hidden'
      })
      $('#playerBig').css({
        'margin-top' : '-30px'
      })
    }
    if(parseInt(jQuery.browser.version) == 9) {

      $('#playerBig').css({
        'margin-top' : '-140px'
      })
    }
  }

  // browserweiche
  var badBrowser;
  if($.browser.msie) {
    if(parseInt(jQuery.browser.version) == 6 || parseInt(jQuery.browser.version) == 7) {
      badBrowser = true;
      $('body').prepend('<div id="IE_notice"></div>')
      $('#IE_notice').load('IE_notice.html');
    }

    //alert("bad browser!");
  }

  //deeplinking function
  if(window.location.hash != "" && !badBrowser) {
    var deeplink = window.location.hash;
    //remove the #
    deeplink = deeplink.substring(1);
    //slice ID and TABLE
    var url_x = deeplink.lastIndexOf('-');
    var deeplinkTable = deeplink.slice(0, url_x);
    var deeplinkId = deeplink.slice(url_x + 1);

    //show Layer
    if(deeplinkTable == "projects") {
      showProjectLayer(deeplinkTable, deeplinkId);
    } else if(deeplinkTable == "directors") {
      showDirectorLayer(deeplinkTable, deeplinkId);
    } else if(deeplinkTable == "playground") {
      showPlaygroundLayer(deeplinkTable, deeplinkId);
    } else if(deeplinkTable == "illustrators") {
      showIllustratorLayer(deeplinkTable, deeplinkId);
    }
  }

  //thumb overview fade-------------------------------------------------------------------

  $("img.thumb").hover(function() {
    $(this).stop().animate({
      "opacity" : "0.14"
    }, 350);
  }, function() {
    $(this).stop().animate({
      "opacity" : "1"
    }, 350);
  });
  //thumb to artikel @ DIRECTORS -------------------------------------------------------------------
  $(window).load(function() {
    var elems = $('.director');
    var count =      elems.size() - 1;
    elems.each(function(i) {
      $(this).delay(50*i).fadeIn(250, function() {
        if(i == count) {
          $('#full-page-container').jScrollPane();
        }
      });
    });
  });
  $('.director').click(function() {

    var tableValue = $(this).find(".table").text();
    var idValue = $(this).find(".id").text();
    window.location.hash = tableValue + "-" + idValue;
    showDirectorLayer(tableValue, idValue);
  });
  function showDirectorLayer(tableValue, idValue) {
    $('#wrap').find("#box").load('getDirector.php', {
      id : idValue,
      table : tableValue
    }, function() {
      Cufon.refresh();
      $("#box").css({
        'display' : 'block',
        'z-index' : '200'
      })
      $("#box .view ").fadeIn('slow');

      $('a').bind("focus", function() {
        $(this).blur()
      });
      $('.closeArticle').bind('click', function(e) {
        $('#box .view ').fadeOut('100', function() {
          $("#box").empty();
          $('#box').fadeOut('50', function() {
            $("#box").css({
              'z-index' : '0'
            })
          });
        });
      });
      $(document).bind('click', function(e) {
        $('#box .view ').fadeOut('100', function() {
          $("#box").empty();
          $('#box').fadeOut('50', function() {
            $("#box").css({
              'z-index' : '0'
            })
          });
        });
      });
      $('#box .content').click(function(e) {
        e.stopPropagation();
      });
    });
  }

  //thumb to artikel @ PROJECT -------------------------------------------------------------------
  $(window).load(function() {
    var elems = $('.project');
    var count =  elems.size() - 1;
    elems.each(function(i) {
      $(this).delay(50*i).fadeIn(250, function() {
        if(i == count) {
          $('#full-page-container').jScrollPane();
        }
      });
    });
  });
  $('.project').click(function() {
    var tableValue = $(this).find(".table").text();
    var idValue = $(this).find(".id").text();
    window.location.hash = tableValue + "-" + idValue;
    showProjectLayer(tableValue, idValue);
  });
  function showProjectLayer(tableValue, idValue) {
    $('#wrap').find("#box").load('getProject.php', {
      id : idValue,
      table : tableValue
    }, function() {
      Cufon.refresh();
      $("#box").css({
        'display' : 'block',
        'z-index' : '200'
      })
      $("#box .view ").fadeIn('slow');

      $('a').bind("focus", function() {
        $(this).blur()
      });
      $('.closeArticle').bind('click', function(e) {
        $('#box .view ').fadeOut('100', function() {
          $("#box").empty();
          $('#box').fadeOut('50', function() {
            $("#box").css({
              'z-index' : '0'
            })
          });
        });
      });
      $(document).bind('click', function(e) {
        $('#box .view ').fadeOut('100', function() {
          $("#box").empty();
          $('#box').fadeOut('50', function() {
            $("#box").css({
              'z-index' : '0'
            })
          });
        });
      });
      $('#box .content').click(function(e) {
        e.stopPropagation();
      });
    });
  }

  //thumb to artikel @ PlAYGROUND -------------------------------------------------------------------
  $(window).load(function() {
    var elems = $('.playground');
    var count =  elems.size() - 1;
    elems.each(function(i) {
      $(this).delay(50*i).fadeIn(250, function() {
        if(i == count) {
          $('#full-page-container').jScrollPane();
        }
      });
    });
  });
  $('.playground').click(function() {
    var tableValue = $(this).find(".table").text();
    var idValue = $(this).find(".id").text();
    window.location.hash = tableValue + "-" + idValue;
    showPlaygroundLayer(tableValue, idValue);
  });
  function showPlaygroundLayer(tableValue, idValue) {
    $('#wrap').find("#box").load('getPlayground.php', {
      id : idValue,
      table : tableValue
    }, function() {
      Cufon.refresh();
      $("#box").css({
        'display' : 'block',
        'z-index' : '200'
      })
      $("#box .view ").fadeIn('slow');

      $('a').bind("focus", function() {
        $(this).blur()
      });
      $('.closeArticle').bind('click', function(e) {
        $('#box .view ').fadeOut('100', function() {
          $("#box").empty();
          $('#box').fadeOut('50', function() {
            $("#box").css({
              'z-index' : '0'
            })
          });
        });
      });
      $(document).bind('click', function(e) {
        $('#box .view ').fadeOut('100', function() {
          $("#box").empty();
          $('#box').fadeOut('50', function() {
            $("#box").css({
              'z-index' : '0'
            })
          });
        });
      });
      $('#box .content').click(function(e) {
        e.stopPropagation();
      });
    });
  }

  //thumb to artikel @ ILLUSTRATOR -------------------------------------------------------------------
  $(window).load(function() {
    var elems = $('.illustrator');
    var count =      elems.size() - 1;
    elems.each(function(i) {
      $(this).delay(50*i).fadeIn(250, function() {
        if(i == count) {
          $('#full-page-container').jScrollPane();
        }
      });
    });
  });
  $('.col .illustrator').click(function() {

    var tableValue = $(this).find(".table").text();
    var idValue = $(this).find(".id").text();
    window.location.hash = tableValue + "-" + idValue;
    showIllustratorLayer(tableValue, idValue);
  });
  function showIllustratorLayer(tableValue, idValue) {

    $('#wrap').find("#box").load('getIllustrator.php', {
      id : idValue,
      table : tableValue
    }, function() {

      $("#box .view ").delay(200).fadeIn('slow');
      Cufon.refresh();
      $("#box").css({
        'display' : 'block',
        'z-index' : '200'
      })
      startSlideshow();
      $('.IthumbWrap').click(function() {
        $('.IthumbWrap').removeClass('active');
        index = $(this).index();
        loadSlideImage(index);
        $(this).addClass('active');
      });
      $('.closeIllu').bind('click', function(e) {
        $('#box .view ').fadeOut('100', function() {
          $("#box").empty();
          $('#box').fadeOut('50', function() {
            $("#box").css({
              'z-index' : '0'
            })
          });
        });
      });
      $(document).bind('click', function(e) {
        $('#box .view ').fadeOut('100', function() {
          $("#box").empty();
          $('#box').fadeOut('50', function() {
            $("#box").css({
              'z-index' : '0'
            })
          });
        });
      });
      $('#box .content').click(function(e) {
        e.stopPropagation();
      });
    });
  }

  function loadSlideImage(index) {
    imgBox = $('.imageFull .currentSlide');
    imgBox.removeClass('currentSlide').fadeOut();

    $('.imageFull').find('img').eq(index).addClass('currentSlide').one("load", function() {
    resizeSlideImage();
    })
    .each(function() {
      if(this.complete || (jQuery.browser.msie ))
        $(this).trigger("load");
    });
  }

  function startSlideshow() {
    imgBox = $('.imageFull');
    imgBox.find('img:first').addClass('currentSlide').load(function() {
      resizeSlideImage();
    });
    /*
     $('.imageFull img:first').delay(2000).fadeIn(1000, function() {
     $('.imageFull img:gt(0)').hide();
     setInterval( function() {
     $('.imageFull :first-child').fadeOut(1000).next('img').fadeIn(1000).end().appendTo('.imageFull');
     },4000);
     });
     */
  }

  $(window).resize(function() {
    resizeSlideImage();
  });
  function resizeSlideImage() {
    imgBox = $('.imageFull');
    var maxWidth =      imgBox.innerWidth() - 244;
    var maxHeight =      imgBox.innerHeight() - 187;
    slideImage = $('.imageFull').find('.currentSlide');
    slideImageHeight = slideImage.height();
    slideImageWidth = slideImage.width();

    if(slideImageHeight > slideImageWidth) {
      max_size = maxHeight;
      var h = max_size;
      var w = Math.ceil(slideImageWidth / slideImageHeight * max_size);
      if(w > maxWidth) {
        max_size = maxWidth;
        var w = max_size;
        var h = Math.ceil(slideImageHeight / slideImageWidth * max_size);
      }

    } else {
      max_size = maxWidth;
      var w = max_size;
      var h = Math.ceil(slideImageHeight / slideImageWidth * max_size);
      if(h > maxHeight) {
        max_size = maxHeight;
        var h = max_size;
        var w = Math.ceil(slideImageWidth / slideImageHeight * max_size);
      }
    }
    //console.log("container width = "+maxWidth+"container height = "+maxHeight+" max_size"+max_size+" bild width = "+slideImageWidth);

    slideImage.css({
    'height': h,
    'width' : w
    }).fadeIn();

  }

  //INIT CUFON FONT REPLACE-------------------------------------------------------------------

  Cufon.replace('.title_1, .clientLogin, .subContent .copy span, .closeFoot, .closeArticle, .subContent .head, .title_2, .title_3, .title, .menu a, h3, h2, .teaser h4, .teaser h5, .clips h4, .clips h5', {
    fontFamily : 'Univers Condensed',
    hover : 'true'
  });

  //DISABLE FOCUS---------------------------------------------------------------

  $('a').bind("focus", function() {
    $(this).blur()
  });
  //FOOT MENU ACCORDION ---------------------------------------------------------------

  $('#foot .menu a').click(function() {
    $('.subContent').hide();
    $('.closeFoot').show();
    $('#foot .menu a').removeClass('active');

    var y = $('#foot .menu a').index(this);
    var speed = 500;
    var style = "easeOutBack";
    var callback = function() {
      $('.subContent').hide()
      $('.subContent').eq(y).show();
      //IMPRESSUM SCROLLER---------------------------------------------------------------

    }
    $('#foot .menu a').eq(y).addClass('active');

    $('#foot .menu').animate({
      'height' : '282px'
    }, {
      duration : speed,
      easing : style,
      complete : callback
    });

    $('#foot ').animate({
      'height' : '335px'
    }, {
      duration : speed,
      easing : style,
      complete : callback
    });
    Cufon.refresh();

    $('.closeFoot').bind('click', function(e) {

      $('.closeFoot').hide();

      var speed = 500;
      var style = "swing";
      var callback = function() {

      }
      $('#foot .menu').animate({
        'height' : '22px'
      }, {
        duration : speed,
        easing : style,
        complete : callback
      });

      $('#foot ').animate({
        'height' : '30px'
      }, {
        duration : speed,
        easing : style,
        complete : callback
      });

      $('.subContent').hide();
      $('#foot .menu a').removeClass('active');
      Cufon.refresh();
    });
    $(document).bind('click', function(e) {

      $('.closeFoot').hide();

      var speed = 500;
      var style = "swing";
      var callback = function() {

      }
      $('#foot .menu').animate({
        'height' : '22px'
      }, {
        duration : speed,
        easing : style,
        complete : callback
      });

      $('#foot ').animate({
        'height' : '30px'
      }, {
        duration : speed,
        easing : style,
        complete : callback
      });

      $('.subContent').hide();
      $('#foot .menu a').removeClass('active');
      Cufon.refresh();
    });
    $('#foot').click(function(e) {
      e.stopPropagation();
    });
  });
  //IE FIX---------------------------------------------------------------

  if(jQuery.browser.msie && jQuery.browser.version == 7) {

    $('#full-page-container').css({
      'top' : '152px'
    })
  }

  //INIT SCROLLBAR-------------------------------------------------------------------

  $(function() {

    var win = $(window);
    var isResizing = false;
    win.bind(
    'resize', function() {
    if (!isResizing) {
    isResizing = true;
    var container = $('#full-page-container');
    container.css({
    'width': 1,
    'height': 1
    }
    );
    container.css({
    'width': win.width(),
    'height': win.height()-185
    }
    );
    isResizing = false;
    container.jScrollPane({
    'showArrows': true
    }
    );
    }
    }
    ).trigger('resize');

    $('body').css('overflow', 'hidden');

    if($('#full-page-container').width() != win.width()) {
      win.trigger('resize');
    }

  });
  //newsSlider script
  var currentPosition = 0;
  var slideWidth = 240;
  var slides = $('.slide');
  var numberOfSlides = slides.length;

  $('#slidesContainer').css('overflow', 'hidden');

  slides.wrapAll('<div id="slideInner"></div>').css({
    'float' : 'left',
    'width' : slideWidth
  });

  $('#slideInner').css('width', slideWidth * numberOfSlides);
  $('#slideshow').prepend('<span class="control" id="leftControl"><img src="img/news_cursor_left.png"/></span>').append('<span class="control" id="rightControl"><img src="img/news_cursor_right.png"/></span>');
  manageControls(currentPosition);

  $('.control').bind('click', function() {
    currentPosition = ($(this).attr('id') == 'rightControl') ? currentPosition + 1 : currentPosition - 1;
    manageControls(currentPosition);
    $('#slideInner').animate({
      'marginLeft' : slideWidth * (-currentPosition)
    });
  });
  /*
   *
   */
  function manageControls(position) {
    if(position == 0) {

      $('#leftControl').hide()

    } else {

      $('#leftControl').show()

    }
    if(position == numberOfSlides - 1) {

      $('#rightControl').hide()

    } else {

      $('#rightControl').show()

    }
  }

  //showreel playlist ----------------------------------------------------------------------------------------
  /*
   var globX;
   var globY;
   var tempX;
   var tempY;
   var timerXX = 20;
   var showPlaylistxx = true;
   //PlWidth = $('.playlist').width();
   $('BODY').mousemove( function(e) {
   globX = e.pageX;
   globY = e.pageY;
   });
   function playlistFade() {
   if((tempX == globX && tempY == globY) ) {
   if(timerXX <= 0 ) {
   if(showPlaylistxx == true) {
   //$('.playlist').slideUp(300);
   $('.playlist').stop().animate({
   'right' : -240
   }, 300);
   showPlaylistxx = false;
   }
   }
   } else {
   if(showPlaylistxx == false) {
   //$('.playlist').slideDown(300);
   $('.playlist').stop().animate({
   'right' : 0
   }, 300);
   timerXX = 20;
   showPlaylistxx = true;
   }
   }
   if(timerXX > -1)
   timerXX--;
   tempX = globX;
   tempY = globY;
   }

   window.setInterval( playlistFade,100);
   */
});

