//##############################################################################
//##  DOM																	  ##
//##############################################################################
$(document).ready( externalLinks );
/*###########################################
##  Dropdown Menus						   ##
###########################################*/
/* Options */
var __global_menuContainer = '.mainNav';
/* Controls */
$(document).ready(function($) {
  $(__global_menuContainer+'>li').hover(function(){$(this).addClass('sfhover');}, function(){ $(this).removeClass('sfhover');});
	var timer;
  $('.login input')
    .bind('focus', function() {$(this).parents('form').parents('ul').addClass('opened'); if(timer) clearTimeout(timer);})
    .bind('blur', function() {var leaving = this; timer = setTimeout(function() { $(leaving).parents('.opened').removeClass('opened'); }, 1000);});
});



/*###########################################
##  Legacy Transparency					   ##
###########################################*/
$(document).ready(function() { $('img[src$=png]').addClass('transparent'); });

/*###########################################
##  Cufón Init							   ##
###########################################*/
Cufon.replace('#topPhone h2 b, #promo h1, #testimonials h2, #welcome h1, #announcements h2, #related h2, #support h2', { fontFamily: 'Cardo', hover: true });
Cufon.replace('#testimonials .body p, #pageLabel h1, #related li a, #support li a, .details h2, #categories .list a', { fontFamily: 'Calibri', fontWeight: '400', hover: true });
Cufon.replace('#testimonials .body p b, #testimonials h3, #testimonials h4, #announcements h4, #related li a b, #support li a b', { fontFamily: 'Calibri', fontWeight: '700', hover: true });
Cufon.replace('#navList h2 a, #siteSearch h2', { hover: true, 
												 separate: 'none',
												 fontFamily: 'Calibri',
												 fontWeight: '700',
												 textShadow: '1px 1px rgba(0, 0, 0, 0.3)'
											   });

/*###########################################
##  Content Sliders						   ##
###########################################*/
/* Options */
var __global_sliderObject = '#slides';
var __global_pagerObject = '#sliderNav';

/* Controls */
$(function() {
  if ($(__global_sliderObject).exists()) { //main promotion slider
    var slider = $(__global_sliderObject)
      .cycle({ 
        fx:		'fade', 
        timeout:	10000, //how long to wait between slides
        speed:	600, //how long transition takes
        pause:	1, //stop on mouseover
        pager:	__global_pagerObject
    });

    var cntrl = $('#playerControl').addClass('running').click(function(e) {
      e.preventDefault();
      var el = $(this),
          comm = el.attr('data-command') || 'pause',
          state = el.hasClass('active') ? 'active' : 'running';
     
      if( (comm =='pause'&&state!='running') || (comm=='resume' && state == 'running'))
        return; // nothing to do

      // can safely forget about comm now and just toggle
      slider.cycle('toggle')
      el.toggleClass('active running');

      if(comm!='toggle') el.attr('data-command', 'toggle');

    });

    $(document).bind('youtube.playerstatechange', function(e, newState, player) {
      // 1 = play, 2 = pause, 3 = buffering, 0 = ended, 5 = cued, -1 = unstarted
      if((newState == 1)) {
        cntrl.attr('data-command','pause');
        cntrl.click();
      }
      else if (newState == 2) {
        // just let user manually resume slide show if they pause
      }
      else if (newState == 0) {
        cntrl.attr('data-command', 'resume');
        cntrl.click();
      }
    });

    slider.createPoints();

  }
});
/* Points */
$.fn.createPoints = function() { 
  var pages = $(__global_pagerObject).children('a').get(); //read pagination
  $.each(pages, function(){ $(this).html('&#8226;'); });
};
//##############################################################################
//##############################################################################
//##  App init                          ##
//##############################################################################
jQuery(function($){

  $('.downloads .description').hide().addClass('closed');
  $('.downloads').delegate('dt', 'click', function(e){
    e.preventDefault();
    var n = $(this).nextAll('.description');
    if (n.hasClass('closed'))
      n.show('fast', function() { $(this).removeClass('closed'); });
    else
      n.hide('fast', function() { $(this).addClass('closed'); }); 
  }).find('dt').css('cursor', 'pointer');

  $('.video a').each(function() {
    var href = $(this).attr('href').replace("watch?v=", "v/") ,
        vid = $(this).attr('id') || 'v_'+href.substring(href.lastIndexOf('/')+1),
        parent = $(this).parent(),
        width = $(this).attr('data-width'),
        height = $(this).attr('data-height');
    
    parent.flash({
      swf: href+'?enablejsapi=1&version=3&playerapiid='+vid,
      id: vid,
      wmode: true,
      allowScriptAccess: 'always',
      width: width, height: height
    });

  });

  window.onPlayerStateChange = function(newstate) {
    $(document).trigger('youtube.playerstatechange', [newstate, onPlayerStateChange.player]);
    onPlayerStateChange.player = null;
  };
  window.onYouTubePlayerReady  = function(playerid) {
    var player = document.getElementById(playerid);
    onPlayerStateChange.player = player;
    player.addEventListener('onStateChange', 'onPlayerStateChange');
  };



});

//##############################################################################
//##############################################################################
//##  User Handler Functions												  ##
//##############################################################################
function externalLinks() {
  var exs = $('a[rel=external]');
  $.each(exs, function() {
    if ($(this).attr('href')) { $(this).attr('target','_blank'); }
  });
}
//##############################################################################
//##############################################################################

//##############################################################################
//##############################################################################
//##  Globals																  ##
//##############################################################################
jQuery.fn.exists = function(){return jQuery(this).length>0;}
//##############################################################################
//##############################################################################

