/* 
plugin for setting up jcarousels with callbacks
this is used throughout the site, so dont mess with it without testing extensively
*/
$.fn.jCarouselInit = function(opts){
	var options = jQuery.extend( {
		next: '.next',
		prev: '.prev',
		scroll: 1,
		buttonNextHTML: null,
		buttonPrevHTML: null,
		wrap:'last'
	},opts);

	var jCarouselInitCallback =  function(c){
		jQuery(options.next).bind('click', function() {
			c.next();
			return false;
		})

		jQuery(options.prev).bind('click', function() {
			c.prev();
			return false;
		});
	}
	options.initCallback = jCarouselInitCallback;

	$(this).jcarousel(options)

};

/* plugin for autoclearing inputs when the user clicks on them */
$.fn.inputDefaultState = function(options){
	var options = jQuery.extend( {
		text: 'Search'
	},options);

	$(this)
		.val(options.text)
		.focus(function(e){
			if($(this).val() == options.text)
				$(this).val('');
		}).
		blur(function(e){
			if (this.value === '') {
                this.value = options.text;
            }
		});
};

/* makes image switchers using cycle for rotator and cufon for text */
$.fn.switcher = function(opts) {
	$('#hero a span,#category-hero a span').css({
		display:'none'
	});
	
	var cycleOpts = opts.cycle ? opts.cycle : null;
	var cufonOpts = opts.cufon ? opts.cufon : null;
	
	var cycleOptions = jQuery.extend( {
		fx:     'fade', 
		timeout: 5000,
		speed: 300,
		pager: '#hero-nav',
		pagerAnchorBuilder: function(idx, slide) {
			var id = idx + 1;
			var c = idx==2 ? 'last' : '';
			return '<li class="' + c + '"><a href="#" rel="' + id + '">' + $('span', slide).remove().text() + '</a></li>'; 
		},
		after: function(){
			$('.hero-controls li a').css('color','#1E3C70');
			$('.hero-controls li.activeSlide a').css('color','#45A2D1');
			Cufon.refresh('#hero-nav li a');
		}
	},cycleOpts);
	
	var cufonOptions = jQuery.extend({
		css: {
			fontFamily: 'conduit', 
			hover: {color:'#45A2D1'}
		},
		navId: 'hero-nav',
		navClass: 'hero-controls clearfix'
	}, cufonOpts);

	$(this).after('<ul id="' + cufonOptions.navId + '" class="' + cufonOptions.navClass + '">');
	$(this).cycle(cycleOptions);
	Cufon.replace('#' + cufonOptions.navId + ' li a', cufonOptions.css);
	
	/* style this thing */
	$('.hero-controls li').css({
		marginTop:'0',
		float:'left',
		width:'33%',
		padding:'0',
		textAlign:'center'
	});
	
	$('.hero-controls li a').css({
		font:'bold 18px Helvetica, Arial, sans',
		textTransform:'uppercase',
		display:'block',
		width:'100%',
		lineHeight:'20px',
		padding:'12px 0 8px 0'
	})
	.hover(function(){
		$(this).css('text-decoration','none');
	});
	
	$('#category-promo').css('margin-bottom', '50px');
	
	$('#hero-nav').css({
		marginTop:'2px',
		background:'#E8EBF0',
		clear:'both'
	});
	$('.hero-controls li.activeSlide a').css('color','#45A2D1');
	$('#hero').css({marginTop:'27px'});
	
};


$(document).ready(function(){
	
	/* heros */
	
	if($('#hero').length > 0) {
		$('#hero').switcher({
			cycle:{speed: 2000, timeout:9000}
		});
		Cufon.now();
	}
	/* end heros */
	
	/* what is collective toggle*/	
	$('#what_is_collective').toggle(
	  function () {
	    $(this).removeClass("closed");
		$('em', this).slideDown('slow');
	  },
	  function () {
	    $(this).addClass("closed");
		$('em', this).slideUp('slow');
	  }
	)
		.find('em').hide()
		.end()
		.find('a')
			.click(function(e){
				e.stopPropagation() 
			});
	
	if($(this).jcarousel){
		/* home featured designs */
		$('#featured-designs .jcarousel-skin-keds').jCarouselInit({
			scroll:5, 
			next: '#featured-designs .next', 
			prev: '#featured-designs .prev'
		});
	
		/* home featured members */
		$('#featured-members .jcarousel-skin-members').jCarouselInit({
			scroll:1, 
			next: '#featured-members .next', 
			prev: '#featured-members .prev'
		});


		/* designs/members section carousel */
		$('.top-picks.jcarousel-skin-featured').jCarouselInit({
			scroll:4, 
			next: '.top-picks .next', 
			prev: '.top-picks .prev'
		});

		/* social carousel */
		$('#collective-buzz .social').jCarouselInit({
			scroll:5,
			vertical: true, 
			next: '#collective-buzz .next', 
			prev: '#collective-buzz .prev'
		});

	}
	
	$('#search-field').inputDefaultState({text: 'Search Keds Collective'});
	$('#SubscriptionEmailAddress').inputDefaultState({text: 'Enter Your Email Address'});
	//over states for designs
	$('#featured-designs li.jcarousel-item, ul.fresh li')
	.mouseover(function(){
		$(this).addClass('over');
	})
	.mouseout(function(){
		$(this).removeClass('over');
	});
	
});