/*
 *
 * Kalypso Custom Functions
 *
 * Developed by Steve Hurst, 2008-2009
 * http://novahurst.com
 *
 * Yay for EE .php template extension
 * <script>
 *
 */

$(document).ready(function() {

	// Google Analytics

  $.geekGaTrackPage('UA-4815450-1');

	$('.home #content a').click(function() {
		var intLink = $(this).attr('href').replace('http://kalypso.com','');
		$.geekGaTrackEvent('Home Content', 'Click', intLink);
	});

	// Superfish Navigation
	
	$('#header ul.nav_main').superfish({
		delay:       200,			// one second delay on mouseout 
		animation:   {opacity:'show'},	// fade-in and slide-down animation
		speed:       'normal',		// faster animation speed 
		autoArrows:  true,			// disable generation of arrow mark-up 
		dropShadows: false			// disable drop shadows 
	}); 

	
	// Home Page Featured Box
	
	$('#home_features').css('display', 'block');
	$('#home_features').tabs({
		fx: { opacity: 'toggle' }
	}).tabs('rotate', 4000, true);

	
	// Home Page Drop Menu

  $('.popup_container').each(function () {
    // options
    var xPos = -5;
    var yPos = 45;
    var distance = 10;
    var time = 250;
    var hideDelay = 750;

    var hideDelayTimer = null;

    // tracker
    var beingShown = false;
    var shown = false;
    
    var trigger = $('.popup_activate', this).css('background-position','0 0');
    var popup = $('.popup_menu', this).css('opacity', 0);

    // set the mouseover and mouseout on both element
    $([trigger.get(0), popup.get(0)]).mouseover(function () {
      // stops the hide event if we move from the trigger to the popup element
      if (hideDelayTimer) clearTimeout(hideDelayTimer);

      // don't trigger the animation again if we're being shown, or already visible
      if (beingShown || shown) {return;} else {
        beingShown = true;
        trigger.css('background-position','0 -26px');
        
        // reset position of popup box
        popup.css({
          top: yPos,
          right: xPos,
          display: 'block' // brings the popup back in to view
        })

        // (we're using chaining on the popup) now animate it's opacity and position
        .animate({
          top: '+=' + distance + 'px',
          opacity: 1
        }, time, 'swing', function() {
          // once the animation is complete, set the tracker variables
          beingShown = false;
          shown = true;
        });
      }
    }).mouseout(function () {
      // reset the timer if we get fired again - avoids double animations
      if (hideDelayTimer) clearTimeout(hideDelayTimer);
      
      // store the timer so that it can be cleared in the mouseover if required
      hideDelayTimer = setTimeout(function () {
        hideDelayTimer = null;
        trigger.css('background-position','0 0');
        popup.animate({
          top: '+=' + distance + 'px',
          opacity: 0
        }, time, 'swing', function () {
          // once the animate is complete, set the tracker variables
          shown = false;
          // hide the popup entirely after the effect (opacity alone doesn't do the job)
          popup.css('display', 'none');
        });
      }, hideDelay);
    });
  });
  

	// Categories Tabs
	
	$('#category_box').css('display', 'block');
	$('#category_box').tabs({
		fx: { opacity: 'toggle' }
	}).tabs('rotate', 4000, true);
	
	
	// Home Page Equal Height Content Boxes
	
	$('#home_body .row').equalHeights();

	// Validate Contact Form
	$('#freeform').validate({
	rules: {
		name: "required",
		company: "required",
		phone: "required",
		email: {
			required: true,
			email: true
		},
		captcha: "required"
		}
	});
	
	// Tweetify Links
	
	$.fn.tweetify = function() {
		this.each(function() {
			$(this).html(
				$(this).html()
					.replace(/((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi,'<a href="$1">$1</a>')
					.replace(/(^|\s)#(\w+)/g,'$1<a href="http://search.twitter.com/search?q=%23$2">#$2</a>')
					.replace(/(^|\s)@(\w+)/g,'$1<a href="http://twitter.com/$2">@$2</a>')
			);
		});
		return $(this);
	}
	//$('#social .saluting_innovation .message').tweetify();
	
	// Saluting Innovation Cycle
	
	$('#social .saluting_innovation .list').cycle({ 
		delay:  2500, 
		speed:  2000
	});
	
	// R&D Microsite
	$('#rd_tour_intro').fadeIn('normal');
	$('#rd_tour_intro button').click(function(){$('#rd_tour_intro').fadeOut('normal');});

//	$('body.rd ul.nav_main li a').click(function(){
//		$.cookie('survey_intro', null);
//	});
//	$(function () {
//		if($.cookie('survey_intro') != 'clicked'){
//			$('#rd_survey_intro').fadeIn('normal');
//		}
//	});
//	$('#rd_survey_intro button').click(function(){
//		$('#rd_survey_intro').fadeOut('normal');
//		$.cookie('survey_intro', 'clicked', { expires: 10 });
//	});

	
	$('a.rd_component_link').hover(function() {
		if($.browser.msie){
			$('span.icon', this).show();
		}
		else{
			$('span.icon', this).fadeIn('normal');
		}
	}, function() {
		if($.browser.msie){
			$('span.icon', this).hide();
		}
		else{
			$('span.icon', this).fadeOut('normal');
		}
	});

	$('a.rd_component_link').click(function(){
		$('.rd_component_details_wrapper').fadeOut('normal');
		var container = $(this).attr('href');
		$(container).fadeIn('normal');
		return false;
	});

	$('.rd_component_details_wrapper, .rd_component_details_wrapper button').click(function(){
		$('.rd_component_details_wrapper').fadeOut('normal');
		$('#rd_tour_intro').fadeOut('normal');
	});

});//End document.ready