var rx_safari_2 = /Safari\/419/;
var rx_gecko = /Gecko/;
var rx_win = /Windows/;
var rx_ie = /MSIE/;
var rx_ff = /Firefox/;
var rx_nix = /Linux|Unix|FreeBSD/;

var browser = navigator.userAgent;

var is_safari_2 = browser.search(rx_safari_2);
var is_gecko = browser.search(rx_gecko);
var is_win = browser.search(rx_win);
var is_ie = browser.search(rx_ie);
var is_ff = browser.search(rx_ff);
var is_nix = browser.search(rx_nix);

var restrictions = {
	ethnicity : {
		count : 0,
		rx : /ethnicity/
	},
	bodytype : {
		count : 0,
		rx : /bodytype/
	},
	height : {
		count : 0,
		rx : /height/
	},
	religion : {
		count : 0,
		rx : /religion/
	},
	education : {
		count : 0,
		rx : /education/
	},
	smokes : {
		count : 0,
		rx : /smokes/
	}
};

if (is_win != -1 && is_ff != -1 || is_nix != -1) {
	var small_menu = '<link rel="stylesheet" href="css/small_menu.css" type="text/css" media="screen" charset="utf-8" />';
	document.write(small_menu);
}

if (is_nix != -1) {
	var unix_style = '<link rel="stylesheet" href="css/unix.css" type="text/css" media="screen" charset="utf-8" />';
	document.write(unix_style);
}

if (is_safari_2 != -1) {
   var safari_2_style = '<link rel="stylesheet" href="css/safari.css" type="text/css" media="screen" charset="utf-8" />';
   document.write(safari_2_style);
}

// set location
function ajaxCheckScreenname() {
  var s = document.getElementById("screenname").value;
  if (s.length > 1) {
     var params = "screenname=" + s;
     var ajax = new Ajax.Request(
        "ajax_screenname_checker.php", {
	method: 'get', 
	parameters: params,
	onSuccess: gotScreennameData,
	onFailure: failed
	} );
     
  }
}

function gotScreennameData(data) {
	var res = "";
	var good = false;
	data = eval('(' + data.responseText + ')');
	
	if (data.screenname == document.getElementById("screenname").value) {
		if (data.taken) {
			res = data.screenname + " is taken. Please try something else.";
		}
		else if (data.badformat) {
			res = data.screenname + " is bad! " + data.message;
		}
		else {
			res = data.screenname + " is available! Yours if you want it.";
			good = true;
		}
		
		if (good) {
			$("ajax_screenname").removeClassName("bad");
			$("ajax_screenname").addClassName("good");
		} else {
			$("ajax_screenname").removeClassName("good");
			$("ajax_screenname").addClassName("bad");
		}
		
		Effect.Appear("ajax_screenname");
		$("ajax_screenname").innerHTML=res;
	}
}
function failed() { alert("failed"); }     

function setLoc(loc) {
	var newtext = "location="+escape(loc);
	var cookies = document.cookie.split(';');
	
	var found = false;
	
	for (var i = 0; i < cookies.length; i++) {
		parts = cookies[i].split("=");
		var name = parts[0];
	
		if (name == "location") {
			found = true;
			cookies[i] = newtext;
		}
	}

	if (!found) cookies.push_back(newtext);

	document.cookie = cookies.join(';');

	window.location.reload();
}

// png fix for ie
if (document.all && document.styleSheets && document.styleSheets[0] && document.styleSheets[0].addRule) {
	document.styleSheets[0].addRule('.pngfix', 'behavior: url(js/iepngfix.htc)');
}

// countdown timer
var clockTracker = function(domobj) {
	var x = new Date();
	var m = x.getMinutes();
	var s = x.getSeconds();
	var m_left = 19 - (m % 20);
	var s_left = 59 - s;
	
	if (domobj)
		domobj.innerHTML = m_left + "m " + s_left + "s";
};

Event.addBehavior({
	
	// landing page body loads
	'body#p-landing': function() {
		var input_solo = $('solo');
		var input_double = $('double');
		var input_either = $('either');
		var submit = $('submit-btn');
		
		// hide the radio inputs and submit buttons
		input_solo.setStyle({
			display: 'none'
		});
		input_double.setStyle({
			display: 'none'
		});
		input_either.setStyle({
			display: 'none'
		});
		submit.setStyle({
			display: 'none'
		});
		
		setInterval("clockTracker($('timer'));", 1000);
		
		// we don't support your browser
		if (is_gecko == -1 && is_ie == -1)
		{
			var no_soup_for_you = "It looks like you're using a browser that we don't fully support. You'll need to get Firefox or some alternative in order to use all the goodness that is our website. Cheerio.";
			alert(no_soup_for_you);
		}		
	},
	
	// hide radio buttons and checkboxes
	'body': function() {
		var the_form = $('mr-wizard');
		var user_agent = window.navigator.userAgent;
		var re_ie = /IE/;
		var is_ie = user_agent.search(re_ie);		
		var re_safari_2 = /Safari\/419/;
		var is_safari_2 = user_agent.search(re_safari_2);
		
		// because IE pukes on this
		if (is_ie == -1 && is_safari_2 == -1) {
			if (the_form) {
				var form_inputs = the_form.getElementsByTagName('input');
				var the_labels = the_form.getElementsByTagName('label');
			}
			
			if (form_inputs) {
				for (var i = 0; i < form_inputs.length; i++) {
					if (form_inputs[i].type == "radio" || form_inputs[i].type == "checkbox") {
						form_inputs[i].hide();
						
						// checks to see if the form element is selected
						// if so, then add the approprate class to its label
						if (form_inputs[i].checked) {
							var the_id = form_inputs[i].id;
							var the_type = form_inputs[i].type;
							
							for (var j = 0; j < the_labels.length; j++) {
								if (the_labels[j].readAttribute('for') == the_id) {
									var constructed_class = "selected_" + the_type;
									
									the_labels[j].addClassName(constructed_class);
								}
							}
						}
					}
				}
			}
		}
	},
	
	// make the restrictions warning pseudo intelligent
	// all vars are here, but not used, just in case
	'body#p-restrictions input:click': function() {
		if (this.checked)
		{
				if (this.id.search(restrictions.ethnicity.rx) != -1)
				{
					restrictions.ethnicity.count++;
				}
				
				if (this.id.search(restrictions.bodytype.rx) != -1)
				{
					restrictions.bodytype.count++;
				}
		}
		else
		{
				if (this.id.search(restrictions.ethnicity.rx) != -1)
				{
					restrictions.ethnicity.count--;
				}
				
				if (this.id.search(restrictions.bodytype.rx) != -1)
				{
					restrictions.bodytype.count--;
				}
		}
		
		if (restrictions.ethnicity.count > 0 && restrictions.ethnicity.count < 5 
			|| restrictions.bodytype.count > 0 && restrictions.bodytype.count < 3)
		{
			if ($('too-many-restrictions').style.display == "none")
				Effect.Appear($('too-many-restrictions'), { duration : .5 });			
		}
		else if ($('too-many-restrictions').style.display == "")
		{
			if (restrictions.ethnicity.count >= 5 || restrictions.bodytype.count >= 3)
				Effect.Fade($('too-many-restrictions'), { duration : .5 });
			else
				setTimeout("Effect.Fade($('too-many-restrictions'), { duration : .5 });", 3000);
		}
	},

	// for when a user clicks a label with an input
	// and re-clicks the input ... we still give it focus
	'label.radio input:click': function() {
		document.last_clicked_input = this;
		setTimeout('document.last_clicked_input.focus();', 100);	
	},
	
	// radio buttons
	'label.radio:click': function() {
		var body_id = document.body.id;
		var the_form = $('mr-wizard');
		var the_labels = the_form.getElementsByTagName('label');
		var the_inputs = the_form.getElementsByTagName('input');
		var this_for_attr = this.readAttribute('for');
				
		if (this.hasClassName("radio") && !this.hasClassName("selected_radio") && !this.hasClassName("disabled_radio")) {
			for (var i = 0; i < the_labels.length; i++) {
				
				if (the_labels[i].hasClassName('radio')) {
					var for_attr = the_labels[i].readAttribute('for');
														
					if ($(for_attr).name == $(this_for_attr).name) {			
						the_labels[i].removeClassName("selected_radio");
					}
				}
			}
			
			this.addClassName("selected_radio");
						
			if (this.firstDescendant() && this.firstDescendant().type == "text") {
				document.last_clicked_descendant = this.firstDescendant();
				setTimeout('document.last_clicked_descendant.focus();',100);
			} else {
				var for_attr = this.readAttribute('for');
				$(for_attr).checked = true;
				
				// disables checkboxes on qask when irrelevant is clicked
				if (this_for_attr == "irrelevant" && body_id == "p-qask" && $(this_for_attr).name == "importance") {
					for (var i = 0; i < the_inputs.length; i++) {
						if (the_inputs[i].type == "checkbox") {
							the_inputs[i].disabled = true;
						}
					}
					
					for (var i = 0; i < the_labels.length; i++) {
						if (the_labels[i].hasClassName('checkbox')) {
							the_labels[i].addClassName('disabled_checkbox');
						}
					}
				}
			}
			
			// removes disabled from checkboxes when clicking off of irrelevant		
			if (this_for_attr != "irrelevant" && body_id == "p-qask" && $(this_for_attr).name == "importance") {				
				for (var i = 0; i < the_inputs.length; i++) {
					if (the_inputs[i].type == "checkbox") {
						the_inputs[i].disabled = false;
					}
				}
				
				for (var i = 0; i < the_labels.length; i++) {
					if (the_labels[i].hasClassName('checkbox')) {
						the_labels[i].removeClassName('disabled_checkbox');
					}
				}
			}
			
		}
	},
	
	
	// faux radio buttons ... fixes radio buttons with selects
	'span.faux-radio:click': function() {
		var the_form = $('mr-wizard');
		var the_labels = the_form.getElementsByTagName('label');
		var the_inputs = the_form.getElementsByTagName('input');
		var this_id = this.id;
		var re_faux = /faux-/;
		
		var this_new_id = this_id.replace(re_faux, "");

		var this_type = $(this_new_id).type;
		
		for (var i = 0; i < the_inputs.length; i++) {
			if(this_type == the_inputs[i].type && this_new_id != the_inputs[i].id) {
				the_inputs[i].checked = false;
				
				for (var j = 0; j < the_labels.length; j++) {
					if (the_labels[j].readAttribute('for') == the_inputs[i].id) {
						the_labels[j].removeClassName("selected_" + the_inputs[i].type);
					} 
				}
			}
		}
		
		$(this_new_id).checked = true;
		
		for (var i = 0; i < the_labels.length; i++) {
			var for_attr = the_labels[i].readAttribute('for');
			
			if (for_attr == this_new_id) {			
					the_labels[i].addClassName("selected_radio");
			}
		}
			
	},
	
	// checkboxes
	'label.checkbox:click': function() {
		var for_attr = this.readAttribute("for");
		
		(this.hasClassName("selected_checkbox")) ? (this.removeClassName("selected_checkbox")) : (this.addClassName("selected_checkbox"));

		// In safari2, clicking a label around a checkbox doesn't set the checkbox,
		// so we have to do it all manual and stuff.
		if (is_safari_2 != '-1') {
			if (for_attr) {
				if ($(for_attr).checked == true) {
					$(for_attr).checked = false;
				}
				else {
					$(for_attr).checked = true;
				}
			}
		}		
	},
	
	// a solo date
	'body#p-landing label#a-solo-date:click': function() {
		var the_form = $('mr-wizard');
		$('solo').checked = "checked";
		the_form.submit();
	},
	'body#p-landing label#a-solo-date:mouseover': function() {
		this.setStyle({
			backgroundPosition: 'bottom'
		});
	},
	'body#p-landing label#a-solo-date:mouseout': function() {
		this.setStyle({
			backgroundPosition: 'top'
		});
	},
	
	// a double date
	'body#p-landing label#a-double-date:click': function() {
		var the_form = $('mr-wizard');
		$('double').checked = "checked";
		the_form.submit();
	},
	'body#p-landing label#a-double-date:mouseover': function() {
		this.setStyle({
			backgroundPosition: 'bottom'
		});
	},
	'body#p-landing label#a-double-date:mouseout': function() {
		this.setStyle({
			backgroundPosition: 'top'
		});
	},
	
	// either
	'body#p-landing label#either-date:click': function() {
		var the_form = $('mr-wizard');
		$('either').checked = "checked";
		the_form.submit();
	},
	'body#p-landing label#either-date:mouseover': function() {
		this.setStyle({
			backgroundPosition: 'bottom'
		});
	},
	'body#p-landing label#either-date:mouseout': function() {
		this.setStyle({
			backgroundPosition: 'top'
		});
	},
	
	'p#message': function() {
		var close = '<span style="position: absolute; top: 5px; right: 15px; font-size: 11px;">[ <a href="#" id="close-message" title="Close this message." style="color: #FFF;">close</a> ]</span>';
		
		$('message').innerHTML += close;
	},
	
	'div#message': function() {
		var close = '<span style="position: absolute; top: 5px; right: 15px; font-size: 11px;">[ <a href="#" id="close-message" title="Close this message." style="color: #FFF;">close</a> ]</span>';
		
		$('message').innerHTML += close;
	},
	
	'a#close-message:click': function() {
		Effect.Fade('message');
	},
		
	'body#p-handshake table tr td.details a:click': function() {
		var link_id = this.id;
		var regex = /view-/;
		var tr_id = link_id.replace(regex, "");

		$(tr_id).show();
		return false;
	},
	
	'body#p-handshake table tr.the-details td a.hide:click': function() {
		var tr_id = this.parentNode.parentNode.parentNode.id;

		$(tr_id).hide();
		return false;
	},
	
	'body#p-settings a#delete-account:click': function() {
		Effect.Appear("delete-me");
		return false;
	},
	
	'body#p-settings form#delete-me a#cancel-delete:click': function() {
		Effect.Fade("delete-me");
		return false;
	},
	
	'body#p-handshake form#mr-wizard select#user_okay:change': function() {
		var cancel_warning = $('cancel-warning');
		var p_tags = cancel_warning.getElementsByTagName('p');

		// "Uh...No." is value = 1
		if (this.value == 1)
		{		
			Effect.Fade(cancel_warning);
			Effect.SlideUp(cancel_warning);
		}
		else if (this.value == 2 && cancel_warning.style.display == "none")
		{
			for (var i = 0; i < p_tags.length; i++)
				p_tags[i].style.bottom = "0";
				
			Effect.Appear(cancel_warning);
			Effect.SlideDown(cancel_warning);
		}
	},
	
	'body#p-handshake form#mr-wizard label.checkbox:click': function() {
		var the_radios = document.getElementsByClassName('radio');
		
		if ($(this).hasClassName('selected_checkbox'))
		{
			for (var i = 0; i < the_radios.length; i++)
			{
				if (the_radios[i].readAttribute('for') == "not-a-fan-of")
				{
					$(the_radios[i].readAttribute('for')).click();
					the_radios[i].addClassName('selected_radio');			
				}
				else
				{
					the_radios[i].removeClassName('selected_radio');
				}
			}
		}
	}
});




















