(function($){ 
	$(function(){
		//Fade effect on industry awards
		$('.flip .award-holder').hover(
		function(){
			$(this).find('.grayscale').stop(true,true).fadeOut(200);
		}, function() {
			$(this).find('.grayscale').stop(true,true).fadeIn(200);
		});
		
		
		// Resize video embed to fit content width
		$('#embedded-video object').each(function(){
			var width = $(this).attr('width');
			// normal aspect ratio
			if(width == '480'){ 
				$(this).attr('height','240').find('embed').attr('height','240');
			// widescreen aspect ratio
			} else { 
				$(this).attr('height','260').find('embed').attr('height','260');
			}
			$(this).attr('width','284').find('embed').attr('width','284');
		});

		// Resize homepage video
			$('#video-embed object').each(function(){
				$(this).attr('height','370').find('embed').attr('height','370');
				$(this).attr('width','466').find('embed').attr('width','466');
			});

		// Resize video popup
		if ($('body.page-template-homepage-php').length == 0){
			$('#hidden-video object').each(function(){
				var width = $(this).attr('width');
				// normal aspect ratio
				if(width == '480'){ 
					$(this).attr('height','400').find('embed').attr('height','400');
				// widescreen aspect ratio
				} else { 
					$(this).attr('height','400').find('embed').attr('height','400');
				}
				$(this).attr('width','500').find('embed').attr('width','500');
			});
		}
		
		// Style forms a little
		$('#Pro_Version_Requested__c').parents('li:first').hide();
		$('#mktFrmSubmit').addClass('button blue');

		// CYCLE
		$('.slideshow').each(function(){
	    	$target = $(this);				    	
	    	if($target.find('.slide').length > 1) {
		     	$target.cycle({
		     		fx: 	'scrollVert',
		     		timeout: 0
		     	});
		    } else {
		    	//$target.find('.qarrow').hide();
		    }
	     });
		// Add special click handlers
		$('.previous').click(function(){
			$target = $(this).parent().find('.slideshow').cycle('prev');
		});
		$('.next').click(function(){
			$target = $(this).parent().find('.slideshow').cycle('next');
		});
		
	     
	   // jQuery Traversing for Marketo forms enhancements
	   if ($('#marketo').length > 0){
	   	$('#marketo .mktField label:contains("Terms of service:")').parent().attr('id','terms-of-service');
	   	$('#marketo .mktField label:contains("Terms of service:")').wrapInner('<a href="/index.php?p=366" />');
	   	$('#marketo .mktFormReq label').append('<span class="required">*</span>');
	   }
	   if ($('#form-primary').length > 0){
	   	$('#form-primary .mktField label:contains("Terms of service:")').parent().attr('id','terms-of-service');
	   	$('#form-primary .mktField label:contains("Terms of service:")').wrapInner('<a href="/index.php?p=366" />');
	   	$('#form-primary .mktFormReq label').append('<span class="required">*</span>');
	   }
	   
	   
		// Dropdown Menu
		$('#navigation ul li:not(.current_page_item, .current_page_ancestor, .current_page_parent) ul').slideUp(0).addClass('jquery');
		$('#navigation ul > li.page_item:not(.current_page_item, .current_page_ancestor, .current_page_parent)').hover(function () {
			$(this).addClass('on').find("ul").stop(true, true).slideDown(200);
		},function () {
			$(this).removeClass('on').find("ul").stop(true, true).delay(140).slideUp(100);
		});
		$('#navigation ul:first > li').addClass('first-children');
		
		// Cancel if there's only one subnav item
		$length = $('#navigation .current_page_parent ul li, #navigation .current_page_ancestor ul li, #navigation .current_page_item ul li').length;
		if( $length == 1 ) {
			$('#navigation .current_page_parent > ul, #navigation .current_page_ancestor > ul, #navigation .current_page_item > ul').remove();
			$('body').removeClass('page-parent, page-child');
			
			return false;
		}
		
		// Comments hint box
		$('#commentsform #rules-toggle').show(0);
		$('#commentsform .comment-rules').hide(0);
		$('#commentsform #rules-toggle a').toggle(
			function(){
				$('#commentsform #rules-toggle a').html("hide" );
				$('#commentsform .comment-rules').stop(true,true).slideDown(140);
				return false;
			},
			function(){
				$('#commentsform #rules-toggle a').html("show allowed tags" );
				$('#commentsform .comment-rules').stop(true,true).slideUp(130);
				return false;
			}
		);
	});
})(jQuery);
