// JavaScript Document

$('#spec').html('<img src="media/images/spec.jpg" class="padded" />');

$('.gal a').each
(
 	function ()
		{
			var href = this.href.replace(/.jpg/,'t.jpg');
			$(this).html('<img src="'+href+'" width="90" height="68" />');
		}
);

if ($('a.home').hasClass('sel')) {

	loadVid();
	$('.mainflash').html('<img src="media/images/main.jpg" height="240" align="left" onclick="location.href=\'order.html\'" style="visibility:hidden;cursor:pointer; background: #000;" onload="$(this).css({display: \'none\',\'visibility\':\'visible\'}).fadeIn(\'slow\');" />');
	
} else {

	$('.vflash a').click
	(
	 	function ()
			{
				loadVid();
				return false;
			}
	);

}

function loadVid()
{

	$('.vflash').each
	(
		function ()
			{
			   var so = new SWFObject(root+"media/flash/player.swf", "movief", "220", "165", "9", "#000000");
			   so.addParam("loop", "false");
			   so.addParam("wmode", "window");
			   so.addParam("menu", "false");
			   so.addParam('allowFullScreen', 'true');
			   so.addParam('scale', 'showall');
			   so.addVariable("loc", root);
				if ($('a.home').hasClass('sel')) 
					so.addVariable('playme', 1);
				else
					so.addVariable('playme',2);
			   so.addVariable("vid", 1);
			   so.write(this);			
			}
	);	

}

$('.mainflashx').each
(
 	function ()
		{
//			return false;
			var src = this.id;
			var so = new SWFObject(root+"media/flash/"+src+".swf", "mainf", "500", "240", "9", "#000000");
			so.addParam("loop", "false");
			so.addParam("wmode", "window");
			so.addParam("menu", "false");
			so.addParam("align", "left");
			so.addVariable("loc", root);
			so.write(this);			
		}
);

//if ($('a.home').hasClass('sel')) $('.search').show();

$("#chatlive").click
(
 	function ()
		{
			nw = window.open('http://server.iad.liveperson.net/hc/60624316/?cmd=file&file=visitorWantsToChat&site=60624316&byhref=1','chatlive','top=120,left=120,height=402,width=496,status=yes');
			nw.focus();
			return false;
		}
);

$('#trac').keyup
(
 	function ()
		{
			if ($.trim(this.value)=='') 
				{
				$('.mlist').html('');	
				return false;
				}
			
			if (this.value==this.dv) return false;
			this.dv = this.value;
			
			$(this).addClass('load');
			$.post
			(
			 	root+'main/post',
				{action:'modelcheck', trac:this.value},
				function (data)
					{
						$('.load').removeClass('load');
						$('.mlist').html(data);
					}
			);
		}
);

$('.pane:first input').click
(
 	function ()
		{
			switch (this.name)
				{
					case 'backhoesize':
						$('#ctype').html(this.title);
						break;
					case 'warranty':
						$('#wtype').html(this.title);
						break;
				}
			$('#btype').html('');
			$('.addons').html('');	
			var cval = 0;
			var ac = 2;
			$('.pane:first input:checked').each
			(
			 	function ()
					{
						var p = this.value.indexOf(':')+1;
						cval += +this.value.substr(p);
						if (this.name=='bucket[]'||this.name=='pto') $('#btype').append(this.title.replace(/'/,'"')+ ' $' + this.value.substr(p) + '<br/>');
						ac++;
						$('.addons').append('<input type="hidden" name="item_name_'+(ac)+'" value=\''+this.title+'\'/>');
						$('.addons').append('<input type="hidden" name="item_description_'+(ac)+'" value=\''+this.title+'\'/>');
						$('.addons').append('<input type="hidden" name="item_quantity_'+(ac)+'" value="1"/>');
						$('.addons').append('<input type="hidden" name="item_price_'+(ac)+'" value="'+this.value.substr(p)+'"/>');

					}
			);
			
			if ($('#btype').html()=='') $('#btype').html('None');
			
			$('#cprice span').html(cval);
			
		}
);

$(".button2:contains('Buy Now')").click
(
 	function ()
		{
			location.href = 'order.html';
		}
);

$('.gallery a, .gal a').lightBox();

$('.modopt span').click
(
 	function ()
		{
			$(".tabs a:eq(1)").trigger('click');
			var n = $('.modopt span').index(this);
			//$('.modelname td:eq('+n+')').trigger('click');
			$('.specs tr').each
				(
				 	function ()
						{
							$('td:eq('+n+')',this).addClass('clite').siblings().removeClass('clite');
						}
				);			
		}
);

$('.modopt span').hover
(
 	function ()
		{
			try
			{
			$(this).addClass('over');
			} catch (e) {}
		},
	function ()
		{
			try
			{
			$(this).removeClass('over');
			} catch (e) {}
		}
);

$('.navmenu a').click
(
 	function ()
		{
			$('.navmenu a.sel').removeClass('sel');
			$(this).addClass('sel');
		}
);

$('.navmenu a span').hover
(
 	function ()
		{
			//if ($(this).css('top')!='0px') return false;
			try
			{
			$(this).animate({top: '-40px'},'fast');
			} catch (e) {}
		},
	function ()
		{
			 //$(this).stop();
			 try
			 {
			 $(this).animate({top: '0px'},'fast');
			 } catch (e) {}
		}
);

$('.confidence img').load
(
 	function ()
		{
			if (!$.browser.opera) $(this).reflect({height: 0.5});
		}
);

$('.confidence').each
(
 	function ()
		{
			$('img',this).reflect({height: 0.5});
			$(this).append('<div><img src="'+root+'media/images/sc.gif" border="0" /></div>');
		}
);


$('.button, .button2').hover
(
 	function ()
		{
			$(this).addClass('button3');
		},
	function ()
		{
			$(this).removeClass('button3');
		}
);

var gloaded = false;

$('.tabs a').click
(
 	function ()
		{
			$('.tabs a.sel').removeClass('sel');
			$(this).addClass('sel');
			this.blur();
			var n = $('.tabs a').index(this);
			$('.pane').hide();
			$('.pane:eq('+n+')').show();
			$('.pane:visible input:first').trigger('focus');

			if (gloaded==false) {

			$('div.gallery a').each
			(
				function ()
					{
						var href = this.href.replace(/.jpg/,'t.jpg');
						$(this).html('<img src="'+href+'" width="90" height="68" style="visibility:hidden" onload="$(this).css({display: \'none\',\'visibility\':\'visible\'}).fadeIn(\'slow\');" />');
					}
			);
			
			//$('.gallery a').lightBox();
			
			gloaded=true;
			
			}
			
			return false;
		}
);

$('.specs td').hover
(
 	function ()
		{
			try
				{
				$(this).addClass('hlite').siblings().addClass('hlite');
				} catch (e) {}
		},
	function ()
		{
			try
				{
				$('.hlite').removeClass('hlite');
				} catch (e) {}			
		}
);

$('.specs td').click
(
 	function ()
		{
			$(this).parent().toggleClass('on');
		}
);

$('.rightpanel .rtb').each
(
 	function ()
		{
			this.dv = this.value;
		}
);

$('.modelname td').click
(
 	function ()
		{
			var n = $('.modelname td').index(this);
			//alert('.td:eq('+n+')');
			//$('.clite').removeClass('clite');
			$('.specs tr').each
				(
				 	function ()
						{
							$('td:eq('+n+')',this).toggleClass('clite').siblings().removeClass('clite');
						}
				);
		}
);

$('.leftpanel input.rtb').each
(
 	function ()
		{
			this.value = this.title;
			var dv = this.value;
			this.dv = dv;
			$(this).focus
				(
				 	function ()
						{
							if (this.value==dv)
								this.value = '';
						}
				);
			$(this).blur
				(
				 	function ()
						{
							if (this.value=='') this.value = dv;
						}
				);				
		}
);

$('.leftpanel textarea.tb').each
(
 	function ()
		{
			var dv = this.innerHTML;
			this.dv = dv;
			$(this).focus
				(
				 	function ()
						{
							
							if (this.innerHTML==this.dv)
								$(this).html('');
							if (this.value==this.dv) this.value = '';
						}
				);
			$(this).blur
				(
				 	function ()
						{
							if (this.value=='') this.value = this.dv;
						}
				);					
		}
);


$('form.order, form.inquiry').submit
(
 	function ()
		{
			if (!validate(this)) return false;
			var dt = $(this).serializeArray();
			var form = this;
			$('.fields',this).hide();
			if ($(this).hasClass('order')) 
				$('html,body').animate({scrollTop: 10}, 1000);
			$('.response',this).html("<div class='load'>Processing, please wait...</div>");
			$.post
				(
				 	root+'main/post.html',
					dt,
					function (data)
						{
							$('.response',form).html(data + "<p><a href='#' onclick='return resetform(this)'>Submit another?</a></p>");
							$('a',form).trigger('focus');
						}
				);
			return false;
		}
);

function resetform(from)
{

	var form = $(from).parents('form');
	$(form).trigger('reset');
	$('.pane:first input:first').trigger('click');
	$('.fields',form).fadeIn('normal',function () {$('.rtb:first',form).trigger('focus');} );
	$('.response',form).empty();
	
	return false;
}

$('.copyabove').click
(
 	function ()
		{
			if (!this.checked) {
			$('#dfullname').val('');
			$('#dphone').val('');
			$('#dstreet').val('');
			$('#dcity').val('');
			$('#dstate').val('');
			$('#dzip').val('');
			} else {
			$('#dfullname').val($('#nfullname').val());
			$('#dphone').val($('#nphone').val());
			$('#dstreet').val($('#street').val());
			$('#dcity').val($('#city').val());
			$('#dstate').val($('#state').val());
			$('#dzip').val($('#zip').val());
			}
		}
);

$('.googleb').attr('src',root+'media/images/checkout.gif').click
(
 	function ()
		{
			$('.googleform').trigger('submit');
			return false;
		}
);