(function($) {

	   $.fn.transictionto = function(options) {
	      var settings = $.extend({
	   }, options || {});
	   //wrap into div if no div is present.
	   $(this).each(function() {
	      if ($(this).parent('div').size() == 0) {
	         $(this).wrap('<div></div>')
	      }
	      //now swap with background trick
	      $(this)
	      .parent()
	         .css('background-image', 'url(' + settings.destinationImage + ')')
	         .css('background-repeat', 'no-repeat')
	         .css('background-position', 'top left')
	         .css('background-color', 'transparent')
	      .end()
	      .fadeOut(1000, function() {
	         this.src = settings.destinationImage;
	         $(this).show();
	      });
	   });
	};
	
	$.errors = function(){

		$('span.tooltip').each(function(i){
			var parent = $('ul.errors li',$(this).parent().get(0));
			var element = $('input',$(this).parent().get(0));
			$(this).qtip({
				content: {
						text: parent
					},
					show: 'mouseover',
					hide: 'mouseout',
					position : {corner: { tooltip : 'bottomLeft' , target : 'topRight'} },
					style : {name: 'red',tip: 'bottomLeft'}
				});

			$(this).qtip({
				content: {
						text: parent
					},
					show: {when:{ target:element,event:'focus'}},
					hide: {when:{ target:element,event:'blur'}},
					position : {corner: { tooltip : 'bottomLeft' , target : 'topRight'} },
					style : {name: 'red',tip: 'bottomLeft'}
				});
			
			});
		}
	
	$.getAddress = function () {
        var num = encodeURIComponent($('input#address1').val());
        var pc = encodeURIComponent($('input#postcode').val());
        if(!$('#pcl').get(0)){
        	$.initQAS();
        }
        $('#pcl').load('/index/getaddress/lookupType/pc/num/' + num + '/postcode/' + pc + '/task/search');
        $('#pcl').dialog('open');
    }

	$.initQAS = function(){
		$("form a").each(function(){
			$(this).bind("click", function(e){
				$('#pcl').load('/index/getaddress/task/addLookup');
				$('#pcl').dialog('open');
				return false;
			});
		})

		$('#container').append($('<div>').attr('id','pcl'));
		$('#pcl').append($('<div>').addClass('content'));
		$('#pcl').dialog({modal:true,title:'ADDRESS SEARCH',width:676,resizable:false, minHeight:1,autoOpen: false});

	}
	
	$.showOffers = function (utype) {
        $('#popup-offers').html('<img src="/assets/images/vx_offers_'+utype+'.png" usemap="#m_01" /><map name="m_01" id="m_01"><area shape="rect" coords="272,559,354,590" href="/retailerlocator/" /></map>');
        $('#popup-offers').dialog({
        	modal:false, 
        	title:'VAUXHALL PARTNERS SPECIAL OFFERS',
        	dialogClass: 'offerPopup',
        	width:700, 
        	resizable:false, 
        	minHeight:100,
        	autoOpen: true,
        	close: function(event, ui) { $('#popup-offers').dialog('destroy'); }
        	});
    }
	
	$.showSwappage = function (utype) {
        $('#popup-offers').html('<img src="/assets/images/vx_swappage_'+utype+'.jpg" usemap="#m_01" /><map name="m_01" id="m_01"><area shape="rect" coords="234,302,296,320" href="/retailerlocator/" /></map>');
        $('#popup-offers').dialog({
        	modal:false, 
        	title:'VAUXHALL SWAPPAGE',
        	dialogClass: 'offerPopup',
        	width:646, 
        	resizable:false, 
        	minHeight:100,
        	autoOpen: true,
        	close: function(event, ui) { $('#popup-offers').dialog('destroy'); }
        	});
    }
	
})(jQuery);

function sendData(e) {
    if(e == 'add') {
        var num = encodeURIComponent($('input#LUnum').val());
        var street = encodeURIComponent($('input#LUstreet').val());
        var town = encodeURIComponent($('input#LUtown').val());
        $('#pcl').load('/index/getaddress/task/search/lookupType/add/num/' + num + '/street/' + street + '/town/' + town);
    } else {
        var num = encodeURIComponent($('input#LUnum').val());
        var postcode = encodeURIComponent($('input#LUpostcode').val());
        $('#pcl').load('/index/getaddress/task/search/lookupType/pc/num/' + num + '/postcode/' + postcode);
    }
}

jQuery.fn.labelOver = function() {
	var $bound = false;
	this.each(function() {
		var $label = $(this);
		var $target = $('#' + $label.attr('for'));
		
		if (!$bound) {
			var $parent = $label.parents('form').get(0).id;
			$('form#' + $parent).bind('submit', function() {
				$inputs = $('form#' + $parent + ' input');
				$inputs.each(function() { 
					var $theInput = $(this);
					var $myLabel = $('label[for=' + $theInput.attr('id') + ']');
					if ($myLabel.hasClass('overlaid')) {
						if ($theInput.val() == $myLabel.html()) {
							$theInput.val('');
						}
					}
				});
			});
			$bound = true;
		}
		
		$label.css({ width: '0px', height: '0px', overflow: 'hidden', display: 'block' }).addClass('overlaid');
		if ($target.val() == '') { $target.val($label.html()); }
		$target
			.bind('focus', function($e) {
				var $thisLabel = $('label[for=' + $(this).attr('id') + ']');
				if ($(this).val() == $thisLabel.html()) { 
					$(this).val('');
				}
			})
			.bind('blur', function($e) {
				var $thisLabel = $('label[for=' + $(this).attr('id') + ']');
				if ($(this).val() == '') { 
					$(this).val($thisLabel.html());
				}
			});
	});
}
