/***********************************************
* jQuery functions execute
***********************************************/

jQuery(document).ready(function() {                
   /* email protection */
 	$("span.mailto").each(function(){
 		exp = $(this).text().search(/\((.*?)\)/) != -1 ? new RegExp(/(.*?) \((.*?)\)/) : new RegExp(/.*/);
 		match = exp.exec($(this).text());
 		addr = match[1] ? match[1].replace(/ at /,"@").replace(/ dot /g,".") : match[0].replace(/ at /,"@").replace(/ dot /g,".");
 		link = match[2] ? match[2] : addr;
 		subject = $(this).attr('title') ? "?subject="+$(this).attr('title').replace(/ /g,"%20") : "";
 		$(this).after('<a href="mailto:'+addr+subject+'">'+ link + '</a>');
 		$(this).remove();
 	});
 	           
	/*cmx form */
	if(jQuery.browser.mozilla) {
		$('form.cmxform').hide().find('label:not(.nocmx)').each(function(){
                          	var $labelContent = $(this).html();
                                       var $labelWidth = $(this).css('width');
                                       $(this).empty();
                                       $(this).append('<span style="display: block; width: '+$labelWidth+';">');
                                       $(this).prepend('</span>');
                                       $(this).css('display', '-moz-inline-box');
                                       $(this).find('span').html($labelContent);
                                       $('form.cmxform').show();
                                });
                };             

  /* Validation */
  $("#Finance_Form").validate();
  $("#insurance-quote").validate();
  
  /* Clearing for ie6 BE SEMANTIC WITHOUT COMPRIMISE */
  $("div.text").append('<div class="clear"><!-- &nbsp; --></div>');
  
	/*external link*/
	$('a[rel*=external]').click(function(){this.target = "_blank";});
	/*thickbox opacity*/	
	$('.thickbox').click(function() {$('.TB_overlayBG').css('opacity', '0.75');});


	/*on focus for input boxes*/
	$.fn.search = function() {
		return this.focus(function() {
			if( this.value == this.defaultValue ) {
				this.value = "";
			}
		})
		.blur(function() {
			if( !this.value.length ) {
				this.value = this.defaultValue;
			}
		});
	};

	$("#search input.keyword").search();

	$("#search form").submit(function() {
		if ( $("#search input#sKeywords").attr("value") == $("#search input#sKeywords").attr("title") ) {
			alert('Please enter some keywords to search on.');
			$("#search input#sKeywords").focus();
			return false;
		}
		if ( $("#search input#sKeywords").attr("value").length <= 2 ) {
			alert('Please enter more keywords to search on.');
			$("#search input#sKeywords").focus();
			return false;
		}
	});

	/*disable input box at confirmation*/
	$('.confirmation input').attr("disabled", true);
	$('.confirmation input.submit').attr("disabled", false);
									
	/*dropdown fix*/
	/*$('ul.catalogue-nav li').hover(function() {$(this).addClass('over');}, function() {$(this).removeClass('over');});*/
	$('ul.catalogue-nav li').mouseover(function() {$(this).addClass('over'); });
	$('ul.catalogue-nav li').mouseout(function() {$(this).removeClass('over'); });

	/*set hover class for anything*/
	$('ul.category li').css('cursor', 'pointer').hover(function() {$(this).addClass('boxhover');}, function() {$(this).removeClass('boxhover');});
	$('ul.subcategory li').css('cursor', 'pointer').hover(function() {$(this).addClass('boxhover');}, function() {$(this).removeClass('boxhover');});
	$('ul.subcategory li').click(function(){window.location = $(this).find('a').attr("href");});
	$('a.top-level').hover(function() {$(this).addClass('tophover');}, function() {$(this).removeClass('tophover');});
	$('input.send').css('cursor', 'pointer').hover(function() {$(this).addClass('sendhover');}, function() {$(this).removeClass('sendhover');});
	$('input.searchnow').css('cursor', 'pointer');
	$('input.submit').css('cursor', 'pointer').hover(function() {$(this).addClass('submithover');}, function() {$(this).removeClass('submithover');});
	$('form.cmxform input.submit').css('cursor', 'pointer').hover(function() {$(this).addClass('submithover');}, function() {$(this).removeClass('submithover');});
	
		
	/*simple hide and show*/
	$('#enquiry-list').hover(function() {$('#enquiry-list-window').slideDown(200);}, function() {$('#enquiry-list-window').slideUp('slow');});
	$('#enquiry-list-window').hover(function() {$('#enquiry-list a#enquiry-cart-nav').addClass('onsection');}, function() {$('#enquiry-list a#enquiry-cart-nav').removeClass('onsection');});
	$('#finance-form').hide();
	$('a.openform').click(function() {$('#finance-form').slideDown(360);}, function() {$('#finance-form').slideUp('slow');});
				
});

/***********************************************
* Safe email links - hide Email To field
***********************************************/

function hide_yamaha_EmailForm() {
	var s1 = 'bikesales';
	var s2 = '@';
	var s3 = 'causewayyamaha.com.au';
	document.write('<input type="hidden" name="Em' + 'ail_To" value="' + s1 + s2 + s3 + '" />');
}

function hide_kawasaki_EmailForm() {
	var s1 = 'bikesales';
	var s2 = '@';
	var s3 = 'causewaykawasaki.com.au';
	document.write('<input type="hidden" name="Em' + 'ail_To" value="' + s1 + s2 + s3 + '" />');
}

function hide_ktmducati_EmailForm() {
	var s1 = 'bikesales';
	var s2 = '@';
	var s3 = 'causewayktmducati.com.au';
	document.write('<input type="hidden" name="Em' + 'ail_To" value="' + s1 + s2 + s3 + '" />');
}

function hide_affordablemc_EmailForm() {
	var s1 = 'bikesales';
	var s2 = '@';
	var s3 = 'affordablemc.com.au';
	document.write('<input type="hidden" name="Em' + 'ail_To" value="' + s1 + s2 + s3 + '" />');
}

function hide_highwayyamaha_EmailForm() {
	var s1 = 'bikesales';
	var s2 = '@';
	var s3 = 'highwayyamaha.com.au';
	document.write('<input type="hidden" name="Em' + 'ail_To" value="' + s1 + s2 + s3 + '" />');
}

/* Custom Confirm Dialog using simpleModal.js */

$(document).ready(function () {
	//do a post ajax call to add the item to the cart
	$.post("ajaxAddToCart.asp", function(data){
		//put the retrieved ajax html into the DOM
		$("#enquiry-list").html(data);
	});

	message = "";

	$('.confirm').click(function (e) {
		e.preventDefault();

			var usercolour = "";

			//check to see if we have a colour
			if ( $("#Colour").length > 0 ) {
				usercolour = $("#Colour").val();
			}

			//do a post ajax call to add the item to the cart
			$.post("ajaxAddToCart.asp", { item: $(this).attr("name"), action: "add", count: "1", colour: usercolour}, function(data){
				//put the retrieved ajax html into the DOM
				$("#enquiry-list").html(data);
			});

			$('#enquiry-alert').modal({
				close: false,
				overlayId: 'confirmModalOverlay',
				containerId: 'confirmModalContainer',
    				onOpen: function (dialog) {
					dialog.overlay.fadeIn(300, function () {
					dialog.container.fadeIn(300, function () {
					dialog.data.fadeIn(200);
					});
					});
				},
				onShow: function (dialog) {
					dialog.data.find('.message').append(message);
				},
				onClose: function (dialog) {
					dialog.data.fadeOut(200, function () {
					dialog.container.fadeOut(300, function () {
					dialog.overlay.fadeIn(300, function () {
					$.modal.close();
					});
					});
			 		});
				}
		});
	});
	
	$('.confirm2').click(function (e) {
		e.preventDefault();
			$('#privacy-policy').modal({
				close: false,
				overlayId: 'confirmModalOverlay',
				containerId: 'confirmModalContainer',
    				onOpen: function (dialog) {
					dialog.overlay.fadeIn(300, function () {
					dialog.container.fadeIn(300, function () {
					dialog.data.fadeIn(200);
					});
					});
				},
				onShow: function (dialog) {
					dialog.data.find('.message').append(message);
				},
				onClose: function (dialog) {
					dialog.data.fadeOut(200, function () {
					dialog.container.fadeOut(300, function () {
					dialog.overlay.fadeIn(300, function () {
					$.modal.close();
					});
					});
			 		});
				}
		});
	});
	
	$('.confirm3').click(function (e) {
		e.preventDefault();
			$('#terms-conditions').modal({
				close: false,
				overlayId: 'confirmModalOverlay',
				containerId: 'confirmModalContainer',
    				onOpen: function (dialog) {
					dialog.overlay.fadeIn(300, function () {
					dialog.container.fadeIn(300, function () {
					dialog.data.fadeIn(200);
					});
					});
				},
				onShow: function (dialog) {
					dialog.data.find('.message').append(message);
				},
				onClose: function (dialog) {
					dialog.data.fadeOut(200, function () {
					dialog.container.fadeOut(300, function () {
					dialog.overlay.fadeIn(300, function () {
					$.modal.close();
					});
					});
			 		});
				}
		});
	});

	$('.buy-product').submit(function (e) {
		e.preventDefault();

		with(e.target)
		{
			var iItem = itemPS.value;
			var sColour = "";
			var sSize = ""
			var sAction = actionPS.value;

			//check to see if we have a colour
			if ( $(this).find("select[name='Colour']").length > 0 ) {
				sColour = Colour.value;
			}
			//check to see if we have a colour
			if ( $(this).find("select[name='Size']").length > 0 ) {
				sSize = Size.value;
			}

			//do a post ajax call to add the item to the cart
			$.post("ajaxAddToPitstopCart.asp", { itemPS: iItem, actionPS: sAction, countPS: "1", colour: sColour, size: sSize}, function(data){
				//put the retrieved ajax html into the DOM
				//$("#shopping-items").html(data);
			});

			//do a post ajax call to add the item to the cart
			$.post("ajaxAddToCart.asp", function(data){
				//put the retrieved ajax html into the DOM
				$("#enquiry-list").html(data);
			});

			$('#shopping-alert').modal({
				close: false,
				overlayId: 'confirmModalOverlay',
				containerId: 'confirmModalContainer',
    				onOpen: function (dialog) {
					dialog.overlay.fadeIn(300, function () {
					dialog.container.fadeIn(300, function () {
					dialog.data.fadeIn(200);
					});
					});
				},
				onShow: function (dialog) {
					dialog.data.find('.message').append(message);
				},
				onClose: function (dialog) {
					dialog.data.fadeOut(200, function () {
					dialog.container.fadeOut(300, function () {
					dialog.overlay.fadeIn(300, function () {
					$.modal.close();
					});
					});
			 		});
				}
			});
		}
	});
});



/***********************************************
* Random Images Script
***********************************************/

<!-- Begin
// Set up the image files to be used.
var theImages = new Array() // do not change this
// To add more image files, continue with the
// pattern below, adding to the array.

theImages[0] = 'images/' + sGetWebsiteFolder() + '/header01.jpg'
theImages[1] = 'images/' + sGetWebsiteFolder() + '/header02.jpg'
theImages[2] = 'images/' + sGetWebsiteFolder() + '/header03.jpg'
theImages[3] = 'images/' + sGetWebsiteFolder() + '/header04.jpg'
theImages[4] = 'images/' + sGetWebsiteFolder() + '/header05.jpg'
theImages[5] = 'images/' + sGetWebsiteFolder() + '/header06.jpg'
theImages[6] = 'images/' + sGetWebsiteFolder() + '/header07.jpg'
theImages[7] = 'images/' + sGetWebsiteFolder() + '/header08.jpg'
theImages[8] = 'images/' + sGetWebsiteFolder() + '/header09.jpg'

// do not edit anything below this line
var j = 0
var p = theImages.length;
var preBuffer = new Array()
for (i = 0; i < p; i++){
   preBuffer[i] = new Image()
   preBuffer[i].src = theImages[i]
}
var whichImage = Math.round(Math.random()*(p-1));
function showimage(){
document.write('<img src="'+theImages[whichImage]+'">');
}

//  End -->

