$(document).ready(function() {
  
  $(".obf").each(function(){  
    var self = $(this);
    var email = this.tagName.toLowerCase() == 'a' ? self.attr('href') : self.html();
    email = email.replace(/[a-zA-Z]/g, function(c){return String.fromCharCode((c<="Z"?90:122)>=(c=c.charCodeAt(0)+13)?c:c-26);});
    if(this.tagName.toLowerCase() == 'a'){
      if(self.html() == self.attr('href')){
        self.html(email);
      }
      self.attr('href','mailto:'+email);
    }else{
      self.html(email)
    }
  });
  
  $("#nav .li1").hover(
    function () {
      $(this).addClass("hover");
    },
    function () {
      $(this).removeClass("hover");
    }
  );
  
  if($('#left').height() < $('#content').height()){
    $('#left').height($('#content').height())
  }
  $('#add-to-bookmarks').jFav();
  $('#send-to-friend').click(function(e){
    e.preventDefault()
    var email = prompt('Send page address to a Friend:', 'Enter email address of recipient')
    
    if(email != null){
      var emailReg = /^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/
      if(emailReg.test(email)){
        window.location = "mailto:"+email+"?subject=Take a look at this"+"&body=Hi,\n You may want to check out this site: "+window.location;
      }else{
        alert('The email is inalid!')
      }
    }
    
  })
  
    
  $('#subscribe-form').validate();
  $('#enquiry-form').validate();
  $('#quick-enquiry-form').validate();
  $('#lightbox-form').validate()
  
  
  $('#quick-links-trigger').toggle(function(e){
    e.preventDefault()
    $('#quick-links').show()
    
    //$('html, body').animate({ scrollTop: $('#quick-links').offset().top   }, 1500);
    $($.browser.webkit ? 'body' : 'html').animate({ scrollTop: $(document).height()   }, 1500);
    //$('body').animate({ scrollTop: $(document).height()   }, 1500);
    $(this).addClass('opened')
  }, function(e){
    $('#quick-links').slideUp(500)
    $(this).removeClass('opened')
  })
  
  
  // if($.browser.msie){
  //   $('select').focus(function(e){
  //     $(this).data("origWidth", $(this).css("width")).css("width", "auto");
  //   }).bind('blur mouseup', function(e){
  //     $(this).css("width", $(this).data("origWidth"));
  //   });
  // }
  
  if($.browser.msie && $.browser.version < 9){
    $('iframe').each(function(){
      if (this.src.indexOf('youtube') > -1) {
        this.src += (this.src.indexOf('?') > -1 ? '&' : '?') + 'wmode=transparent';
      };
      
    })
  }
  
  
  
  
  
  $('#query-form').submit(function(e){
    var q = $('#search_keywords')
    if(q.val() == '' || q.hasClass('empty')){
      q.focus()
      return false
    }
  })
  
  $('#query_keywords').focus(function(e){
      $this = $(this)
      if($this.autocomplete('option', 'disabled')){
        $.getJSON( "/products/autocomplete.json", {}, function( data, status, xhr ) {
          $this.autocomplete('option', 'source', data).autocomplete('enable')
        });
      }
      
      $(this).filter('[value="SEARCH BY KEYWORD"]').val('').removeClass('empty')
  }).blur(function(e){
    //$(this).filter('[value=""], [value="SEARCH BY KEYWORD"]').val('SEARCH BY KEYWORD').addClass('empty')
  }).blur().autocomplete({
      minLength: 3,
      disabled: true  
    });
  
  $flash = $('#flash').click(function(e){$flash.stop(true, true).remove()});
  for(i=0; i<=5; i++){
    $flash.animate({opacity:0.6}, 250).animate({opacity:1}, 250).delay(1500)
  }
  $flash.fadeOut(3000)
  //setTimeout(function() { $('.flash').fadeOut(3000); }, 5000);
  
  $(".tooltip").tipTip({delay:0});
  
  
  
});




(function($){  
  $.fn.extend({   
    //plugin name - animatemenu  
    rotateProducts: function(options) {  

      var defaults = {  
          products: [],
          time: 3000,
          path: ''
      };  
      
      var options = $.extend(defaults, options);  
  
      return this.each(function() {  
        var o = options;
        var element = $(this);
        var current = 0
        
        element.find('.nav a').click(function(e){
          e.preventDefault()
          current += $(e.target).hasClass('fright') > -1 ? 1 : -1;
          current = current >= o.products.length ? 0 : (current < 0 ? o.products.length - 1 : current) 
          var p = o.products[current]
          element.find('.image').attr('href', p.url).children('img').attr({src: o.path+p.image, alt: p.title})
          element.find('.title').attr('href', p.url).html(p.title)
        })

        element.data('over', false)
        .hover(function(e){
          element.data('over', true)
        }, function(e){
          element.data('over', false)
        })
        
        function rotate(){
          if(!element.data('over')){
            element.find('.nav .fright').click()
          }
        }
        setInterval(rotate, o.time, element)
      });  
    }  
  });  
})(jQuery);

