var noscroll;
noscroll = 0;
$.fn.reorder = function() {
  function randOrd() { return(Math.round(Math.random())-0.5); }
  return($(this).each(function() {
    var $this = $(this);
    var $children = $this.children();
    var childCount = $children.length;
    if (childCount > 1) {
      $children.remove();
      var indices = new Array();
      for (i=0;i<childCount;i++) { indices[indices.length] = i; }
      indices = indices.sort(randOrd);
      $.each(indices,function(j,k) { $this.append($children.eq(k)); });
    }
  }));
}
   $(document).ready(function(){
    var currentPosition = 0;
    var slideWidth = 330;
    var slides = $('.msg');
    var numberOfSlides = slides.length;
  
    $('#msgContainer').css('overflow', 'hidden');
  
    slides
    .wrapAll('<div id="msgInner"></div>')
    .css({
      'float' : 'left',
      'width' : slideWidth
    });
  
    $('#msgInner').css('width', slideWidth * numberOfSlides);

    $(".sform").submit(function(){
      currentPosition = currentPosition+1;
      $('#msgInner').animate({
        'marginLeft' : slideWidth*(-currentPosition)
      });
    });
    
    
    
    $("#subtotal").text('$0');
    
    $(".sbsup").click(function(){
     newv = $(this).parent().parent().attr("id");
     price = parseFloat(newv.substr(1,9));
     newv = "#" + newv.substr(10);
     $("#subtotal").text('$' + (Math.round((parseFloat($("#subtotal").text().substr(1)) + price)*100)/100).toFixed(2) );
     $(newv).val(parseInt($(newv).val()) + 1);
    });
    
    $(".sbsdown").click(function(){
     newv = $(this).parent().parent().attr("id");
     price = parseFloat(newv.substr(1,9));
     newv = "#" + newv.substr(10);
     if(parseInt($(newv).val()) > 0){
      $("#subtotal").text('$' + (Math.round((parseFloat($("#subtotal").text().substr(1)) - price)*100)/100).toFixed(2) );
      $(newv).val(parseInt($(newv).val()) - 1);
     };
    });
    
    $("#submitcart").click(function(){
     var url = 'http://www.hot.com.au/cart/add/';
     $(".qty-text").each(function(i) {
      if(parseInt($(this).val()) > 0){
       url = url + "p" + $(this).attr("id") + "_q" + $(this).val() + "-";
      };
     });
      
     if(url.length > 31){
      window.location = url + '?destination=cart';
     }else{
      window.alert("Sorry, there are no items to add to your cart");
     }
    });
    
    $("#submitupsell").click(function(){
     var url = 'http://www.hot.com.au/cart/add/';
     if($("#avgupsell").val() == 's1'){
      url = url + "p240560_q1-";
     };
     if($("#avgupsell").val() == 's2'){
      url = url + "p201978_q1-";
     };
     if($("#avgupsell").val() == 's3'){
      url = url + "p201980_q1-";
     };
     if($("#avgupsell").val() == 's4'){
      url = url + "p201981_q1-";
     };
     if($("#avgupsell").val() == 's5'){
      url = url + "p201982_q1-";
     };
     if($("#avgupsell").val() == 's6'){
      url = url + "p201984_q1-";
     };
      
     if(url.length > 31){
      window.location = url + '?destination=cart';
     }else{
      window.alert("Sorry, there are no items to add to your cart");
     }
    });
    
    $(".qty-text").each(function(i) {
     if(parseFloat(($(this).parent().attr("id")).substr(1,9)) > 3999.99){
      $(this).parent().parent().next().html('<a href="/contact"><img src="http://cdn.hot.com.au/quote.gif" style="margin-top:1px;" alt="Get Quote" title="Click for a fast quote!" border="0" /></a>');
     };
     if(parseInt($(this).val()) > 0){
      price = parseFloat(($(this).parent().attr("id")).substr(1,9));
      $("#subtotal").text('$' + (Math.round((parseFloat($("#subtotal").text().substr(1)) + price)*100)/100).toFixed(2) );
     };
    });
    
    $("#content").mouseover(
      function(){
        $('#usermenu').css('display', 'none');
        $('#contactmenu').css('display', 'none');
        $("#shop a").css({backgroundPosition:'-41px 0px'});
        $("#home a").css({backgroundPosition:'-0px 0px'});
    });
    
    $("#shop").mouseover(
      function(){
       $('#contactmenu').css('display', 'none');
       $('#usermenu').css('display', 'block');
       $('#usermenu').fadeIn();
       $("#shop a").css({backgroundPosition:'-41px -34px'});
       $("#home a").css({backgroundPosition:'-0px 0px'});
    });
      
      $("#home").mouseover(
       function(){
        $('#usermenu').css('display', 'none');
        $('#contactmenu').css('display', 'block');
        $('#contactmenu').fadeIn();
        $("#home a").css({backgroundPosition:'-0px -34px'});
        $("#shop a").css({backgroundPosition:'-41px 0px'});
      });
      
      $('.view-content-danepromo').mouseover(function(){
        noscroll = 1;
      });
      
      $('.view-content-danepromo').mouseout(function(){
        noscroll = 0;
      });
      
      $('.prevPage').css('opacity', 0.2);
      
      $('.nextPage').click(function(){noscroll = 0;nextPage();});
      
      $('.prevPage').click(function(){
        noscroll = 0;
        var lefty;
        lefty = '0px';
        if (noscroll != 1) {
          lefty = $('#infinite .items').css('left');
          lefty = parseInt(lefty.substr(0,(lefty.length - 2)));
          
          if(lefty>=0){
            lefty = '0px';
          }else{
            if(lefty % 185 == 0){ //At interval?
              lefty = (lefty + 185) + 'px';
            }else{
              if((lefty%185)<-92){
                lefty = (lefty -((lefty%185))+185) + 'px';
              }else{
                lefty = lefty +-((lefty%185)%185) + 'px';
              }
            }
          };
          
          if(lefty=='0px'){
            $('.prevPage').css('opacity', 0.2).css('filter','alpha(opacity=20)');
          }else{
            $('.nextPage').css('opacity', 1).css('filter','alpha(opacity=100)');
          }
          
          $('#infinite .items').stop().animate({left:lefty});
        }
      });
      
   });
  function nextPage( ){
    var lefty;
    lefty = '0px';
    if (noscroll != 1) {
      lefty = $('#infinite .items').css('left');
      lefty = parseInt(lefty.substr(0,(lefty.length - 2)));
      
      if(lefty<=-925){
        lefty = '-925px';
      }else{
        if(lefty == 0){ //At start?
          lefty = (lefty - 185) + 'px';
        }else{
          if(lefty % 185 == 0){ //At interval?
            lefty = (lefty - 185) + 'px';
          }else{
            if((lefty%185)<-92){
              lefty = (lefty -((lefty%185))+185) + 'px';
            }else{
              lefty = lefty +-((lefty%185)%185) + 'px';
            }
          }
        }
      };
      
      if(lefty=='-925px'){
        $('.nextPage').css('opacity', 0.2).css('filter','alpha(opacity=20)');
      }else{
        $('.prevPage').css('opacity', 1).css('filter','alpha(opacity=100)');
      }
      
      $('#infinite .items').stop().animate({left:lefty});
    };
    
  };
  
  function rotate(){
    var lefty;
    lefty = '0px';
    lefty = $('#infinite .items').css('left');
    lefty = parseInt(lefty.substr(0,(lefty.length - 2)));
    
      if(lefty<=-925){
        $('#infinite .items').stop().css({left:'0px'});
        $('.prevPage').css('opacity', 0.2).css('filter','alpha(opacity=20)');
        $('.nextPage').css('opacity', 1).css('filter','alpha(opacity=100)');
      } else {
        nextPage();
      }
  }