var Boost = {
	
	choices:Object,
	link_url:String,
	slides: ['intro','choice','form','business_credit','credit_repair','all'],
	slide_choice:0,
	
	slide_num : Number = 0,
	slide_int : Number,
	slide_total : Number = 5,
	slide_z : Number = 100,
	
	init : function()
	{
		if($('a.slide').size()) Boost.slide_int = setInterval('Boost.change_slide()',4000);
		$('#bbbuttons a').each(function(index) {
		  $(this).bind('click', Boost.select_slide);
		});
		
		$('a.menu').bind('mouseover', Boost.button_over).bind('mouseout', Boost.button_out);
		$('a.lightbox,area.lightbox').bind('click', { type:'intro' }, Boost.popup);
		$('a.debt_reduction').bind('click', { type:'debt_reduction' }, Boost.popup);
		$('a.credit_application').bind('click', { type:'credit_application' }, Boost.popup);
		$('a.repaircredit').bind('click', { type:'repaircredit' }, Boost.popup);
		$('a.businesscredit').bind('click', { type:'businesscredit' }, Boost.popup);
		
		$('#submenu, a.has_submenu').bind('mouseover', Boost.submenu_over);
		$('#submenu, a.has_submenu').bind('mouseout', Boost.submenu_out); 
		
		$('button.add_to_cart').bind('click', Boost.add_to_cart);
		$('a.card').live('click',Boost.remove_from_cart)
					.bind('click',Boost.remove_from_cart);
					
		$('#autofill').bind('click',Boost.autofill);
		$('#review_your_order').bind('click', Boost.review_and_submit);
		
		// swfobject.embedSWF('/swf/flashplayer.swf', "boostmyscore_video", "600", "404", "9.0.0","/js/swfobject/expressInstall.swf", false, { wmode:'transparent' }, false);
		
		/*$('#boostmyscore_video').flash({
			swf: '/swf/flashplayer.swf',
			width: 600,
			height: 404
		});*/
		
		// alert('ds');
		
		/*$('#flashintro_video').flash({
			swf: 	'/swf/intro_v8.swf',
			width: 	600,
			height: 338,
			params: { bgcolor:'#000000', wmode:'transparent' }
		})*/
		
		Boost.check_cart();
		Boost.choices = {};
	},
	
	change_slide : function(id)
	{
		Boost.slide_num++;
		// var slide = $('a.slide:first-child');
		if(id)
		{
			var slide = $('#'+id);
			Boost.slide_num = parseInt(id.replace('slide_',''));
		}
		else
		{
			var slide = $('#slide_'+((Boost.slide_num%3)+1));
		}
		
		if(window.console) window.console.log(slide.attr('href'));
		
		
		slide.hide();
		$('#billboard').append(slide);
		slide.fadeIn(1000);
		
		$('#bbbuttons a.active').removeClass('active');
		$('#'+slide.attr('id')+'_button').addClass('active');
	},
	
	select_slide : function()
	{
		var slide_id = $(this).attr('rel');
		Boost.change_slide(slide_id);
		clearInterval(Boost.slide_int);
		// Boost.slide_int = setInterval('Boost.change_slide()',4000);
		return false;
	},
	
	autofill : function()
	{
		$('#cardholder_first_name').val($('#first_name').val());
		$('#cardholder_last_name').val($('#last_name').val());
		$('#billing_address').val($('#street_address').val());
		$('#billing_city').val($('#city').val());
		$('#billing_state').val($('#state').val());
		$('#billing_zip').val($('#zipcode').val());
		$('#billing_email').val($('#email_address').val());
		$('#billing_phone').val($('#phone_number').val());
		return false;
	},
	
	review_and_submit : function()
	{
		$('#review_message').show();
		$.scrollTo('#review_message', 800, {easing:'easeInOutCubic'});
		$('#review_your_order').hide();
		$('#submit_order').show();
	},
	
	add_to_cart : function()
	{
		var num_cards = parseInt($('a.card').size(),10);
		// window.console.log(num_cards);
		if(num_cards<3)
		{
			var id = $(this).attr('id');
			var idnum = id.split('_')[1];
			var name = $('#name_'+id).html();
			/*if($(this).hasClass('black'))
			{
				var cardtype = 'black';
			}
			else if($(this).hasClass('platinum'))
			{
				var cardtype = 'platinum';
			}
			else if($(this).hasClass('gold'))
			{
				var cardtype = 'gold';
			}
			var cost = parseInt($('#cost_'+id).html().replace(/[^0-9\.]/gi,""));*/
			if($('#_'+id).is('*'))
			{
				return;
				// alert('You already have that card in your cart.');
			}
			else
			{
				$.post('/customajax/add_to_cart',
					{id:idnum},
					function(resp) 
					{
				  		$('#summary').html(resp);
						Boost.check_cart();
					}
				);
			}
		}
		else
		{
			// alert($('a.card').size());
			alert("You cannot add more than three cards to your cart");
		}
		
		return false;
	},
	
	remove_from_cart : function()
	{
		var id = $(this).attr('id');
		var idnum = id.split('_')[2];
		$.post('/customajax/remove_from_cart',
			{id:idnum},
			function(resp) 
			{
		  		$('#summary').html(resp);
				Boost.check_cart();
			}
		);
		return false;
	},
	
	check_cart : function()
	{
		if(!$('a.card').size() && $('#checkout').is(":visible"))
		{
			$('#checkout').hide();
			// $('#checkout').animate({
			// 			    height: 'toggle'
			// 			  }, 500, 'easeInOutQuad');
		}
		else if($('a.card').size() && $('#checkout').is(":hidden"))
		{
			$('#checkout').show();
			// $('#checkout').animate({
			// 			    height: 'toggle'
			// 			  }, 500, 'easeInOutQuad');
		}
		var cards = [];
		if($('a.card').size())
		{
			$('a.card').each(function(index) {
			  cards.push($(this).attr('id').split('_')[2]);
			});
			$.cookie('cart', cards.join('|'), { expires: 3, path: '/' });
		}
		
		// check for boost discount
		switch($('a.card').size())
		{
			case 2:
			$.post('/customajax/cart_notification',
				{num:2},
				function(resp) 
				{
			  		$('#checkout_message').html(resp).show();
				}
			);
			break;
			
			case 1:
			$.post('/customajax/cart_notification',
				{num:1},
				function(resp) 
				{
					if(!resp)
					{
						$('#checkout_message').hide();
					}
					else
					{
			  			$('#checkout_message').html(resp).show();
					}
				}
			);
			break;
			
			case 3:
			case 0:
			$('#checkout_message').hide();
			break;
		}
		
		// get total
		$('#checkout_total').html('<img src="/images/icons/loader.gif" alt="Loading" />');
		$.post('/customajax/get_total',
			{},
			function(resp) {
		  		$('#checkout_total').html(resp);
			}
		);
		
		
		// alert(cards);
	},
	
	submenu_over : function()
	{
		clearTimeout(Boost.submenu_int);
		Boost.show_submenu();
	},
	
	submenu_out : function()
	{
		Boost.submenu_int = setTimeout(Boost.hide_submenu,500);
	},
	
	show_submenu : function()
	{
		$('#submenu').show();
	},
	
	hide_submenu : function()
	{
		$('#submenu').hide();
	},
	
		alert : function(msg)
	{
		var box = $$('div',
					msg,
					{ className:'box alert', id:'alert_box' });
					
		$('body').append(box);
		box.hide();
		
		var top = ($(window).height()*.5)-(box.height()*.5);
		//alert(box.height());
		box.css({
			width:700,
			top:(top<50)?50:top,
			left:($(window).width()*.5)-350
		});
		
		box.bind('click', function(){ $(this).remove(); }).show();
		
		return false;
	},
	
	popup : function(e)
	{
		// alert(e.data.type);
		
		if($('#overlay').length)
		{
			$('#popup_box,#overlay,#alert_box').remove();
		}
		
		Boost.choices = null;
		Boost.choices = {};
		
		var choice = $(this).attr('rel');
		if(!choice) choice = e.data.type;
		Boost.choices[choice] = true;
		
		var overlay = $$('div',
						'',
						{ className:'overlay', id:'overlay' });
		var box = $$('div',
					$$('div','',{ className:'inset', id:'content' }),
					$$('a',
						$$('img','',{src:'/images/buttons/close.gif'}),
						{ id:'close_button' }),
					$$('a',
						$$('img','',{src:'/images/buttons/continue.gif'}),
						{ id:'continue_button' }),
					$$('a',
						$$('img','',{src:'/images/buttons/back.jpg'}),
						{ id:'startover_button' }),
					{ className:'boostbox', id:'popup_box' });
		
		overlay.css({
			height:$(document).height(),
			width:$(document).width(),
			opacity:0.7
		});
		var top = ($(window).height()*.5)-260+$(document).scrollTop();
		box.css({
			height:520,
			width:884,
			top:top,
			left:($(window).width()*.5)-442
		});
		
		$('body').append(overlay).append(box);
		$('#close_button').bind('click', function(){ $('#popup_box,#overlay').remove(); });
		
		var type = e.data.type;
		switch(type)
		{
			case 'debt_reduction':
				Boost.link_url = 'http://www.debtredeemer.com/';
				break;
				
			case 'credit_application':
				Boost.link_url = 'http://boostmyscore.myoptimizedcredit.com/';
				break;
		}
		
		Boost.load_slide(type);
		
		return false;
	},
	
	button_over : function()
	{
		var img = $(this).children('img');
		var newsrc = img.attr('src').replace('_off','_on');
		img.attr('src', newsrc);
	},
	
	button_out : function()
	{
		var img = $(this).children('img');
		var newsrc = img.attr('src').replace('_on','_off');
		img.attr('src', newsrc);
	},
	
	load_slide : function(type)
	{
		$.post(
			'/ajax/popup/',
			{ slide:(type=='repaircredit' || type=='businesscredit')?'intro':type },
			function(resp)
			{
				$('#content').html(resp);
				switch(type)
				{
					case 'intro':
						$('#continue_button').one('click', function(){ Boost.load_slide('choice') });
						$('#startover_button').hide();
						break;
						
					case 'repaircredit':
						Boost.choices.credit_repair = true;
						$('#continue_button').one('click', function(){ Boost.load_slide('form') });
						$('#startover_button').hide();
						break;
						
					case 'businesscredit':
						Boost.choices.business_credit = true;
						$('#continue_button').one('click', function(){ Boost.load_slide('form') });
						$('#startover_button').hide();
						break;
						
					case 'debt_reduction':
					case 'credit_application':
						$('#continue_button').one('click', function(){ Boost.load_slide('form2') });
						$('#startover_button').hide();
						break;
						
					case 'choice':
						$('#continue_button').one('click', function()
							{ 
								Boost.load_slide('form');
							});
						$('.choice').bind('click', Boost.select_choice).each(function(index) {
							var c = $(this).attr('id');
							if(Boost.choices[c])
							{
								var src = $(this).attr('src');
								var newsrc = src.replace('off','on');
								$(this).attr('src', newsrc);
							}
						});
						$('#startover_button').show().one('click',Boost.popup);
						
						break;
						
					case 'form':
						if((Boost.choices.business_credit && Boost.choices.credit_repair) || 
						(!Boost.choices.business_credit && !Boost.choices.credit_repair))
						{
							var slide = 'all';
						}
						else
						{
							var slide = (Boost.choices.business_credit) ? 'business_credit' : 'credit_repair';
						}
						$('#continue_button').bind('click', function(){ if(Boost.validate_form()) Boost.load_slide(slide) });
						$('.disclaimer').bind('click', Boost.show_alert );
						
						break;
						
					case 'form2':
						$('#continue_button').attr('href',Boost.link_url)
											.attr('target','_blank')
											.attr('rel',Boost.link_url)
											.bind('click', function()
											{ 
												var resp = Boost.validate_form();
												if(resp)
												{ 
													Boost.send_email(); 
													return true; 
												}
												else
												{
													return false;
												}
											});
						$('.disclaimer').bind('click', Boost.show_alert );
						break;
						
					case 'business_credit':
					case 'credit_repair':
					case 'all':
						$('#continue_button').remove();
						$('a.link').bind('click', Boost.send_email);
						break;
				}
				
			}
		);
	},
	
	send_email : function()
	{
		$.post(
			'/ajax/send_email/',
			{ first_name:Boost.formvars.first_name,
				last_name:Boost.formvars.last_name,
				phone_number:Boost.formvars.phone_number,
				email_address:Boost.formvars.email_address,
				credit_repair:Boost.choices.credit_repair,
				business_credit:Boost.choices.business_credit,
				debt_reduction:Boost.choices.debt_reduction,
				credit_application:Boost.choices.credit_application,
				link:$(this).attr('rel') },
			function(resp)
			{
				//alert(resp);
			}
		);

		
	},
	
	show_alert : function()
	{
		var type = $(this).attr('rel');
		$.post(
			'/ajax/alerts/'+type,
			{ },
			function(resp)
			{
				Boost.alert(resp);
			}
		);
		return false;
	},
	
	validate_form : function()
	{
		var a = ['first_name','last_name','email_address','phone_number'];
		Boost.formvars = {};
		var ok = true;
		var error_msg = 'Please review the highlighted fields for errors and resubmit.';
		var patt1=new RegExp(/[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/i);
		var patt2=new RegExp(/(\()?([0-9]{3})(\)|-)?( )?([0-9]{3})(-)?([0-9]{4}|[0-9]{4})/i);
		
		$('input').removeClass('ERROR');
		for (var i=0; i<a.length; i++)
		{
			var field = $('#'+a[i]);
			if(a[i]=='email_address' && !patt1.test(field.val()))
			{
				field.addClass('ERROR');
				error_msg += '\n\nYou must enter a valid email address.';
				ok = false;
			}
			else if(a[i]=='phone_number' && !patt2.test(field.val()))
			{
				field.addClass('ERROR');
				error_msg += '\n\nYou must enter a valid phone number.';
				ok = false;
			}
			else if(!field.val().length)
			{
				field.addClass('ERROR');
				// alert(field.attr('id'));
				ok = false;
			}
			
			else
			{
				Boost.formvars[a[i]] = field.val();
			}
		};
		if(!$('#agreed:checked').length)
		{
			error_msg += '\n\nYou must read and agree to the user agreement and the disclosures.';
			ok = false;
		}
		if(!ok) alert(error_msg);
		
		return ok;
	},
	
	select_choice : function()
	{
		var id = $(this).attr('id');
		Boost.choices[id] = !Boost.choices[id];
		
		var src = $(this).attr('src');
		var newsrc = (src.indexOf('on')>-1) ? src.replace('on','off') : src.replace('off','on');
		$(this).attr('src', newsrc);
		
		//alert(Boost.choices[id]);
	},
	
	validate_checkout_form : function(form)
	{
		if(!form) form = document.forms[0];
		var submit_it = true;
		$(form).find(".REQUIRED").each(function(req)
		{
			if(($(this).attr('type')!='checkbox' && this.value == '') || 
				($(this).attr('type')=='checkbox' && !this.checked))
			{
				$(this).parents("div.form").addClass('error');
				submit_it = false;
			}
			else
			{
				$(this).parents("div.form").removeClass('error');
			}			
		});
		if( !submit_it )
		{
			$('#error_message').html('Please be sure to complete all required fields.').fadeIn(250);
			$.scrollTo('#error_message', 800, {easing:'easeInOutCubic'});
		}
		return submit_it;
	},
	
	validate_contact_form : function(form)
	{
		if(!form) form = document.forms[0];
		var submit_it = true;
		$(form).find(".REQUIRED").each(function(req)
		{
			if(($(this).attr('type')!='checkbox' && this.value == '') || 
				($(this).attr('type')=='checkbox' && !this.checked))
			{
				$(this).parents("div.formfield").addClass('error');
				submit_it = false;
			}
			else
			{
				$(this).parents("div.formfield").removeClass('error');
			}			
		});
		if( !submit_it )
		{
			$('#error_message').html('Please be sure to complete all required fields.').fadeIn(250);
			$.scrollTo('#error_message', 800, {easing:'easeInOutCubic'});
		}
		return submit_it;
	}
	
}

$(document).ready(function() {
	Boost.init();
});
