$(document).ready(function(){
		
	$('.body-container img[align]').each(function(){
		if ($(this).attr('align')) {
			var $float = $(this).attr('align');
			$(this).removeAttr('align');
			if ($float == 'left' || $float == 'right') {
				$(this).css({
					'float' : $float
				});
			}
		}
	});
	
	/* *
	 * HOW-TO VIDEO SECTION
	 * * * * * * */
	$('.video-table-head ul.navigation a.active').each(function(){
		$(this).parent().find('div.tl').css('background-position', '0 -32px');
		$(this).parent().find('div.tr').css('background-position', '-8px -32px');
	});

	$('.video-table-head ul.navigation li a').hover(function(){
		$(this).parent().find('div.tl').css('background-position', '0 -32px');
		$(this).parent().find('div.tr').css('background-position', '-8px -32px');
	},function(){
		if (!$(this).hasClass('active')){
			$(this).parent().find('div.tl').css('background-position', '0 -16px');
			$(this).parent().find('div.tr').css('background-position', '-8px -16px');
		}
	});
		
	// Get the page via the Body Class
	var $page = $('body').attr('class');
	$page = $page.split(" ");
	if ($page[0] == 'portal') {
		// Get the height of the navigation page
		var $navHeight = $('.portal-left-col').height();
		$('div.file-container').height($navHeight - 32);
		$('span.desc').width(246);
	}
		
		$("label.required").append(' <span class="required">*</span>')
		
		$('.parent-categories').height();
	
		if ($('.how-to-section').length != 0) {
			howToWidget();
		}

		if ($('.resources').length != 0) {
			resourceWidget();
		}

		$('.portal-right-col .files li:odd').addClass('alt-row');
		$('.forum li:odd').addClass('alt-row');
		$('#site-map li:even').addClass('alt-row');
		
		comment_form(); // controls the comment form
		k_menu(); // controls the dropdown menu
	
		$('a.modal').facebox(); // Applies modal window to any link with attribute rel="modal"
		
		$('.video-hold').mouseover(function(){
			$(this).addClass('video-hover');
		});
		$('.video-hold').mouseout(function(){
			$(this).removeClass('video-hover');
		});

	});

	$(window).load(function(){
		/*image slider (alternate)*/	
		if ($('#featured_image img').length > 0 ) {	
			$('#featured_image img').not('.current_img').css({display:"none"});
			// set the automatic image rotation, number is time between transitions in miliseconds
			interval = setInterval(function() { k_fader("#featured_image img",'1'); }, 4000); 	
		}
	});
	



	function k_menu(){
		$("#main-navigation ul ul").each(function(){
			$(this).parent().find('a:first').addClass('sub-nav');
		});
		$("#main-navigation a, .subnav a").removeAttr('title');
		$("#main-navigation li ul").css({display: "none"}); // Opera Fix
		
		$("#main-navigation li").each(function() {	
			
			var $sublist = $(this).find('ul:first');
			
			$(this).hover(function() {	
				$sublist.stop().css({overflow:"hidden", height:"auto", display:"none"}).slideDown(400, function() {
					$(this).css({overflow:"visible", height:"auto"});
				});	
			},
			function() {	
				$sublist.stop().slideUp(400, function() {	
					$(this).css({overflow:"hidden", display:"none"});
				});
			});	
		});
	}






	function k_fader($items_to_fade, $next_or_prev)
	{
		var $items = $($items_to_fade);
		var $currentitem = $items.filter(":visible");
		var $new_item;
		var $selector;
		
		$items.css('visibility','visible');
		
		if($items.length > 1) {
			for(i = 0; i < $items.length; i++) {
				if($items[i] == $currentitem[0]) {
					$selector = $next_or_prev >= 0 ? i != $items.length-1 ? i+1 : 0 : i == 0 ? $items.length-1 : i-1;
					
					$new_item = $($items[$selector]);
					break;
				}
			}
			
			if($new_item.css("display") == "none") {	
				$currentitem.css({zIndex:2});
				$new_item.css({zIndex:3}).fadeIn(1200, function() {
					$currentitem.css({display:"none"});
				});
			}
		}
	}
	
	function howToWidget() {
		$(".how-to-section ul li ul").hide();
		$(".how-to-section ul li a.active").parent().find('ul').slideToggle("slow");
		$(".how-to-section ul li a.nav-top-item").click(function(){
			$(this).parent().siblings().find('a.active').removeClass('active');
			$(this).addClass('active');
			$(this).parent().siblings().find("ul").slideUp("normal");
			$(this).next().slideToggle("normal");
			return false;
		});
		$('.how-to-section ul li ul li:even').addClass('alt-row');
		$('.how-to-section map area').click(function(){
			var id = $(this).attr('id');
			$(this).parent().parent().find('li a.active').removeClass('active');
			$(this).parent().parent().find("li ul").slideUp("normal");
			$(this).parent().parent().find('li a#' + id).addClass('active');
			$(this).parent().parent().find('li a#' + id).next().slideToggle("normal");
			return false;
		});
	}

	function resourceWidget() {
		$(".resources ul li ul").hide();
		$(".resources ul li a.active").parent().find('ul').slideToggle("slow");
		$(".resources ul li a.nav-top-item").click(function(){
			$(this).parent().siblings().find('a.active').removeClass('active');
			$(this).addClass('active');
			$(this).parent().siblings().find("ul").slideUp("normal");
			$(this).next().slideToggle("normal");
			return false;
		});		
		$('.resources ul li ul li:even').addClass('alt-row');
	}

	function my_lightbox($elements)
	{
		$($elements).prettyPhoto({
			"theme": 'light_square' /* light_rounded / dark_rounded / light_square / dark_square */																
		});

		$($elements).each(function() {	
			var $image = $(this).contents("img");
			$newclass = 'lightbox_video';
		
			if($(this).attr('href').match(/(jpg|gif|jpeg|png|tif)/)) $newclass = 'lightbox_image';
			
			if ($image.length > 0) {	
				if($.browser.msie &&  $.browser.version < 7) $(this).addClass('ie6_lightbox');
			
				var $bg = $("<span class='"+$newclass+" ie6fix'></span>").appendTo($(this));
			
				$(this).bind('mouseenter', function() {
					$height = $image.height();
					$width = $image.width();
					$pos =  $image.position();		
					$bg.css({height:$height, width:$width, top:$pos.top, left:$pos.left});
				});
			}
		});	
		
		$($elements).contents("img").hover(function() {
			$(this).stop().animate({opacity:0.5},400);
		},function() {
			$(this).stop().animate({opacity:1},400);
		});
	
	
	}


	function comment_form() {
		var my_error;
		$(".ajax_comment_form #send").bind("click", function(){						 
			my_error = false;
			$(".ajax_comment_form #name, .ajax_comment_form #comment-box, .ajax_comment_form #email ").each(function(i){
				var value = $(this).attr("value");
				var check_for = $(this).attr("id");
				var surrounding_element = $(this).parent();
				if (check_for == "email") {
					if(!value.match(/^\w[\w|\.|\-]+@\w[\w|\.|\-]+\.[a-zA-Z]{2,4}$/)) {
						$(this).addClass("input-error");
						my_error = true;
					} else {
						surrounding_element.attr("class","").addClass("valid");	
					}
				}
				if (check_for == "name" || check_for == "comment-box") {
					if (value == "") {
						$(this).addClass("input-error");
						my_error = true;
					} else {
						$(this).addClass("valid");
					}
				}
				if ($(".ajax_comment_form #name, .ajax_comment_form #comment-box, .ajax_comment_form #email").length  == i + 1) {
					if (my_error == false) {
						$(".ajax_form").slideUp(400);
						var $datastring = "ajax=true";
						$(".ajax_comment_form input, .ajax_comment_form textarea").each(function(i) {
							var $name = $(this).attr('name');	
							var $value = $(this).attr('value');
							$datastring = $datastring + "&" + $name + "=" + $value;
						});
						$(".ajax_comment_form #send").fadeOut(100);					
						$.ajax({
							type: "POST",
							url: "/comments",
							data: $datastring,
							success: function(response) {
							   $(".ajax_comment_form").before("<div class='ajaxresponse' style='display: none;'></div>");
							   $(".ajaxresponse").html(response).slideDown(400); 
							   $(".ajax_comment_form #name, .ajax_comment_form #email, .ajax_comment_form #phone, .ajax_comment_form #website, .ajax_comment_form #message, .ajax_comment_form #heard").val("");
							}
						});
					}
				}
			});
			return false;
		});
	} // end contact_form()

