/*  PETER LANG
 *  File:       peterlang.js
 *  Abstract:   javascript for petertlang.net
 *  Version:    1.0
 *  (CC):       Marc Wright | Team Tejat Hamburg 2010
 


/* some pre-loads */
if (document.images){
    pre = new Image();
    pre_image_url = new Array();
    /* insert your preloads here */
    pre_image_url[0] = 'http://www.petertlang.net/wp-content/themes/peter_thomas_lang/images/loading.gif';
    pre_image_url[1] = 'http://www.petertlang.net/wp-content/themes/peter_thomas_lang/images/badge.png';
    pre_image_url[2] = 'http://www.petertlang.net/wp-content/themes/peter_thomas_lang/images/story.png';
    pre_image_url[3] = 'http://www.petertlang.net/wp-content/themes/peter_thomas_lang/images/tipsy.gif';
    
    var i = 0;
    for(i=0; i<= 3; i++) 
    pre.src = pre_image_url[i];
}


$(document).ready(function(){
  /* open all external links in a new window  */
  $("a.ext", this).click(function(){
    window.open(this.href);
    return false;
  });
  
  /* sanatize images */
  $(".two-collumns img").each(function() {
    
    $(this).removeAttr('alt')
    $(this).removeAttr('title')
    $(this).removeAttr('height')
    $(this).removeAttr('width')
    $(this).removeAttr('style')
    $(this).removeAttr('class')
    
  });
  
  
  $("a[class=thickbox]").click(function(){
    return false;
  });
  
  
  /* dropdown */
  $(".dropdown dt a").click(function() {
      $(".dropdown dd ul").toggle();
      $("#shade").show();
  });
              
  $(".dropdown dd ul li a").click(function() {
      var text      = $(this).html();
      
      //$(".dropdown dt a span").html(text);
      window.setTimeout('$(".dropdown dd ul").hide(); ', 400);
      openWin(getSelectedValue('dropdown'));
  });
  
  
  
  
  $(document).bind('click', function(e) {
      var $clicked = $(e.target);
      if (! $clicked.parents().hasClass("dropdown"))
          $(".dropdown dd ul, #shade").hide();
  });
  
  
  /* init tipsy */
  $('.tipsy').tipsy({html: true, gravity: 'w', fade: true, delayIn: 500, delayOut: 900});
  $('.tipsyE').tipsy({html: true, gravity: 'w', fade: true, delayIn: 500, delayOut: 900});
  $('.tipsy-ne').tipsy({html: true, gravity: 'nw', fade: true, delayIn: 500, delayOut: 900});
  $('.tipsy-nw').tipsy({html: true, gravity: 'ne', fade: true, delayIn: 500, delayOut: 900});
  
  $('.tipsy-se').tipsy({html: true, gravity: 'sw', fade: true, delayIn: 500, delayOut: 900});
  
  $('.tipsy-s').tipsy({html: true, opacity: 0.8, gravity: 'sw', fade: true, delayIn: 500, delayOut: 900});
  
  $('div#breadcrumb a, #home_link').tipsy({html: true, gravity: 'n', fade: true, delayIn: 500, delayOut: 900});
  
  
  /* graphic stories iFrame*/
  /*
  $("#home_link").fancybox({
    'width'             : 700,
    'height'            : 500,
    'autoScale'         : false,
    'transitionIn'      : 'none',
    'transitionOut'     : 'none',
    'type'              :'iframe',
    'scrolling'         : 'no',
    'overlayOpacity'  : 0.95,
    'overlayShow'     : true,
    'onComplete'	:	function() {
    		$("body").css({'overflow':'hidden'});
    },
    'onClosed'		: function() {
    		$("body").css({'overflow':'auto'});
    	}
    
  });
  */
  /* init fancybox for picasa images */
  
  $("a[class=thickbox]").fancybox({
    'showNavArrows'   : true,
    'overlayOpacity'  : 0.8,
    'padding'         : 0,
    'transitionIn'    : 'elastic',
    'transitionOut'   : 'elastic',
    'speedIn'         : 200, 
    'speedOut'        : 200, 
    'overlayShow'     : true
  });
  
  

  /* init fancybox for picasa images */
  $('a[href*="wp-content"]').fancybox({
    'showNavArrows'   : true,
    'overlayOpacity'  : 0.8,
    'padding'         : 20,
    'transitionIn'    : 'elastic',
    'transitionOut'   : 'elastic',
    'speedIn'         : 200, 
    'speedOut'        : 200, 
    'overlayShow'     : true
  });
  
  /* vimeo */
  $('a[href*="player.vimeo"]').click(function() {
      
      $.fancybox({
                  'padding'       : 0,
                  'overlayOpacity'  : 0.8,
                  'autoScale'     : false,
                  'transitionIn'  : 'none',
                  'transitionOut' : 'none',
                  'title'         : this.title,
                  'width'         : 700,
                  'height'        : 495,
                  'href'          : this.href.replace(new RegExp("([0-9])","i"),'moogaloop.swf?clip_id=$1').replace("player.",'').replace("video/",''),
                  'type'          : 'swf'
              });

              return false;
    });
  
  
  /* youtube */
  /*$('a[href*="youtube"]').click(function() {
        $.fancybox({
    			'padding'		: 0,
    			'autoScale'		: false,
    			'transitionIn'	: 'none',
    			'transitionOut'	: 'none',
    			'title'			: this.title,
    			'width'		: 680,
    			'height'		: 495,
    			'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/') //--> Youtube
    			'type'			: 'swf',
    			'swf'			: {
    			   	 'wmode'		: 'transparent',
    				'allowfullscreen'	: 'true'
    			}
    		});

    	return false;
    
    });
  
  */
  // 
    /* count elemets; >2 init cycle */
      numImages = $('#inner_images div').children().size();
      
      if(numImages >= 2 ){
              /* init cycle */
              $('#inner_images div').each(function() {

                  $(this)
                    .cycle({ 
                     fx: 'fade',
                        delay:              5000,            // additional delay (in ms) for first transition (hint: can be negative)
                        speed:              1500,
                        easing:             "easeOutCirc", 
                        timeout:            7000,
                        pause:              0,          // true to enable "pause on hover" 
                        pauseOnPagerHover:  0,          // true to pause when hovering over pager link 
                        pager:              '#pager',
                        next:               '.next',
                        prev:               '.prev',
                        fastOnEvent:        1
              });
              
            });
      }else{
        /* init cycle */
        $('#inner_images').each(function() {

            $(this)
              .cycle({ 
               fx: 'fade',
                  delay:              5000,            // additional delay (in ms) for first transition (hint: can be negative)
                  speed:              1500,
                  easing:             "easeOutCirc", 
                  timeout:            7000,
                  pause:              0,          // true to enable "pause on hover" 
                  pauseOnPagerHover:  0,          // true to pause when hovering over pager link 
                  pager:              '#pager',
                  next:               '.next',
                  prev:               '.prev',
                  fastOnEvent:        1
        });
        
      });
        
      } // end if

});

$(document).ready(function(){
  $('#inner_images div').cycle('pause');
  $('.next').trigger('click');

  $('#excerpt').mouseover(function(){
    $('#inner_images div').cycle('pause');
  })
})

 /* little win opener */
function openWin(url){
    var v = "http://.";
    var result = url.search(v);
    
    if(result == -1){
        location.href = url;
      }else{
          myWin = window.open(url);
          myWin.focus();
       
      }
}
function getSelectedValue(id) {
    return $("#" + id).find("dt a span.value").html();
}


/* open admin */
function win2(popup2) {
  OpenWin = this.open(popup2, "CtrlWindow2", "toolbar=no,menubar=no,location=no,scrollbars=no,resize=yes,status=yes, width=980,height=700");
}


/* the key bindings */
$(document).keydown(function (e) {
      
      /* right */
      if (e.which == 39) {
        $("#next").trigger('click');
        $('#inner_images').cycle('pause');
      /* left */
      } 
       if (e.which == 37) {
        $("#prev").trigger('click');
        $('#inner_images').cycle('pause');
      }
});

