/*
* @package    mediacluster.de custom jQuery functions
* @author     Matthias Schrodt, m.schrodt@mediacluster.de
*/

(function($) {

function dbg(msg) {
  if(typeof(console) != 'undefined') console.log(msg);
}

$(function () {
  
  // showcase slider
  if ($('#slider1').length > 0) {
    
    var anchor = document.location.href.split('#'), declinator = '';    
    if (anchor[1]) anchor = anchor[1]; else anchor = 1;    
    
    category == 'Kunde' ? declinator = 'r' : declinator = '';
    category == 'Kunde' ? $('#slider1_all').attr('href', '/19' ) : $('#slider1_all').attr('href', '/20' );
    
  	$('#slider1').anythingSlider({
      startPanel      : anchor,
  		autoPlay        : false, 
  		width           : 405,
  		theme           : 'default',
      pauseonHover    : true,
      delay           : 6000,
      easing          : 'easeInOutCubic',
      animationTime   : 600,
      forwardText     : "N&auml;chste" + declinator + " " + category,
		  backText        : "Vorige" + declinator + " " + category,
      onSlideBegin    : function(slider){
        $('#claim').animate({left : '-440px'}, 500, 'easeInOutElastic');
        $('#bg img').animate({opacity:0},300);
      },
  		onSlideComplete : function(slider){  		  
  		  getSlideInfo(slider.currentPage);
  		  $('#claim').css({opacity : 0, left : '-60px'}).animate({opacity : 1}, 200);
        $('#bg img').animate({opacity:1},300);
  		}
  	});  	
  	getSlideInfo( $('#slider1').data('AnythingSlider').currentPage );    
  	$('#slider1_all').find('.category').text(category);
    
  	Cufon.replace('.arrow a, #slider1_all', {hover:true});
  	$('#slider1_more').appendTo('.anythingSlider');
  	$('#slider1_all').appendTo('.anythingSlider');  	  	    
  }
  
  // text content slider
  if ( $('td.block', '#content').length > 0) {
  
    var $cols = $('td.block', '#content').find('.col'), 
        maxH = 0;
    
    $.each($cols, function(i,c) {
      var h = $(c).height();
      if ( h > maxH ) maxH = h;      
    });        
    
    $('#content-slider').anythingSlider({
      autoPlay      : false,
      width         : 900,
      height        : maxH,
      forwardText   : "Weiter",
      backText      : "Zur&uuml;ck"
    });
  }
  
  // general set up following...
  Cufon.replace('h1, h2, span.s0', {textShadow : '1px 1px rgba(0,0,0,.4)'});
  Cufon.replace('h3, #nav #top a, .news-item a, .arrow a', {hover:true});  
  
  // set up hnav
  var subnav = $('#sub');
  if (subnav.children('li').length == 0) {subnav.remove();
  }
  else {
    var subpos = $('#top li.active').offset(); 
    $('#sub').css('left', (subpos.left+30) + 'px').children('li.divider:eq(0)').remove();     
  }
  // ...and subnav
  if ( $('#sub2').length > 0 ) $('#sub2').css({'top': $('#col2').position().top - 4}).children('li.divider:eq(0)').remove();
   
  // jScrollPane for people page
  if ( $('#people').length > 0 ) {  
    var w = 0;
    $('#people').children().children('div').each(function() {
      w += $(this).outerWidth(true);    
    });
    $('.scrollpane').jScrollPane();
    $('.scrollpane').find('.jspPane').css('width', w + 'px');
  }  
  
  // fancybox, obviously
  $('.fancybox').fancybox({cyclic : true, overlayOpacity : .73, titleShow : false});
  
  // hnav sharing animations
  $('#share a, #mailto a').each(function() {
  
    var self = $(this),    
        img = self.find('img'),
        width = parseInt(img.css('width')),
        height = parseInt(img.css('height')),
        label = self.find('label');
        
    self.addClass('closed').hover(function(e) {
      
      if (self.hasClass('closed')) {        
        img.animate({width : (2*width)+'px', height : (2*height)+'px', top : 0}, 150, function() {
          self.addClass('open').removeClass('closed');
        });        
        label.show().animate({'opacity' : 1}, 450);
      }      
    }, 
    function(e) {
      
        img.animate({width : width+'px', height : height+'px', top : '9px'}, 300, function() {
          self.addClass('closed').removeClass('open');
        });        
        label.animate({'opacity' : 0}, 100).hide();      
    });
  });
  
}); // end $(function())


$(document).ajaxSuccess(function() {  
	Cufon('h1, h2');
});

$(window).unload(function() {
  $('#bg img').attr('src', '');
});


function getSlideInfo(sliderIndex) {  
  
  var currentSlide = $('#content .slide' + sliderIndex),
      // copy project labels to external div
      project = currentSlide.find('.project').html(),
      client = currentSlide.find('.client').html(),  
      descr = currentSlide.find('.descr').eq(0).text();
      bgimg = currentSlide.find('.bgimg').html();
  
  $('#bg img').attr('src', bgimg);
  $('#claim').find('h1').html(client)
    .end().find('h2').html(project)
    .end().find('div.text').html(descr);
    
  if (typeof(Hyphenator) != 'undefined') Hyphenator.run();
  Cufon.refresh();
  
  // clone url to second fancybox trigger  
  var url = currentSlide.find('.url').eq(0).text();  
  $('#slider1_more').attr('href', url);

  var anchor = '<a class="fancybox" href="' + url + '"></a>';
  if( $('.teaserPic a').length ) {
    $('.teaserPic a').attr('href', url);
  } else {
    $('.teaserPic img').wrap(anchor);
  }

}

function showPanel(idx) {
  $('#panel' + idx).animate({'opacity' : 1 }, 500, 'easeInOutBounce');
}

}(jQuery));

