$(document).ready(function() {
  
  // dummysubmit forms
  $('form.dummysubmit').each(function() {

    $(this).find('input[type="image"], input[type="submit"]').click(function(e) {
      
      e.preventDefault();
      
      $(this).parents('form').find('input.dummyinput').each(function() {
        
        if($(this).val() == $(this).attr('title')) {
          $(this).attr('value','');
        }
        
      });
      
      // now submit
      $(this).parents('form').submit();
      
    });
    
  });
  
  
  // multi parcel plus button
  $('.dims_row .plus').click(function(e) {
    e.preventDefault();
    
    var row=$(this).parent();
    
    var url = '/quotev2.php?prefill=' + row.find('input[name^="weight"]').val() + '-' + row.find('input[name^="length"]').val() + '-' + row.find('input[name^="width"]').val() + '-' + row.find('input[name^="height"]').val() + '-' + row.parent().find('*[name="destination"]').val();
    window.location=url;
         
  });
  
  // Service selector
  $('select.services').change(function() {
    
    var newlocation=$(this).val();

    if(newlocation != '') {
      window.location=newlocation;
    }
    
  });
  
  // Emptyblank
  $('.emptyBlank').each(function() {

	var defarr=$(this).attr('value');
	defarr=defarr.split('---');


	if(defarr != $(this).val()) {

		var def=defarr[0];
		$(this).val(defarr[1]);

	}else{
		var def=$(this).attr('value');
	}

    if($(this).val() == def) {
    	$(this).css('color','#666');
    }

    $(this).focus(function() {
      if($(this).attr('value') == def){
        $(this).attr('value','');
        $(this).css('color','#333');
      }
    });

    $(this).blur(function() {
      if($(this).attr('value').length ==0){
        $(this).attr('value',def);
        $(this).css('color','#666');
      }
    });

  });
  
  // Price expander
  $('.price-expander').each(function() {
    
    var divs=$(this).find('div');
    
    var i=0;
    
    divs.each(function() {
      
      if(i > 10) {
        $(this).hide();
      }
      i=i+1;
      
    });
        
  });
  
  $('.price-expander-more').click(function() {
    
    var expander=$(this).prev();
    var divs=expander.find('div');
    
    var i=0;
    divs.each(function(){
            
      if($(this).is(':hidden')) {
        i = i + 1;
      }else{
        // not hidden
      }
      
      if((i > 0) && (i < 10)) {        
        $(this).show();
      }
      
    });
    
  });
  
  // jumpMenu
  $('.jumpMenu').change(function() {
    
    var newlocation=$(this).val();

    if(newlocation != '') {
      window.location=newlocation;
    }
    
  });
  
  // facebox
  $('.facebox').facebox();
  
  // slideshow
  $('.slideshow1').cycle({
    timeout: 5000,
    speed: 320
  });
  
  $('.slideshow2').cycle({
    timeout: 5000,
    speed: 320,
    delay:50
  });
  
  $('.slideshow3').cycle({
    timeout: 5000,
    speed: 320,
    delay: 70
  });
  
  $('.slideshow4').cycle({
    timeout: 5000,
    speed: 320,
    delay: 90
  });
  
  $('.pagedbox').cycle({
    fx: 'scrollHorz',
    speed: 300,
    timeout: 3500,
    pause: 1,
    pager: '#pagedbox_nav',
    pagerAnchorBuilder: function(index, el) {
      return '<a href="#">&bull;</a>'; // whatever markup you want
    }
    
  });
  
  // Quote box tabs
  
  $('#tab_left').click(function(e) {
    e.preventDefault();
    $('#tab_left').addClass('on');
    $('#tab_right').removeClass('on');    
    $('#ukquote').show();
    $('#intlquote').hide();
    window.location.hash = 'uk';
  });
  
  $('#tab_right').click(function(e) {
    e.preventDefault();
    $('#tab_right').addClass('on');
    $('#tab_left').removeClass('on');
    $('#ukquote').hide();
    $('#intlquote').show(); 
    window.location.hash = 'international';  
  });
  
  // Check the hash
  if(window.location.hash == '#international') {
    $('#tab_right').click();
  }
  
  
  // grey text input handling
  
  $('.dummyinput').each(function() {            
     if($(this).val() == '') {
       $(this).attr('value',$(this).attr('title')); 
     }
     
     if($(this).val() != $(this).attr('title')) {
       $(this).removeClass('off');
     }
     
  });
  
  $('.dummyinput').livequery('focus',function() {
     
     
     if($(this).hasClass('.hiddeninput')) {
       $(this).hide();
       $(this).next().show().focus();
       
       $(this).next().blur(function() {
                  
         if($(this).val() == '') {
           
           $(this).hide();
           $(this).prev('input').show();
         }
       });
       
     } else {
       
       if($(this).attr('value') == $(this).attr('title')) {
          $(this).attr('value','');         
        }
        $(this).removeClass('off');
       
     }
     
   });
   
   
   $('.dummyinput').livequery('blur',function() {
      if($(this).val() == '') {
        $(this).attr('value',$(this).attr('title'));
        $(this).addClass('off');        
      }
    });
  
  
  // numeric only fields
  $('.isnumeric').keyup(function() {
    
    var val=$(this).val();
    
    
    if(val.length > 0) {
      
      var lastdigit=val.substr((val.length -1),1);
      if(!isNumber(lastdigit) && (lastdigit.substr(0,1) != '.')) {
        $(this).val(val.substr(0,(val.length -1)));
      }
      
    }
    
  });
  
  
  // Expander
  $('.expander_trigger').click(function(e) {
    e.preventDefault();
    $('.pagedbox').cycle('destroy').css('height','auto');
    $('.pagedbox').find('div').css('position','').css('height','auto');
    $('#pagedbox_nav').hide();
    $(this).next('.expander').show();
    $(this).hide();
  });
  
  
  // flags
  $('#intlflags a.facebox img').css('opacity',0.45);
  
  
  
  
  
  
});

function isNumber(n) {
  return !isNaN(parseFloat(n)) && isFinite(n);
}

function openwindow(url,name,width,height) {
  var blocker=window.open (url,name,"status=no,width=" + width + ",height=" + height + ",location=no,menubar=no,resizable=yes,scrollbars=yes,directories=no,toolbar=no");

  if(!blocker) {
    alert('Please turn off your popup blocker');
  }

  if (window.focus) {blocker.focus()}

}
