
//requires jQuery


//wait for the DOM to be loaded
jQuery( document ).ready( function() {
	
	//ORDERED LIST
	//edit ordered list dom to allow styling
	jQuery( 'ol li' ).wrapInner( '<span class="orderedListItemContent"></span>' );
	//activate ordered list styling via adding class - edit styles in stylesheet
	jQuery( 'ol' ).addClass( 'javaScriptStyled' );
		
	//Style hero images.
	HeroImg.init();
	
	//Style kitchen gallery images.
	KitGalleryImg.init();
	
	//Structure definition lists.
	SetColumnsList.init();
	
	//Setup home page.
	HomePage.init();
	
	//GALLERIES.
	if( $().fancybox )
	{
		$( 'a[rel=imageGallery]' ).fancybox({
			'ajax' 				: { cache : false },
			'autoDimensions'	: true,
			'centerOnScroll'	: true,
			'padding'			: 10,
			'overlayColor'		: '#000000',
			'overlayOpacity'	: 0.5,
			'transitionIn'		: 'elastic',
			'transitionOut'		: 'elastic',
			'titlePosition' 	: 'outside',
			'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
				return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
			}
		});
	};
	
	if( $().cycle )
	{
		$( 'ul.gallery' ).cycle({
				fx: 'fade'
		});
	};
	
	//Video.
	Video.init();
	
} );


HomePage = 
{
	init: function()
	{
		if( !$('body').hasClass('home') ){ return; };
		
		//Place necessary DOM elements
		//$( 'ul#kitchenHero' ).insert( '<div id="slideshowNav"><h4>See More</h4><a id="slideshowPrev" href="javascript:;">Prev</a><div id="pager"></div><a id="slideshowNext" href="javascript:;">Next</a></div>' );
		
		$( 'div#kitchenHero' ).append( '<div id="slideshowNav"></div>' );
		$( 'div#kitchenHero div#slideshowNav' ).append( '<a id="slideshowPrev" href="javascript:;">Prev</a>' );
		$( 'div#kitchenHero div#slideshowNav' ).append( '<div id="pager"></div>' );
		$( 'div#kitchenHero div#slideshowNav' ).append( '<a id="slideshowNext" href="javascript:;">Next</a>' );
		
		$( 'div#kitchenHero ul' ).cycle({
			fx: 'fade',
			speed: 500, 
	    	timeout: 6000,
			pause: true,
			startingSlide: 0,
			cleartypeNoBg: true,
			prev: 'div#slideshowNav a#slideshowPrev', 
	    	next: 'div#slideshowNav a#slideshowNext',
			pager: 'div#slideshowNav div#pager',
			before: HomePage.onBefore,
			after: HomePage.onAfter
		});
	}
	,
	onBefore : function(curr, next, opts)
	{
		//if( $('body').hasClass( 'home' ) )
		//{
			//alert("Before");
			//$(curr).children('div.info').hide();
			//$(curr).children('div.info').css( 'height', 0 );
			$(curr).children('div.info').css( 'left', -320 );
			//$(next).children('div.info').hide();
			//$(next).children('div.info').css( 'height', 0 );
			$(next).children('div.info').css( 'left', -320 );
			//console.log($(curr).children('div.info'))
		//};//end if
	}
	,
	onAfter : function(curr, next, opts)
	{
		//if( $('body').hasClass( 'home' ) )
		//{
			//$(next).children('div.info').show('fast');
			//$(next).children('div.info').animate( { opacity: 'show', height: 308 }, 1000 );
			//$(next).children('div.info').animate( { height: 308 }, 1000 );
			//$(next).children('div.info').animate( { width: 316 }, 1000 );
			$(next).children('div.info').animate( { left: 0 }, 500 );
			//$(next).children('div.info').show();
			//alert("After");
			//console.log(opts)
		//};//end if
	}
}


//Restructure Definittion Lists. Illegal validation but had to get it happening quickly.
SetColumnsList =
{
	init : function()
	{
		$( 'dl.info' ).each( function( pmIndex ){
			//Wrap dt/dd pairs with divs.						
			var myLength = $(this).children('dt').size();
			for( var i=0; i < myLength; i++ )
			{
				$(this).children( "dt:eq(0),dd:eq(0)" ).wrapAll( '<div></div>' );
			};//end for
			
			//Add clearing div to ensure height of each div.
			$(this).children( 'div' ).append( '<div class="clearAll"></div>' );
			
			//Set class to initiate styling.
			$(this).addClass( 'columnsList' );
			$(this).children( 'div:odd' ).addClass('odd');
			$(this).children( 'div:even' ).addClass('even');
		} );
	}

};//end SetColumnsList


//Style hero images.
HeroImg =
{
	init : function()
	{
		if( $('body').hasClass('home') ){ return; };
		
		jQuery( 'img.hero' ).each( function(){
			jQuery( this ).wrap( '<div class="heroImg"></div>' );
			var myString = jQuery( this ).attr( 'alt' );
			jQuery( this ).after( '<p>' + myString +'</p>' );
		});
		
		jQuery( 'img.heroRow' ).each( function(){
			jQuery( this ).wrap( '<div class="heroImgRow"></div>' );
			var myString = jQuery( this ).attr( 'alt' );
			jQuery( this ).after( '<p>' + myString +'</p>' );
		});
	}
};//end object literal HeroImg


//Style kitchen gallery images.
KitGalleryImg =
{
	init : function()
	{
		jQuery( 'ul#imageGallery li' ).each( function( pmIndex ){
			jQuery( this ).click( function(){ KitGalleryImg.setGalleryByIndex( pmIndex ); } );
		} );
		
		KitGalleryImg.setGalleryByIndex( 0 );
	}
	,
	setGalleryByIndex : function( pmIndex )
	{
		jQuery( 'ul#imageGallery li' ).each( function( pmListIndex ){
			if( pmIndex == pmListIndex ) //Major image
			{
				jQuery( this ).addClass( 'major' );
				jQuery( this ).removeClass( 'minor' );
				var myCurrentImgPath = jQuery( this ).children( 'img' ).attr( 'src' );
				var myNewImgPath = myCurrentImgPath.replace( 'Minor', 'Major' );
				//alert( myCurrentImgPath + " , " + myNewImgPath );
				jQuery( this ).children( 'img' ).attr( 'src', myNewImgPath );
			}
			else //Minor image
			{
				jQuery( this ).removeClass( 'major' );
				jQuery( this ).addClass( 'minor' );
				var myCurrentImgPath = jQuery( this ).children( 'img' ).attr( 'src' );
				var myNewImgPath = myCurrentImgPath.replace( 'Major', 'Minor' );
				//alert( myCurrentImgPath + " , " + myNewImgPath );
				jQuery( this ).children( 'img' ).attr( 'src', myNewImgPath );
			};//end if
			//jQuery( this ).click( function(){ alert( pmListIndex ); } );
		} );
	}
};//end object literal KitGalleryImg


Video = 
{
	init : function()
	{
		//Look for videos to insert.
		Video.play_video({
			wrapper_id: 'blum_servo_drive_video',
			video_path: '/video/blum-servo-drive.flv',
			width: '425',
			height: '300'
		});
		
		Video.play_video({
			wrapper_id: 'aplan_difference_video',
			video_path: '/video/aplan-difference.mp4',
			width: '600',
			height: '337'
		});
	}
	,
	play_video : function(pm_options)
	{
		//Check Embedding function is available.
		//if(!jwplayer){ return; };
		//if(!swfobject){ return; };
		
		//Get options.
		var my_wrapper_id = pm_options.wrapper_id;
		var my_video_path = pm_options.video_path;
		var my_width = pm_options.width;
		var my_height = pm_options.height;
		//console.log(my_wrapper_id);
		
		//Check video holder can be found.
		if($('#' + my_wrapper_id).size() == 0){ return; };
		
		//Set video.
		/*
		jwplayer('case_study_video').setup({
			flashplayer: "../flash/video-player.swf",
			file: my_video_path,
			image: '',
			width: 720,
			height: 400,
			autostart: true,
			volume: 100,
			skin: '../flash/nacht.zip'
		});
		*/
		
		swfobject.embedSWF(
			"/flash/videoPlayer.swf",
			my_wrapper_id,
			my_width,
			my_height,
			"9.0.28",
			"/flash/expressInstall.swf",
			{ file: my_video_path, image: '', autostart: true, volume: 100, repeat: 'none',controlbar: 'over', skin: '/flash/bekle.swf' },
			{ allowfullscreen: true, wmode: 'transparent' }
		);
	}
};//end Video
