var rcc = {
	
	selectAddress: function(theID) {
		
		$.post("/order_ajax.php", { action: "selectAddress", addressId: theID },
		function(data){
			//$('#address_selector').html(data);
			location.href = "/mycheckout/3/";
		});
	},
	

	
	setup: function() {
		
		this.createMouseEvents();
		
		
		try {
		var basketTop = $('#floating_basket').position();
		var siteTop = $('#block_contenttop').position();

		rcc.basketPos = (siteTop.top+basketTop.top+50);
		
		rcc.initHeight = 1500;
		
		setTimeout(function() {
			rcc.initHeight = (document.body.offsetHeight+200);
		},1000);
		
		
		
		
		$(window).scroll(function(e) {
			
			var scrollTop = $(document).scrollTop();
			
			if (scrollTop > rcc.basketPos && scrollTop < rcc.initHeight) {
				var newPos = scrollTop-rcc.basketPos+20;
				$('#floating_basket').css("margin-top",newPos+"px");
			
			} else if (scrollTop < rcc.basketPos) {
				$('#floating_basket').css("margin-top","0px");
			}
		});
		
		}
		catch(e) {rcc.log(e)};
		
		try {
			this.showTestimonials();
		}
		catch(e) {rcc.log(e)};
	},
	
	submitForm: function() {
		
		if ($('input[name=firstname]').val() == "" || $('input[name=lastname]').val() == "" || $('input[name=street1]').val() == "" || $('input[name=email]').val() == "" || $('input[name=phone]').val() == "" || $('input[name=city]').val() == "" || $('input[name=zip]').val() == "" || $('input[name=state]').val() == "" ) {
			alert("Please fill out all the required fields.");
		} else {
			if(!$('#disclaimer').attr('checked'))
				alert("Please accept the disclaimer.");
			else
				$('#clientform').submit();
		}
	},
	
	createMouseEvents: function() {
		
		var imgObj = new Image();
		imgObj.src = "http://www.redcarpetcleanses.com/images/popup_image.jpg"
		
		$('.popup_link').mouseover(function(e){
			
			if (rcc.popupShow == true) {
				var topPos = e.pageY-360;
				var leftPos = e.pageX-420;
				$('#giftpopup').css('top',topPos+"px");
				$('#giftpopup').css('left',leftPos+"px");
				$('#giftpopup').fadeIn('fast');
				rcc.popupShow = false;
			}
			
		});
		
		$('.popup_link').mouseout(function(e){
			rcc.popupShow = true;
			$('#giftpopup').fadeOut();
		});
		
		
	},
	
	
	
	calendarClick: function(theID) {
	
		var cleanse_id = $(theID).attr('cleanseid');
		var cleanse_date = $(theID).attr('date');
		$.post("/order_ajax.php", { action: "addCleanse", cleanse_date: cleanse_date, cleanse_id: cleanse_id },

		function(data){
			
			var stuff = JSON.parse(data);

			$('#smallbasket').html(stuff.basket);
		});
	},
	
	giltClick: function(theID) {
	
		var cleanse_id = $(theID).attr('cleanseid');
		var cleanse_date = $(theID).attr('date');
//		alert(cleanse_date);
		$.post("/order_ajax.php", { action: "addCleanse", cleanse_date: cleanse_date, cleanse_id: cleanse_id },
		function(data){

			var stuff = JSON.parse(data);
			$('#cleanse_amount').html(stuff.amount);
			$('#smallbasket').html(stuff.basket);

		});
	},

	
	calendarMouseOver: function(theID) {
		$(theID).addClass('cal_mouseover');
	},
	
	calendarMouseOut: function(theID) {
		$(theID).removeClass('cal_mouseover');
	},
	
	changeMonth: function(m,y,s,id,addfunc,weekdaysAvailable,margin) {
		$.post("/order_ajax.php", { action: "goMonth", m: m, y: y, s: s,id: id,addfunc: addfunc,weekdaysAvailable:weekdaysAvailable,margin:margin },
		function(data){
	 		$('#calin_'+id).html(data);
		});
	},
	
	
	
	orderSelf: function(theID) {
	
		var cleanse_id = $(theID).attr('cleanseid');
		var cleanse_date = $(theID).attr('date');
		var zip = $('#self_zip').attr('value');
		
		if (zip == "") {
			
			alert("Please Fill out your zipcode");
			return;
			
		}
		
		$.post("/order_ajax.php", { action: "addCleanse", cleanse_date: cleanse_date, cleanse_id: cleanse_id, gift: giu.gift, dCity: giu.city, zip: zip  },
		function(data){
			$('#smallbasket').html(data);
		});
		
	},
	
	orderGift: function(theID) {
	
	
				
		var cleanse_id = $(theID).attr('cleanseid');
		var cleanse_date = $(theID).attr('date');
		var zip = $('#mail_zip').attr('value');
		
		
		
		if (giu.mail == "mail") {
			if ($('#mail_email').attr('value') == "" || $('#mail_fullname').attr('value') == "" || $('#mail_zip').attr('value') == "" || $('#mail_city').attr('value') == "" || $('#mail_street1').attr('value') == "") {
				alert("please fill out all the required fields");
			} else {
				var formdata = $('#myForm').serializeArray();
				$.post("/order_ajax.php", { action: "addCleanse", cleanse_date: cleanse_date, cleanse_id: cleanse_id, mail: giu.mail, gift: giu.gift, dCity: giu.city, zip: zip,formdata: formdata  },function(data){
					$('#smallbasket').html(data);
				});
			}
		} else if (giu.mail == "email"){
			if ($('#mail_email').attr('value') == "" || $('#mail_fullname').attr('value') == "" || $('#mail_zip').attr('value') == "") {
				alert("please fill out all the required fields");
			} else {
				var formdata = $('#myForm').serializeArray();
				$.post("/order_ajax.php", { action: "addCleanse", cleanse_date: cleanse_date, cleanse_id: cleanse_id, mail: giu.mail, gift: giu.gift, dCity: giu.city, zip: zip,formdata: formdata  },function(data){
					$('#smallbasket').html(data);
				});
			}
		}
		
		
		//rcc.log(formdata);
		
		
		
		
	},
	
	showDropdown: function(theID) {
	
		$('#product_dropdown').slideDown('fast');
		$('#product_dropdown').mouseover(function() {
			
			clearTimeout(rcc.dropdownTimeout);
			
		});
		
		$('#product_dropdown').mouseout(function() {
			
			rcc.dropdownTimeout = setTimeout(rcc.reallyHide,200);
			
		});
	},
	
	hideDropdown: function(theID) {
		rcc.dropdownTimeout = setTimeout(rcc.reallyHide,300);
	},
	
	reallyHide: function() {
		$('#product_dropdown').slideUp();
	},
	
	redeemDate: function(theID) {
	
		
		var cleanse_date = $(theID).attr('date');
		
		$.post("/order_ajax.php", { action: "redeemDate", cleanse_date: cleanse_date },
		function(data){
			if (data == 0)
				location.href = "/redeem/";
			else
				location.href = "/redeem/2/";
		});
		
	},
	
	showTestimonials: function() {
		 options = {
        	loadCss: 1
    	 };
    	 Testimonials.fetch('079b488be44317389f29943e4b5bc33d', 'testimonials_2802', options);
	}
	

};

var giu = {
	
	city: "",
	gift: 0,
	mail: "",
	buttonActive: true,
	selectGift: function(obj) {
		
		if (!this.buttonActive)
			return;
		
		if (this.gift == $(obj).attr('gift')) {
			return;
		} else {
			$('#giftselect DIV').removeClass('order_selection_active');
			$('#mailselect DIV').removeClass('order_selection_active');
			this.gift = 0;
			this.mail = "";
			$('#calin_9').html("");
			$('#mailselect').hide();
			$('#mailregular').hide();
			$('#zipselect').hide();
			$('#selectDesc').hide();
		}
		
		if (this.gift == 0) {
			this.gift = $(obj).attr('gift');
			$(obj).addClass('order_selection_active');
			
			if (this.gift == 2)
				$('#mailselect').show();
				
			if (this.gift == 1) {
				
				this.showCalendar();
				
				if (this.city == "US")
					$('#zipselect').show();
			}
		}	
		
	},
	selectMail: function(obj) {
		
		if (!this.buttonActive)
			return;
			
		if (this.mail == $(obj).attr('mailing')) {
			return;
		} else {
			$('#mailselect DIV').removeClass('order_selection_active');
			this.mail = "";
		}
		
		if (this.mail == "") {
			this.mail = $(obj).attr('mailing');
			
			if (this.mail == "email") {
				$('.hidewithemail').hide();
				$('#mailselect').css('height','350px');
			} else {
				$('.hidewithemail').show();
				$('#mailselect').css('height','480px');
			}
			$(obj).addClass('order_selection_active');
			$('#mailregular').show();
				
			this.showCalendar();	
			
		}
		
		
		
		
	},
	selectCity: function(obj) {
	
		if (!this.buttonActive)
			return;

		if (this.city == $(obj).attr('city')) {
			return;
		} else {
			// reset
			$('.order_selection_active').removeClass('order_selection_active');
			this.city = "";
			this.mail = "";
			this.gift = 0;
			$('#mailemail').hide();
			$('#mailmail').hide();
			$('#mailselect').hide();
			$('#calin_9').html("");
			$('#zipselect').hide();
			$('#selectDesc').hide();
			
		}
		
		
		if (this.city == "") {
			this.city = $(obj).attr('city');
			$(obj).addClass('order_selection_active');
			$('#giftselect').show();
		}
	},
	showCalendar: function() {
		
		if (this.gift > 0 && this.city != "") {
			$.post("/order_ajax.php", { action: "getCalendar", city: this.city, gift: this.gift, cleanse: 9 },
			function(data){
				
				if (giu.gift == 2) {
					$('#selectDesc').show();
					rcc.log("asd");
				} else {
					$('#selectDesc').hide();
				}
		 		$('#calin_9').html(data);
			});
		}
	}

}

rcc.log = function(thelog) {try {console.log(thelog)}catch(e){}};

$(document).ready(function(){rcc.setup();});
