/* MENU FUNCTION */

function mainmenu(){
$(" #topnav ul ").css({display: "none"}); // Opera Fix
$(" #topnav li").hover(function(){
	$(this).find('ul:first').css({visibility: "visible",display: "none"}).show(1);
		},function(){
		$(this).find('ul:first').css({visibility: "hidden"});
		});
}

/* CYCLE */
$(function() {
    $('#promo').before('<ul id="promo-nav"></ul>').cycle({ 
        fx:     'fade',
        speed:  'fast',
        timeout: 0,
		/* BREAK ME */
		pagerEvent: 'mouseover',
        pager:  '#promo-nav',
		slideExpr: 'img',
		pagerAnchorBuilder: function(idx, slide) { 
			TheLink = jQuery(slide).attr("alt");
			if (TheLink.length > 24){LinkClass="TwoLine";}
			else {LinkClass="OneLine";}
        return '<li class="' + LinkClass + '"><a href="#">' + TheLink + '</a></li>';
    	} 
    });
});

$(document).ready(function(){
	
	/* FORM SUBMISSION */
	$(".button-submit").click(function() {
		error = false;
		ErrorString = "ERROR\n\n";
		$(".ErrorMessage").remove();
		$(this).parent().parent().parent().find(".required").each(function(index) {
			TheLength = $(this).val().length;
			TheFieldName = $(this).parent().find("label").text();
			TheFieldName = TheFieldName.replace("*", "");
			TheFieldName = TheFieldName.replace(":", "");
			if (TheLength < 1)
			{
				error = true;
				$(this).parent().find("label").css("color", "#c00");
				ErrorString = ErrorString + "Please enter a value for: " + TheFieldName + "\n";
			}
			else 
			{
				$(this).parent().find("label").css("color", "#000");
			}
		});
		/* SUBMIT FORM IF NO ERRORS */	
		if (error == false){
			$(this).parent().parent().parent().submit();
		}
		/* DISPLAY ALERT IF ERRORS */
		else
		{
			alert (ErrorString);
			$(this).parent().parent().parent().find('p:first').append('<br /><strong style="color: #c00" class="ErrorMessage">Errors in form submission data, please review the highlighted fields<br /></strong>');
			return false;
		}
	});
	
	mainmenu();
	
	/* BASKET FORM JS */
	$(".addqty").click(function(){
		$(this).next().val( Number($(this).next().val()) + 1 );
		x = $(this).parent().attr('name');
        $(".changetheqty").change();
	});
	
	$(".subtractqty").click(function(){
		$(this).prev().val( Number($(this).prev().val()) - 1 );
		x = $(this).parent().attr('name');
        $(".changetheqty").change();
	});
	
	$(".changetheqty").change(function() {
		alert
	  t = eval("document." + x + ".quantity")
        failed = false
        if (isNaN(t.value))
        {
           alert("Please enter a valid number in quantity ");
           t.value = 1;
           t.focus();
           failed = true
     
        }
        else 
        {
          if (t.value <= 0 )
          {
                        alert("Quantity cannot be zero or less");
                        t.value = 1;
                        t.focus();
      
                        failed = true
          }
        }
  		eval("document." +x+ ".submit()")
        if (failed)
           document ['navigation'].quanttest.value = 'nogo'
	});
	
	/* Close button (closes parent) */
	$('.close').click(function () {
		$(this).parent().slideToggleWidth();
	});
	
	/* FORM NAV FUNCTIONS */
	$('#homedemo-link').click(function () {
		$('#homepageforms form').hide(0);
    $('#homedemo').slideToggleWidth();
	});
	
		$('.homedemo-link').click(function () {
			$('#homepageforms form').hide(0);
		$('#homedemo').slideToggleWidth();
		});
	
	$('#callback-link').click(function () {
		$('#homepageforms form').hide(0);
    $('#callback').slideToggleWidth();
	});
	 
	$('#brochure-link').click(function () {
		$('#homepageforms form').hide(0);
    $('#brochure').slideToggleWidth();
	});
	
	$('#call-link').click(function () {
		$('#homepageforms form').hide(0);
    $('#call').slideToggleWidth();
	});
	
	$('#dealer-link').click(function () {
		$('#homepageforms form').hide(0);
    $('#dealer').slideToggleWidth();
	});
	 
	$.fn.extend({
	  slideRight: function() {
		return this.each(function() {
		  $(this).hide("slide", { direction: "right" }, 500);
		});
	  },
	  slideLeft: function() {
		return this.each(function() {
		  $(this).show("slide", { direction: "left" }, 500);
		});
	  },
	  slideToggleWidth: function() {
		return this.each(function() {
		  var el = $(this);
		  if (el.css('display') == 'none') {
			el.slideLeft();
		  } else {
			el.slideRight();
		  }
		});
	  }
	});

	/* STYLE FUNCTIONS */

	$("#header-nav li:not(:last)").addClass('bright');
	$("#footer div.block:not(:first)").addClass('bright');
	$("#brand-icons a:not(:last)").addClass('mright');
	$(".summary-row:last").addClass('bnone');
	$(".partnobox tr:first").addClass('bold');
	$(".partnobox tr:odd").addClass("beige");
	$("#topnav ul:last").css("width","184px");
	$("#promo-nav li:first").css("display", "none");
	
	$("#topnav li a").hover(function(){
		$(this).removeAttr("title");
	});
	
	$(".summary-row").each(function(index) {
		$(this).find(".psum:first").css("margin-left" , "0px");
	});
	
	$("a#WatchVideo").fancybox({
		'titleShow'     : false
	});
	
	$("a.EnquiryFormLink").fancybox({
		'titleShow'     : false
	});
	
	$("#buy-submit").click(function () {
		$('#ato-m').submit();
	});
	
	/* FOR VALIDATIONS */
	
	
	
});

