Epson = new Object;
MMFactory = null;


$(document).ready(function(){

	// Accordions
	if ( jQuery( '.heroNavigation' ).length ) jQuery( 'ul.heroNavigation' ).accordion( { active: 'li.selected', header: 'h4', alwaysOpen: false, animated:'easeslide', autoHeight:false, navigation:true, navigationFilter: function() { return jQuery( this.parentNode ).hasClass( 'selected' ) } } );
	
	// set up nugget layer triggers
	if ( Epson.Nuggets ) jQuery( '.nuggetParent' ).each( Epson.Nuggets.setup );

	// Language Dropdown
	if ( jQuery( '#selCountryDropdown' ).length )	Epson.LanguageDropdown.setup();
	
	// Generic Dropdowns
	jQuery( 'select.jsDropdown' ).each( function() { var dropdown = this; setTimeout( function() { new Epson.GenericDropdown( dropdown ) }, 1 ) } );
	
	// set up forms
	if ( jQuery( 'form' ).length ) Epson.Forms.setup();
	
	// set up Collapsable Modules
	jQuery( '.genericCollapsibleModule .header, .genericCollapsibleSection .header' ).each( function( i, item ) { new Epson.CollapsableModule( this, jQuery( '.collapsibleContent' )[i] ) } );
	
	// set up teaser carousels
	if ( Epson.TeaserCarousel ) jQuery( '.teaserCarousel' ).each( function() { new Epson.TeaserCarousel( this ) } );
	
	// setup pro graphics showcase
	if ( Epson.ProGraphics && jQuery( '#ProShowCase' ).length ) Epson.ProGraphics.setup();
	
	// setup category tabs
	if ( Epson.Category && jQuery( '#mainbody .categoryTabs .categoryNavigation' ).length ) Epson.Category.setup();
	
	// Fragment Popups
	if ( Epson.UI.Popup ) jQuery( '.triggerPopup' ).each( function() { jQuery( this ).bind( 'click', Epson.UI.Popup.loadGeneric ) } );
	
	// setup product data for various components
	//if ( Epson.ProductBrowser && Epson.ProductBrowser.data.length ) Epson.ProductBrowser.loadProductData();

	// setup product browser
	if ( Epson.ProductBrowser && jQuery( '#ProductBrowser' ).length ) Epson.ProductBrowser.setup();

	// setup product comparisons
	//if ( Epson.ProductComparison && jQuery( '#CompareItems' ).length ) Epson.ProductComparison.setup();	

    // bind myepson home dropdown change event
    jQuery('#MyEpsonProductSelection').bind('change', function(){
    window.location = this.value;

});

   // bind operating system dropdown change event
    jQuery('#OsSelection').bind('change', function(){
    osSwitch(this.value);

});   

	//add for form inital value autorun	
jQuery( '#FindProduct input[type="text"], .newsletterSignup input[type="text"]' ).each( function (){
jQuery( this ).focus( function () {
if ( this .value == this .defaultValue ) {
this .value = "" ;
}
}).blur( function () {
if ( ! this .value.length ) {
this .value = this .defaultValue;
}
});
});
 

});

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;
		window.location = 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 )
		}
	}
}

// GENERIC DROPDOWNS
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();
			_self.options[i][2] = jQuery( this ).attr( 'class' )

			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;
	
	// setting max width for the dropdowns
	width = width > 420 ? 420 : width;

	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 in_popup = this.element.parents( '#Popup' ).length ? true : false;
		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;
		var top = this.position[1];
		if ( in_popup && !( jQuery.browser.msie && jQuery.browser.version < 7 ) ) top = top - jQuery( document ).scrollTop();

		// 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:top+'px', left:this.position[0]+'px' } );
		if ( in_popup && !( jQuery.browser.msie && jQuery.browser.version < 7 ) ) this.ddelement.css( { position:'fixed' } );
		if ( jQuery.browser.msie && jQuery.browser.version < 7 && in_popup ) { var dd = this.ddelement; jQuery( window ).bind( 'scroll', function() { dd.css( { top:top + jQuery( document ).scrollTop() } ) } ) };
		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' );

			if (this.options[x][2]) item.addClass( this.options[x][2] )

			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];
}

// ToolTips

Epson.ToolTip =
{
	open: function( top, left, text )
	{
		// destroy any existing tooltips
		jQuery( '#container .genericTooltip' ).remove();

		// create tooltip
		var tooltip = jQuery( '<div class="genericTooltip"><div class="top"></div><div class="content"></div><div class="bottom"></div></div>' );
		if ( !jQuery.browser.msie || jQuery.browser.version > 7 ) tooltip.css( { opacity:0 } );
		tooltip.find( '.content' ).html( text );
		jQuery( '#container' ).append( tooltip );

		// position
		tooltip.css( { height:'auto' } );
		var height = tooltip.height();
		tooltip.css( { height:0 } );
		tooltip.css( { top:top + 'px', left:left + 'px' } );

		if ( !jQuery.browser.msie || jQuery.browser.version > 7 ) setTimeout(function(){tooltip.animate( { top:top-height, height:height, opacity:1 }, 300, 'easeout' )},250);
		else setTimeout(function(){tooltip.animate( { top:top-height, height:height }, 300, 'easeout' )},250);
	},
	
	close: function(e)
	{
		if ( !jQuery.browser.msie || jQuery.browser.version > 7 ) jQuery( '#container .genericTooltip' ).animate( { opacity:0 } ).queue( function() { jQuery( this ).remove() } );
		else jQuery( '#container .genericTooltip' ).remove();
	}
}


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( '.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 );
				}
			} );
		}
	},
	HiddenTerms:
	{
		setup: function( link, field )
		{
			// hide the privacy policy
			var pp_height = field.parents( '.fi_content' ).height();
			if ( pp_height == 0) pp_height = 150;

			field.parents( '.formItem' ).css( { display:'none', height:0 } );

			// add privacy policy event
			link.bind( 'click', function(e)
			{
				var l = link; var f = field.parents( '.formItem' );
				if ( !f.hasClass( 'open' ) ) f.show().addClass( 'open' ).animate( { height:pp_height }, 300, 'swing', Epson.Forms.HiddenTerms.finish );
				else f.removeClass( 'open' ).animate( { height:0 }, 300, 'swing', function() { jQuery( this ).hide(); Epson.Forms.HiddenTerms.finish() } );

				e.preventDefault();
			} );
		},

		finish: function()
		{
			if ( Epson.UI.Popup.element ) Epson.UI.Popup.position();
		}
	}

}



/* Generic Comparison Collapsible Module */
Epson.CollapsableModule = function ( header, content )
{
	jQuery( header ).bind( 'click', function()
	{
		jQuery( content ).slideToggle();
		jQuery( this ).toggleClass( 'open' );				
	} );

	if ( jQuery( content ).find('#TermsConditions').length ) Epson.Forms.HiddenTerms.setup( jQuery( '#TermsConditions' ), jQuery( '#f_terms_conditions' ) );
}


// Fragment Popups

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();

				// set up dropdowns
				jQuery( '#Popup select.jsDropdown' ).each( function() { new Epson.GenericDropdown( this ) } );

				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 = unescape( 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 clearfix"></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 );

			if (jQuery.browser.msie) if (e) e.preventDefault();	
			//else  if ( e.preventDefault() ) 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' );
			}
		}
	}
}


// Epson.Download

Epson.Download =
{
	Validation:{},

	setup: function()
	{
		// set up hidden terms
		//Epson.Forms.HiddenTerms.setup( jQuery( '#PrivacyPolicy' ), jQuery( '#f_dl_privacypolicy' ) );
		//Epson.Forms.HiddenTerms.setup( jQuery( '#LicenseAgreement' ), jQuery( '#f_dl_licenseagreement' ) );

		// set up events
		jQuery( '#DownloadForm' ).bind( 'submit', Epson.Download.send );
		jQuery( '#DownloadForm a.button.close' ).bind( 'click', Epson.UI.Popup.close );					
	},
	setPopup: function()
	{
		
		jQuery('.buttonDownload').each(function(){ jQuery(this).unbind( 'click', Epson.UI.Popup.loadGeneric )}); 		
	},
	send: function(e)
	{
		if ( Epson.Download.Validation.validateForm() ) 
		{
			setTimeout( Epson.UI.Popup.close, 100 );
			if (jQuery('#f_dl_privacy').attr('checked') )  jQuery('.buttonDownload').each(function()
			{ 
				jQuery(this).unbind( 'click', Epson.UI.Popup.loadGeneric )								
			}); 
		}
		else e.preventDefault();			
	}

}

