/* --------------------------------------------------------------------
// Author: Patrick Forringer ( patrick@forringer.com )
// File info: JS for H&M site
// Ver: 0.1
// Last modified: 11-16-09
//
*/
$().ready(function(){
	
	// --------------------------------------------------------
	// Hover stuff for Subpages in top bar navigation
	//
	rate = 40; // dist per 100 ms	
	
	$('#nav li:has(ul)').hover(function(){
		
		// get distance
		var dist = $.data( this , 'dist');
		
		$navul = $(this).find('ul');
		
		if( !dist ){
			var dist = $.data( this , 'dist', $navul.height() );
			$navul.height(0);
		}
		
		// calculate speed for hover
		ddspeed = (dist/rate) * 100;
		
		// Animate!
		$navul.stop().animate({ height: dist }, ddspeed, 'swing' ).show();
	},function(){
		
		$navul.stop().animate({ height: 0 }, ddspeed, 'swing', function(){ $(this).hide() });
	});
	
	// --------------------------------------------------------
	//  Search bar text
	//	
	var autofillop = {
		value: 'search our site',
		toValue: '',
		defaultTextColor: '#777',
		activeTextColor: '#222'
	}
	
	// autofill add url form
	$('#s').autofill(autofillop);
	
	
	$('#s').bind('keydown',function(event){
		
		if(( event.control || event.meta ) && event.key == 's' ) {
		
			console.log(event);
			
			event.preventDefault();
			alert('saved');
			//$('').submit();
		}
		
	});
		
	// --------------------------------------------------------
	// create fav click 
	//
	
	$('.bk').fav({name:'H&M Pipe Beveling Machine Company. Inc.'});	
	
	
	// --------------------------------------------------------
	// Product drop down roll up.
	//
	
	$prdnbox = $('.product_nav');
	
	if($prdnbox){
	
		$prdnbox.append('<a class="toggle"><b></b><span>hide</span></a>');
		
		$toggle = $prdnbox.find('.toggle');
		
		var pnheight = $prdnbox.height();
		var pndist = $prdnbox.find('h3').height()-7;
		var pnspeed = 300;
		
		$toggle.toggle(
			function(){
				$prdnbox.stop().animate({ height: pndist }, pnspeed, 'swing' );
				$(this).toggleClass('down');
			},
			function(){
				$prdnbox.stop().animate({ height: pnheight }, pnspeed, 'swing' );
				$(this).toggleClass('down');
			}
		);
		
		// if on a video page for a product hide product nav.
		if( $('body.page-id-78').length || $('body.page-id-79').length ){
			$toggle.click();
			
		}
	}
	
});
