Epson.Registration =
{
	setup: function()
	{
		Epson.Registration.LiveSearch.height = jQuery( '#RegistrationSearch' ).height();
		Epson.Registration.LiveSearch.url = jQuery( '#pr-url' ).val();
		Epson.Registration.LiveSearch.field = jQuery( '#RegistrationSearchField' );
		Epson.Registration.LiveSearch.field.bind( 'keyup', Epson.Registration.LiveSearch.listen );
		if ( document.addEventListener ) Epson.Registration.LiveSearch.field[0].addEventListener( 'blur', function() { setTimeout( Epson.Registration.LiveSearch.close, 500 ) }, false );
		else if ( document.attachEvent ) jQuery('body').bind('click', function() {setTimeout( Epson.Registration.LiveSearch.close, 500 ) });

		if ( jQuery( '#RegistrationProduct.visible' ).length ) jQuery( '#RegistrationSearch' ).css( { height:0, display:'none' } );

		jQuery( '#RegistrationProduct a.button' ).bind( 'click', Epson.Registration.Product.addMore );		
		jQuery( '.pr-change' ).bind( 'click', Epson.Registration.Product.change );
		jQuery( '.pr-remove' ).bind( 'click', Epson.Registration.Product.remove );

		jQuery('#ProductReg').bind('submit', function() 
		{ 
			// check if livesearch field is more than 2 and is not blank
			if (( Epson.Registration.LiveSearch.field.val().length > 2 ) && ( Epson.Registration.LiveSearch.field.val() != Epson.Registration.LiveSearch.field.prev().text())) { Epson.Registration.LiveSearch.submitSearch() ; return false }
			else return true;		
		});
	},

	LiveSearch:
	{
		url:null,
		query:'',
		timer:null,
		field:null,
		dropdown:null,
		opened:false,
		height:0,
		results:null,

		listen: function(e)
		{
			clearTimeout( Epson.Registration.LiveSearch.timer );
	
			Epson.Registration.LiveSearch.query = Epson.Registration.LiveSearch.field.val();
	
			if ( e.keyCode == 40 && Epson.Registration.LiveSearch.dropdown ) Epson.Registration.LiveSearch.downSuggestions(e);
			else if ( e.keyCode == 38 && Epson.Registration.LiveSearch.dropdown ) Epson.Registration.LiveSearch.upSuggestions(e);
			else if ( e.keyCode != 13 && Epson.Registration.LiveSearch.query.length > 2 ) { Epson.Registration.LiveSearch.displayLoader(); Epson.Registration.LiveSearch.timer = setTimeout( function() { Epson.Registration.LiveSearch.suggest(e) }, 300 ); }
			else if ( e.keyCode == 13 ) { Epson.Registration.LiveSearch.submitSearch();  e.preventDefault();}
			else Epson.Registration.LiveSearch.close();
		},

		suggest: function()
		{
			
			jQuery.getJSON( Epson.Registration.LiveSearch.url, { query:encodeURI(Epson.Registration.LiveSearch.query) }, Epson.Registration.LiveSearch.renderSuggestions );

			if ( !Epson.Registration.LiveSearch.opened ) Epson.Registration.LiveSearch.open();
		},

		renderSuggestions: function( json )
		{
			if ( Epson.Registration.LiveSearch.opened )
			{
				
				// empty the content to prevent repeating
				Epson.Registration.LiveSearch.dropdown.find( 'ul' ).empty();

				//add the data to the html
				if ( json.results && json.results.length )
				{
					
					Epson.Registration.LiveSearch.results = json.results;

					jQuery.each( json.results, function()
					{
						Epson.Registration.LiveSearch.dropdown.find( '.empty' ).remove();
						
						var data = this;
						Epson.Registration.LiveSearch.dropdown.find( 'ul' ).append( jQuery( '<li><a href='+this.url+'><img src="'+this.thumbnail+'" alt="'+this.alt+'" /></a><div><a class="header" href='+this.url+'>'+this.title+'</a></div></li>' ).bind( 'mouseover', Epson.Registration.LiveSearch.clearActive ).bind( 'click', function(e) { (function(product) { Epson.Registration.Product.add( product ) } )(data); e.preventDefault() } ) );	
					} );
				}
				else
				{
					if ( !Epson.Registration.LiveSearch.dropdown.find( '.empty' ).length ) jQuery( '<div class="empty">' + Epson.Localisation.Registration.TXT001 + '</div>' ).insertAfter( Epson.Registration.LiveSearch.dropdown.find( 'ul' ) );
				}

				Epson.Registration.LiveSearch.hideLoader();
			}
		},

		open: function()
		{
			// prepare the dropdown position and styles
			var position = Epson.Registration.LiveSearch.field.offset( { border:true, padding:true } );
			var width = Epson.Registration.LiveSearch.field.width() + 10;

			Epson.Registration.LiveSearch.dropdown = jQuery( '<div id="LiveSearch" class="genericRoundedCorners"><div class="content"><ul></ul></div><div class="bottom"><div class="bl"></div><div class="br"></div><div class="b"></div></div></div>' );
			Epson.Registration.LiveSearch.dropdown.find( '.buttonSmall' ).bind( 'click', function() { Epson.Registration.LiveSearch.field.parents( 'form' ).submit() } );
			Epson.Registration.LiveSearch.dropdown.css( { position:'absolute', top:position.top+Epson.Registration.LiveSearch.field.height()+16+'px', left:position.left+'px', width:width+'px' } );

			// add to container
			jQuery( '#Container' ).append( Epson.Registration.LiveSearch.dropdown );
	
			Epson.Registration.LiveSearch.opened = true;

			//IE fix for z-index bug
			jQuery('#LiveSearch').bgiframe();
				
			
		},
	
		close: function()
		{
			Epson.Registration.LiveSearch.hideLoader();
			
			Epson.Registration.LiveSearch.active = null;
			Epson.Registration.LiveSearch.opened = false;
			if ( Epson.Registration.LiveSearch.dropdown )
			{
				Epson.Registration.LiveSearch.dropdown.remove();
				Epson.Registration.LiveSearch.dropdown = null;
			}
		},

		upSuggestions: function(e)
		{
			var active = Epson.Registration.LiveSearch.dropdown.find( 'ul li.active' );
			if ( active.length && active.prev( 'li' ).length ) active.prev( 'li' ).addClass( 'active' );
			else if ( active.length && !active.prev( 'li' ).length ) Epson.Registration.LiveSearch.dropdown.find( 'ul li:last-child' ).addClass( 'active' );
			else Epson.Registration.LiveSearch.dropdown.find( 'ul li:last-child' ).addClass( 'active' );
	
			active.removeClass( 'active' );
	
			Epson.Registration.LiveSearch.active = Epson.Registration.LiveSearch.dropdown.find( 'ul li.active' );
	
			e.preventDefault();
		},
	
		downSuggestions: function(e)
		{
			var active = Epson.Registration.LiveSearch.dropdown.find( 'ul li.active' );
			if ( active.length && active.next( 'li' ).length ) active.next( 'li' ).addClass( 'active' );
			else if ( active.length && !active.next( 'li' ).length ) Epson.Registration.LiveSearch.dropdown.find( 'ul li:first-child' ).addClass( 'active' );
			else Epson.Registration.LiveSearch.dropdown.find( 'ul li:first-child' ).addClass( 'active' );
	
			active.removeClass( 'active' );
	
			Epson.Registration.LiveSearch.active = Epson.Registration.LiveSearch.dropdown.find( 'ul li.active' );
	
			e.preventDefault();
		},

		clearActive: function()
		{
			if ( Epson.Registration.LiveSearch.active ) Epson.Registration.LiveSearch.active.removeClass( 'active' );
			Epson.Registration.LiveSearch.active = null;
		},

		displayLoader: function()
		{
			if ( Epson.Registration.LiveSearch.field )
			{
				var width = Epson.Registration.LiveSearch.field.width();
				var offset = Epson.Registration.LiveSearch.field.offset();

				jQuery( '#Container' ).append( jQuery( '<div class="loader"></div>' ).css( { position:'absolute', top:offset.top+9+'px', left:offset.left+width-10+'px' } ) );
			}			
		},

		hideLoader: function()
		{
			jQuery( '#Container .loader' ).remove();
		},

		submitSearch: function()
		{
						
			if (Epson.Registration.LiveSearch.dropdown) 
			{
				// set conditions for one or multiple results
				if (Epson.Registration.LiveSearch.results != null)
				{
					
				if ((  Epson.Registration.LiveSearch.results.length == 1) || ( Epson.Registration.LiveSearch.active == null )) var link = Epson.Registration.LiveSearch.dropdown.find( 'ul li:first-child a.header' ).attr( 'href' )
				else var link = Epson.Registration.LiveSearch.active.find('a.header').attr( 'href' );
				}

				
			}
			
			
			
			jQuery('.f_error_text').remove();
			jQuery('.f_error').removeClass('f_error');			
		}
	},

	Product:
	{
		add: function( product )
		{
			// hide dropdown
			Epson.Registration.LiveSearch.dropdown.remove();
			Epson.Registration.LiveSearch.dropdown = null;

			// animate the closure of the search module
			jQuery( '#RegistrationSearch' ).animate( { height:0 }, 300, 'swing', function()
			{
				// clear and hide search module
				jQuery( '#RegistrationSearch' ).hide();
				Epson.Registration.LiveSearch.field.val( '' );

				// pull in form module
				jQuery.get( product.url, Epson.Registration.Product.handleLoad );
			} );

		},

		handleLoad: function( response )
		{
			var form_module_container = jQuery( '#RegistrationProduct' );
			var old_height = form_module_container.height();

			// set up events on the form module
			jQuery( response ).insertBefore( jQuery( '#RegistrationProduct a.button' ) );
			var form_module = jQuery( '#RegistrationProduct .productRegDetails:last' );
			form_module.find( '.pr-change' ).bind( 'click', Epson.Registration.Product.change );
			form_module.find( '.pr-remove' ).bind( 'click', Epson.Registration.Product.remove );

			// animate loading in of form items
			form_module_container.css( { position:'absolute', width:'100%', visibility:'hidden' } );
			var height = form_module_container.height();
			form_module_container.css( { height:old_height+'px', position:'static', visibility:'visible', width:'auto' } );

			form_module_container.animate(  { height:height }, 300, 'swing', function() { jQuery( '#RegistrationProduct' ).css( { height:'auto' } ) } );
			
		},

		addMore: function(e)
		{
			// open the search module again
			jQuery( '#RegistrationSearch' ).animate( { height:Epson.Registration.LiveSearch.height }, 300, 'swing' );

			e.preventDefault();
		},

		change: function(e)
		{
			var form_module = jQuery( this ).parents( '.productRegDetails' );

			// hide product module
			form_module.animate( { height:0 }, 300, 'swing', function()
			{
				// remove module
				form_module.remove();

				if ( !jQuery( '#RegistrationProduct .productRegDetails' ).length ) jQuery( '#RegistrationProduct' ).css( { position:'absolute', width:'100%', visibility:'hidden' } );

				jQuery( '#RegistrationSearch' ).animate( { height:Epson.Registration.LiveSearch.height }, 300, 'swing' );
			} );

			e.preventDefault();
		},

		remove: function(e)
		{
			var form_module = jQuery( this ).parents( '.productRegDetails' );

			// hide product module
			form_module.animate( { height:0 }, 300, 'swing', function()
			{
				// remove module
				form_module.remove();

				if ( !jQuery( '#RegistrationProduct .productRegDetails' ).length )
				{
					jQuery( '#RegistrationProduct' ).css( { position:'absolute', width:'100%', visibility:'hidden' } );

					jQuery( '#RegistrationSearch' ).animate( { height:Epson.Registration.LiveSearch.height }, 300, 'swing' );
				}
			} );

			e.preventDefault();
		}		
	}
}


