/*Labelify for placeholders*/
/*Put labels inside input fields, i.e. on track and trace form on home page*/
jQuery.fn.labelify = function(settings) {
  settings = jQuery.extend({
    text: "title",
    labelledClass: "placeholder"
  }, settings);
  var lookups = {
    title: function(input) {
      return $(input).attr("title");
    },
    label: function(input) {
      return $("label[for=" + input.id +"]").text();
    }
  };
  var lookup;
  var jQuery_labellified_elements = $(this);
  return $(this).each(function() {
    if (typeof settings.text === "string") {
      lookup = lookups[settings.text]; // what if not there?
    } else {
      lookup = settings.text; // what if not a fn?
    };
    // bail if lookup isn't a function or if it returns undefined
    if (typeof lookup !== "function") { return; }
    var lookupval = lookup(this);
    if (!lookupval) { return; }

    // need to strip newlines because the browser strips them
    // if you set textbox.value to a string containing them    
    $(this).data("label",lookup(this).replace(/\n/g,''));
    $(this).focus(function() {
      if (this.value === $(this).data("label")) {
        this.value = this.defaultValue;
        $(this).removeClass(settings.labelledClass);
      }
    }).blur(function(){
      if (this.value === this.defaultValue) {
        this.value = $(this).data("label");
        $(this).addClass(settings.labelledClass);
      }
    });
    
    var removeValuesOnExit = function() {
      jQuery_labellified_elements.each(function(){
        if (this.value === $(this).data("label")) {
          this.value = this.defaultValue;
          $(this).removeClass(settings.labelledClass);
        }
      })
    };
    
    $(this).parents("form").submit(removeValuesOnExit);
    $(window).unload(removeValuesOnExit);
    
    if (this.value !== this.defaultValue) {
      // user already started typing; don't overwrite their work!
      return;
    }
    // actually set the value
    this.value = $(this).data("label");
    $(this).addClass(settings.labelledClass);

  });
};


$(document).ready( function() {
		/*Zebra stripe standard table */
	   	$(".zebra tr").mouseover(function(){$(this).addClass("over");}).mouseout(function(){$(this).removeClass("over");});
   		$(".zebra tr:even").addClass("alt");
		/*Clickable table rows */
	    $('.clickable_rows tr').click(function() {
   		    var href = $(this).find("a").attr("href");
	        if(href) {
       		    window.location = href;
        	}
	    });
	    $('.clickable_rows tr').find("a").parent().hide();
		/*Replace all submits with jquery ui buttons */
		$("input[type=submit],[type=button]").button();
		$("button").button();
		$("a.btn").button();
				
		/*Change Track & trace form on home page on page load*/
		/*Hide and remove elements*/
		$(".frontpage .boxcontent br").remove();
		$(".frontpage .boxcontent form[name='searchtxt']").hide();
		$(".boxcontent label").hide();
		//$(".boxcontent label[for='ref']").hide();
		/*Style other elements differently*/
		$(".frontpage .boxcontent:first").css({'min-height' : '120px'});
		$(".frontpage .boxcontent form input.input_field").css({'width' : '135px'});
		$(".frontpage .boxcontent form input[type='submit']").css({'float' : 'right', 'margin-top' : '43px'});
		/*Create radio button for search types*/
		/*$('.boxcontent form[action*="/SOPS"]').appendTo('<div class="radio"><input type="radio" id="tollpost_search" value="tp" name="searchtype"  checked="checked"> <label for="tollpost_search">Tollpost Globe</label></div>');
		$(".boxcontent form[action*='/SOPS']").appendTo('<div class="radio"><input type="radio" id="mypack_search" value="mp" name="searchtype"> <label for="mypack_search">MyPack</label></div>');
		$(".boxcontent form[action*='/SOPS']").appendTo('<div class="radio"><input type="radio" id="dpd_search" value="dpd" name="searchtype"> <label for="dpd_search">DPD utland</label></div>');
		$('.frontpage .boxcontent:first form').insertAfter('<div class="radio"><input type="radio" id="tollpost_search" value="tp" name="searchtype"  checked="checked"> <label for="tollpost_search">Tollpost Globe</label></div>');*/
		$('<div class="radio"><input type="radio" id="dpd_search" value="dpd" name="searchtype"> <label for="dpd_search">DPD utland</label></div>').insertAfter('.frontpage .boxcontent:first form[action*="dpd"]');
		$('<div class="radio"><input type="radio" id="mypack_search" value="tp" name="searchtype"> <label for="mypack_search">MyPack</label></div>').insertAfter('.frontpage .boxcontent:first form[action*="dpd"]');
		$('<div class="radio"><input type="radio" id="tollpost_search" value="tp" name="searchtype"  checked="checked"> <label for="tollpost_search">Tollpost Globe</label></div>').insertAfter('.frontpage .boxcontent:first form[action*="dpd"]');

		/*Change search form from tollpost to dpd on radio button click on DPD utland*/
		$('#dpd_search').click(function() {
		  		$(".boxcontent form").hide();
		  		$(".boxcontent form[name='searchtxt']").show();
		});
		/*Change search form to tollpost form on radio button click on Tollpost Globe or MyPack*/
		$('#tollpost_search').click(function() {
		  		$(".boxcontent form").show();
		  		$(".boxcontent form[name='searchtxt']").hide();
		});
		$('#mypack_search').click(function() {
		  		$(".boxcontent form").show();
		  		$(".boxcontent form[name='searchtxt']").hide();
		});
		
		/*Also change layout on MyPack page*/
		$(".mypack .boxcontent form input.input_field").css({'width' : '135px'});
		$(".mypack .boxcontent form input[type='submit']").css({'margin' : '10px 0 0 103px'});

		/*Call labelify for search field and hide label  (placeholders)*/
		$(".boxcontent form[action*='/SOPS'] input.input_field").labelify({
  			text: function(input) { return $(".boxcontent label[for='ref']").html(); }
		});
		$(".boxcontent form[action*='/PLUS'] input.input_field").labelify({
  			text: function(input) { return $(".boxcontent label[for='DZipCode']").html(); }
		});
	} 
); 



