(function($){
	/* This plugin manages all popup show,hide,center popup
	 * slide pages,close popup and more stuff
	*/
	$.fn.doPopup = function(options) {
	
 		var defaults = {
			//which button do we fire?
			button_type: "",
			slide: 0,
			pages_show_type: ""//can be slide or fade -- slide makes the pages slide left to right and fade makes the pages fade out and in while changing pages.
		};
		
		//popup window div
		var popup_window = $("#popup_window");
		//loader
		var loader = $(".loader");
		//submit loader
		var submit_loader = $(".submit_loader");
		//popup window title
		var title = $(".popup_header_title");
		//state of popup
		//if its 0 its hidden , 1 its shown
		var state = 0;
		//location var
		var location;
		
		var options = $.extend(defaults, options);
		
		//self explanotary, centers the popup window
		function centerPopup(){
			var windowWidth = document.documentElement.clientWidth;
			var windowHeight = document.documentElement.clientHeight;
			var popupWidth = popup_window.width();
			//centering
			popup_window.css({
				"position": 'absolute',
				"top": '21%',
				"left": windowWidth/2-popupWidth/1
			});
			$("#bg_color").css({  
				"height": windowHeight  
			});
		}
		
		//remove the background color when popup is active
		function backgroundFade(){
			$("#bg_color").css({  
				"opacity": "0.7"  
			});
			$("#bg_color").fadeIn("fast");
		}
		
		//closes the popup
		function closePopup(){
			//remove background fade
			$("#bg_color").fadeOut("fast")
			//close popup
			popup_window.fadeOut("fast");
		}
		
		//clears all inputs of the popup
		function clearFields(){
			//hide loader
			submit_loader.hide();
			//message errors
			$(".message_error").html('');
			$(".message_error").hide();
			//reset buttons names && remove inputs red-green borders
			$("#signin_submit").html("Sign in");
			$("#signup_submit").html("Sign up");
			$("#recoverpass_submit").html("Recover Password");
			//clear sign in fields && remove inputs red-green borders
			$("#username_input_signin").val('');
			$("#username_input_signin").css('border', '1px solid #CCC');
			$("#password_input_signin").val('');
			$("#password_input_signin").css('border', '1px solid #CCC');
			//clear sign up fields && remove inputs red-green borders
			$("#username_input_signup").val('');
			$("#username_input_signup").css('border', '1px solid #CCC');
			$("#firstname_input_signup").val('');
			$("#firstname_input_signup").css('border', '1px solid #CCC');
			$("#lastname_input_signup").val('');
			$("#lastname_input_signup").css('border', '1px solid #CCC');
			$("#password_input_signup").val('');
			$("#password_input_signup").css('border', '1px solid #CCC');
			$("#retype_password_input_signup").val('');
			$("#retype_password_input_signup").css('border', '1px solid #CCC');
			$("#email_input_signup").val('');
			$("#email_input_signup").css('border', '1px solid #CCC');
			$("#gender_input_signup option[name='0']").attr("selected", "selected");
			$("#gender_input_signup").css('border', '1px solid #CCC');
			//clear password recover fields
			$("#email_input_recpass").val('');
			$("#email_input_recpass").css('border', '1px solid #CCC');
			//clear activation code page fields
			$("#email_input_activateaccount").val('');
			$("#email_input_activateaccount").css('border', '1px solid #CCC');
			$("#code_input_activateaccount").val('');
			$("#code_input_activateaccount").css('border', '1px solid #CCC');
			//clear resent activation page fields
			$("#email_input_resent_activation_code").val('');
			$("#email_input_resent_activation_code").css('border', '1px solid #CCC');
		}
		
		//gets the title of the page from its button name
		function getTitle(button){
			if (button == "sign_in"){
				return 'Sign In';
			}else if (button == "sign_up"){
				return 'Sign Up'
			}else if (button == "recoverpass"){
				return 'Recover Password';
			}else if (button == "activate_account"){
				return 'Activate Account';
			}else if (button == "resent_activation_code"){
				return 'Resent Activation Code';
			}
		}
		
		//show the page content
		function showContent(button_type, pages_show_type){
			//show div containing the forms
			$(".popup_content_data").show();
			if (pages_show_type == "slide" || pages_show_type == ""){
				if (button_type == "sign_in"){
					title.html(getTitle(button_type));
					//show sign in form
					$(".popup_content_sign_in").css("margin-left", "0");
					$(".popup_content_sign_in").show();
				}else if (button_type == "sign_up"){
					title.html(getTitle(button_type));
					//show sign up form
					$(".popup_content_sign_up").css("margin-left", "0");
					$(".popup_content_sign_up").show();
				}else if (button_type == "recoverpass"){
					title.html(getTitle(button_type));
					//show sign up form
					$(".popup_content_recoverpass").css("margin-left", "0");
					$(".popup_content_recoverpass").show();
				}else if (button_type == "activate_account"){
					title.html(getTitle(button_type));
					//show sign up form
					$(".popup_content_activate_account").css("margin-left", "0");
					$(".popup_content_activate_account").show();
				}else if (button_type == "resent_activation_code"){
					title.html(getTitle(button_type));
					//show sign up form
					$(".popup_content_resent_activation_code").css("margin-left", "0");
					$(".popup_content_resent_activation_code").show();
				}
			}else if (pages_show_type == "fade"){
				if (button_type == "sign_in"){
					title.html(getTitle(button_type));
					//show sign in form
					$(".popup_content_sign_in").fadeIn();
				}else if (button_type == "sign_up"){
					title.html(getTitle(button_type));
					//show sign up form
					$(".popup_content_sign_up").fadeIn();
				}else if (button_type == "recoverpass"){
					title.html(getTitle(button_type));
					//show sign up form
					$(".popup_content_recoverpass").fadeIn();
				}else if (button_type == "activate_account"){
					title.html(getTitle(button_type));
					//show sign up form
					$(".popup_content_activate_account").fadeIn();
				}else if (button_type == "resent_activation_code"){
					title.html(getTitle(button_type));
					//show sign up form
					$(".popup_content_resent_activation_code").fadeIn();
				}
			}
		}
		
		//hide all pages
		function hideAllPages(){
			$(".popup_content_sign_in").hide();
			$(".popup_content_sign_up").hide();
			$(".popup_content_recoverpass").hide();
			$(".popup_content_activate_account").hide();
			$(".popup_content_resent_activation_code").hide();
		}
		
		//slides all pages back to be ready for the animation
		function slideBack(){
			$(".popup_content_sign_in").animate({ marginLeft: "-425px" }, "fast" );
			$(".popup_content_sign_up").animate({ marginLeft: "-425px" }, "fast" );
			$(".popup_content_recoverpass").animate({ marginLeft: "-425px" }, "fast" );
			$(".popup_content_activate_account").animate({ marginLeft: "-425px" }, "fast" );
			$(".popup_content_resent_activation_code").animate({ marginLeft: "-425px" }, "fast" );
		}
		
		//this is needed so the popup remains shown
		popup_window.click(function(e){
			e.stopPropagation();
		});
		
		//when we click the X button on the top right corner of the popup
		//we close the window
		$(".popup_window_close").click(function(){
			state=0;
			closePopup();
		});
		
		//when we click on the body we close the popup window
		$("body").click(function(){
			if (state == 1){
				closePopup();
				state = 0;
			}
		});
		
		//Shows the Popup
		function showPopup(type){
			if (state == 0){
				clearFields();
				hideAllPages();
				//fade background on click
				backgroundFade()
				if (type == ""){
					$(".popup_content_data").hide();
					//show the loading text and image
					loader.show();
				}
				//dynamically show the popup window
				centerPopup();
				//show the popup window
				popup_window.show();
				//animate the popup window and hide the loader
				setTimeout(function(){
					loader.hide();
					showContent(defaults.button_type, defaults.pages_show_type);
				}, 200);
				state = 1;
			}
		}
		
		//Slides left and right
		//and serves each page requested
		//via the location parameter
		function slidePages(location){
			clearFields();
			slideBack();
			title.html(getTitle(location));
			$(".popup_content_"+location).animate({
				marginLeft: "-425px"
			},
			"fast",
			function(){
				hideAllPages();
				$(this).show();
				$(this).animate({
					marginLeft: "0px"
				}, "fast");
			});
		}
		
		return this.each(function() {
			location = defaults.button_type;
			if (defaults.slide == 0){
				if (defaults.pages_show_type == "fade"){
					showPopup("fade");
				}else
					showPopup("");
			}else if (defaults.slide == 1){
				slidePages(location);
			}
		});
	};
})(jQuery);
