invites = {
	
	fetch : function(username, domain, password, language) {
		this.language = language;
		console.log(username)
		console.log(domain)
		console.log(password)
		console.log(language)
		
		$("#btnImportContacts").parent().append("<img src='/media/images/all/placeholders/ajax-loader.gif' alt='loading...' id='ajaxLoader' class='fRight' />");
		
		if(username != "" && domain != "" && password != ""){
			$.ajax({
			  type: "POST",
			  url: "/beautykits/fetch_address_book.json",
			  data: "email="+username+"@"+domain+"&password="+password,
			  dataType: "json",
			  success: function(msg) {
					// Empty the containers before filling them.
					$("#contactList").html('');

					$("#contactList").append("<ul>");
					for(i = 0; i < msg.length; ++i) {
						// Check if email and name are the same
						if(msg[i].name == msg[i].email){
							outputText = msg[i].email;
						}else{
							outputText = "<strong>" + msg[i].name + "</strong> (" + msg[i].email + ")";
						}
						if(i%2){
							evenOrOdd = "even";
						}else{
							evenOrOdd = "odd"
						}
						$("#contactList ul").append("<li class='"+evenOrOdd+"'><label><input type=\"checkbox\" class=\"checkbox\" name=\"data[InviteFromMail]["+ i +"][checked]\" value=\""+1+"\" />&nbsp;" + outputText +"</label><input type=\"hidden\"  name=\"data[InviteFromMail]["+ i +"][email]\" value=\""+msg[i].email+"\"  /><input type=\"hidden\"  name=\"data[InviteFromMail]["+ i +"][complete_name]\" value=\""+ msg[i].name+"\" /></li>");
				     }
			    	$('#importedContactNumber').html(msg.length);
					$('#selectContactsInstructions').show();
					$("#contactList").parent().find('span:eq(0)').text(i);
				
					// Show the container
					$('#contactContainer').show();
				
					// Hide the loader
					$('#ajaxLoader').remove();
				
				},
			  error: function(request,error, exeption){
					errorMessage = new Array();
					if(invites.language == "fr"){
						errorMessage[errorMessage.length] = "- Le contact n'a pu être établi avec votre fournisseur de courriel.";
					}else{
						errorMessage[errorMessage.length] = "- We were unable to establish connection with you email provider.";
					}
					
					// Hide the loader
					$('#ajaxLoader').remove();
					if(errorMessage.length > 0){
						$('#importError').show();
						$('#importError .errorContent ul').empty();
						$(errorMessage).each(function(i){
							$('#importError .errorContent ul').append('<li>'+ errorMessage[i] +'</li>')
						});
					}
			  },
				complete: function(object, type){
					errorMessage = new Array();
					if($("#contactList").parent().find('span:eq(0)').text() == '0'){
						if(invites.language == "fr"){
							errorMessage[errorMessage.length] = "- Votre nom d'usager et/ou mot de passe sont invalides.";
						}else{
							errorMessage[errorMessage.length] = "- Your username and/or password are invalid.";
						}
					}
					// Hide the loader
					$('#ajaxLoader').remove();
					if(errorMessage.length > 0){
						$('#importError').show();
						$('#importError .errorContent ul').empty();
						$(errorMessage).each(function(i){
							$('#importError .errorContent ul').append('<li>'+ errorMessage[i] +'</li>')
						});
					}
					
			  }	 
	 		}); // end of ajax request
		}else{
			errorMessage = new Array();
			if(username == ""){
				if(language == 'en'){
					errorMessage[errorMessage.length] = "- Please enter your username";
				}else{
					errorMessage[errorMessage.length] = "- Veuillez entrer votre nom d'usager";
				}
			}			
			if(domain == ""){
				if(language == 'en'){
					errorMessage[errorMessage.length] = "- Please select your provider";
				}else{
					errorMessage[errorMessage.length] = "- Veuillez s&eacute;lectionnez votre fournisseur";
				}
			}
			if(password == ""){
				if(language == 'en'){
					errorMessage[errorMessage.length] = "- Please enter your password";
				}else{
					errorMessage[errorMessage.length] = "- Veuillez entrer votre mot de passe";
				}
			}

			// Hide the loader
			$('#ajaxLoader').remove();

			$('#importError').show();
			$('#importError .errorContent ul').empty();
			$(errorMessage).each(function(i){
				$('#importError .errorContent ul').append('<li>'+ errorMessage[i] +'</li>')
			});
		}
	},
		
	validate : function(el) {
		li = $(el).parent()
		val = $(el).val();
		if (val != 'undefined') {
			$.ajax({
			  type: "POST",
			  url: "invites/validate_email.json",
			  data: "data[Invite][email_address]="+val,
			 	dataType: "json",
			  success: function(msg) {
					if (msg.ok) {
						$(li).append("&nbsp;valid!");
						
					} else if(msg.error) {
						$(li).append("&nbsp;invalid!");
						
					}
				} 
	 		});
		};
	},
	open : function(){
		$('#invites_form').show();
	   	$('#invites_open').hide();
	},
	close : function(){
		$('#invites_form').hide();
		$('#invites_open').show();
	}
}

form = {
	open : function(anchorToGo, contentToFlash){
		if(!anchorToGo) { anchorToGo = '#UserRegistrationForm'; }
		$('#btnClose').show();
		$('#btnStartInviting').hide();
		$('#wrap .mainContent .content .inviteContainer').animate({'height':'show'}, 200, function(){ page.gotoanchor(anchorToGo,contentToFlash); });
	},
	close : function(anchorToGo,contentToFlash){
		if(!anchorToGo) { anchorToGo = '#wrap'; }
		$('#btnClose').hide();
		$('#btnStartInviting').show();
		$('#wrap .mainContent .content .inviteContainer').animate({'height':'hide'}, 500, function(){ page.gotoanchor(anchorToGo,contentToFlash); });
	}
}

productDetails = {
	init : function(){
		$('div.plus > a').bind('mouseover',function(){
			productDetails.open(this);
		});
		
		$('div.plus > a').bind('mouseout',function(){
			productDetails.close(this);
		});
		
		$('div.plus > a').bind('click',function(){
			return false;
		});
	},
	open : function(caller){
		if(jQuery.browser.msie){
			$(caller).parent().css('z-index',20000)
		}
		$(caller).addClass('active');
		$(caller).next().show();
	},
	close : function(caller){
		if(jQuery.browser.msie){
			$('div.plus').css({'z-index':0,'positon':'relative'});
		}
		$(caller).next().hide();
		$(caller).removeClass('active');
	}
}

overlay = {
	toggle : function(which){
		if($(which).height() != 10){
			$(which).css({'height':10,'border':'1px red solid'});
		}else{
			$(which).css({'height':'auto', 'border':0});
		}
	}
}


// functions for the subscribtion
users = {
	subscribe : function(email,language){
		$("#newsletterSubscribe").append("<img src='/media/images/all/placeholders/ajax-loader-bottom.gif' alt='loading...' id='ajaxLoader' />");
		$.ajax({
			url: "/beautykits/subscribe_user/" + email,
			success : function(){
				if(language == "fr"){
					confirmMessage = "Vous êtes maintenant enregistré."
				}else{
					confirmMessage = "You are now registered."
				}
				$("#newsletter-email").attr({
					'value':confirmMessage,
					'disabled':'disabled'
				});
				$("#newsletter-email").fadeOut('fast',function(){
					$("#newsletter-email").fadeIn('fast',function(){
						$("#newsletter-email").fadeOut('fast',function(){
							$("#newsletter-email").fadeIn();
						});
					});
				})
				$("#ajaxLoader").remove();
			}
		});
	}
}

content = {
	init : function(page,language){
		$("#dynamicDetailContent").append("<img src='/media/images/all/placeholders/ajax-loader-bottom.gif' alt='loading...' id='ajaxLoader' />");
		$("#dynamicDetailContent").load("/beautykits/" + page,'',function(){ $('#ajaxLoader').remove(); });
		$("#our_brand").addClass('selected');
	},
	change_page : function(page,language, caller){
		$("#dynamicDetailContent").prepend("<img src='/media/images/all/placeholders/ajax-loader-bottom.gif' alt='loading...' id='ajaxLoader' />");
		$("#dynamicDetailContent").load("/beautykits/" + page,'',function(){ $('#ajaxLoader').remove(); });
		$(caller).parent().parent().find('li.selected').removeClass('selected');
		$(caller).parent().addClass('selected');
	}
}

page = {
	gotoanchor : function(which, contentToFlash){
		var targetOffset = $(which).offset().top;
		$("html,body").animate({scrollTop: targetOffset }, "slow", function(){ if(contentToFlash) page.flashContent(contentToFlash,3); } );
	},
	flashContent : function(which,numberOfFlash){
		$(which).fadeOut('normal',function(){ $(which).fadeIn('normal',function(){$(which).fadeOut('normal',function(){ $(which).fadeIn('normal',function(){}); })}); })
	}
}


send = {
	slide : function() {
	

		if($("#footer").css("display")=="none"){
			$("#footer").slideDown( function() {
				var targetOffset = 	$("#footer").offset().top
				$("html,body").animate({scrollTop:targetOffset},'slow')
			})
		
		}else{
			$("#footer").slideUp()	
		}
	}
}

