/* --------------------------------------------------------------------
// Author: Patrick Forringer ( patrick@forringer.com )
// File info: JS for H&M site
// Ver: 0.1
// Last modified: 11-16-09
//
*/
$().ready(function(){

	// initialize the jquery code
	// changer links when clicked
	$("a.changer").click(function(){
		
		//set the div with class mainText as a var called $mainText 
		var $mainText = $('div.page');
		
		// set the current font size of .mainText as a var called currentSize
		var currentSize = $mainText.css('font-size');
		
		// parse the number value out of the font size value, set as a var called 'num'
		var num = parseFloat( currentSize, 10 );	
		
		// make sure current size is 2 digit number, save as var called 'unit'
		var unit = currentSize.slice(-2);
		
		//console.log(unit);
		
		// javascript lets us choose which link was clicked, by ID
		if (this.id == 'linkLarge'){
			num = num * 1.2;
		} else if (this.id == 'linkSmall'){
			num = num / 1.2;
		}
		
		num = Math.floor( num );
		
		num = ( num < 10 ) ? 10 : num ;
		num = ( num > 25 ) ? 25 : num ;
		
		// jQuery lets us set the font Size value of the mainText div
		$mainText.css('font-size', num + unit);
		
		return false;
		   
	});
		
/*
	// hide switchLinks on page load
	$('#switchLinks').hide();
	
	// show the switchLinks div if showme is clicked
	$('#showMe').click(function(){
		$(this).hide();
		$('#switchLinks').show();
		return false;
	});
	
	// hide switchlinks if it is clicked
	$('#switchLinks').click(function(){
		$(this).hide();
		$('#showMe').show();
		return false;
	});
*/


	// --------------------------------------------------------
	// Hover stuff for subpages in top navigation
	//
	// remove title attributes
	$('#nav li a').attr('title', ''); 
	
	$('#nav li:has(ul)').each(function(){
	
		$navul = $(this).find('ul');
		
		// get distance
		var dist_to = $navul.height();
		
		var rate = 40; // dist per 100 ms
		
		// calculate speed for hover		
		var ddspeed = ( dist_to/rate ) * 100 ;
		ddspeed = ( ddspeed > 400 ) ? 400 : ddspeed;
		
		$.data( this , 'nav_info', { dist: dist_to , speed: ddspeed, ptop: $navul.css('padding-top'), pbot: $navul.css('padding-bottom') } );
		//var dist = 
		$navul.height(0);
			
	}).hover(function(){
	
		$navul = $(this).find('ul');
		
		$(this).toggleClass('hov');
			
		nv_inf = $.data( this , 'nav_info');
		
		// Animate!
		$navul.stop( true, true ).animate({ height: nv_inf.dist, display: 'block', paddingTop: nv_inf.ptop, paddingBottom: nv_inf.pbot }, nv_inf.speed, 'swing' );
		
	},function(){
		
		nv_inf = $.data( this , 'nav_info');
		
		// Animate!
		$navul.stop( true ).animate({ height: 0, paddingTop: 0, paddingBottom: 0 }, nv_inf.speed, 'swing', function(){
			$(this).hide();
			$(this).parent('li').toggleClass('hov');
		});
		
	});

	
	// --------------------------------------------------------
	//  Search bar text
	//	
	var autofillop = {
		value: 'search our site',
		toValue: '',
		defaultTextColor: '#777',
		activeTextColor: '#222'
	}
	
	// autofill add url form
	$('#s').autofill(autofillop);
	
		
	// --------------------------------------------------------
	// create fav click
	//
	
	//$('.bk').fav({name:'H&M Pipe Beveling Machine Company. Inc.'});
	
	// adds title value to submitted form on Product page.
	$('#cf_field_4').attr( 'value', $('.post_head h2').text() );
	
	// --------------------------------------------------------
	// 
	//
	
/*
	var $step_imgs = $('.meth_page .step_cont a[href$=.jpg]');
		
	$step_imgs.lightBox({
		imageLoading:			'http://www.bovairdsupply.com/images/lightbox-ico-loading.gif',		// (string) Path and the name of the loading icon
		imageBtnPrev:			'http://www.bovairdsupply.com/images/lightbox-btn-prev.gif',			// (string) Path and the name of the prev button image
		imageBtnNext:			'http://www.bovairdsupply.com/images/lightbox-btn-next.gif',			// (string) Path and the name of the next button image
		imageBtnClose:		'http://www.bovairdsupply.com/images/lightbox-btn-close.gif',		// (string) Path and the name of the close btn
		imageBlank:				'http://www.bovairdsupply.com/images/lightbox-blank.gif',	
		overlayBgColor:'#FFF',
		overlayOpacity: 0.6,
		txtImage: 'step',
		txtOf: 'of'
	});
*/
	
	if( $('.page-template-page_methodology-php').length ){
	
		$('div.ngg-galleryoverview').hide();
	
		// pull gallerries applied to post into the media div.
		$('.step').each(function(){
			
			$target = $(this).find('div.media');
			
			$gallery = $(this).find('.ngg-galleryoverview');
			
			if( $gallery.text() ){
				$gallery.appendTo( $target );
				
				$first_img = $gallery.find('img').eq(0);
				
				$thumb = $target.append('<div class="thumb"></div>').find('.thumb');
				
				$first_img.clone().appendTo( $thumb );
			}else{
				$target.addClass('empty');
				$target.find('span').prepend('No ');
				//$target.hide();
			}
			
		});
		
		$('.media span, .media .thumb').click( function(){
			//rel = $(this).attr('rel');
			//$('#'+rel).find('div.ngg-galleryoverview').slideToggle(500);
			$(this).parent().find('div.ngg-galleryoverview').slideToggle(500);
			return false;
		});
		
	}
	
	if( $('.page-id-21').length ){
		
		$('#carosel').roundabout({ childSelector:'div', tilt: -1 });
		
	}
	
});