/*
Bones Scripts File
Author: Eddie Machado

This file should contain any js scripts you want to add to the site.
Instead of calling it in the header or throwing it inside wp-head()
this file will be called automatically in the footer so as not to 
slow the page load.

*/

/* imgsizer (flexible images for fluid sites) */
var imgSizer={Config:{imgCache:[],spacer:"/path/to/your/spacer.gif"},collate:function(aScope){var isOldIE=(document.all&&!window.opera&&!window.XDomainRequest)?1:0;if(isOldIE&&document.getElementsByTagName){var c=imgSizer;var imgCache=c.Config.imgCache;var images=(aScope&&aScope.length)?aScope:document.getElementsByTagName("img");for(var i=0;i<images.length;i++){images[i].origWidth=images[i].offsetWidth;images[i].origHeight=images[i].offsetHeight;imgCache.push(images[i]);c.ieAlpha(images[i]);images[i].style.width="100%";}
if(imgCache.length){c.resize(function(){for(var i=0;i<imgCache.length;i++){var ratio=(imgCache[i].offsetWidth/imgCache[i].origWidth);imgCache[i].style.height=(imgCache[i].origHeight*ratio)+"px";}});}}},ieAlpha:function(img){var c=imgSizer;if(img.oldSrc){img.src=img.oldSrc;}
var src=img.src;img.style.width=img.offsetWidth+"px";img.style.height=img.offsetHeight+"px";img.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+src+"', sizingMethod='scale')"
img.oldSrc=src;img.src=c.Config.spacer;},resize:function(func){var oldonresize=window.onresize;if(typeof window.onresize!='function'){window.onresize=func;}else{window.onresize=function(){if(oldonresize){oldonresize();}
func();}}}}

// as the page loads, cal these scripts
$(document).ready(function() {
	var moreHeight = 0;

	$('.tabs').fwTabs();
	
	$('.social').css('marginTop', ( ( $('#logo').height() + 40 ) / 2 ) - 7 + 'px' );

	$('#searchform #s').blur();
	
 	if(!Modernizr.input.placeholder){
		$('[placeholder]').focus(function() {
		  var input = $(this);
		  if (input.val() == input.attr('placeholder')) {
			input.val('');
			input.removeClass('placeholder');
		  }
		}).blur(function() {
		  var input = $(this);
		  if (input.val() == '' || input.val() == input.attr('placeholder')) {
			input.addClass('placeholder');
			input.val(input.attr('placeholder'));
		  }
		}).blur();
		
		$('[placeholder]').parents('form').submit(function() {
		  $(this).find('[placeholder]').each(function() {
			var input = $(this);
			if (input.val() == input.attr('placeholder')) {
		  	input.val('');
			}
	  		})
		});
	}

	$('.menu ul li').mouseenter(function(){
		jQuery('ul:first',this).css('visibility', 'visible').css('opacity', '0').stop(true, false).animate({ 'opacity': 1 }, 300);
	}).mouseleave( function(){
		jQuery('ul:first',this).css('visibility', 'visible').css('opacity', '1').stop(true, false).animate({ 'opacity': 0 }, 300);
	});
	
	$('.menu > ul > li').each( function(){
		prt = $(this);
		newText = $( 'a', prt ).html() + ' <small>&darr;</small>'
		//alert(newText);
		if ( $(this).has('.sub-menu').length > 0 ) $( 'a:first', prt ).html( newText );
	});


}); /* end of as page load scripts */

$.fn.fwTabs = function(options){
	
	var settings = $.extend({}, $.fn.fwTabs.defaults, options);
	var active = 0;
	
	var tabs = $(this);
		
	$('.nav-tab li', tabs ).click( function(){
		idx = $(this).index();
		if ( idx == $('.nav-tab li.tab-active', tabs ).index() ) return false;
		startTab(tabs, idx);
		return false;
	});
				
	function startTab(tabs, idx){
		$('.active', tabs).fadeOut().removeClass('active').addClass('hide');
		$('.tab-content', tabs).eq(idx).removeClass('hide').addClass('active').fadeIn();
		$('li.tab-active', tabs).removeClass('tab-active');
		$('.nav-tab li', tabs).eq(idx).addClass('tab-active');
	}

}

$.fn.fwSlider = function(options) {

	defaults = {
		autoPlay: true,
		delay: 5000
	};
	$.extend( defaults, options );
	var settings = defaults; 
	var active = 0;
	var width = $(this).width();
	var slider = $(this);
	var count = $('.item',slider).length;
	//$('.item', slider ).hide();
	
	startSlide(slider,0);
	var timer = 0;
	if ( settings.autoPlay ){
		timer = setInterval( function() { active++; startSlide(slider,active); }, settings.delay );
	}
	
	$('.item', slider).mouseenter( function() {
		clearInterval(timer);
		timer = '';
	})

	$('.item', slider).mouseleave( function() {
		if ( settings.autoPlay ){
			timer = setInterval( function() { active++; startSlide(slider,active); }, settings.delay );
		}
	})

	$('.slider-nav .index', slider ).click( function(){
		var ths = $(this);
		clearInterval(timer);
		timer = '';
		idx = $('.slider-nav .index', slider ).index(ths) ;
		active = idx;
		startSlide(slider, idx);
		return false;
	});

	$('.slider-nav .slider-prev', slider ).click( function(){
		var ths = $(this);

		clearInterval(timer);
		timer = '';
		if ( ( active ) == 0 ){ idx = count - 1 ; }
		else { idx = active - 1; }
		startSlide(slider, idx);
		return false;
	});

	$('.slider-nav .slider-next', slider ).click( function(){
		var ths = $(this);

		clearInterval(timer);
		timer = '';
		if ( active == ( count - 1 ) ){ idx = 0; }
		else { idx = active + 1 ; }
		startSlide(slider, idx);
		return false;
	});

	$(window).resize( function(){
		width = slider.width();
	});

	function startSlide( slider, idx ){
		if ( idx == count ) idx = 0;
		$('.active-slide', slider ).removeClass('active-slide');
		marg = '-' + idx*width + 'px';
		hei = $('.item', slider).eq(idx).height() + 'px' ;
		$('.items', slider ).animate({ marginLeft : marg }, 500, 'swing',
									 function(){ 
										if (settings.autoPlay && timer == '' ) {
											timer = setInterval( function() { active++; startSlide(slider,active); }, settings.delay ); 
										}
									});
		
		$('.curr-number', slider).text(idx + 1);
		$('.item', slider ).eq(idx).addClass('active-slide');
		$('.slider-nav .active-dot', slider ).removeClass('active-dot');
		$('.slider-nav .index', slider ).eq(idx).addClass('active-dot');
		active = idx;
		if ( active == ( count - 1 ) ){
			//active = -1;
		}
	}

}
