/** =default.js
************************************************************
@project		Kalsec
************************************************************/

/** =defaulttext
************************************************************/
$('#search-main-input').defaulttext();

/** =slideshow
************************************************************/
$(document).ready(function() {	
	var $slides = $('.slideshow > .slides');
	if (!$slides.length) { return; }
	$.getScript(FM.scriptDir + 'jquery.cycle.min.js', function(){
	 	$($slides).cycle({ 
		    fx:      'fade', 
		    timeout:  4000 
		});
	});
});

/** =nav
************************************************************/
$(document).ready(function() { 
    $('ul.nav-main').superfish({ 
	    animation: { height: 'show' },
	    delay: 400,
	    speed: 300
    }); 
}); 

/** =gray boxes
************************************************************/
$(document).ready(function() {
	var $box = $('div.feature-box');
	$box.find(':last-child').addClass('last');
	
	$box.each(function() {
		boxHeight = $(this).height() + 20;
		$(this).parents('.content-main, .content-side').css('paddingBottom', boxHeight);
	});
	
});

/** =biglinks
************************************************************/
$(document).ready(function() {
	var $els = $('.home-content .feature');
	if (!$els.length) { return; }
	$.getScript(FM.scriptDir + 'jquery.biglinks.js', function(){
		$els.biglinks();
	});
});

/** =tabs
************************************************************/
$(document).ready(function() {
	var $tabs = $('.tabs');
	if (!$tabs.length) { return; }
	$.getScript(FM.scriptDir + 'jquery-ui-1.7.2.core-tabs.min.js', function(){
		$tabs.tabs();
	});
	
});

$(document).ready(function() {
  var $carousel = $('.carousel');
  if (!$carousel.length) { return; }
  var $tickerList = $carousel.find('ul'),
      $tickerItems = $carousel.find('li');

  var numItems = $tickerItems.length;
  $carousel.height($tickerItems.outerHeight() * numItems);

  if ($tickerItems.length < 4) {
    return;
  }
  
  // set first and last list item to nearly transparent
  $tickerList.find('li:first-child, li:last-child').css({opacity: 0.1});

  var tickerInfo = {};

  tickerInfo.height = $tickerList.outerHeight() + 8;
  tickerInfo.speed = function($el) {
    return 50 * (tickerInfo.height + parseInt($el.css('marginTop'), 10));
  };
  $tickerList.clone().removeClass().insertAfter($tickerList);


  $(window).bind('load', function() {
    var steps = tickerInfo.speed($tickerList) / numItems;
    var fadeRepeat;

    var runTicker = function() {
      var list1Counter = 1,
          list2Counter = -1;
      
      // swap the two lists
      $carousel.find('ul:first').insertAfter($carousel.find('ul:last'));

      // reset both lists' margins
      $carousel.find('ul').css({marginTop: 0});
      
      var $firstList = $carousel.find('ul:first');
      var $lastList = $firstList.next();
      
      // first list: fade out second item and fade in last item
      $firstList
        .find('li:eq(1)').fadeTo(steps, 0.1)
      .end()
        .find('li:last').fadeTo(steps, 1.0);

      // last list: set all items to nearly transparent and fade in the first
      $lastList.find('li').css({opacity: 0.1}).eq(0).fadeTo(steps*2, 1.0);

      // clear, then set the interval
      clearInterval(fadeRepeat);
      fadeRepeat = setInterval(function() {
        list1Counter++;
        list2Counter++;
        fadeItems(list1Counter, list2Counter);
      }, steps);

      
      // move list up, rinse, repeat
      $firstList.stop().animate({marginTop: -tickerInfo.height}, tickerInfo.speed($firstList), 'linear', runTicker);
    };
    
    runTicker();
    
    function fadeItems(count1, count2) {
      $carousel.find('ul:first').find('li:eq(' + (count1) + ')').fadeTo(steps, 0.1);
      
      $carousel.find('ul:last')
        .find('li:eq(' + (count2) + ')').fadeTo(steps*2, 1.0);
      if (count1-numItems >= 0) {
        $carousel.find('ul:last').find('li').eq(count1-numItems).fadeTo(steps, 0.1);
      }
    };
    
  });
 
});

/** =shadowbox
************************************************************/
$(document).ready(function() {
	// Video Shadowbox
	if (typeof Shadowbox !== 'undefined') {
	  $('a[href$=.mov], a[href$=.flv]').attr({
	    rel: 'shadowbox;width=528;height=392', 
	    title: 'click to close the video'
	  });

	  var sboptions = {
	    assetURL:  '/assets/scripts/shadowbox/',
	    loadingImage: 'styles/images/loading.gif',
	    overlayBgImage: 'styles/images/overlay-85.png',      
	    flvPlayer: 'flvplayer.swf', 
	    keysClose:          ['c', 27]
	  };

	  // ie6 only options
	  if ($.browser.msie === true && parseInt($.browser.version,10) < 7) {
	    sboptions.onFinish = function() {
	      $('#shadowbox_nav_close')
	      .find('a').ifixpng().end()
	      .css('left', '-20px');
	    };
	  }

	  Shadowbox.init(sboptions);    
	}
});