
UI.components = {};
var UIC = UI.components;

UIC.progress_indicator = {
							node: $('#spinner'),
							toggle: function () {
										var pi = this.node;
										
										if (pi.is(':hidden')) {
											pi.show();
										}
										else {
											pi.hide();
										}
									},
							show: 	function() {
										this.node.show();
									},
							hide: 	function() {
										this.node.hide();
									}
						};
						
$('.link_add_domain').click(function () {
	return false;
});

UIC.tabs = {
			node: $('#empty_basket'),
			active: 'active_tab'
			};

UIC.tabs.tabs = UIC.tabs.node.find('.tabs li');

UIC.tabs.activate = function() {
						var $t = $(this);
						var active = UIC.tabs.active;
						
						if (!($t.is('#'+active))) {
							$('#'+active).removeAttr('id');
							$t.attr('id', active);
						}
						$('.tabs_content').find('>div').css('display','none');
						
						/* IE<7 will return whole URL with http:// as a result */
						/* of $(node).attr('href') */
						/* so there's some string manipulation to make sure it's always correct */
						var href_id = $t.children('a').attr('href');
						href_id = href_id.substr(href_id.indexOf('#'));
						/* end of IE7 workaround */
						
						$(href_id).css('display','block');
					
						return false;
						
					}
					
UIC.tabs.tabs.click(UIC.tabs.activate);
					
UIC.empty_basket= {

					show : function() {
						var empty_node = $($('#empty_basket_html').val());
						$('.content:last').empty().append(empty_node);
						$('#empty_basket').find('.tabs li').click(UIC.tabs.activate);
					}
				}

UIC.continue_validation_message =	{
										node: $('#continue_validation_message'),
										
										show: function(message) {
											var self = this;
											if (message && !this.is_shown) {
												this.node.text(message).fadeIn(function() {
													self.is_shown = true;
												});
											}
										},
										
										is_shown:false,
										
										reset: function() {
											this.is_shown = false;
											this.node.css('display','').addClass('hidden');
										}

									}
									
if (window.OP) {
OP.UI = new (function() {
		
		function tokenize_extension(ext) {
								//simple replaces all . for _ in domain extension
								return ext.replace(/\./g,'_');
							};

		function truncate_hostname(str_domain,count) {
							
								count=count||6;
								var middle = '...';
								
								if (str_domain.length > count*2 + middle.length) {
									str_domain = str_domain.substr(0,count).concat(middle,str_domain.substr(str_domain.length-count));
								}
								return str_domain;
								
							};
		
		this.views = {
				
				basket:	{
				
						ITEMS: {
							
								'Domain': function Item_UI(item) {
					
								var self = this;
								
								var selector = '#item_'+item.type.replace(/ /g,'_')+'_'+item.id;
								var hostname_extension_truncated = truncate_hostname(item.hostname)+item.extension;
								var billing_data = D.PROD_CONF[item.type].PROD.prices(item.extension);
								
								var elems = {
										node :$(selector),
										parent_node: $('.sub_items')
										
								};
								
								if (item.type === 'Domain') { //Fixme, where is the abstraction?
									
								elems.html= function() {
											
												return $(
													
													['<dl class="subitem_properties" id="'+selector.substr(1)+'">',
														'<input type="submit" value="DELETE" class="delete">',
														'<dt class="domain_name">'+hostname_extension_truncated+'</dt>',
														'<dd></dd>',
														'<br />',
														'<dt>Billing period</dt>',
														'<dd class="item_billing_period_wrap">',
														'</dd>',
													'</dl>'
													].join("\n"));
												};
									
								}
								
								if (item.type === 'Domain Transfer') {
									
									elems.html= function() {
												
												return $(
													
													['<dl class="subitem_properties" id="'+selector.substr(1)+'">',
														'<input type="submit" value="DELETE" class="delete">',
														'<dt class="domain_name">'+hostname_extension_truncated+' (transfer)</dt>',
														'<dd class="item_billing_period_wrap">',
														'</dd>',
													'</dl>'
													].join("\n"));
												};
												
								}
								
								if (!elems.node.length) {
									//console.log('recreating the item\'s UI', elems.node, selector);
									elems.node = elems.html();
									var billing_node = create_billing_node().addClass('item_billing_period');
									billing_node.change(handle_change);
									elems.node.find('.item_billing_period_wrap').append(billing_node);
									elems.parent_node.append(elems.node);
								}	
								
								elems.billing = elems.node.find('>dd.item_billing_period_wrap .item_billing_period');
								elems.billing.change(handle_change);
								//console.log(elems.billing.length);
											
								/*			if (elems.billing.length) {
												
												//console.log('length pass')
												
												if (elems.billing.is('select')) {//multiple billings
													
													//alert('is select pass')
													
													//test for current instance but assign event to freshly
													//found object - cause it's live object
													$(selector).
														find('>dd.item_billing_period_wrap .item_billing_period').
														change(function() {
														$(item).trigger('billing_change_request',[$(this).val()])
													})
													
												}

											}
								*/
										
								elems.node.find('>.delete').click(function() {
									
									item.remove_from_basket();
									return false;
								
								});
								
								function handle_change() {
									$(item).trigger('billing_change_request',[$(this).val()])
								}
								
								function create_billing_node() {
									
									//console.log('this gets called')
											
									var select_value = item.billing_period;
									
									var billing_periods = D.PROD_CONF[item.type].PROD.get_billing_periods(item);
									//console.log(billing_periods);
									
									var new_node;
										
									var billing_data = [];
									
									if (billing_periods[1] && billing_periods[1].billing) {
									
										for (var i =0;i<billing_periods.length;i++) {
											
											if (billing_periods[i]) {
											
												billing_data.push({
													value:billing_periods[i].billing.name,
													label:	function () {
																var ret = 	this.billing_description+
																			' - '+
																			F.to_price(this.net_price);
																if (this.net_price>this.old_net_price) {
																	ret +=	' (was '+F.to_price(this.old_net_price)+')'
																}
																return ret;
															},
													billing_description:billing_periods[i].billing.description,
													net_price:			+billing_periods[i].net_price,
													old_net_price:		+billing_periods[i].old_net_price
												});
											
											}
										
										}
									
									}
									
									else {
										
										billing_data.push({
											
											label: function () {
														var ret = F.to_price(this.net_price);
														if (this.net_price>this.old_net_price) {
															ret +=	' (was '+F.to_price(this.old_net_price)+')'
														} 
														return ret;
													},
											net_price:+billing_periods[0].net_price,
											old_net_price:+billing_periods[0].old_net_price
										
										});
									
									}
															
									new_node = $(UI.prepare_choice(
															billing_data,
															select_value
																	));
								
									return new_node;
									
								}
								
								this.update_billing_period = function() {
											
									var new_node = create_billing_node()
									
									new_node.addClass('item_billing_period');
									
									new_node.change(handle_change);
									
									elems.node.find('>dd.item_billing_period_wrap').empty().append(new_node);
											
									$(B).trigger('item_properties_UI_updated', [item]);
										
								};
								
								this.update_price = this.update_billing_period;
										
								this.remove = function() {
									
									elems.node.fadeOut(
										function() {
											elems.node.remove();
											$(B).trigger('item_UI_removed', [item]);
										}
									);
									
								}
								
								this.group_item = function() {
								
								}
								
								//console.log(elems, selector)
							
								}
								
								,
								
								'Dedicated Server': function Item_UI(item) {
					
										var self = this;
										
										var selector = '#item_'+item.type.replace(/ /g,'_')+'_'+item.id;
										
										var elems = {
												node :$(selector)
										};
										
										elems.billing = elems.node.find('.item_properties .item_billing_period');
											
											if (elems.billing.length) {
												
												if (elems.billing.is('select')) {//multiple billings
													
													elems.billing.change(function() {
														//console.log('value read', $(this).val())
														$(item).trigger('billing_change_request',[$(this).val()])
													})
													
												}

										}
										
										elems.os = elems.node.find('.item_properties .item_operating_system');
											
											if (elems.os.length) {
												
												if (elems.os.is('select')) {//multiple billings
													
													elems.os.change(function() {
														//console.log('os dropdown change caught');
														$(item).trigger('os_change_request',[$(this).val()])
													})
													
												}

										}

										elems.node.find('>.delete').click(function() {
											//console.log(item);
											item.remove_from_basket();
											return false;
										
										});
										
										function handle_billing_change() {
											$(item).trigger('billing_change_request',[$(this).val()])
										}
										
										function handle_os_change() {
											$(item).trigger('os_change_request',[$(this).val()])
										}
								
										this.remove = function() {
											
											elems.node.fadeOut(
												function() {
													elems.node.remove();
													$(B).trigger('item_UI_removed', [item]);
												}
											);
											
										}
										
										this.update_name = function() {
											elems.node.find('span.item_name').text(item.package);
										}
										
										this.update_billing_period = function update_billing_period() {
											
											//console.log('change on billing node dropdown');
											
											var select_value = item.billing_period;
											
											//console.log(select_value);
											//console.log(select_value);
											
											/*if (elems.billing.length && elems.billing.is('select')) {
												
												elems.billing.find("[value='"+select_value+"']").attr('selected', 'selected');
											
											}
											
											else {*/
											
											var billing_periods = D.PROD_CONF[item.type].PROD.get_billing_periods(item);
											//console.log(billing_periods);
											
											var billing_node,
												setup_node;
												
											var billing_data = [];
										
											for (var bp in billing_periods) {
												
												billing_data.push({
													value:billing_periods[bp].billing.name,
													label:	function () {
																var ret = 	this.billing_description+
																			' - '+
																			F.to_price(this.net_price);
																if (this.net_price>this.old_net_price) {
																	ret +=	' (was '+F.to_price(this.old_net_price)+')'
																}
																return ret;
															},
													billing_description:billing_periods[bp].billing.description,
													net_price:			+billing_periods[bp].net_price,
													old_net_price:		+billing_periods[bp].old_net_price
												})
											
											}

											billing_node = $(UI.prepare_choice(
																	billing_data,
																	select_value
																	)).addClass('item_billing_period');
											
											billing_node.change(function() {$(item).trigger('billing_change_request',[$(this).val()])});
											
											elems.node.find('.item_properties dd.item_billing_period_wrap').empty().append(billing_node);
											
											if (billing_periods[select_value].setup_fee !== '0.00') {
												var fee_string = ' + '+F.to_price(billing_periods[select_value].setup_fee);
												var ending = '';
												}
											else {
												var fee_string = '(no';
												var ending = ')';
											}
												setup_node = $('<br /><span class="item_setup_fee">'+fee_string+' set up fee'+ending+'</span>');
												elems.node.find('.item_properties dd.item_billing_period_wrap').append(setup_node);
											
											//}
											
											$(B).trigger('item_properties_UI_updated', [item]);
											
											
										
										};
										
										this.update_price = this.update_billing_period;
										
										this.update_os = function() {
										
											var oss = D.PROD_CONF[item.type].PROD.get_OSs(item);
											var new_node;
												
											var os_data = [];
											
											for (var o in oss) {
												
												os_data.push({
													value:o,
													label:o
												})
											
											}
												
											new_node = $(UI.prepare_choice(
																	os_data,
																	item.os
																	)).addClass('item_operating_system');
											
											//console.log(oss, os_data, item.os)
											
											elems.node.find('dd.item_operating_system_wrap').empty().append(new_node);
										}
										
										this.refresh = function() {
											//console.log('refreshing UI')
											this.update_name();
											this.update_billing_period();
											this.update_os();
										}
										
										//console.log(elems, selector)
									
								}
								
							
							},
							
							API: {
							
								'search': function domain_search_UI(parent) {
							
										var main = parent;
										var self = this;
										
										var elems = {
													domain_search_wrap: $('dl.add_domain'),
													domain_search_submit: $('#item_domain_submit'),
													domain_input: $('#item_domain_search'),
													search_form: $('#domain_subitem_search'),
													primary_results_wrap: $('#domain_result_row'),
													results_table: $('#domain_results_table'),
													results_container: $('#domain_results_table tbody'),
													results_row: $([
																'<tr>',
																	'<td class="domain_name">',
																		'<span title=""></span>',
																	'</td>',
																	'<td class="domain_status">',
																		'<span></span>',
																	'</td>',
																	'<td class="domain_input">',
																	'</td>',
																	'<td class="domain_add">',
																		'<input type="hidden" value="" name="extension"/>',
																		'<input type="hidden" value="" name="hostname"/>',
																		'<a href="#add" class="click_add_domain"></a>',
																	'</td>',
																'</tr>'
										
															].join('')),
													get_more_results_link: $('#get_more_results_link')
													}
													
										$(self).bind('domain_search_object_ready',function(e, data_obj) {
											self.capture_domain_search();
										});
										
										$(self).bind('domain_search_in_progress',function(e, data_obj) {
											display_search_progress();
										});
										
										$(self).bind('present_domain_results',function(e, data_obj) {
											ui_present_results(data_obj);
										});
										
										$(self).bind('item_in_basket_OK',function(e, data_obj) {
											ui_hide_results();
										});
										
										$(self).bind('show_domain_search_input', function() {
											show_domain_search_input();
										});
										
										function display_search_progress() {
											UI.components.progress_indicator.toggle();
										};
										
										
										function ui_present_results(data_obj) {
											
											//remove search progress
											UI.components.progress_indicator.hide();

											show_domain_search_input();
											elems.get_more_results_link.removeClass('hidden');
											//update table rows with results
											var tbody = elems.results_table.find('tbody');
											
											if (data_obj.status) {
												
												var hostname = data_obj.hostname;
												var domain_results = data_obj.domains;
												
												//go through the results and display search statuses
												//actually depending on the status it's going to be a different product
												
												for (var d=0; d<domain_results.length; d++) {
													
													var domain_result			= domain_results[d];
													
													var status					= domain_result.status;
													var extension				= domain_result.extension
													
													var domain_with_ext			= hostname+extension;
													var short_domain_with_ext	= truncate_hostname(hostname)+extension;
													var class_from_extension	= 'r'+tokenize_extension(extension);
													var product_type			= D.PROD_CONF.recognize_product_by_status(status);
													
													var billing_data			= D.PROD_CONF[product_type].PROD.prices(extension);

													var chosen_billing;
													
													//console.log(billing_data);
													

													if (billing_data.length > 1) {
														var years_input = '<select>'+
														UI.populate_select(billing_data,'billing_name',function () {
																									return this.years_label + ' - ' + F.to_price(this.price);
																									},1)
														+'</select>';
													}
													
													else {
														var years_input = '<span class="years">'+billing_data[0].years_label+'</span> - ' + F.to_price(billing_data[0].price);
														chosen_billing = billing_data[0].billing_name;
													}

													var row = elems.results_row.
														clone().addClass('only_row').
															attr('id', class_from_extension).
															find('.domain_name span').text(short_domain_with_ext).attr('title',domain_with_ext).end().
															find('.domain_status span').text(status).addClass(status).end();
															
															
													if (status === 'available') {
													
														row.
															find('.domain_input').html(years_input).end().
															find('.click_add_domain').text('add').end();
															
													}
													
													if (status === 'taken') {
														//console.log('billing data for taken', billing_data);
														row.
															find('.domain_input').remove().end().
															find('.domain_add').attr('colspan','2').end().
															find('.click_add_domain').html('I own this, transfer it for '+F.to_price(billing_data[0].price)).end();
															
													}
				
													row.appendTo(tbody);
													
													$.data(
																		row.find('.domain_add .click_add_domain').get(0),
																		'domain_data',
																		{
																		hostname:hostname,
																		extension:domain_result.extension,
																		billing_period:chosen_billing,
																		status:status,
																		product_type: product_type
																		}
																		);
																		
													$.data(
																		row.find('.domain_add .click_add_domain').get(0),
																		'parent_row',
																		row
																		);
														
													$('.domain_add .click_add_domain').click(function () {
																						
																						var row = $.data(this, 'parent_row');
																						var user_input = $.data(this, 'domain_data');
																						var is_multiple_choice = row.find('.domain_input select').length>0;
																						
																						user_input.billing_period = is_multiple_choice ? row.find('.domain_input select').val() : user_input.billing_period;
																						
																						
																						
																						$(OP).trigger('domain_chosen',[user_input]);
													
																						return false;
																						
																						});
																						
													elems.get_more_results_link.find('a').attr('href', '/order/domain-search?domain='+hostname)
																								
													} //ends producing table rows
													
											}//ends main if
										
										};//ends UI function
										
										function ui_hide_results() {
											//console.log('hiding UI results')
											elems.primary_results_wrap.addClass('hidden');
											elems.domain_search_wrap.addClass('hidden');
											elems.get_more_results_link.addClass('hidden');
										};
										
										function show_domain_search_input() {
											//show results
											elems.primary_results_wrap.removeClass('hidden');
											elems.results_table.find('tbody').empty();
											elems.domain_search_wrap.removeClass('hidden');
										};
										
										this.capture_domain_search = function() {
											elems.search_form.submit(function() {
												
												var user_input = elems.domain_input.val();
												var extensions = $.map(D.domain_search_standard_extensions, function(a){return a}); //flatten
												var dom_ext = F.get_hostname_extension(user_input, extensions);
												dom_ext.hostname = F.cleanup_hostname(dom_ext.hostname, dom_ext.extension);
												elems.domain_input.val('www.'+dom_ext.hostname+dom_ext.extension); //put sanitized values in the form
												$(main).trigger('domain_search_initiated', [dom_ext]);
												
												return false;
											});
										}	

									}
								
							},
							
					basket: function basket_ui(parent) {
						
						var main = parent;
						var self = this;
						
						var elems = {
							parent: $('#body .cols-subn-right .content'),
							node: $('#main_basket_form'),
							empty_basket: $($('#empty_basket_html').text()),
							price: $('#total_money .netprice'),
							setup_fee: $('#setup_fee_money .netprice'),
							savings: $('#savings_money .netprice')
							}
						
						function update_pricing(new_price) {
							elems.price.html(F.to_price(new_price.total));
							elems.setup_fee.html(F.to_price(new_price.setup_fee));
							elems.savings.html(F.to_price(new_price.savings));
						}
						
						//events
						$(self).bind('item_in_basket_OK', function(e,item, new_price) {
							update_pricing(new_price);
						});
						
						$(self).bind('item_deleted_OK', function(e, new_price) {
							update_pricing(new_price);
						});
						
						$(self).bind('grouping_in_basket_OK', function(e, new_price) {
							update_pricing(new_price);
						});
						
						$(self).bind('item_properties_UI_updated', function(e, new_price) {
							update_pricing(new_price);
						});
						
						
						
						$(self).bind('no_empty_slots_in_basket_FAIL', function(e,item) {
							//console.warn('Can\'t add to basket because basket\'s full')
						});
						
						$(self).bind('basket_empty', function(e,item) {
							elems.parent.empty();
							elems.empty_basket.appendTo(elems.parent);
							$('#empty_basket').find('.tabs li').click(UIC.tabs.activate);
						});
						
						

					}
						
				},
				
				'domain-search':{
						
						ITEMS: {
							'Domain': function Item_UI(item) {
					
								var self = this;
								
								var selector = '.item_'+item.type.replace(/ /g,'_')+'_'+item.id;
								var hostname_extension_truncated = truncate_hostname(item.hostname)+item.extension;
								
							
								var elems = {
										node :$(selector),
										sibling: {
											after:$('dt.total')
										}
								};
								
								var billing_description = item.billing_period; //by default it will be short name
								
								if (item.type === 'Domain') { //Fixme, where is the abstraction?
									
									var billing_periods = D.PROD_CONF[item.type].PROD.get_billing_periods(item);
									
									for (var i =0;i<billing_periods.length;i++) {
											
											if (billing_periods[i]) {
												
												if (billing_periods[i].billing.name===item.billing_period) {
													
													billing_description = billing_periods[i].billing.description
													
												}
												
											}
											
									}
									
									var billing = 		['<span class="item_contract">',
														billing_description,
														'</span> - ',
														'<span class="item_price">',
															'<span class="netprice">',
																F.to_price(item.price.net_price),
															'</span>',
														'</span>'].join('');
									
								}
								
								if (item.type === 'Domain Transfer') { //Fixme, where is the abstraction?
								
									var billing = 		['<span class="item_price">',
															'<span class="netprice">',
																F.to_price(item.price.net_price),
															'</span>',
														'</span>'].join('');
									
								}
								
								elems.html= function() {
												
												return $(
													
													['<dt class="'+selector.substr(1)+'">',
														'<span class="item_product">',
														item.type,
														'</span>',
														'<input type="submit" name="delete_item_'+item.id+'" value="1" class="delete"/>',
													'</dt>',
													'<dd class="'+selector.substr(1)+'">',
														'<span class="item_hostname">',
														hostname_extension_truncated,
														'</span><br/>',
														billing,
													'</dd>'
													].join(''));
													
												
											};
								
								if (!elems.node.length) {
									//console.log('showing the item\'s UI', elems.html());
									elems.node = elems.html();
									elems.sibling.after.before(elems.node);
								}	
								//console.log(elems, elems.node, 'searching for delete button')
								//console.log('domain node', elems.node.find('>.delete'));
								
								elems.node.find('>.delete').click(function() {
									item.remove_from_basket();
									return false;
								
								});
								
								this.remove = function() {
									//console.log('domain to be removed =>', elems.node);
									elems.node.fadeOut(
										function() {
											elems.node.remove();
											$(B).trigger('item_UI_removed', [item]);
										}
									);
									
								}
								
								this.group_item = function() {
								
								}
							
							},
							
							'Dedicated Server': function Item_UI(item) {
							
								var self = this;
										
								var selector = '.item_'+item.type.replace(/ /g,'_')+'_'+item.id;
								
								var elems = {
										node :$(selector),
										html: function() {}
								};
								
								if (!elems.node.length) {
									elems.node = elems.html();
									elems.sibling.after.before(elems.node);
								}
								
								elems.node.find('>.delete').click(function() {
									//console.log('delete =>', elems.node);
									item.remove_from_basket();
									return false;
								
								});
								
								this.remove = function() {
									
									//console.log('hosting to be removed =>', elems.node);
									
									elems.node.fadeOut(
										
										function() {
										//console.log('fadeOut')
											elems.node.remove();
											$(B).trigger('item_UI_removed', [item]);
										}
									);
									
								}
								
								
								this.group_item = function() {
								
								}
								
								//console.log('creating UI');
							
							}
						},
						
						API: {
								
							'search': function domain_search_UI(parent) {
							
							var main = parent;
							var self = this;
							
							var elems = {
								domain_input: $('#domain_search_input'),
								results: $('#domain_results_wrap'),
								primary_results_wrap: $('#domain_primary_results_wrap'),
								secondary_results_wrap: $('#domain_secondary_results_wrap'),
								results_table: $('#domain_results_table'),
								results_container: $('#domain_results_table tbody'),
								results_row: $([
													'<tr>',
														'<td class="domain_name">',
															'<span title=""></span>',
														'</td>',
														'<td class="domain_status">',
															'<span></span>',
														'</td>',
														'<td class="domain_input">',
														'</td>',
														'<td class="domain_add">',
															'<input type="hidden" value="" name="extension"/>',
															'<input type="hidden" value="" name="hostname"/>',
															'<a href="#add" class="click_add_domain"></a>',
														'</td>',
													'</tr>'
							
												].join("\n")),
								no_available_domains_message: $('<tr><td colspan="4">We sorry we couldn\'t find any available domains. Please try again with a different domain name.</td></tr>'),
								search_form:$('#domain_search_form'),
								search_progress:$('<p class="loading">Working...</p>')

							}
							
							$(self).bind('domain_search_object_ready', function() {
								capture_domain_search();
								capture_location_params();
							});
							
							$(self).bind('domain_search_in_progress',function(e, data_obj) {
								display_search_progress();
							});
							
							$(self).bind('present_domain_results',function(e, data_obj) {
								ui_present_results(data_obj);
							});
							
							$(self).bind('show_more_input_prompt', show_domain_input_prompt);
							
							function use_valid_domain(dom_ext) {
								elems.domain_input.val('www.'+dom_ext.hostname+dom_ext.extension); //put sanitized values in the form
							}
							
							function show_domain_input_prompt() {
								alert('Please enter domain name');
							}
							
							function validate_domain(user_input) {
								
								var extensions = $.map(D.domain_search_standard_extensions, function(a){return a}); //flatten
								var dom_ext = F.get_hostname_extension(user_input, extensions);
								dom_ext.hostname = F.cleanup_hostname(dom_ext.hostname, dom_ext.extension);
															
								return dom_ext;
							
							}
							
							function capture_domain_search() {
								elems.search_form.submit(function() {
									var user_input = elems.domain_input.val();
									var dom_ext = validate_domain(user_input);
									
									if (dom_ext.hostname) {
										use_valid_domain(dom_ext);
										$(main).trigger('domain_search_initiated', [dom_ext]);
									}
									else {
										$(main).trigger('too_little_input');
									}
									return false;
								})
							};
							
							function capture_location_params() {
								
								var params = F.get_location_object();
								
								if (params.domain) {
									var dom_ext = validate_domain(params.domain);
									use_valid_domain(dom_ext);
									$(main).trigger('domain_search_initiated', [dom_ext]);
								}
							
							};
							
							function remove_search_results() {
								elems.primary_results_wrap.addClass('hidden');
							}
							
							function display_search_progress() {
								UI.components.progress_indicator.show();
							};
							
							
							function prepare_request_data_from_user_input(user_input) {
								
								//get basic request
								var request_map = []
								var fields = D.session.request.fields;
								//and some more from user input
								fields.hostname = user_input.hostname;
								fields.extension = user_input.extension
								fields.dyears = user_input.chosen_billing
								
								for (var i in fields) {
									request_map.push(
											{
												'name':i,
												'value':fields[i]
											
											});
								}
								
								return request_map;

							};

							function ui_present_results(data_obj) {
								
								//remove search progress
								UI.components.progress_indicator.hide();
								
								elems.primary_results_wrap.removeClass('hidden');
								
								var tbody = elems.results_table.find('tbody').empty();
								//update table rows with results
								
								if (data_obj.status) {
									
									var hostname = data_obj.hostname;
									var domain_results = data_obj.domains;
									
									for (var d=0; d<domain_results.length; d++) {
										
										var domain_result			= domain_results[d];
										
										var status					= domain_result.status;
										var extension				= domain_result.extension
										
										var domain_with_ext			= hostname+extension;
										var short_domain_with_ext	= truncate_hostname(hostname)+extension;
										var class_from_extension	= 'r'+tokenize_extension(extension);
										var product_type			= D.PROD_CONF.recognize_product_by_status(status);
										
										var billing_data			= D.PROD_CONF[product_type].PROD.prices(extension);

										var chosen_billing;
										
										//display multi or single billing period
											
										if (billing_data.length > 1) {
											var years_input = '<select>'+
											UI.populate_select(billing_data,'billing_name',function () {
																						return this.years_label + ' - ' + F.to_price(this.price);
																						},1)
											+'</select>';
										}
										
										else {
											var years_input = '<span class="years">'+billing_data[0].years_label+'</span> - ' + F.to_price(billing_data[0].price);
											chosen_billing = billing_data[0].billing_name;
										}

										
										//append stuff to row
										
										var row = elems.results_row.
											clone().
												attr('id', class_from_extension).
												find('.domain_name span').text(short_domain_with_ext).attr('title',domain_with_ext).end().
												find('.domain_status span').text(status).addClass(status).end();
													
													
										if (status === 'available') {
										
											row.
												find('.domain_input').html(years_input).end().
												find('.click_add_domain').text('add').end();
												
										}
										
										if (status === 'taken') {
											//console.log('billing data for taken', billing_data);
											row.
												find('.domain_input').remove().end().
												find('.domain_add').attr('colspan','2').end().
												find('.click_add_domain').html('I own this, transfer it for '+F.to_price(billing_data[0].price)).end();
												
										}

										row.appendTo(tbody);
										
										//put some product data on the lin, so it's available for click object
										
										
										
										$.data(
															row.find('.domain_add .click_add_domain').get(0),
															'domain_data',
															{
															hostname:hostname,
															extension:domain_result.extension,
															billing_period:chosen_billing,
															status:status,
															product_type: product_type
															}
															);
															
										$.data(
															row.find('.domain_add .click_add_domain').get(0),
															'parent_row',
															row
															);
										
								
										//add click event to the add link
										
										$('.domain_add .click_add_domain').click(function () {
																			

																			var row = $.data(this, 'parent_row');
																			var user_input = $.data(this, 'domain_data');
																			var is_multiple_choice = row.find('.domain_input select').length>0;
																			
																			user_input.billing_period = is_multiple_choice ? row.find('.domain_input select').val() : user_input.billing_period;
																	
																			$(OP).trigger('domain_chosen',[user_input]);
																			
																			return false;
																			
																			});
																					
										} //ends producing table rows
										
										
										elems.results_table.find('tbody tr:even').addClass('alternate');
										
										
								}//ends main if
							
							};//ends UI function
							
					
						}//ends domain_search_UI
						
					}, //ends API
					
					basket: function basket_ui(parent) {
						
						var main = parent;
						var self = this;
						
						var elems = {
							parent: $('#body .cols-subn-right .content'),
							node: $('#main_basket_form'),
							price: $('#total_money .netprice'),
							savings: $('#savings_money .netprice')
							}
						
						function update_pricing(new_price) {
							elems.price.html(F.to_price(new_price.total));
							elems.savings.html(F.to_price(new_price.savings));
						}
						
						//events
						$(self).bind('item_in_basket_OK', function(e,item, new_price) {
							update_pricing(new_price);
						});
						
						$(self).bind('item_deleted_OK', function(e, new_price) {
							update_pricing(new_price);
						});
						
						$(self).bind('grouping_in_basket_OK', function(e, new_price) {
							update_pricing(new_price);
						});
						
						$(self).bind('item_properties_UI_updated', function(e, new_price) {
							update_pricing(new_price);
						});

						$(self).bind('no_empty_slots_in_basket_FAIL', function(e,item) {
							//console.warn('Can\'t add to basket because basket\'s full')
						});

					}
				
				},
				
				'confirm-transfers':{ITEMS:{}},
				
				'sign-up':{},
				
				'payment-details':{}
				
		
		}
		
		this.views.basket.ITEMS['Domain Transfer'] = this.views.basket.ITEMS.Domain;
		this.views.basket.ITEMS['Hosting Reseller'] = this.views.basket.ITEMS['Dedicated Server'];
		this.views.basket.ITEMS['Hosting'] = this.views.basket.ITEMS['Dedicated Server'];
		
		this.views['domain-search'].ITEMS['Domain Transfer'] = this.views['domain-search'].ITEMS.Domain;
		this.views['domain-search'].ITEMS['Hosting Reseller'] = this.views['domain-search'].ITEMS['Dedicated Server'];
		this.views['domain-search'].ITEMS['Hosting'] = this.views['domain-search'].ITEMS['Dedicated Server'];
		
		//confirm-transfers
		
		this.views['confirm-transfers'].ITEMS['Domain'] = this.views['domain-search'].ITEMS.Domain;
		this.views['confirm-transfers'].ITEMS['Domain Transfer'] = this.views['domain-search'].ITEMS['Domain Transfer'];
		this.views['confirm-transfers'].ITEMS['Dedicated Server'] = this.views['domain-search'].ITEMS['Dedicated Server'];
		this.views['confirm-transfers'].ITEMS['Hosting Reseller'] = this.views['domain-search'].ITEMS['Hosting Reseller'];
		this.views['confirm-transfers'].ITEMS['Hosting'] = this.views['domain-search'].ITEMS['Hosting'];
		
		this.views['confirm-transfers'].basket = this.views['domain-search'].basket;
		
		this.error_messages = 
			{
				'no_domain':'You must add a domain name to your order before continuing.'
			}
			
		this.extension_order = ['.co.uk', '.com', '.net', '.org', '.info', '.biz', '.org.uk', '.me.uk', '.ltd.uk', '.plc.uk'];

})();
}

UI.HoverEffectIE6	= function(sel, css_class) {
	$(sel).hover(  
		function() {  
			$(this).addClass(css_class);  
		},  
		function() {  
			$(this).removeClass(css_class);  
		});
	}

UI.prepareTooltips = 			function () {

										var tooltipsLinks = 'a.what, div.expandbar a',
											timeout;
											
										$(tooltipsLinks).
										
											mouseover(function () {
												clearTimeout(timeout);
												var _t=$(this),
													_title = $.data(_t.get(0),'ttl');
													
												var  _o=_t.offset(),
													_parent=$('#body'),
													_Po=_parent.offset(),
													_timestamp=new Date().getTime(),
													_tooltip=$('<div class="tooltip">'+_title+'</div>').
												css('top', _o.top-_Po.top+_t.height()/2+20+'px').
												css('left', _o.left-_Po.left+_t.width()/2+20+'px').
												attr('id', _timestamp);
												
												//get the mouse coords
												//update when table expands
												$.data(_t.get(0),'tooltip',_timestamp);
												timeout=setTimeout(function () {
														_parent.append(_tooltip).find('#'+_timestamp);
																	}, 500);
												return false;
										
											}).
										
											mouseout(function () {
												clearTimeout(timeout);
												var _t=$(this),
												_title = $.data(_t.get(0),'ttl'),
												_tooltip = '#'+$.data(_t.get(0),'tooltip');
											
												$(_tooltip).remove();
											}).
										
											each (function () {
												var _t=$(this);
												$.data(_t.get(0),'ttl',_t.attr('title'));
												_t.removeAttr('title');
											});

	
									};
								
UI.handleLiveChatButtons = 		function () {
									
										$('a, li').filter('.chat_with_us').click(function () {
																		   pssGoGow();
																		   return false;
																		   });
										
									};


$(document).ready(function() {
	if ($.browser.msie && parseInt($.browser.version)< 7) {
		UI.HoverEffectIE6("#nav li", "ie_hover");
	}
	UI.prepareTooltips();
	UI.handleLiveChatButtons();
});