


var Scroller = {
	bar_position:0,
	position:0,
	max_scroll_y:627,
	x_scroll_ctl_offset:-8,
	
	total_items:0,
	scroller_item_width:98,
	scroll_item_window_right_pad:695,
	
	init:function(){
		this.total_items = $('scroller_items_inner').select('.scroller_item').length;
	},
	
	snap:function(x,y,d){
		if(x<this.x_scroll_ctl_offset) x = this.x_scroll_ctl_offset;
		if(x>this.max_scroll_y) x = this.max_scroll_y;
		
		return [x,0];
	},
	
	scroll_left:function(){Scroller.scroll_discrete(-1);},
	scroll_right:function(){Scroller.scroll_discrete(1);},
	scroll_discrete:function(dir){
		var sc = $('scroll_control');
		n = parseInt(sc.style.left.gsub('px',''));
		n = n + dir*100;
		if(n>627) n = 627;
		if(n<-8) n = -8;
		sc.style.left = n.toString()+'px';
		Scroller.change(Scroller_drag);
	},
	
	change:function(d){
		var cd = d.currentDelta();
		
		var x = cd[0] - this.x_scroll_ctl_offset;
		var max_x = (this.total_items * this.scroller_item_width)-this.scroll_item_window_right_pad;
		
		var percent_s = x/(this.max_scroll_y + Math.abs(this.x_scroll_ctl_offset));
		var scroll_items_offset = Math.round(percent_s * max_x);
		
		var sci = $('scroller_items_inner');
		sci.style.marginLeft = -scroll_items_offset+'px';
		
		//fix safari display bug
		if(BrowserDetect.safari){
			setTimeout(function(){	$('scroller_items').setOpacity(0.9);
															setTimeout(function(){$('scroller_items').setOpacity(1);},1);
														},1);
		}
	},
	
	on_enter:function(){
		var s = $F('search_products');
		if(s.length==0){
			new Effect.Shake('search_products');
			return;
		}
		Scroller.search(s);
	},
	search:function(s){
		window.open('/products/search?s='+s, '_self');
	}
}



var SearchSite = {
	keyup:function(){
		
	},
	on_enter:function(){
		var s = $F('search_site');
		if(s.length==0){
			new Effect.Shake('search_site');
			return;
		}
		SearchSite.search(s);
	},
	on_enter_prod_cat:function(){
		var s = $F('search_products_nb');
		if(s.length==0){
			new Effect.Shake('products_search_box_inline_nav_left');
			return;
		}
		SearchSite.search(s);
	},
	search:function(s){
		window.open('/products/search?s='+s, '_self');
	}
}




var ButtonDeck = {
	c_ndx:0,
	count:function(){
		return $('deck_inner_area').select('.item').length;
	},
	
	next:function(){
		this.move(1);
	},
	prev:function(){
		this.move(-1);
	},
	move:function(dir){
		var max = this.count()-1;
		var min = 0;
		
		var ndx = this.c_ndx + dir;
		
		if(ndx >= max){
			this.scroll(0);
		}else if(ndx < 0){
			this.scroll(max-1);
		}else{
			this.scroll(ndx);
		}
	},
	scroll:function(ndx){
		var width = 422;
		//$('deck_inner_area').style.marginLeft = (-ndx*width).toString()+'px';
		new Effect.Move($('deck_inner_area'), { x: -(ndx-this.c_ndx)*width, y: 0, mode: 'relative', duration:0.5, queue: 'end' });
		
		this.c_ndx = ndx;
	}
	
}



var NewsItems = {
	c_ndx:0,
	count:function(){
		return $('newsitems').select('.news_item').length;
	},
	
	change:function(){
		var max = this.count()-1;
		
		var ndx = this.c_ndx;
		ndx = ndx + 1;
		
		if(ndx>max){
			ndx = 0;
		}
		
		var i=0;
		$('newsitems').select('.news_item').each(function(t){
			if(i==this.c_ndx){
				t.fade({duration:0.5, queue: 'front'});
			}else if(i==ndx){
				t.appear({duration:0.5, queue: 'end'});
			}else{
				t.hide();
			}
			i = i + 1;
		});
		
		this.c_ndx = ndx;
	}
}



var Deck = {
	lock:null,
	offset:0,
	items_available:6,
	item_width:226,
	deck_width:750,
	prevent_multi_click:false,
	per_page:3,
	forward:function(){
		Deck.move(1);
	},
	back:function(){
		Deck.move(-1);
	},
	move:function(direction){
		if(this.prevent_multi_click && this.lock==true) return;
		this.lock = true;
		var d = $('deck_inner_area');
		
		var r_offset = this.offset + direction;
		var max_offset = this.items_available - this.per_page;
		
		if(r_offset > max_offset) return;
		
		if(r_offset < 0){
			this.offset = 0;
		}else{
			this.offset = r_offset;
			new Effect.Move(d, { x: -direction * this.item_width, y: 0, mode: 'relative', duration:0.5, queue: 'end' });
		}
		
		//show hide arrows
		if(this.offset > 0 && this.items_available > this.per_page){
			this.light_back();
		}else{
			this.dim_back();
		}
		
		if(this.offset < max_offset){
			this.light_forward();
		}else{
			this.dim_forward();
		}
		
		
		if(this.prevent_multi_click) setTimeout(function(){Deck.lock = null;}, 1*1000)
	},
	dim_forward:function(){
		$('deck_forward_button').src = '/images/public/right_arrow_deck_fade.png';
	},
	light_forward:function(){
		$('deck_forward_button').src = '/images/public/right_arrow_deck.png';
	},
	dim_back:function(){
		$('deck_back_button').src = '/images/public/left_arrow_deck_fade.png';
	},
	light_back:function(){
		$('deck_back_button').src = '/images/public/left_arrow_deck.png';
	}
}






var Login = {
	controller_path:'/users',
	
	logout:function(){
		Request(this.controller_path+'/logout', {}, function(r){
			window.location.reload();
		}.bind(this));
	},
	
	start_login:function(){
		Poster.show('login');
	},
	hide_login:function(){
		Poster.hide('login');
	},
	login:function(){
		var p = $F('login_password');
		var e = $F('login_email');
		
		if(p==''){
			this.login_error("You must enter a password.");
			return;
		}
		
		if(e==''){
			this.login_error("You must enter an email.");
			return;
		}
		
		$('login_spin_pane').show();
		$('login_pane').hide();
		
		Request(this.controller_path+'/login', {email:e, password:p}, function(r){
			
			if(r.ok){
				window.location.reload();
			}else{
				$('login_spin_pane').hide();
				$('login_pane').show();
				this.login_error(r.msg);
			}
		}.bind(this));
	},
	login_error:function(m){
		Warn(m);
		Poster.flash_error('login');
	},
	
	
	
	start_signup:function(){
		Poster.show('sign_up');
	},
	hide_signup:function(){
		Poster.hide('sign_up');
	},
	signup:function(){
		var p = $F('signup_password');
		var p2 = $F('signup_password2');
		var e = $F('signup_email');
		var n = $F('signup_name');
		
		if(p==''){
			this.signup_error("You must enter a password.");
			return;
		}
		if(p==''){
			this.signup_error("You must enter an email.");
			return;
		}
		if(p!=p2){
			this.signup_error("Your passwords must match.");
			$('signup_password').reset();
			$('signup_password2').reset();
			return;
		}
		
		$('signup_spin_pane').show();
		$('signup_pane').hide();
		
		Request(this.controller_path+'/signup', {email:e,name:n,password:p}, function(r){
			
			$('signup_spin_pane').hide();
			$('signup_pane').show();
			
			if(r.ok){
				window.location.reload();
			}else if(r.requires_request){
				Dialogue.show({html:[r.email + " is not a pre-authorized email", "If you'd like to request access, you may do so. We will email you with instructions on how to activate your account, however there may be a delay before we can manually review your request."],
				img:'/images/public/block_user_64.png',
				ok_label:'Request access',
				on_ok:this.start_access_request.bind(this)});
			}else{
				this.signup_error(r.msg);
			}
		}.bind(this));
		
	},
	signup_error:function(m){
		Warn(m);
		Poster.flash_error('sign_up');
	},
	
	
	
	
	start_forgot_password:function(){
		Poster.show('forgot_password');
	},
	forgot_password:function(){
		var e = $F('forgot_password_email');
		
		if(e==''){
			this.forgot_password_error("You must enter an email.");
			return;
		}
		
		Request(this.controller_path+'/forgot_password', {email:e}, function(r){
			
			if(r.ok){
				window.open('/users/check_email', '_self');
			}else{
				this.forgot_password_error(r.msg);
			}
		}.bind(this));
		
	},
	forgot_password_error:function(m){
		Warn(m);
		Poster.flash_error('forgot_password');
	},
	hide_forgot_password:function(){
		Poster.hide('forgot_password');
	},
	
	
	start_access_request:function(){
		var p = $F('signup_password');
		var p2 = $F('signup_password2');
		var e = $F('signup_email');
		var n = $F('signup_name');
		
		Request(this.controller_path+'/request_access', {email:e, password:p, name:n}, function(r){
			
			
			if(r.ok){
				Dialogue.show({html:["Thanks for requesting access " + r.email + "!", "We will email you as soon as we have reviewed your request.", "If you need information right away please call us at 201-342-3338."],
				img:'/images/public/user_wait_64.png',
				ok_label:'Back to the web site',
				on_ok:function(){
					Dialogue.hide_dialogue();
					this.hide_signup();
					this.hide_login();
				}.bind(this),
				suppress_cancel_button:true});
			}
			
		}.bind(this));
	},
	access_request:function(){
		
	}
	
}






var _Prefs = Class.create(PosterController, {
	
	reload_page_on_save:false,
	
	switch_tab:function(m,o){
		o=$(o);
		$('uprefs_tabs').select('a').each(function(t){t.removeClassName('selected');});
		o.addClassName('selected');
		$$('.uprefs_body').each(function(t){t.hide();});
		$('uprefs_'+m).show();
		
		if(m=='info'){
			$('prefs_button_password').hide();
			$('prefs_button_info').show();
		}else{
			$('prefs_button_password').show();
			$('prefs_button_info').hide();
		}
		
		//adjust the poster to the new contents
		Poster.fit_poster_to_content('poster_preferences');
	},
	
	after_inspect:function(r){
		$('prefs_button_password').hide();
		$('prefs_button_info').show();
		
		var pch = $('poster_preferences').select('.poster_content_holder')[0];
		pch.style.backgroundImage = 'url(/images/public/prefs_back_top.png)';
		pch.style.backgroundRepeat = 'no-repeat';
	},
	
	
	
	save_info:function(){
		var args = Find([['up_', 'email name company address address_2 city state zip phone fax']]);
		
		if(args['email']==null){
			Warn('You must provide a valid email address.');
			this.flash_inspector_error();
			return;
		}
		
		Request(this.controller_path+'/save_info', args, function(r){
			
			if(r.ok){
				this.flash_inspector_ok();
			}else{
				this.flash_inspector_error();
			}
			
			try{
				if(this.reload_page_on_save==true && Rma.products.length==0){
					window.location.reload();
				}
			}catch(ex){}
			
		}.bind(this));
		
		
	},
	
	change_password:function(){
		var cp = $F('up_curr_password');
		var p = $F('up_password');
		var p2 = $F('up_password_2');
		
		if(cp==null){
			Warn('You must enter your current password.');
			this.flash_inspector_error();
			return;
		}else if(p==null || p.length <4){
			Warn('You must enter a new password of at least 4 characters.');
			this.flash_inspector_error();
			return;
		}else if(p!=p2){
			Warn('You entered two different new passwords.');
			this.flash_inspector_error();
			return;
		}
		
		Request(this.controller_path+'/change_password', {current:cp, password:p}, function(r){
			
			if(r.ok){
				this.flash_inspector_ok();
				$('up_curr_password').clear();
				$('up_password').clear();
				$('up_password_2').clear();
			}else{
				this.flash_inspector_error();
			}
		}.bind(this));
	}
	
	
});
var Prefs = new _Prefs('preferences', '/users');










