$(document).ready(function() {
  
	$('body').supersleight();
	$('ul.sf-menu').superfish(); 	
	$('p:empty').remove();
	
	// Search Textbox
	if ($(".searchterms").val() != "") {
		$("#searchText").hide();    
	}
	
	$("#searchText").click(function() { $(".searchterms").focus(); $(this).hide(); });
	
	$(".searchterms")
	.click(function() {
	   if ($(this).val() == "") {
		  $("#searchText").hide();
	   }
	})
	.blur(function() {
	   if ($(this).val() == "") {
		  $("#searchText").show();
	   }
	});

	// Search Button
	$("#search .searchbutton")
	.mouseover(function() {
	   $(this).css("background", "url(/images/btn_search_over.jpg) 0 0 no-repeat");
	})
	.mouseout(function() {
	   $(this).css("background", "url(/images/btn_search.jpg) 0 0 no-repeat");
	})
	.click(function() {
		// If this page has form validation, remove all rules
		if(jQuery().validate) {
            		$(".frm input").each(function() { $(this).rules("remove"); });
            		$(".frm select").each(function() { $(this).rules("remove"); });
            		$(".frm textarea").each(function() { $(this).rules("remove"); });
		}
	});
	
	$(".nav-img")
	.mouseover(function() {
	   $(this).children('img').fadeTo(0, 0.5);
	})
	.mouseout(function() {
	   $(this).children('img').fadeTo(0, 1);
	});
	
	// Set defaults for left navigation
	$("#left_nav > ul > li > ul").css("display", "none");
	$("#left_nav > ul > li.nav_left_on_section > ul").css("display", "block");
	$("#left_nav > ul > li.nav_left_on_section > ul > li > ul").css("display", "none");
	$("#left_nav > ul > li.nav_left_on_section > ul > li.nav_left_on_section > ul").css("display", "block");



 /*----------------- TRACKING  -----------------*/
	/*
		TRACK EXTERNAL LINKS CLICKS
		Elements that are given a class of trackExternal will be tracked 

		To use this:
		- add a class of "trackExternal" to your link.
		- add a title attribute to your link to be used in your reports

		Click reported as: /link-tracking/outgoing-link/{title}
	*/	
 
	$('a.trackExternal').click(function(){
		var trackingName = '/link-tracking/outgoing-link/' + $(this).attr('title');
		VSLT(trackingName);
	});	
 
 
	/*
		TRACK EMAIL LINK CLICKS
		This simply tracks the click on any link that uses the "mailto:email@domain.com" method.
		Click reported as: /link-tracking/email-link/{title}
	*/
 
	$('a[href*=mailto]').click(function () {
		var trackingName = '/link-tracking/email-link/' + $(this).attr('title');
		VSLT(trackingName);
	});
 

	/*
		TRACK PDF FILE CLICKS
		This adds the tracking code to any link with a .pdf extension, allowing you to track file downloads
		Click reported as: /link-tracking/pdf-link/{title}
	*/
 
	$('a[href*=\\.pdf]').click(function () {
		var trackingName = '/link-tracking/pdf-link/' + $(this).attr('title');
		VSLT(trackingName);
	});
 
 
	/*
		TRACK POPUP WINDOW CLICKS
		The popup links are given a class of trackPopup, so they will automatically get this click event
		Click reported as: /link-tracking/popup-link/{title}
	*/
 
	$('a.trackPopUp').click(function () {
		var trackingName = '/link-tracking/popup-link/' + $(this).attr('title');
		VSLT(trackingName);
	});



	/*
		TRACK VIDEO CLICKS
		Elements that are given a class of trackVideo will be tracked

		To use this:
		- add a class of "trackVideo" to your link.
		- add a title attribute to your link to be used in your reports

		Click reported as: /link-tracking/video-link/{title}
	*/
 
	$('.trackVideo').click(function () {
		var trackingName = '/link-tracking/video-link/' + $(this).attr('title');
		VSLT(trackingName);
	});


	/*
		TRACK IMAGE CLICKS
		Elements that are given a class of trackImage will be tracked

		To use this:
		- add a class of "trackImage" to your link.
		- add a title attribute to your link to be used in your reports

		Click reported as: /link-tracking/image-link/{title}
	*/
 
	$('.trackImage').click(function () {
		var trackingName = '/link-tracking/image-link/' + $(this).attr('title');
		VSLT(trackingName);
	});


	/*
		TRACK CUSTOM ELEMENTS
 
		If you want to track anything specific, this will allow you to do so anywhere you want.
 
		To use this:
		- add a class of "trackCustom" to your link.
		- add a title attribute to your link to be used in your reports
 
		Would report as /link-tracking/page-element/{title}
	*/
 
	$('.trackCustom').click(function(){
		var trackingName = '/link-tracking/page-element/' + $(this).attr('title');
		VSLT(trackingName);
	});	
 
 /*------------------------------------------------*/

	
});


/**
* loops through querystring name/values and 
* gets associated value
* 
*@param str the String to look for
* @return 
*/
function querySt(str) {
	qs = window.location.search.substring(1);
	gy = qs.split("&");
	for (i = 0; i < gy.length; i++) {
		ft = gy[i].split("=");
		if (ft[0] == str) {
			return ft[1];
		}
	}
}


/*----------------- LIGHTBOX -----------------*/
var popupStatus = 0;
function loadPopup(popupDiv) {
	if (popupStatus == 0) {
		$("#popupBackground"+popupDiv).css({ "opacity": "0.7" });
		$("#popupBackground"+popupDiv).fadeIn("def");
		$("#popupContainer"+popupDiv).fadeIn("def");
		popupStatus = 1;
	}
}
function disablePopup(popupDiv) {
	if (popupStatus == 1) {
		$("#popupBackground"+popupDiv).fadeOut("def");
		$("#popupContainer"+popupDiv).fadeOut("def");
		popupStatus = 0;
	}
}
function centerPopup(popupDiv) {

	var windowWidth = $(window).width();
	var windowHeight = $(window).height();
	var popupHeight = $("#popupContainer"+popupDiv).height();
	var popupWidth = $("#popupContainer"+popupDiv).width();
	var documentHeight = $(document).height();

	var scrolltop = $(window).scrollTop();
	var topPosition = (windowHeight / 2 - popupHeight / 2) + scrolltop;
	
	//center window
	$("#popupContainer"+popupDiv).css({ "position": "absolute", "top": topPosition, "left": windowWidth / 2 - popupWidth / 2 });
	
	//only need force for IE6
	$("#popupBackground"+popupDiv).css({ "height": documentHeight, "width": windowWidth });
}
/*------------------------------------------------*/



/*----------------- SUPERFISH -----------------*/

/*
 * Superfish v1.4.8 - jQuery menu widget
 * Copyright (c) 2008 Joel Birch
 *
 * Dual licensed under the MIT and GPL licenses:
 * 	http://www.opensource.org/licenses/mit-license.php
 * 	http://www.gnu.org/licenses/gpl.html
 *
 * CHANGELOG: http://users.tpg.com.au/j_birch/plugins/superfish/changelog.txt
 */
(function($){
	$.fn.superfish = function(op){

		var sf = $.fn.superfish,
			c = sf.c,
			$arrow = $(['<span class="',c.arrowClass,'"> &#187;</span>'].join('')),
			over = function(){
				var $$ = $(this), menu = getMenu($$);
				clearTimeout(menu.sfTimer);
				$$.showSuperfishUl().siblings().hideSuperfishUl();
			},
			out = function(){
				var $$ = $(this), menu = getMenu($$), o = sf.op;
				clearTimeout(menu.sfTimer);
				menu.sfTimer=setTimeout(function(){
					o.retainPath=($.inArray($$[0],o.$path)>-1);
					$$.hideSuperfishUl();
					if (o.$path.length && $$.parents(['li.',o.hoverClass].join('')).length<1){over.call(o.$path);}
				},o.delay);	
			},
			getMenu = function($menu){
				var menu = $menu.parents(['ul.',c.menuClass,':first'].join(''))[0];
				sf.op = sf.o[menu.serial];
				return menu;
			},
			addArrow = function($a){ $a.addClass(c.anchorClass).append($arrow.clone()); };
			
		return this.each(function() {
			var s = this.serial = sf.o.length;
			var o = $.extend({},sf.defaults,op);
			o.$path = $('li.'+o.pathClass,this).slice(0,o.pathLevels).each(function(){
				$(this).addClass([o.hoverClass,c.bcClass].join(' '))
					.filter('li:has(ul)').removeClass(o.pathClass);
			});
			sf.o[s] = sf.op = o;
			
			$('li:has(ul)',this)[($.fn.hoverIntent && !o.disableHI) ? 'hoverIntent' : 'hover'](over,out).each(function() {
				if (o.autoArrows) addArrow( $('>a:first-child',this) );
			})
			.not('.'+c.bcClass)
				.hideSuperfishUl();
			
			var $a = $('a',this);
			$a.each(function(i){
				var $li = $a.eq(i).parents('li');
				$a.eq(i).focus(function(){over.call($li);}).blur(function(){out.call($li);});
			});
			o.onInit.call(this);
			
		}).each(function() {
			var menuClasses = [c.menuClass];
			if (sf.op.dropShadows  && !($.browser.msie && $.browser.version < 7)) menuClasses.push(c.shadowClass);
			$(this).addClass(menuClasses.join(' '));
		});
	};

	var sf = $.fn.superfish;
	sf.o = [];
	sf.op = {};
	sf.IE7fix = function(){
		var o = sf.op;
		if ($.browser.msie && $.browser.version > 6 && o.dropShadows && o.animation.opacity!=undefined)
			this.toggleClass(sf.c.shadowClass+'-off');
		};
	sf.c = {
		bcClass     : 'sf-breadcrumb',
		menuClass   : 'sf-js-enabled',
		anchorClass : 'sf-with-ul',
		arrowClass  : 'sf-sub-indicator',
		shadowClass : 'sf-shadow'
	};
	sf.defaults = {
		hoverClass	: 'sfHover',
		pathClass	: 'overideThisToUse',
		pathLevels	: 1,
		delay		: 300,
		animation	: {opacity:'show',height:'show'},
		speed		: 'normal',
		autoArrows	: false,
		dropShadows : true,
		disableHI	: false,		// true disables hoverIntent detection
		onInit		: function(){}, // callback functions
		onBeforeShow: function(){
			
			//** BUCYRUS ADDITION			
			if ($(this).parent().parent().length != 0) {
				
				var menuWidth = $(this).parent().parent().width();

				var numItems = $(this).children().size();
				var widths = numItems * 128;  //131
				
				if (widths < 200) {
					widths = 138;
				}else if (widths > 524) {
					widths = 510;
				}
					
				$(this).width(widths);
				
				// SET X
				var parentCenterX = $(this).parent().position().left + ($(this).parent().width()/2);
				var newLeft = parentCenterX - (widths/2);
				var rightEnd = widths + newLeft;
				
				if (rightEnd > menuWidth) {
					var overlap = rightEnd - menuWidth;
					$(this).css("left", newLeft-overlap +"px");
				}else{
					$(this).css("left", newLeft +"px");
				}

			}
			//**  

	},
		onShow		: function(){ },
		onHide		: function(){}
	};
	$.fn.extend({
		hideSuperfishUl : function(){
			var o = sf.op, 
				not = (o.retainPath===true) ? o.$path : '';
			o.retainPath = false;
			var $ul = $(['li.',o.hoverClass].join(''),this).add(this).not(not).removeClass(o.hoverClass)
					.find('>ul').hide().css('visibility','hidden');
			o.onHide.call($ul);
			return this;
		},
		showSuperfishUl : function(){
			var o = sf.op,
				sh = sf.c.shadowClass+'-off',
				$ul = this.addClass(o.hoverClass)
					.find('>ul:hidden').css('visibility','visible');
			sf.IE7fix.call($ul);
			o.onBeforeShow.call($ul);
			$ul.animate(o.animation,o.speed,function(){ sf.IE7fix.call($ul); o.onShow.call($ul); });
			return this;
		}
	});

})(jQuery);
/*------------------------------------------------*/



/*----------------- hoverIntent -----------------*/
/**
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne <brian@cherne.net>
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:10,interval:50,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);
/*------------------------------------------------*/



/*----------------- SUPERSLEIGHT -----------------*/
jQuery.fn.supersleight = function(settings) {
	settings = jQuery.extend({
		imgs: true,
		backgrounds: true,
		shim: '/images/supersleight.gif',
		apply_positioning: true
	}, settings);
	
	return this.each(function(){
		if (jQuery.browser.msie && parseInt(jQuery.browser.version, 10) < 7 && parseInt(jQuery.browser.version, 10) > 4) {
			jQuery(this).find('*').andSelf().each(function(i,obj) {
				var self = jQuery(obj);
				// background pngs
				if (settings.backgrounds && self.css('background-image').match(/\.png/i) !== null) {
					var bg = self.css('background-image');
					var src = bg.substring(5,bg.length-2);
					var mode = (self.css('background-repeat') == 'no-repeat' ? 'crop' : 'scale');
					var styles = {
						'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='" + mode + "')",
						'background-image': 'url('+settings.shim+')'
					};

					self.css(styles);
				};
				// image elements
				if (settings.imgs && self.is('img[src$=png]')){
					var styles = {
						'width': self.width() + 'px',
						'height': self.height() + 'px',
						'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + self.attr('src') + "', sizingMethod='scale')"
					};
					self.css(styles).attr('src', settings.shim);
				};
				// apply position to 'active' elements
				if (settings.apply_positioning && self.is('a, input') && (self.css('position') === '' || self.css('position') == 'static')){
					self.css('position', 'relative');
				};
			});
		};
	});
};
/*------------------------------------------------*/
function openVideo() {
	window.open('http://www.bucyrus.com/arv10.htm', 'Store', 'width=850, height=655, toolbar=no, menubar=yes, scrollbars=yes, resizable=yes, location=yes, directories=no, status=yes');
}
$(document).ready(function() {

	/* for keeping track of what's "open" */
	var activeClass = 'dropdown-active', showingDropdown, showingMenu, showingParent;
	/* hides the current menu */
	var hideMenu = function() {
		if(showingDropdown) {
			showingDropdown.removeClass(activeClass);
			showingMenu.hide();
		}
	};
	
	/* recurse through dropdown menus */
	$('.dropdown').each(function() {
		/* track elements: menu, parent */
		var dropdown = $(this);
		var menu = dropdown.next('div.dropdown-menu'), parent = dropdown.parent();
		/* function that shows THIS menu */
		var showMenu = function() {
			hideMenu();
			showingDropdown = dropdown.addClass('dropdown-active');
			showingMenu = menu.show();
			showingParent = parent;
		};
		/* function to show menu when clicked */
		dropdown.bind('click',function(e) {
			if(e) e.stopPropagation();
			if(e) e.preventDefault();
			showMenu();
		});
		/* function to show menu when someone tabs to the box */
		dropdown.bind('focus',function() {
			showMenu();
		});
	});
	
	/* hide when clicked outside */
	$(document.body).bind('click',function(e) {
		if(showingParent) {
			var parentElement = showingParent[0];
			if(!$.contains(parentElement,e.target) || !parentElement == e.target) {
				hideMenu();
			}
		}
	});
});

/* MACINTYPE */
function formvalidation()
{
	//RETRIEVING VARIABLES
	var name = $("#name").val();
	var address = $("#address").val();
	var phone = $("#phone").val();
	var email = $("#email").val();
	var comments = $("#comments").val();

	//REGEX
	var regEmail = new RegExp(/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/);
	var regNumeric = new RegExp(/^[0-9 _]*$/);
	
	var errorNumber = 0;
	if (name == "") 
	{
		$(".response").html("Name field is mandatory"); 
		errorNumber++
		return false;
	}
	else
	{ 
		$(".response").html("&nbsp;");
	}
	if (address == "") 
	{
		$(".response").html("Address field is mandatory"); 
		errorNumber++
		return false;
	}
	else
	{ 
		$(".response").html("&nbsp;");
	}

	if (phone == "") 
	{
		$(".response").html("Phone field is mandatory"); 
		errorNumber++
		return false;
	}
	else
	{
		if(!regNumeric.test(phone))
		{ 
			$(".response").html("Please enter numeric values only, no spaces."); 
			errorNumber++
			return false;
		}
		else
		{ 
			$(".response").html("&nbsp;");
		}
	}
	
	if (email == "") 
	{
		$(".response").html("Email field is mandatory"); 
		errorNumber++
		return false;
	}
	else
	{ 
	 if (!regEmail.test(email))
	 {
	 	$(".response").html("Please enter a valid Email Address."); 
		errorNumber++
		return false;
	 }
	 else
	 {
			$(".response").html("&nbsp;");
	 }
	}
	
	if (errorNumber == 0)
	{
		$('#btnSub').css('display', 'none');

		   var dataString = 'name='+ name + '&email=' + email + '&address=' + address + '&phone=' + phone  + '&comments=' + comments;  
		  	//alert(dataString);
		   $.ajax({  
			 type: "POST",  
			 url: "contact_mailer.php",  
			 data: dataString,  
			 success: function(data) {  
				 //Leave a confirmation message
				//$(".response").html(data);
				$("#form1").html(data);
			 }  
		   });
		  
		   return false;  
	}
	else
	{
		return false;	
	}
}
