/* CYCLE */

$(function() {
    $('#testimonials').cycle({
        fx:     'scrollDown',
        speed:  1000,
        timeout: 5000,
		slideExpr: 'p'
    });
});

$(document).ready(function(){	
	/* FORM SUBMISSION */
	$(".button-submit").click(function() {
		error = false;
		ErrorString = "ERROR\n\n";
		$(".ErrorMessage").remove();
		$(this).parent().parent().find(".required").each(function(index) {
			TheLength = $(this).val().length;
			TheFieldValue = $(this).val();
			TheFieldName = $(this).parent().find("label").text();
			TheFieldName = TheFieldName.replace("*", "");
			TheFieldName = TheFieldName.replace(":", "");
			if (TheLength < 1)
			{
				error = true;
				$(this).parent().find("label").css("color", "#d22525");
				ErrorString = ErrorString + "Please enter a value for: " + TheFieldName + "\n";
			}
			else 
			{
				$(this).parent().find("label").css("color", "#444");
			}
		});
		/* CHECK IF EMAIL ADDRESS IS VALID */
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		EmailAddress = $(this).parent().parent().find(".email").val();
		if(!emailReg.test(EmailAddress)) {
            ErrorString = "ERROR\n\nPlease enter a valid Email Address";
            error = true;
			$(this).parent().parent().find(".email").parent().find("label").css("color", "#d22525");
        }
		/* SUBMIT FORM IF NO ERRORS */	
		if (error == false){
			$(this).parent().parent().submit();
		}
		/* DISPLAY ALERT IF ERRORS */
		else
		{
			alert (ErrorString);
			$(this).parent().parent().find('p:first').prepend('<strong style="color: #d22525" class="ErrorMessage">Errors in form submission data, please review the highlighted fields<br /><br /></strong>');
			return false;
		}
	});
	
	/* FANCYBOX */
	
	$(".various1").fancybox();
	$("a#sendenquiry").fancybox();
	$("a#requestbrochure").fancybox();
	$("a#productimage").fancybox();
	$("a#promo-box").fancybox();
	$("a#sendenquiry").fancybox({
		'titleShow'     : false
	});
	$("#closesendenquiryform").click(function(){
		$.fancybox.close();
	});
	
	/* CART BUTTON */
	$(".cartbutton").click(function(){
		$(this).parent().find(".cartquantity").val('1');
		$('#ato-m').submit();
	});

	$(" #nav ul:empty").css("display", "none");
	$(" #nav li a.current").parent().find("ul").css("display", "block");
	
	/* MORE TEXT */
	$(".short .readmore").click(function(){
		$(this).parent().find(".hidden").show(0);
	});
	
	/* 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'
	});


	/* 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");
	
	/* LIMIT BREADCRUMB */
	$(function() {
        var limit = 20;
        var chars = $("#breadcrumb .last").text(); 
        if (chars.length > limit) {
            var visiblePart = chars.substr(0, limit-1);
            var dots = "...";

            $("#breadcrumb .last").empty()
                .append(visiblePart)
                .append(dots)
        }
    });	
	
	/* NEW WINDOW FUNCTION */
	$(function(){
		$('a.new-window').click(function(){
			window.open(this.href);
			return false;
		});
	});
	
	
	
	/* IMAGE MANAGEMENT */
	$(".psum img").error(function(){
	  $(this).attr('src', 'images/product_images/none.gif');
	});	
	

	
});
