// JavaScript Document
$j = jQuery.noConflict();
$j(document).ready(function($){
	var special_font = [
		'.page_title'
		//,'.main_nav a'
		,'.ui-tabs-nav li a'
		,'.tout h1'
		,'.testimony .testimony_content'
		,'.case_study_title span'
		,'#footer_content h1'
		,'#footer_info .large'
		,'#footer_info .small'
		,'.content h1'
		,'.tout #faq dd > span'
		,'.tout #faq dt > span'
		,'#footer #footer_contact a'
		,'.tout_more'
		,'.rotate_content_next'
	];
	for(i in special_font){
		$(special_font[i]).addClass('custom_font').addClass('typeface-js');
	}
	$('#tabs').tabs({ fx: { opacity: 'toggle' },
		show: function(event, ui) {
			 //_typeface_js.initialize();	
		}
	});	
	$('#employees').tabs({ fx: { height: 'toggle', duration: 'slow'} });	
	$('.cycle').cycle({ 
		fx:     'scrollHorz', 
		prev:   '#home_prev', 
		next:   '#home_next', 
		//after:   onAfter, 
		timeout: 0 
	});
	$('.rotate-content').rotate_content();	
	$('.main_nav > li').children('ul').hide();
	$('.main_nav > li').hover(function(){
		$(this).children('ul').css({top: $(this).height(),minWidth:$(this).width()}).slideDown('fast');
		$(this).addClass('hover');
	});
	$('.main_nav > li').mouseleave(function(){
		$(this).removeClass('hover');
		$(this).children('ul').slideUp('fast');
	});
	$('.employee_photo').prepend('<div class="employee_photo_dec" />');
	if($('#home_feature_wrapper').length > 0){
		$('#home_prev').css('left',$('.home_feature_content:visible').offset().left - $('#home_prev').width());
		$('#home_next').css('left',$('.home_feature_content:visible').offset().left + $('.home_feature_content:visible').width());
		
		$(window).resize(function() {
		  $('#home_feature, #home_feature_wrapper ,.home_feature_container, .home_feature').width($(window).width());
		  $('#home_prev').css('left',$('.home_feature_content:visible').offset().left - $('#home_prev').width());
			$('#home_next').css('left',$('.home_feature_content:visible').offset().left + $('.home_feature_content:visible').width());
		});
	}
	
	$('.faq').faq();
	$('.employees_small').faq({question:'.employees_small_link',answer:'.employees_small_content'});
});

function onAfter(curr, next, opts) {
	var index = opts.currSlide;
	if(index == 0){
		$j('#home_prev').addClass('off');
		$j('#home_next').removeClass('off');
	}else if(index == opts.slideCount - 1){
		$j('#home_prev').removeClass('off');
		$j('#home_next').addClass('off');
	}else{
		$j('#home_prev').removeClass('off');
		$j('#home_next').removeClass('off');
	}
} 

(function($){
 $.fn.rotate_content = function(options) {
  var defaults = {
   handle_title: "Next"
  };
  
  var options = $.extend(defaults, options);
  var wrapper_class = "rotate_content_wrapper";
  
  return this.each(function() {
   obj = $(this);
   obj.css({
	   overflow:'hidden',
		height:obj.height()
	});
	
   var kids = obj.children('div');
   var kid_width = kids.outerWidth();
   var kid_index = 0;
   //var wrapper = $("<div class='" + wrapper_class + "' />");
 	var wrapper = document.createElement('div');
	wrapper = $(wrapper);
	wrapper.addClass(wrapper_class);
	
   //var next = $("<a class='rotate_content_next'>" + options.handle_title + "</a>");
   var next = $(document.createElement('a'));
   next.addClass('rotate_content_next').html(options.handle_title);
   next.addClass('typeface-js');
   next.click(function(){
	   
	   var wrapper = $(this).parent().find('.' + wrapper_class);
	   wrapper.stop(true,true);
	   var move_left = wrapper.position().left - kid_width;
	   if(kid_index == kids.length - 1){
		   move_left = 0;
	   }
		wrapper.animate({left:move_left},1000,function(){
			//$(this).filter(':first-child').appendTo(wrapper);
		});
		
		if(kid_index == kids.length - 1){
		   kid_index = 0;
	   }else{
		   kid_index += 1;
	   }
	   
	});
   kids.each(function(){
		$(this).css('width',$(this).outerWidth());
	});   
   
   wrapper.css({
		position:'absolute',
		top:0,
		left:0,
		width:kids.outerWidth() * kids.length
	});
	obj.parent().css({position:'relative'}).append(next);
   		kids.wrapAll(wrapper);
  });
 };
})(jQuery);


(function( $ ){

  var methods = {
    init : function( options ) { 
		var settings = {
		  question : 'h2',
		  answer : 'p'
		};
	
		return this.each(function() {        		  
		  if ( options ) { 
			$.extend( settings, options );
		  }
		   	var obj = $(this);
		   	obj.find(settings.answer).css({marginBottom:'20px'}).hide();
		  	obj.find(settings.question).css({cursor:'pointer'}).click(function(){
				$(this).parent().find(settings.answer).slideUp();
				if(!$(this).next(settings.answer).is(':visible')){				
					$(this).next(settings.answer).slideDown();
				}
				return false;
			});
		  
		});
	}
  };

  $.fn.faq = function( method ) {
    
    // Method calling logic
    if ( methods[method] ) {
      return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
    } else if ( typeof method === 'object' || ! method ) {
      return methods.init.apply( this, arguments );
    } else {
      $.error( 'Method ' +  method + ' does not exist on jQuery.tooltip' );
    }    
  
  };

})( jQuery );
