Epson = {};

// Live Search
Epson.Search = function( element )
{
	var _url = '';
	var _searchfield = jQuery( element );
	var _dropdown =  null;
	var _results = [];
	var _active = null;
	var _timer = null;
	var _opened = false;
	var _header_search = false;
	var _searching = false;
	var _query = '';
	var saved_json = null;

	function setup()
	{
		// save ajax url
		_url = _searchfield.parents( 'form' )[0].className.match( /ls-url-([^\s]+)/ );

		_searchfield.attr( 'autocomplete', 'off' );

		if ( _url )
		{
			_url = _url[1];

			_header_search = _searchfield.parents( '.mastheadSearch' ).length ? true : false;

			_searchfield.bind( 'keyup', timeSuggestions );

			if ( document.addEventListener )
			{
				_searchfield[0].addEventListener( 'blur', function() { _timer = setTimeout( close, 500 ) }, false );
				_searchfield[0].addEventListener( 'focus', function() { clearTimeout( _timer ) }, false );
			}
			else if ( document.attachEvent )
			{
				//_searchfield[0].attachEvent( 'onblur', function() { _timer = setTimeout( close, 500 ) } );
				jQuery('body').bind('click', function() { _timer = setTimeout( close, 500 ) });
				_searchfield[0].attachEvent( 'onfocus', function() { clearTimeout( _timer ) } );
			}
		}
		

		if ( !_header_search ) _searchfield.parents( 'form' ).bind( 'submit', function(e) { e.preventDefault() } );
		else
		{
			_searchfield.parents( 'form' ).bind('submit', function(e)
			{
				 // prevent the user from submitting blanks
				var value = _searchfield.val().replace( /^\s+|\s+$/g, '' );
				var label = _searchfield.parents( 'form' ).find( 'label.fl_info' ).text();

				if ( value == '' || value == label ) e.preventDefault();
			});
		}
	}

	function open()
	{
		// prepare the dropdown position and styles
		var position = _searchfield.offset( { border:true, padding:true } );

		_dropdown = jQuery( '<div id="LiveSearch" class="genericRoundedCorners"><div class="content"><ul></ul><div class="clear"><a href="#" class="buttonSmall"><span>' + Epson.Localisation.LiveSearch.TXT001 + '</span></a></div></div><div class="bottom"><div class="bl"></div><div class="br"></div><div class="b"></div></div></div>' );
		_dropdown.find( '.buttonSmall' ).bind( 'click', function() { _searchfield.parents( 'form' ).submit() } );
		_dropdown.css( { position:'absolute', top:position.top+_searchfield.height()+16+'px', left:position.left+'px'/* , opacity:0 */ } );
		_dropdown.bind( 'mousedown', function(e) { setTimeout( function() { _searchfield[0].focus() }, 100 ) } ); // fix for safari scrolling bug

		// add to container
		jQuery( '#Container' ).append( _dropdown );

		_opened = true;	
		
		//IE fix for z-index bug
		jQuery('#LiveSearch').bgiframe();
	}

	function close()
	{
		hideLoader();
		
		_active = null;
		_opened = false;
		if ( _dropdown )
		{
			_dropdown.remove();
			_dropdown = null;
		}
	}

	function timeSuggestions(e)
	{
		
		clearTimeout( _timer );
		
		if ( e.keyCode == 40 && _dropdown )
		{
			clearTimeout( _timer );
			downSuggestions(e);
		}
		else if ( e.keyCode == 38 && _dropdown )
		{
			clearTimeout( _timer );
			upSuggestions(e);
		}
		else if ( e.keyCode != 13 && _searchfield.val().length > 2 )
		{
			displayLoader();
			clearTimeout( _timer );
			_searching = true;
			_timer = setTimeout( function() { triggerSuggestions(e) }, 300 );
		}
		else if ( e.keyCode == 13 ) submitSearch(e);
		else close();
	}

	function triggerSuggestions(e)
	{
		if ( _query != _searchfield.val() )
		{
			_query = _searchfield.val();
			_query = encodeURI(_query);

			jQuery.getJSON( _url, { query:_query }, handleResult );			
		}
		else { handleResult( saved_json ); 	_searching = false; }
	}

	function submitSearch(e)
	{
		jQuery.cookie('categoryTab', null );
		if ( _active )
		{
			window.location = _active.find( 'a' ).attr( 'href' );
		}
		else if ( !_header_search && _results.length == 1 && !_searching )
		{
			window.location = _dropdown.find( 'ul li:first-child a.header' ).attr( 'href' );
		}
	}

	function upSuggestions(e)
	{
		var active = _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 ) _dropdown.find( 'ul li:last-child' ).addClass( 'active' );
		else _dropdown.find( 'ul li:last-child' ).addClass( 'active' );

		active.removeClass( 'active' );

		_active = _dropdown.find( 'ul li.active' );

		e.preventDefault();
	}

	function downSuggestions(e)
	{
		var active = _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 ) _dropdown.find( 'ul li:first-child' ).addClass( 'active' );
		else _dropdown.find( 'ul li:first-child' ).addClass( 'active' );

		active.removeClass( 'active' );

		_active = _dropdown.find( 'ul li.active' );

		e.preventDefault();
	}

	function clearActive()
	{
		if ( _active ) _active.removeClass( 'active' );
		_active = null;
	}

	function handleResult( json )
	{
		_searching = false;

		if ( !_opened ) open();

		// empty the content to prevent repeating
		_dropdown.find( 'ul' ).empty();

		//add the data to the html
		if ( json.results && json.results.length )
		{
			// save results
			_results = json.results;

			_dropdown.find( '.empty' ).remove();

			jQuery.each( json.results, function()
			{
				var options = '';
				if ( this.options ) jQuery.each( this.options, function() { options += '<div ><a class='+this.preRepId+' href='+this.url+' id='+this.newPage+'>'+this.title+'</a></div>' } );

				
					_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><div class="options">'+options+'</div></li>' ).bind( 'mouseover', clearActive ));
						if ( this.options )
					{
							jQuery.each( this.options, function() {
									$('.'+this.preRepId).each(function()	{
												$(this).bind( 'click', 
												function(e)
												{ 							
													if(jQuery( this ).attr( 'id' )=='true' )
													{
														window.open(jQuery( this ).attr( 'href' ),'','scrollbars=yes,menubar=yes,height=600,width=800,resizable=yes,toolbar=yes,location=no,status=no');
														e.preventDefault();
													}
													else
														window.location = jQuery( this ).attr( 'href' );
												}		
											  );});
								});
					}
					
				_dropdown.find( 'a' ).bind( 'click', function() {try { cmCreatePageviewTag("Search Results - Predictive", "100.4.1.1", _query, json.results.length ) } catch(e){};} );
			
			} );
						
			if ( _searchfield.parents( '.mastheadSearch' ).length )
			{
				if ( json.results.length > 4 ) _dropdown.find( '.buttonSmall' ).show();
				else _dropdown.find( '.buttonSmall' ).hide();					
			}
			else
			{
				_dropdown.find( '.buttonSmall' ).hide();
				_dropdown.width( _searchfield.width() + 11 );			
			}

			
		}
		else
		{
			//custom requirement to pass coremetrics params
			try { cmCreatePageviewTag("Search Results", "100.30.70", _query, "0" ) } catch(e){};

			_dropdown.find( '.buttonSmall' ).hide();

			//added for change request ECR 53
			if ( !_dropdown.find( '.empty' ).length ){
					
						jQuery( '#LiveSearch' ).remove();
						
					
					}
			//commented for change request ECR 53
			//if ( !_dropdown.find( '.empty' ).length ) jQuery( '<div class="empty">' + Epson.Localisation.LiveSearch.TXT002 + '</div>' ).insertAfter( _dropdown.find( 'ul' ) );
		}
		
		saved_json = json;
		hideLoader();
	}

	function displayLoader()
	{
		if ( _searchfield )
		{
			var width = _searchfield.width();
			var offset = _searchfield.offset();

			var y;
			if ( _searchfield.parents( '.mastheadSearch' ).length ) y = 4
			else y = 9

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

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


	setup();
}
jQuery( document ).ready( function() { new Epson.Search( jQuery( '#SiteSearch' ) ) } );


Epson.Carousel =
{
	assets_url:'',
	teasers:[],

	setup: function()
	{
		var swf = jQuery( '#FlashCarousel' )[0].className.match( /fc-swf-([^\s]+)/ );
		var config = jQuery( '#FlashCarousel' )[0].className.match( /fc-config-([^\s]+)/ );
		var data = jQuery( '#FlashCarousel' )[0].className.match( /fc-data-([^\s]+)/ );
		var panel = jQuery( '#FlashCarousel' )[0].className.match( /fc-panel-([^\s]+)/ );
		var alt_content_url = jQuery( '#FlashCarousel' )[0].className.match( /fc-alt-([^\s]+)/ );
		Epson.Carousel.assets_url = jQuery( '#FlashCarousel' )[0].className.match( /fc-assets-([^\s]+)/ );

		if ( panel ) panel = panel[1];
		if ( !swf || !config || !data ) return;

		var flashvars = { carouselDataXmlPath:config[1], panelDataXmlPath:data[1] };

		// if the page has a pre-defined item, set it
		var anchor = window.location && window.location.toString().match( /#([a-zA-Z]+)$/ );
		if ( anchor ) anchor = anchor[1];
		flashvars.panelName = panel || anchor;

		// write in flash module or pull in static content if no flash
		if ( swfobject.getFlashPlayerVersion().major > 0 ) swfobject.embedSWF( swf[1], 'FlashCarousel', 940, 400, '9.0.28', false, flashvars, { wmode:'transparent' } );
		else
		{
			var content = jQuery( '#FlashCarousel' ).html().replace( /<\/?noscript>/ig, '' );
			if ( content != '' ) jQuery( '#FlashCarousel' ).empty().append( content );
			else jQuery( '#FlashCarousel' ).load( alt_content_url[1] );
		}

		// load in correct teaser
		if ( anchor ) Epson.Carousel.loadTeasers( anchor[1], 0 );		
	},

	setupTracking: function()
	{
		// set up TeaLeaf tracking for Flash on window unload
		jQuery( window ).bind( 'beforeunload', jQuery( '#FlashCarousel' )[0].tlFlushXML );
		jQuery( window ).bind( 'unload', jQuery( '#FlashCarousel' )[0].tlFlushXML );
	},

	loadTeasers: function( item, direction )
	{
		var teasers = Epson.Carousel.teasers[ item ];

		if ( teasers )
		{
			// set up scrolling dimensions
			jQuery( '#Teasers' ).css( { overflow:'hidden', position:'relative' } ).find( 'ul' ).css( { width:'1880px' } );

			var x = direction ? 0 : 2;
			while ( true )
			{
				var teaser;

				if ( teasers[x].type == 0 )
				{
					teaser = jQuery( '<li class="type0"><h4><a href="'+teasers[x].link+'">'+teasers[x].title+'</a></h4><p>'+teasers[x].description+'</p><a href="'+teasers[x].link+'"><img src="'+teasers[x].image+'" /></a></li>' );
				}
				else if ( teasers[x].type == 1 )
				{
					teaser = jQuery( '<li class="type1"><h4><a href="'+teasers[x].link+'">'+teasers[x].title+'</a></h4><p>'+teasers[x].description+'</p><a href="'+teasers[x].link+'"><img src="'+teasers[x].image+'" /></a></li>' );
				}
				else if ( teasers[x].type == 2 )
				{
					teaser = jQuery( '<li class="type2"><a href="'+teasers[x].link+'"><img src="'+teasers[x].image+'" /></a></li>' );
				}

				if ( direction )
				{
					jQuery( '#Teasers ul' ).append( teaser );
					if ( ++x > 2 ) break;
				}
				else
				{
					jQuery( '#Teasers ul' ).prepend( teaser );
					jQuery( '#Teasers .content' ).css( { marginLeft:parseInt( jQuery( '#Teasers .content' ).css( 'marginLeft' ) ) - 314 + 'px' } );
					if ( --x < 0 ) break;
				}
			}

			// animate new teasers in
			if ( direction )
			{
				jQuery( '#Teasers .content' ).animate( { marginLeft:-940 }, 750, 'swing', function(){ for ( var x = 0; x < 3; x++ ) { jQuery( '#Teasers ul li:first' ).remove(); jQuery( '#Teasers .content' ).css( { marginLeft:parseInt( jQuery( '#Teasers .content' ).css( 'marginLeft' ) ) + 314 + 'px' } ) }; jQuery( '#Teasers .content' ).css( { marginLeft:'' } ); jQuery( '#Teasers' ).css( { overflow:'' } ).find( 'ul' ).css( { width:'' } ) } );

				jQuery( '#Teasers ul li:nth(3)' ).append( jQuery('<div class="wc_tl"></div><div class="wc_bl"></div>') );
				jQuery( '#Teasers ul li:last' ).append( jQuery('<div class="wc_tr"></div><div class="wc_br"></div>') );
				
			}
			else
			{
				jQuery( '#Teasers .content' ).animate( { marginLeft:0 }, 750, 'swing', function(){ for ( var x = 0; x < 3; x++ ) { jQuery( '#Teasers ul li:last' ).remove(); }; jQuery( '#Teasers .content' ).css( { marginLeft:'' } ); jQuery( '#Teasers' ).css( { overflow:'' } ).find( 'ul' ).css( { width:'' } ) } );

				jQuery( '#Teasers ul li:first' ).append( jQuery('<div class="wc_tl"></div><div class="wc_bl"></div>') );
				jQuery( '#Teasers ul li:nth(2)' ).append( jQuery('<div class="wc_tr"></div><div class="wc_br"></div>') );
			}

			// update position in window location
//			window.location = window.location.toString().match( /^([^#]+)/ )[1] + '#' + item;
		}
	},

	cacheAssets: function()
	{
		if ( Epson.Carousel.assets_url ) jQuery.getJSON( Epson.Carousel.assets_url[1], null, function( json )
		{
			var path = json.path || '';
			if ( json.css ) jQuery.each( json.css, function() { jQuery.ajax( { url:path+'css/'+this, cache:true, dataType:'text', type:'GET' } ) } );
			if ( json.js ) jQuery.each( json.js, function() { jQuery.ajax( { url:path+'js/'+this, cache:true, dataType:'text', type:'GET' } ) } );
		} );
	}
}
jQuery( document ).ready( Epson.Carousel.setup );


Epson.TeaserCarousel = function( element )
{
	var _self = this;
	var _carousel = jQuery( element );
	var _next;
	var _previous;
	var _list = _carousel.find( 'ul' );
	var _index = _carousel.find( '.pageIndex' );
	var _pages = 0;

	setup();

	function setup()
	{
		_pages = Math.ceil( _list.find( 'li' ).length / 3 );

		// set up pagination index
		_index.append( '<strong></strong>' );
		for ( var x = 1; x < _pages; x++ ) _index.append( '<span></span>' );

		// set up pagination buttons
		if ( _pages > 1 ) _carousel.find( '.content:first' ).append( '<a href="#" class="prevPagination"></a><a href="#" class="nextPagination"></a>' );
		
		_next = _carousel.find( '.nextPagination' ).bind( 'click', next );
		_previous = _carousel.find( '.prevPagination' ).bind( 'click', previous );
	}

	function next(e)
	{
		var page = Math.abs( parseInt( parseInt( _list.css( 'marginLeft' ) ) / 915 ) );
		var new_page = page < _pages - 1 ? page + 1 : 0;
		var amount = -915 * new_page;

		_list.animate( { marginLeft:amount }, 750, 'swing' );

		_index.find( 'strong' ).replaceWith( '<span></span>' );
		_index.find( 'span:eq( ' + new_page + ' )' ).replaceWith( '<strong></strong>' );

		e.preventDefault();
	}

	function previous(e)
	{
		var page = Math.abs( parseInt( parseInt( _list.css( 'marginLeft' ) ) / 915 ) );
		var new_page = page > 0 ? page - 1 : _pages - 1;
		var amount = -915 * new_page;

		_list.animate( { marginLeft:amount }, 750, 'swing' );

		_index.find( 'strong' ).replaceWith( '<span></span>' );
		_index.find( 'span:eq( ' + new_page + ' )' ).replaceWith( '<strong></strong>' );

		e.preventDefault();
	}
}


Epson.LanguageDropdown =
{
	url:'',
	timer:null,
	dropdown_element: null,
	dropdown_menu: null,

	setup: function()
	{
		Epson.LanguageDropdown.dropdown_element = jQuery( '#selCountryDropdown' );

		// bind events to the selects
		jQuery( Epson.LanguageDropdown.dropdown_element.find( 'select' )[0] ).bind( 'change', Epson.LanguageDropdown.changeCountry );
		jQuery( Epson.LanguageDropdown.dropdown_element.find( 'select' )[1] ).bind( 'change', Epson.LanguageDropdown.changeLanguage );

		// create dropdown
		Epson.LanguageDropdown.dropdown_menu = jQuery( '<div class="selCountryContainer genericRoundedCorners"><div class="top"><div class="tl"></div><div class="tr"></div><div class="t"></div></div><div class="content clear"></div><div class="bottom"><div class="bl"></div><div class="br"></div><div class="b"></div></div></div>' );
		Epson.LanguageDropdown.dropdown_menu.find( '.content' ).append( Epson.LanguageDropdown.dropdown_element.find( 'select' ) );
		Epson.LanguageDropdown.dropdown_menu.find( 'select' ).wrap( '<div class="row"></div>' );
		Epson.LanguageDropdown.dropdown_menu.css( { display:'none' } );

		Epson.LanguageDropdown.url = jQuery( '#UserLinks form' ).attr( 'action' );
		Epson.LanguageDropdown.dropdown_element.empty();
		Epson.LanguageDropdown.dropdown_element.append( jQuery( '<a href="#">' + Epson.LanguageDropdown.dropdown_menu.find( 'select:first option[@selected]' ).text() + '</a>' ).bind( 'click', Epson.LanguageDropdown.open ) );

		jQuery( '#Container' ).append( Epson.LanguageDropdown.dropdown_menu );
	},

	changeCountry: function(e)
	{
		window.location = 'http://' + this.value;

		e.preventDefault();
	},

	changeLanguage: function(e)
	{
		window.location = '/' + this.value;

		e.preventDefault();
	},

	open: function(e)
	{
		// position menu
		var position = Epson.LanguageDropdown.dropdown_element.offset( { border:true, padding:true } );
		var dimensions = [ Epson.LanguageDropdown.dropdown_element.outerWidth(), Epson.LanguageDropdown.dropdown_element.outerHeight() ];
		Epson.LanguageDropdown.dropdown_menu.css( { display:'block', position:'absolute', top:position.top+dimensions[1]+5+'px', left:position.left+dimensions[0]+'px'  } );
		Epson.LanguageDropdown.dropdown_menu.css( { left:parseInt( Epson.LanguageDropdown.dropdown_menu.css( 'left' ) )-Epson.LanguageDropdown.dropdown_menu.width()+'px' } );

		// add close event
		setTimeout( function() { jQuery( 'body' ).bind( 'click', Epson.LanguageDropdown.timeClose ) }, 1);

		// remove open event
		Epson.LanguageDropdown.dropdown_element.find( 'a' ).unbind( 'click', Epson.LanguageDropdown.open )

		// add unbubble event
		Epson.LanguageDropdown.dropdown_menu.bind( 'click', function() { setTimeout( function() { clearTimeout( Epson.LanguageDropdown.timer ); Epson.LanguageDropdown.timer=null }, 1 ) } );

		e.preventDefault();
	},

	timeClose: function()
	{
		Epson.LanguageDropdown.timer = setTimeout( Epson.LanguageDropdown.close, 10 );
	},

	close: function()
	{
		if ( Epson.LanguageDropdown.timer ) // IE fix
		{
			jQuery( '#Container .selCountryContainer' ).css( { display:'none' } );
	
			// remove close event
			jQuery( 'body' ).unbind( 'click', Epson.LanguageDropdown.timeClose );
	
			// add open event
			Epson.LanguageDropdown.dropdown_element.find( 'a' ).bind( 'click', Epson.LanguageDropdown.open )
		}
	}
}
jQuery( document ).ready( Epson.LanguageDropdown.setup );


Epson.SecondaryNavDropdowns = function( element )
{
	var _element = element;
	var _submenu;
	var _ddelement = null;
	var _width = 0;
	var _timer;

	setup();


	function setup()
	{
		_element = jQuery( element );
		_submenu = _element.find( 'ul' );

		// calculate max width
		var tmp = jQuery( '<div class="jsDropdownMenu" style="position:static"><ul><li style="display:inline; padding:0"></li></ul></div>' );
		jQuery( '#Container' ).append( tmp );
		for ( var x = 0; x < _submenu.find( 'li' ).length; x++ )
		{
			tmp.find( 'li' ).text( jQuery( _submenu.find( 'li' )[x] ).text() );
			if ( tmp.find( 'li' )[0].offsetWidth > _width ) _width = tmp.find( 'li' )[0].offsetWidth + 20;
		}
		tmp.remove();

		_element.bind( 'mouseover', open );
	}
	
	function open(e)
	{
		// get position for dropdown
		var jquery_offset = _element.offset( { border:true, padding:true } );
		_position = [ jquery_offset.left-10, jquery_offset.top + _element.outerHeight() ];

		// create dropdown
		_ddelement = jQuery( '<div class="jsDropdownMenu genericRoundedCorners"><div class="bottom"><div class="bl"></div><div class="br"></div><div class="b"></div></div></div>' ).css( { width:_width+20 });
		_ddelement.css( { top:_position[1]+7+'px', left:_position[0]+'px' } );
		_submenu.addClass( 'content' ).insertBefore( _ddelement.find( '.bottom' ) );

		// append dropdown to container
		jQuery( '#Container' ).append( _ddelement );

		// set events
		_element.unbind( 'mouseover', open );
		setTimeout( function() { _element.bind( 'mouseover', cancelCloseTimer ) }, 1);
		_ddelement.bind( 'mouseover', cancelCloseTimer );
		setTimeout( function() { _element.bind( 'mouseout', setCloseTimer ) }, 1);
		_ddelement.bind( 'mouseout', setCloseTimer );
		jQuery( 'body' ).bind( 'click', close );

		e.preventDefault();
	}
	
	function setCloseTimer()
	{
		_timer = setTimeout( close, 250 );
	}
	
	function cancelCloseTimer()
	{
		clearTimeout( _timer );
	}
	
	function close()
	{
		// remove menu
		_element.append( _ddelement.find( 'ul' ) );
		_ddelement.remove();

		// set events
		setTimeout( function() { _element.bind( 'mouseover', open ) }, 1);
		_element.unbind( 'mouseover', cancelCloseTimer );
		_ddelement.unbind( 'mouseover', cancelCloseTimer );
		_element.unbind( 'mouseout', setCloseTimer );
		_ddelement.unbind( 'mouseout', setCloseTimer );
		jQuery( 'body' ).unbind( 'click', close );
	}
}
jQuery( document ).ready( function() { jQuery( '#MastHead .secondaryNavigation .dropdown' ).each( function() { new Epson.SecondaryNavDropdowns( this ) } ) } );


Epson.GenericDropdown = function( element )
{
	var _self = this;
	this.element = this.element ? this.element : element;
	this.field;
	this.ddelement = null;
	this.options = this.options ? this.options : [];
	this.selectedIndex = 0;
	this.width = 0;
	this.position = [];
	this.change = [];
	this.attributes = {};
	this.className = this.className ? this.className : 'jsDropdown';
	this.value = null;
	this.keySelected = 0;

	this.eventOpen = this.eventOpen ? this.eventOpen : function(e) { _self.open(e) };
	this.eventSelect = this.eventSelect ? this.eventSelect : function(e) { _self.select(e) };
	this.eventKeydown = this.eventKeydown ? this.eventKeydown : function(e) { _self.keydown(e) };
	this.eventClose = this.eventClose ? this.eventClose : function(e) { _self.close(e) };
	this.eventCancel = this.eventCancel ? this.eventCancel : function(e) { _self.cancel(e) };

	if ( this.element )
	{
		// save options data
		jQuery( this.element ).find( 'option' ).each( function(i)
		{
			if ( !_self.options[i] ) _self.options[i] = [];
			_self.options[i][0] = jQuery( this ).val();
			_self.options[i][1] = jQuery( this ).text();

			if ( this.selected ) _self.selectedIndex = i;
		} );

		// get name
		var name = jQuery( this.element ).attr( 'name' );

		// save attributes
		var attributes = this.element.attributes;
		for ( var x = 0; x < attributes.length; x++ ) this.attributes[ attributes[x].nodeName ] = attributes[x].nodeValue;

		// save class names
		this.className = this.className + ' ' + this.element.className.replace( this.className, '' );
	
		// copy over events
		var events = jQuery.data( this.element, 'events' ) ? jQuery.data( this.element, 'events' )[ 'change' ] : null;
		if ( events ) for ( var handler in events ) this.change.push( events[handler] );

		// calculate max width
		var tmp = jQuery( '<div class="jsDropdownMenu" style="position:static"><ul><li style="display:inline; padding:0"></li></ul></div>' );
		jQuery( '#Container' ).append( tmp );
		for ( var x = 0; x < this.options.length; x++ )
		{
			tmp.find( 'li' ).text( this.options[x][1] );
			if ( tmp.find( 'li' )[0].offsetWidth > this.width ) this.width = tmp.find( 'li' )[0].offsetWidth;
		}
		tmp.remove();
	
		// draw styled dropdown
		this.draw();

		// save reference to object in jQuery
		this.element.data( 'jDropdown', _self );

		// create hidden field with data
		this.field = jQuery( '<input type="hidden" name="' + name + '" value="' + this.options[this.selectedIndex][0] + '" />' );
		this.element.after( this.field );
	
		// save data into jQuery
		this.element.data( 'name', name );
		this.element.data( 'value', this.options[this.selectedIndex][0] );
		this.element.val( this.options[this.selectedIndex][0] );
	
		// save selected value
		this.value = this.options[this.selectedIndex][0];

		// bind window close event to replace normal selects for server data
		// jQuery( window ).bind( 'beforeunload', unload );
	}
}

Epson.GenericDropdown.prototype.draw = function()
{
	var self = this;

	// get id if there is one
	var id = ( this.element.id ) ? this.element.id : null;

	// build up dropdown
	var width = ( jQuery( this.element ).css( 'width' ) != 'auto' ) ? ( parseInt( jQuery( this.element ).css( 'width' ) ) > this.width ) ? parseInt( jQuery( this.element ).css( 'width' ) ) - 27 : this.width + 5 : this.width + 5;
	var dropdown = jQuery( '<a href="#" class="' + this.className + '"><span>' + this.options[this.selectedIndex][1] + '</span></a>' );

	// bind events
	dropdown.bind( 'click', self.eventOpen );
	dropdown.bind( 'keydown', self.eventKeydown );

	dropdown.find( 'span' ).css( { width:width+'px' } );
	if ( id ) dropdown.attr( 'id', id );

	// replace existing dropdown with styled one
	var select = jQuery( this.element );
	this.element = jQuery( dropdown );
	select.replaceWith( this.element );
}


Epson.GenericDropdown.prototype.unload = function()
{
	var select = jQuery( '<select></select>' ).css( { visibility:'hidden' } );
	
	// copy back attributes
	jQuery.each( this.attributes, function(key, value) { select[0].setAttribute( key, value ) } );

	// add in options
	jQuery.each( this.options, function() { select.append( jQuery( '<option value="' + this[0] + '">' + this[1] + '</option>' ) ) } );

	// set selected option
	select.find( 'option' )[ this.selectedIndex ].selected = 'selected';

	// replace jsDropdown
	this.element.replaceWith( select.removeClass( 'jsDropdown' ) );
	this.element = select;
}

Epson.GenericDropdown.prototype.open = function(e)
{
	var self = this;

	if ( this.element.attr( 'disabled' ) != 'disabled' )
	{
		// get position for dropdown
		var jquery_offset = this.element.offset( { border:true, padding:true } );
		this.position = [ jquery_offset.left, jquery_offset.top + this.element.outerHeight() ];
		var width = ( this.element.innerWidth() > this.width ) ? this.element.innerWidth() : this.width+20;

		// create dropdown
		this.ddelement = jQuery( '<div class="jsDropdownMenu genericRoundedCorners"><ul class="content"></ul><div class="bottom"><div class="bl"></div><div class="br"></div><div class="b"></div></div></div>' ).css( { width:width });
		this.ddelement.css( { top:this.position[1]+'px', left:this.position[0]+'px' } );
		for ( var x = 0; x < this.options.length; x++ )
		{
			var item = jQuery( '<li>' + this.options[x][1] + '</li>' );

			if ( x == this.selectedIndex ) item.addClass( 'selected' );

			item.bind( 'click', self.eventSelect );
			item.bind( 'mouseover', function(e)
			{
				self.ddelement.find( 'li.selected' ).removeClass( 'selected' );
				jQuery( this ).addClass( 'selected' );

				self.keySelected = self.ddelement.find( 'li' ).index( this );
			} );

			this.ddelement.find( 'ul' ).append( item );
		}
	
		// append dropdown to container
		jQuery( '#Container' ).append( this.ddelement );
	
		// remove open event and add cancel event
		this.element.unbind( 'click', self.eventOpen );
		this.element.bind( 'click', self.eventCancel );
	
		// add close event
		setTimeout( function() { jQuery( 'body' ).bind( 'click', self.eventClose ) }, 1);
	}

//	e.stopPropagation();
	e.preventDefault();
}

Epson.GenericDropdown.prototype.keydown = function(e)
{
	if ( e.keyCode == 32 ) // space
	{
		if ( !this.ddelement )
		{
			this.keySelected = this.selectedIndex;

			this.open(e);
		}
		else
		{
			this.ddelement.find( 'li.selected' ).click();

			e.preventDefault();
		}
	}
	else if ( e.keyCode == 40 ) // down
	{
		if ( ++this.keySelected > this.options.length - 1 ) this.keySelected = 0;

		if ( this.ddelement )
		{
			this.ddelement.find( 'li.selected' ).removeClass( 'selected' );
			this.ddelement.find( 'li:eq(' + this.keySelected + ')' ).addClass( 'selected' );
		}
		else
		{
			this.selectedIndex = this.keySelected;

			// set text dropdown
			this.element.find( 'span' ).text( this.options[this.selectedIndex][1] );
	
			// save value in jQuery
			this.element.data( 'value', this.options[this.selectedIndex][0] );
	
			// save selected value
			this.value = this.options[this.selectedIndex][0];
			this.element.val( this.options[this.selectedIndex][0] );
			this.field.val( this.options[this.selectedIndex][0] );
	
			// perform custom event listeners if they exist
			e.target = this.element[0];
			if ( this.change.length ) for ( var x = 0; x < this.change.length; x++ ) this.change[x].call( this, e);
		}

		e.preventDefault();
	}
	else if ( e.keyCode == 38 ) // up
	{
		if ( --this.keySelected < 0 ) this.keySelected = this.options.length - 1;

		if ( this.ddelement )
		{
			this.ddelement.find( 'li.selected' ).removeClass( 'selected' );
			this.ddelement.find( 'li:eq(' + this.keySelected + ')' ).addClass( 'selected' );
		}
		else
		{
			this.selectedIndex = this.keySelected;

			// set text dropdown
			this.element.find( 'span' ).text( this.options[this.selectedIndex][1] );
	
			// save value in jQuery
			this.element.data( 'value', this.options[this.selectedIndex][0] );
	
			// save selected value
			this.value = this.options[this.selectedIndex][0];
			this.element.val( this.options[this.selectedIndex][0] );
			this.field.val( this.options[this.selectedIndex][0] );
	
			// perform custom event listeners if they exist
			e.target = this.element[0];
			if ( this.change.length ) for ( var x = 0; x < this.change.length; x++ ) this.change[x].call( this, e);
		}

		e.preventDefault();
	}
	else if ( e.keyCode == 13 ) // return
	{
		this.ddelement.find( 'li:eq(' + this.keySelected + ')' ).click();
		
		e.preventDefault();
	}
	else if ( e.keyCode == 9 && this.ddelement ) // tab
	{
		this.ddelement.find( 'li:eq(' + this.keySelected + ')' ).click();
	}
}

Epson.GenericDropdown.prototype.select = function(e)
{
	// get index of selected item
	var new_selection = -1;
	var target_li = e.target;
	if ( target_li.nodeName != 'LI' ) target_li = jQuery( target_li ).parents( 'li' )[0];
	var list_items = this.ddelement.find( 'li' );
	for ( var x = 0; x < list_items.length; x++ ) if ( list_items[x] == target_li ) { new_selection = x; break }

	if ( new_selection != this.selectedIndex )
	{
		this.selectedIndex = new_selection;

		// set text dropdown
		this.element.find( 'span' ).html( this.options[this.selectedIndex][1] );

		// save value in jQuery
		this.element.data( 'value', this.options[this.selectedIndex][0] );

		// save selected value
		this.value = this.options[this.selectedIndex][0];
		this.element.val( this.options[this.selectedIndex][0] );
		this.field.val( this.options[this.selectedIndex][0] );

		// perform custom event listeners if they exist
		e.target = this.element[0];
		if ( this.change.length ) for ( var x = 0; x < this.change.length; x++ ) this.change[x].call( this, e);
	}

	this.eventClose(e);

	e.stopPropagation();
	e.preventDefault();
}

Epson.GenericDropdown.prototype.close = function(e)
{
	var self = this;

	this.ddelement.remove();
	delete this.ddelement;

	// remove close event
	jQuery( 'body' ).unbind( 'click', self.eventClose );

	// add open event and remove cancel event
	setTimeout( function() { self.element.bind( 'click', self.eventOpen ) }, 1 );
	this.element.unbind( 'click', self.eventCancel );

	if (e) e.preventDefault();
}
	
Epson.GenericDropdown.prototype.cancel = function(e)
{
	this.eventClose(e);

	e.stopPropagation();
}

Epson.GenericDropdown.prototype.reset = function( dropdown )
{
	var select = jQuery( '<select></select>' );
	
	// copy back attributes
	jQuery.each( this.attributes, function(key, value) { select[0].setAttribute( key, value ) } );
	if ( dropdown.id ) select[0].id = dropdown.id;

	// add in options
	jQuery.each( this.options, function() { select.append( jQuery( '<option value="' + this[0] + '">' + this[1] + '</option>' ) ) } );

	// set selected option
	select.find( 'option' )[ this.selectedIndex ].selected = 'selected';

	// replace jsDropdown
	jQuery( dropdown ).replaceWith( select.removeClass( 'jsDropdown' ) );

	// remove adjacent hidden field
	select.next( 'input[type=hidden]' ).remove();

	return select[0];
}
jQuery( document ).ready( function() { jQuery( 'select.jsDropdown' ).each( function() { var dropdown = this; setTimeout( function() { new Epson.GenericDropdown( dropdown ) }, 1 ) } ) } );


Epson.UI =
{
	Popup:
	{
		element:null,
		positions:null,
		dimensions:null,

		load: function( url, callback )
		{
			// display loader
			Epson.UI.Popup.Loader.display();

			// ajax load of a url
			jQuery( Epson.UI.Popup.element.find( '.content' )[0] ).load( url, null, function()
			{
				Epson.UI.Popup.Loader.hide();
				
				// set up generic functionality
				jQuery( '#Popup .triggerPopup' ).each( function() { jQuery( this ).bind( 'click', Epson.UI.Popup.loadGeneric ) } );

				// set up custom close buttons
				Epson.UI.Popup.element.find( '.closePopup' ).bind( 'click', Epson.UI.Popup.close );

				// set up tooltips
				Epson.Forms.ToolTips.setup();

				callback();
			} );
		},

		loadGeneric: function(e)
		{
			var trigger = this;
			var title = this.className.match( /tp-title-([^\s]+)/ );
			var url = this.className.match( /tp-url-([^\s]+)/ );
			var callback = this.className.match( /tp-callback-([^\s]+)/ );

			if ( title ) title = decodeURI( title[1] );

			if ( url )
			{
				Epson.UI.Popup.open( title );
				Epson.UI.Popup.load( url[1], function()
				{
					if ( callback ) eval( callback[1] + '( trigger )' );

					Epson.UI.Popup.position();
				} );
			}

			e.preventDefault();
		},

		open: function( title )
		{
			if ( !jQuery( '#Popup' ).length )
			{
				// render popup
				Epson.UI.Popup.element = jQuery( '<div id="Popup" class="genericRoundedCorners genericPopup"><div class="header"><div class="headerContent"><p>' + title + '</p><div class="closeButton"><a href="#" class="buttonBlueClose"><span>close</span></a></div></div></div><div class="content clear"></div><div class="bottom"><div class="bl"></div><div class="br"></div><div class="b"></div></div>	</div>' );	
				Epson.UI.Popup.element.find( '.closeButton a' ).bind( 'click', Epson.UI.Popup.close );
				jQuery ( '#Container' ).append( Epson.UI.Popup.element );

				// render backdrop
				jQuery ( '#Container' ).append( jQuery( '<div id="PopupBackground"></div>' ).css( { width:jQuery( window ).width()+'px', height:jQuery( window ).height()+'px' } ) );
				if ( jQuery.browser.msie && parseInt( jQuery.browser.version ) == 6 ) jQuery( window ).bind( 'scroll', function() { Epson.UI.Popup.position(); jQuery( '#PopupBackground' ).css( { top:document.documentElement.scrollTop+'px' } ) } );

				// set up window resize event to reposition popup
				jQuery( window ).bind( 'resize', Epson.UI.Popup.position );
			}
			else jQuery( '#Popup .header p' ).text( title );
		},

		position: function()
		{
			var inited = Epson.UI.Popup.positions;

			Epson.UI.Popup.positions = ( jQuery( window ).height() / 2 ) - ( Epson.UI.Popup.element.height() / 2 );
			if ( jQuery.browser.msie && parseInt( jQuery.browser.version ) == 6 ) Epson.UI.Popup.positions += document.documentElement.scrollTop;

			if ( inited ) Epson.UI.Popup.element.stop().animate( { top:Epson.UI.Popup.positions }, 500, 'swing' );
			else Epson.UI.Popup.element.css( { top:Epson.UI.Popup.positions+'px' } );

			// ensure backdrop is still right dimensions
			jQuery ( '#PopupBackground' ).css( { width:jQuery( window ).width()+'px', height:jQuery( window ).height()+'px' } );
		},

		populate: function( content )
		{
			jQuery( Epson.UI.Popup.element.find( '.content' )[0] ).append( content );
		},

		clear: function()
		{
			Epson.UI.Popup.element.find( '.content' ).empty();
		},

		dimension: function( callback )
		{
			if ( !arguments.length ) var callback = function() { };

			// save old dimensions
			var old_height = jQuery( Epson.UI.Popup.element.find( '.content' )[0] ).height();

			// reset element to get new dimensions
			jQuery( Epson.UI.Popup.element.find( '.content' )[0] ).css( { height:'' } );

			// get new dimensions
			var height = jQuery( Epson.UI.Popup.element.find( '.content' )[0] ).height();

			// set old dimensions again		
			jQuery( Epson.UI.Popup.element.find( '.content' )[0] ).css( { height:old_height+'px' } );

			// position popup if necessary
			if ( Epson.UI.Popup.positions )
			{
				var top = Epson.UI.Popup.element.offset().top - jQuery( window ).scrollTop() - ( ( height - old_height ) / 2 );

				Epson.UI.Popup.element.animate( { top:top }, 500, 'swing' );
			}
			else
			{
				var top = ( jQuery( window ).height() / 2 ) - ( Epson.UI.Popup.element.height() / 2 );
				Epson.UI.Popup.element.css( { top:top+'px' } );
			}

			// animate resize
			jQuery( Epson.UI.Popup.element.find( '.content' )[0] ).animate( { height:height }, 500, 'swing', callback );

			// save the position of the popup
			Epson.UI.Popup.positions = top;
		},

		undimension: function()
		{
			jQuery( Epson.UI.Popup.element.find( '.content' )[0] ).css( { height:'' } );
		},

		addButton: function( name, event )
		{
			if ( !Epson.UI.Popup.element.find( '.headerContent' ).text().match( name ) )
			{
				Epson.UI.Popup.element.find( '.headerContent' ).prepend ( jQuery( '<div class="backButton"><a href="#" class="buttonSmall"><span>' + name + '</span></a></a></div> ' ).find( 'a' ).bind( 'click', event ).parent() );
			}
		},

		addNormalButton: function( name, event )
		{
			if ( !Epson.UI.Popup.element.find( '.headerContent' ).text().match( name ) )
			{
				if ( Epson.UI.Popup.element.find( '.headerContent .normalButtons' ).length ) Epson.UI.Popup.element.find( '.headerContent .normalButtons' ).append( jQuery( '<a href="#" class="buttonSmall"><span>' + name + '</span></a>' ).bind( 'click', event ) );
				else Epson.UI.Popup.element.find( '.headerContent' ).append ( jQuery( '<div class="normalButtons"><a href="#" class="buttonSmall"><span>' + name + '</span></a></div> ' ).find( 'a' ).bind( 'click', event ).parent() );
			}
		},

		removeButton: function()
		{
			Epson.UI.Popup.element.find( '.header .backButton, .header .normalButtons' ).remove();
		},

		close: function(e)
		{
			// remove popup
			Epson.UI.Popup.element.remove();
			Epson.UI.Popup.element = null;
			Epson.UI.Popup.positions = null;
			Epson.UI.Popup.dimensions = null;

			// remove backdrop
			jQuery( '#PopupBackground' ).remove();

			// remove window resize event to reposition popup
			jQuery( window ).unbind( 'resize', Epson.UI.Popup.position );

			e.preventDefault();
		},

		Loader:
		{
			display: function()
			{
				jQuery( Epson.UI.Popup.element.find( '.content' )[0] ).addClass( 'genericPopupLoader' );
			},

			hide: function()
			{
				jQuery( Epson.UI.Popup.element.find( '.content' )[0] ).removeClass( 'genericPopupLoader' );
			}
		}
	}
}
jQuery( document ).ready( function() { jQuery( '.triggerPopup' ).each( function() { jQuery( this ).bind( 'click', Epson.UI.Popup.loadGeneric ) } ) } );

Epson.Forms =
{
	setup: function()
	{
		Epson.Forms.ToolTips.setup();

		Epson.Forms.AutoGender.setup();

		// set up checkout stages selection
		if ( jQuery( 'form .toggleFieldsets' ).length ) Epson.Forms.CheckoutStages.setup();

		// set up hidden label fields
		jQuery( 'label.fl_info' ).each( Epson.Forms.InfoLabels.setup );
	},

	ToolTips:
	{
		setup: function()
		{
			jQuery( 'form .f_tooltip' ).each( function() 
			{
				if ( jQuery( this ).prev( '.f_tooltip_text' ).length )
				{
					jQuery( jQuery( this ).prev( '.f_tooltip_text' )[0] ).bind( 'mouseover', function()
					{
						var icon = jQuery( jQuery( this ).next( '.f_tooltip' )[0] );
						Epson.ToolTip.open( jQuery( this ).offset().top, jQuery( this ).offset().left-30, icon.html() );
					} ).bind( 'mouseout', Epson.ToolTip.close );

					jQuery( this ).css( { padding:0 } );
				}
				else
				{
					jQuery( this ).bind( 'mouseover', function()
					{
						Epson.ToolTip.open( jQuery( this ).offset().top, jQuery( this ).offset().left-30, jQuery( this ).html() );
					} ).bind( 'mouseout', Epson.ToolTip.close );
				}
			} );
		}
	},

	displayError: function( valid, input, message )
	{
		message = escape(message);
		var form_item = jQuery( jQuery( input )[0] ).parents( '.formItem' );

		if ( jQuery('.warrantyIntro').length && jQuery('.productRegDetails').length) form_item = jQuery( jQuery( input )[0] ).parent( 'div' );

		if ( !form_item.length ) form_item = jQuery( jQuery( input )[0] ).parents( 'form' );
		var errors = escape(form_item.find( '.f_error_text' ).text());		
		if ( !valid )
		{
			if ( !errors.match( message ) )
			{
				form_item.addClass( 'f_error' ).prepend( jQuery( '<div class="f_error_text" id="generalError.'+unescape(message)+'"><strong>'+unescape(message)+'</strong></div>' ) );

				var height = form_item.find( '.f_error_text' ).height();

				form_item.find( '.f_error_text' ).css( { height:0, visibility:'', minHeight:0 } ).animate( { height:height }, 300, 'swing', function() { jQuery( this ).css( { minHeight:'' } ) } );
        		//commented as part of 1.5 scope to remove all the tealeaf references
        		//var tlExceptionEvent = new TeaLeaf.Event("INFO", "EXCEPTION");
        		//var tlExceptionEventArray = new Array(
             	  // "Class", "f_error_text",
              	   //"Id", "generalError "+unescape(message));
       			//tlExceptionEvent.tlAddData(tlExceptionEventArray);
        		//tlExceptionEvent.tlSend();
			}
		}
		else
		{
			form_item.find( '.f_error_text' ).animate( { height:0 }, 300, 'swing', function(){ form_item.removeClass( 'f_error' ); form_item.find( '.f_error_text' ).remove() } );
		}
	}
}
