/* 
	Written By: Peter Evans
	URL: http://www.chillifish.net
	Last Modified: 09/10/2008
*/

(function($) {
	var num_emails = 0;
	//var max_emails = 3;
	var left;
	var agentsArray = new Array();
	var index = 1;
	
	var providerClicked = false;
	 
	jQuery.stripPx = function(obj) {
		return obj.substring(0, obj.length - 2);
	};
	jQuery.pos = function(obj) {
		//var w = $(window).width();
		var h = $(window).height();
		
		//var wcut = $(obj).css("width");
		var hcut = $(obj).css("height");
		
		//var objw = $.stripPx(wcut);
		var objh = $.stripPx(hcut);
	
		//var nw = (w - objw) / 2 + "px";
		var nh = (h - objh) / 2 + "px";
		
		$(obj).css("top", nh);
		//$(obj).css("left", nw);
	};
	jQuery.validateLogin = function(provider, maxfriends) {
		var uname = document.loginForm.username.value;
		var pword = document.loginForm.password.value;
		
		if(uname.length > 0 && pword.length > 0) {
			$.showAddresses(provider, uname, pword, maxfriends);
		}
		else {
			// show error.
			alert('complete all fields');
		}
	};
	jQuery.submitEnter = function(provider, e, max) {
		var keycode;
		if (window.event) keycode = window.event.keyCode;
		else if (e) keycode = e.which;
		else return true;

		if (keycode == 13) {
   			$.validateLogin(provider, max);
   			return false;
   		}  
		else {
   			return true;
   		}
	};
	jQuery.showHide = function(id) {
		($(id).css("display") == 'none') ? $(id).css("display", "block") : $(id).css("display", "none");
		($(id).css("visibility") == 'hidden') ? $(id).css("visibility", "visible") : $(id).css("visibility", "hidden");
	};
	jQuery.darkenScreen = function() {
		if(($.browser.msie && $.browser.version == 6) == false) {
			var obj = "<div id=\"darken\" style=\"background-color: #000000; filter:alpha(opacity=70); opacity: 0.7; -moz-opacity: 0.70; z-index: 1; height: 100%; width: 100%; background-repeat: repeat; position: fixed; top: 0px; left: 0px; visibility: visible;\"></div>";
		 $("body").prepend(obj);
		}
	};
	jQuery.writePopup = function() {
		if($.browser.msie || $.browser.safari) {
			var obj = "<div id=\"pop-window\"><div id=\"header\"><div id=\"popup-close\" onclick=\"$.closeWindow();\"></div></div><div id=\"body\">Maximise your chance of winning this luxury Bond giveaway by referring all of your friends. The more emails you send, the more likely it is that you will become Bond! Just enter you email and password into the form below and sit back while our Q inspired search tool populates with all your friends emails.<h3 id=\"counter\"></h3><div id=\"login-container\"></div><div id=\"contactframe\"><table id=\"tableContainer\" width=\"100%\"></table></div><div id=\"recruit-btn\" onclick=\"$.recruitChecked();\"></div></div><div id=\"footer\"></div></div>";
		}
		else {
			var obj = "<div id=\"pop-window\"><div id=\"header\"><div id=\"popup-close\" onclick=\"$.closeWindow();\"></div></div><div id=\"body\">Maximise your chance of winning this luxury Bond giveaway by referring all of your friends. The more emails you send, the more likely it is that you will become Bond! Just enter you email and password into the form below and sit back while our Q inspired search tool populates with all your friends emails.<h3 id=\"counter\"></h3><div id=\"login-container\"></div><div id=\"contactframe\"></div><div id=\"recruit-btn\" onclick=\"$.recruitChecked();\"></div></div><div id=\"footer\"></div></div>";	
		}
		$("body").prepend(obj);
		$.pos('#pop-window');
	};
	jQuery.showLogin = function(provider, max) {
		if(providerClicked) {
			if(window.location.href == 'http://www.bondmakeover.com/competition/2/#') {
				window.location = 'http://www.bondmakeover.com/competition/4/';
			}
			else {
				window.location.reload();
			}
		}
		providerClicked = true;
		if($("#pop-window").is(":hidden")) {
			$("#pop-window").fadeIn("slow");
		}
		else {
			$.writePopup();
		}
		$.darkenScreen();
		$.clearAgents();
		$("#pop-window #login-container").load('http://www.bondmakeover.com/includes/login-form.php?provider=' + provider + '&maxfriends=' + max);
	};
	jQuery.showAddresses = function(provider, user, pass, maxfriends) {
		$("#login-container").fadeOut("slow");
		$("#contactframe").css("display", "block");
		$("#recruit-btn").css("display", "block");
		$("#counter").fadeIn("slow");
		$("#counter").html("You may add " + maxfriends + " more agents.");
		if($.browser.msie) {
			$("#contactframe").css("background", "url(../media/loader.gif) center no-repeat");
			$("#tableContainer").load('http://www.bondmakeover.com/includes/grabber/email-table-ie.php?provider=' + provider + "&u=" + user + "&pw=" + pass + '&m=' + maxfriends);
			$("#tableContainer").ajaxComplete(function(request, settings){
  		 		$("#contactframe").css("background", "none");
	 		});
		}
		else if($.browser.mozilla) {
			$("#contactframe").css("background", "url(../media/loader.gif) center no-repeat");
			$("#contactframe").load('http://www.bondmakeover.com/includes/grabber/email-table.php?provider=' + provider + "&u=" + user + "&pw=" + pass + '&m=' + maxfriends);
			$("#contactframe").ajaxComplete(function(request, settings){
  		 		$("#contactframe").css("background", "none");
	 		});
		}
		else {
			$("#contactframe").css("background", "url(../media/loader.gif) center no-repeat");
			$("#tableContainer").load('http://www.bondmakeover.com/includes/grabber/email-table.php?provider=' + provider + "&u=" + user + "&pw=" + pass + '&m=' + maxfriends);
			$("#tableContainer").ajaxComplete(function(request, settings){
  		 		$("#contactframe").css("background", "none");
	 		});
		}
	};
	jQuery.closeWindow = function() {
		$("#darken").fadeOut("slow");
		$.showHide('#pop-window');
	};
	jQuery.resetPopup = function() {
		
	};
	jQuery.recruitChecked = function() {
		$("#pop-window").fadeOut("slow");
		$("#darken").fadeOut("slow");
		$.resetPopup();
	};
	jQuery.addAgent = function(name, email, max_emails) {
		left = max_emails - num_emails;
		if(num_emails < max_emails) {
			var textbox = "friend" + index;
			$("#" + textbox).attr("value", email);
			$(name).attr("disabled", true);
			
			index++;
			num_emails++
			left--;
			
			if(left == 0) { 
				$("#pop-window").fadeOut("slow");
				$("#darken").fadeOut("slow");
			}
			else if(left == 1) { $("#counter").html('You may add one more agent.'); }
			else { $("#counter").html('You may add ' + left + ' more agents.'); }
		}
		else {
			$("#pop-window").fadeOut("slow");
			$("#darken").fadeOut("slow");
		}
	};
	jQuery.clearAgents = function() {
		$(".agent, .bondcheck").each(function(i){
   			this.value = null;
   			this.checked = false;
   			this.disabled = false;
   			$("#friend" + i).attr("value", "");
 		});
 		index = 1;
 		num_emails = 0;
	};
})(jQuery);

$(document).ready(function() {
	var providerClicked = false;
	
	/*var iesixsucks = Array('.form-el', '#popup-close', '#header', '#body', '#footer');
	
	for(var i = 0; i < iesixsucks.length; i++) {
		$('div' + iesixsucks[i]).ifixpng();
	}
	if($.browser.msie) {
		$("div").ifixpng();
	}*/
	if(providerClicked) {
		$.writePopup();
	}
	$(window).resize(function() {
		$.pos('#pop-window');							
	});
});
