// JavaScript Document written controller file view here for more details http://www.sohtanaka.com/web-design/mega-drop-downs-w-css-jquery/

$(document).ready(function() {
		
		//On Hover Over
		function megaHoverOver(){
			$(this).find(".sub").stop().fadeTo('fast', 1).show(); //Find sub and fade it in
			(function($) {
				//Function to calculate total width of all ul's
				jQuery.fn.calcSubWidth = function() {
					rowWidth = 0;
					//Calculate row
					$(this).find("ul").each(function() { //for each ul...
						rowWidth += $(this).width(); //Add each ul's width together
					});
				};
			})(jQuery); 
		
			if ( $(this).find(".row").length > 0 ) { //If row exists...
		
				var biggestRow = 0;	
		
				$(this).find(".row").each(function() {	//for each row...
					$(this).calcSubWidth(); //Call function to calculate width of all ul's
					//Find biggest row
					if(rowWidth > biggestRow) {
						biggestRow = rowWidth;
					}
				});
		
				$(this).find(".sub").css({'width' :biggestRow}); //Set width
				$(this).find(".row:last").css({'margin':'0'});  //Kill last row's margin
		
			} else { //If row does not exist...
		
				$(this).calcSubWidth();  //Call function to calculate width of all ul's
				$(this).find(".sub").css({'width' : rowWidth}); //Set Width
		
			}
		}
		//On Hover Out
		function megaHoverOut(){
		  $(this).find(".sub").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
			  $(this).hide();  //after fading, hide it
		  });
		}
		
		//Set custom configurations
		var config = {
			 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
			 interval: 100, // number = milliseconds for onMouseOver polling interval
			 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
			 timeout: 500, // number = milliseconds delay before onMouseOut
			 out: megaHoverOut // function = onMouseOut callback (REQUIRED)
		};
		
		$("ul#topnav li .sub").css({'opacity':'0'}); //Fade sub nav to 0 opacity on default
		$("ul#topnav li").hoverIntent(config); //Trigger Hover intent with custom configurations
		
		

		
                /* edited to take our read more slide up replace with basic hover effect when image on home page is hovered over */
		$('img.chain-products').hover(function(){
			//$(".cover", this).stop().animate({top:'100px'},{queue:false,duration:160});
                        $("h2.chain-products").addClass('remotehover');
                        $("h1.chain-products").addClass('remotehover');
		}, function() {
			//$(".cover", this).stop().animate({top:'260px'},{queue:false,duration:160});
                        $("h2.chain-products").removeClass('remotehover');
                        $("h1.chain-products").removeClass('remotehover');
		});

                $('img.chain-harrows').hover(function(){
                        $("h2.chain-harrows").addClass('remotehover');
                        $("h1.chain-harrows").addClass('remotehover');
		}, function() {
                        $("h2.chain-harrows").removeClass('remotehover');
                        $("h1.chain-harrows").removeClass('remotehover');
		});

                $('img.lifting').hover(function(){
                        $("h2.lifting").addClass('remotehover');
                        $("h1.lifting").addClass('remotehover');
		}, function() {
                        $("h2.lifting").removeClass('remotehover');
                        $("h1.lifting").removeClass('remotehover');
		});

                $('img.training').hover(function(){
                        $("h2.training").addClass('remotehover');
                        $("h1.training").addClass('remotehover');
		}, function() {
                        $("h2.training").removeClass('remotehover');
                        $("h1.training").removeClass('remotehover');
		});


                /* adding remote hover effects on the products landing page */


		
		
		
		
		
		//accordian scripts here
		//Set default open/close settings
		$('.acc_container').hide(); //Hide/close all containers
		
		$(window).load(function() {
				if ($('.acc_trigger').is('.activepage')) {
					//alert('active page found - will open the active box');
					$('.acc_trigger.activepage').addClass('active').next().show();
				} else {
					//alert('active page not found - will open the first box');
					$('.acc_trigger:first').addClass('active').next().show(); //Add "active" class to first trigger, then show/open the immediate next container
					//check if its the home page...
					
				}
		});

		//On Click
		$('.acc_trigger').click(function(){
			if( $(this).next().is(':hidden') ) { //If immediate next container is closed...
				$('.acc_trigger').removeClass('active').next().slideUp(); //Remove all "active" state and slide up the immediate next container
				$(this).toggleClass('active').next().slideDown(); //Add "active" state to clicked trigger and slide down the immediate next container
			}
			return false; //Prevent the browser jump to the link anchor
		});
		
		//working to save state of the accordian by highlighting the current page based on the url
		var url = window.location.toString()
		
                

                

		
		//accordian scripts have ended
		
		
		//Products read more toggle here...
		$('.catdescriptionhiddden').hide(); //Hide/close all containers

                $('.catdescmore').click(function(){
			
			$('.catdescriptionhiddden').slideToggle('slow');
			
			if ($(this).is('.hideagain')) {
					//alert('active page found - will open the active box');
					$(this).removeClass('hideagain');
					
			} else {
					//alert('active page not found - will open the first box');
					$(this).addClass('hideagain');
			}
			
		}); 

                $('#accordian a').each(function(){

			  var myHref= $(this).attr('href');

			  if(url.match(myHref)) {
				$(this).addClass('activeClassNameForCSSHighlight');
				$(this).closest('ul').show();
			  }

		});
		
		
		//validation of the contact form
		// validate the comment form when it is submitted
		$("#contactform").validate();
		
		$("#calculator").validate({
		  rules: {
			Weight: {
			  required: true,
			  range: [100, 200000]
			}
		  }
		});
		
		$("#trainingstore").validate();
		
		//styles for the product details tables
		$('.datatable tbody tr:even').css('background-color','#fff');
		
		
		
		//this prints the product details page
		$("#printpage").click(function() {	
			$("#productdetail").jqprint();
		});




                //advert slider code - used in news and products

                //Show the paging and activate its first link
                $(".paging").show();
                $(".paging a:first").addClass("active");

                //Get size of the image, how many images there are, then determin the size of the image reel.
                var imageWidth = $(".window").width();
                var imageSum = $(".image_reel img").size();
                var imageReelWidth = imageWidth * imageSum;

                //Adjust the image reel to its new size
                $(".image_reel").css({'width' : imageReelWidth});


                //Paging  and Slider Function
                rotate = function(){
                    var triggerID = $active.attr("rel") - 1; //Get number of times to slide
                    var image_reelPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide

                    $(".paging a").removeClass('active'); //Remove all active class
                    $active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)

                    //Slider Animation
                    $(".image_reel").animate({
                        left: -image_reelPosition
                    }, 500 );

                };

                //Rotation  and Timing Event
                rotateSwitch = function(){
                    play = setInterval(function(){ //Set timer - this will repeat itself every 7 seconds
                        $active = $('.paging a.active').next(); //Move to the next paging
                        if ( $active.length === 0) { //If paging reaches the end...
                            $active = $('.paging a:first'); //go back to first
                        }
                        rotate(); //Trigger the paging and slider function
                    }, 7000); //Timer speed in milliseconds (7 seconds)
                };

                rotateSwitch(); //Run function on launch

                //On Hover
                $(".image_reel a").hover(function() {
                    clearInterval(play); //Stop the rotation
                }, function() {
                    rotateSwitch(); //Resume rotation timer
                });

                //On Click
                $(".paging a").click(function() {
                    $active = $(this); //Activate the clicked paging
                    //Reset Timer
                    clearInterval(play); //Stop the rotation
                    rotate(); //Trigger rotation immediately
                    rotateSwitch(); // Resume rotation timer
                    return false; //Prevent browser jump to link anchor
                });

                //end advert slider...

				
		
	});
