jQuery.noConflict();
jQuery(document).ready(function() {

  jQuery('#slider').bxSlider({
        infiniteLoop: true,
        pager: true,
        pagerSelector: '#product_slider_pager',
        controls: false,  
        auto: true,
        speed: 1000,
        easing: 'easeInOutExpo',
        pause: 9000,
        autoHover: true
      });
      jQuery('#test-slider').bxSlider({
        infiniteLoop: true,
        controls: true,  
        auto: false,
        speed: 1000,
        easing: 'easeInOutExpo',
        pause: 9000,
        autoHover: true
      });

  jQuery(".enlarge_me").fancybox({
    'overlayShow' : false,
    'transitionIn'  : 'elastic',
    'transitionOut' : 'elastic'
  });

  jQuery('.cust_attr-Text_Or_Image_To_Be_Printed').after('<div class="newtext">');
  jQuery('.newtext').append('<label>Sending Your Own Artwork</label>').append('<p class="pspace pspace2">Upon receipt of your order confirmation, click onto the required link<br /> to email your own artwork.</p>').append('<p class="ul_header">Preferred file formats:</p>').append('<ul class="artwork">');
  jQuery('.artwork').append('<li>PDF</li>').append('<li>JPG</li>').append('<li>Coreldraw</li>').append('<li>Photoshop</li>');

  jQuery('.cust_attr-Text_Or_Image_To_Be_Printed .custom_attr_title').after('<p class="pspace">Please state your required text / image to be printed. Please try to<br /> provide as much information as possible. i.e font style, design idea<br /> if you have one etc.</p>');


  jQuery('#soa_favour-boxes .cust_attr-Text_Line_3_-_Optional').parent().before('<p class="selectnew">Please select either an extra line of text <span class="capital">or</span> additional clipart:</p>');

  jQuery('#soa_favour-boxes .cust_attr-Text_Line_3_-_Optional').hide();
  jQuery('#soa_favour-boxes .cust_attr-Text_Line_3_-_Optional').before('<h2><a href="#" id="extratext">Click here to select an extra line of text</a></h2>');
  jQuery('#extratext').click(function(e){
    e.preventDefault();
    if(clipart = true){
      jQuery('#clipart').fadeIn(500);
      jQuery('#soa_favour-boxes .cust_attr-Clipart_-_Optional').slideUp(500); 
      jQuery('#extratext').slideUp(500);
      jQuery('#soa_favour-boxes .cust_attr-Text_Line_3_-_Optional').slideDown(500);       
    }
    else{
      jQuery('#extratext').fadeOut(500);
      jQuery('#soa_favour-boxes .cust_attr-Text_Line_3_-_Optional').slideDown(500);
      extratext = true;     
    }
  });
  jQuery('#soa_favour-boxes .cust_attr-Clipart_-_Optional').hide();
  jQuery('#soa_favour-boxes .cust_attr-Clipart_-_Optional').before('<h2><a href="#" id="clipart">Click here to select clipart</a></h2>');
  jQuery('#clipart').click(function(e){
    e.preventDefault();
    if(extratext = true){
      jQuery('#extratext').fadeIn(500);
      jQuery('#soa_favour-boxes .cust_attr-Text_Line_3_-_Optional').slideUp(500);
      jQuery('#clipart').slideUp(500);  
      jQuery('#soa_favour-boxes .cust_attr-Clipart_-_Optional').slideDown(500);       
    }
    else{
      jQuery('#clipart').fadeOut(500);
      jQuery('#soa_favour-boxes .cust_attr-Clipart_-_Optional').slideDown(500);
      clipart = true;     
    }
  });

  jQuery('#Cheque').next().next().after('<div id="cheque_note"></div>');
  jQuery('#cheque_note')
  .append('<h2>Please send all cheques to</h2>')
  .append('<p>20 Brookside</p>')
  .append('<p>Hucknall</p>')
  .append('<p>Nottingham </p>')
  .append('<p>NG15 7QJ</p>')
  .append('<p class="space-top">Please make all cheques payable to "H &amp; S Hotfoil" accompanied with your purchase order or purchase order number (sent to you via email)</p>')
  .append('<p class="bold">Please note: Your order will not be processed until your cheque has cleared.</p>');
  jQuery('#cheque_note').hide();
  if(jQuery('#Cheque').is(':checked')){
    jQuery('#cheque_note').slideDown(500);
  }

  jQuery('#Cheque').change(function(){
    jQuery('#cheque_note').slideDown(500);    
  });

  jQuery('#PayPal').change(function(){
    jQuery('#cheque_note').slideUp(500);    
  });

  /////////////////////////////// PRODUCT PAGE TABS
  jQuery(".tab_content").hide();
  jQuery(".tab_content:first").show();
  jQuery("ul.tabs li:first").addClass("active");

  jQuery("ul.tabs li a").click(function() {
    jQuery("ul.tabs li").removeClass("active");
    jQuery(this).parent().addClass('active');

    var currentTab = jQuery(this).attr('href');
    jQuery(".tab_content").hide();
    jQuery(currentTab).show();
    return false;
  });
  /////////////////////////////// CALCULATOR
  function calculate(){
    var quantity = jQuery(".inputboxquantity").val();
    var eachprice = jQuery("#eachprice").val();
    if (quantity >= 25 && quantity <= 49) {
      var pricevalue = eachprice;
      jQuery('#eachpricechange').val(eachprice);
    }
    if (quantity >= 50 && quantity <= 99) {
      var pricevalue = eachprice - 0.80;
      jQuery('#eachpricechange').val(pricevalue.toFixed(2));
    }
    if (quantity >= 100 && quantity <= 174) {
      var pricevalue = eachprice - 1.18;
      jQuery('#eachpricechange').val(pricevalue.toFixed(2));
    }
    if (quantity >= 175 && quantity <= 249) {
      var pricevalue = eachprice - 1.28;
      jQuery('#eachpricechange').val(pricevalue.toFixed(2));
    }
    if (quantity >= 250 && quantity <= 374) {
      var pricevalue = eachprice - 1.38;
      jQuery('#eachpricechange').val(pricevalue.toFixed(2));
    }
    if (quantity >= 375 && quantity <= 499) {
      var pricevalue = eachprice - 1.41;
      jQuery('#eachpricechange').val(pricevalue.toFixed(2));
    }
    if (quantity >= 500 && quantity <= 749) {
      var pricevalue = eachprice - 1.44;
      jQuery('#eachpricechange').val(pricevalue.toFixed(2));
    }
    if (quantity >= 750 && quantity <= 999) {
      var pricevalue = eachprice - 1.46;
      jQuery('#eachpricechange').val(pricevalue.toFixed(2));
    }
    if (quantity >= 999) {
      var pricevalue = eachprice - 1.48;
      jQuery('#eachpricechange').val(pricevalue.toFixed(2));
    }
    var totalprice = quantity * pricevalue;
    var newtotalprice = (totalprice).toFixed(2);
      
    return newtotalprice;   
  }

  jQuery(function(){
    var minorder = parseInt(jQuery('#minorder').text());
    var maxorder = parseInt(jQuery('#maxorder').text());      
    jQuery("#slider3").slider({ 
      value: 10,
      min: minorder,
      max: maxorder,
      step: 5,
      slide: function(event, ui) {
        jQuery(".inputboxquantity").val(ui.value).css({"border-style" : "solid"});
        jQuery("#totalprice").val(calculate());
      }
    });
  });
  jQuery(".inputboxquantity").live('keyup', function(){
    jQuery("#totalprice").val(calculate())
  });

  /////////////////////////////// SHOW & HIDE CART
  jQuery('#cart_contents').hide();
  jQuery('#cart_text').find('a').addClass('show_cart_link');
  jQuery('.show_cart_link').click(function(e){
    e.preventDefault();   
    jQuery('#cart_contents').slideToggle(500, 'easeOutCirc', function(){
      if (jQuery('.show_cart_link').text() == 'Show Cart') {
        jQuery('.show_cart_link').text('Hide Cart');
      }
      else {
        jQuery('.show_cart_link').text('Show Cart');
      }
    });
  });

  jQuery('#testimonials').hide();
  jQuery('#testimonial-link').click(function(e){
    e.preventDefault();
    jQuery('#testimonials').slideToggle(500);
  })

  /*///////////////////////////////////////////////////////////////////////////////*/
  /*                                                     CONTACT FORM & VALIDATION */
  /*///////////////////////////////////////////////////////////////////////////////*/
  jQuery('.validateresponse').hide();
  jQuery('.clear').click(function(){
    jQuery('.validateresponse').hide();
    jQuery('.required, .email').css({'border' : ''});
  });
  jQuery('input.phone').keyup(function(e) {
      if (String.fromCharCode(e.keyCode).match(/[^0-9]/g)){
        jQuery(this).css({'border-color' : '#A31018'});
        jQuery('.validateresponse').fadeIn(500).text('Must be a number');
        return false;       
      }
  });
  jQuery('.send').click(function() {
    // VARIABLES
    var formid = jQuery('#form'),
        regEmail = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/,
        validated = true,
        counter = 0,
        pathname = document.location.href,
        error = jQuery('.validateresponse', formid).fadeIn(500, function(){jQuery(this).animate({left : '+=5px'}, 25).animate({left : '-=5px'}, 25).animate({left : '+=5px'}, 25).animate({left : '-=5px'}, 25);});
    // INPUT REQUIRED VALIDATION
    jQuery('input.required', formid).css({'border-color' : '#AAAAAA #DDDDDD #DDDDDD #AAAAAA'});
    jQuery('input.required', formid).each(function(){
      if (jQuery(this).val().length == 0){
        jQuery(this).css({'border-color' : '#A31018'});
        if (!counter){
          validated = false;
          jQuery(this).focus();
          counter++;
          var field = jQuery(this).prev('label').text().slice(0, -1);
          jQuery(error).text('Please complete the ' + field).append(' field');
          jQuery(this).focus();
        }
      }
      else {        
        jQuery(this).css({'border-color' : ''})
      }
    });

    // SELECT BOX VALIDATION. FIRST OPTION MUST HAVE 'SELECT' VALUE
    jQuery('select.required', formid).each(function(){
      if (jQuery('option:selected', this).val() == 'Select' && validated){
        validated = false;
        jQuery(error).text('Please select your reason for contact');
        jQuery('select.required', formid).css({'border-color' : '#A31018'});
      }
      else {        
        jQuery(this).css({'border-color' : ''})
      }
    });

    // INPUT EMAIL ADDRESS VALIDATION
    jQuery('input.email', formid).each(function(){
      if (!regEmail.test(jQuery('input.email', formid).val()) && validated){
        validated = false;
        jQuery(error).text('Valid email address required')
        jQuery('input.email', formid).css({'border-color' : '#A31018'});
      }
      else {        
        jQuery(this).css({'border-color' : ''})
      }
    });

    // SUCCESS FUNCTION
    if(validated){
      jQuery('.validateresponse').hide();
      jQuery.ajax({
        type: 'POST',
        url: pathname,
        data: jQuery('#form').serialize(),
        success: function() {
            var enquiryname = jQuery('input[name="name"]').val();
            jQuery('#form_wrapper').html('<div id="message"></div>');
            jQuery('#message').html('<h2 class="enquiry_title"><strong>Thanks for your enquiry ' + enquiryname)
            .append('<p class="enquiry_body">We will be in touch soon</p>').hide().fadeIn(1000);   
          }
      });
    }
  return false;
  });

  
//////////////////////////////////////////// END DOCUMENT READY
});

/////////////////////////////////////////////////////////////// 
//                                                       EASING
///////////////////////////////////////////////////////////////
jQuery.extend( jQuery.easing,
{
  easeInQuad: function (x, t, b, c, d) {
    return c*(t/=d)*t + b;
  },
  easeOutQuad: function (x, t, b, c, d) {
    return -c *(t/=d)*(t-2) + b;
  },
  easeInOutQuad: function (x, t, b, c, d) {
    if ((t/=d/2) < 1) return c/2*t*t + b;
    return -c/2 * ((--t)*(t-2) - 1) + b;
  },
  easeInCubic: function (x, t, b, c, d) {
    return c*(t/=d)*t*t + b;
  },
  easeOutCubic: function (x, t, b, c, d) {
    return c*((t=t/d-1)*t*t + 1) + b;
  },
  easeInOutCubic: function (x, t, b, c, d) {
    if ((t/=d/2) < 1) return c/2*t*t*t + b;
    return c/2*((t-=2)*t*t + 2) + b;
  },
  easeInQuart: function (x, t, b, c, d) {
    return c*(t/=d)*t*t*t + b;
  },
  easeOutQuart: function (x, t, b, c, d) {
    return -c * ((t=t/d-1)*t*t*t - 1) + b;
  },
  easeInOutQuart: function (x, t, b, c, d) {
    if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
    return -c/2 * ((t-=2)*t*t*t - 2) + b;
  },
  easeInQuint: function (x, t, b, c, d) {
    return c*(t/=d)*t*t*t*t + b;
  },
  easeOutQuint: function (x, t, b, c, d) {
    return c*((t=t/d-1)*t*t*t*t + 1) + b;
  },
  easeInOutQuint: function (x, t, b, c, d) {
    if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
    return c/2*((t-=2)*t*t*t*t + 2) + b;
  },
  easeInSine: function (x, t, b, c, d) {
    return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
  },
  easeOutSine: function (x, t, b, c, d) {
    return c * Math.sin(t/d * (Math.PI/2)) + b;
  },
  easeInOutSine: function (x, t, b, c, d) {
    return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
  },
  easeInExpo: function (x, t, b, c, d) {
    return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
  },
  easeOutExpo: function (x, t, b, c, d) {
    return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
  },
  easeInOutExpo: function (x, t, b, c, d) {
    if (t==0) return b;
    if (t==d) return b+c;
    if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
    return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
  },
  easeInCirc: function (x, t, b, c, d) {
    return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
  },
  easeOutCirc: function (x, t, b, c, d) {
    return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
  },
  easeInOutCirc: function (x, t, b, c, d) {
    if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
    return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
  },
  easeInElastic: function (x, t, b, c, d) {
    var s=1.70158;var p=0;var a=c;
    if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
    if (a < Math.abs(c)) { a=c; var s=p/4; }
    else var s = p/(2*Math.PI) * Math.asin (c/a);
    return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
  },
  easeOutElastic: function (x, t, b, c, d) {
    var s=1.70158;var p=0;var a=c;
    if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
    if (a < Math.abs(c)) { a=c; var s=p/4; }
    else var s = p/(2*Math.PI) * Math.asin (c/a);
    return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
  },
  easeInOutElastic: function (x, t, b, c, d) {
    var s=1.70158;var p=0;var a=c;
    if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5);
    if (a < Math.abs(c)) { a=c; var s=p/4; }
    else var s = p/(2*Math.PI) * Math.asin (c/a);
    if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
    return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
  },
  easeInBack: function (x, t, b, c, d, s) {
    if (s == undefined) s = 1.70158;
    return c*(t/=d)*t*((s+1)*t - s) + b;
  },
  easeOutBack: function (x, t, b, c, d, s) {
    if (s == undefined) s = 1.70158;
    return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
  },
  easeInOutBack: function (x, t, b, c, d, s) {
    if (s == undefined) s = 1.70158; 
    if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
    return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
  },
  easeInBounce: function (x, t, b, c, d) {
    return c - jQuery.easing.easeOutBounce (x, d-t, 0, c, d) + b;
  },
  easeOutBounce: function (x, t, b, c, d) {
    if ((t/=d) < (1/2.75)) {
      return c*(7.5625*t*t) + b;
    } else if (t < (2/2.75)) {
      return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
    } else if (t < (2.5/2.75)) {
      return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
    } else {
      return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
    }
  },
  easeInOutBounce: function (x, t, b, c, d) {
    if (t < d/2) return jQuery.easing.easeInBounce (x, t*2, 0, c, d) * .5 + b;
    return jQuery.easing.easeOutBounce (x, t*2-d, 0, c, d) * .5 + c*.5 + b;
  }
});
