jQuery(document).ready(function($) {
	// Ensure IE does not cache our ajax calls
	
	$.ajaxSetup({
		cache: false
	});

	$( '.clickDisable' ).live( 'click', function() {
		$( this ).addClass( 'disabled' );
		$( this ).prop( 'disabled', true );
		$( this ).blur();
		$( this ).parents( 'form' ).submit();
	});

	// Launch Facebox
	$( 'a[ rel*=facebox ]' ).facebox();
	
	
	// Toggle status dropdown
	$("a.dropdown#online-status").click(
		function(){
			$("div.pane-status ul").toggle();
			return false;
		}
	);
	$("a.dropdown").toggle(
		function () { $(this).addClass('hide'); },
		function () { $(this).removeClass('hide'); }
	);
	
	// Toggle mood dropdown 
	// (Actually, only show.  Hide is handled by $("a.dropdown").toggle. 
	//  It can be combined with online-status, $("a.dropdown#online-status").click, by a brilliant person.)
	$("a.dropdown#mood").click(
		function(){
			$("div.pane-mood ul").toggle();
			return false;
		}
	);

	// Toggle favorites dropdown
	$("a.dropdown#favorites").click(
		function(){
			$("div.pane-favorites ul").toggle();
			return false;
		}
	);

	// Dropdown fix for IE6

	if ($.browser.msie && $.browser.version=="6.0") {
		$(".nav-main").find("li").hover(
			function () { $(this).addClass("over"); },
			function () { $(this).removeClass("over"); }
		);
	};


	// Selectbox overlay fix for IE6

	if ($.browser.msie && $.browser.version == '6.0') {
		$('.nav-main ul li ul').each(function(){
			var obj = $(this);
			var parent = obj.parent();

			obj.after('<iframe class="iframe hide" style="position: absolute; top: 36px; left: 0; width: ' + obj.width() + 'px; height: ' + obj.height() + 'px; z-index: 20;" src="javascript:\'<html></html>\'" frameBorder="0" scrolling="no"></iframe>');

			parent.mouseover(function(){
				if (obj.parents('.nav-personal').get(0)) obj.next().css({ 'left' : 'auto', 'right' : '0' });
				obj.next().removeClass('hide');
			});

			parent.mouseout(function(){
				obj.next().addClass('hide');
			});
		});
	};


	// IE6/7 fix for disabled options

	if ($.browser.msie && ($.browser.version == '6.0' || $.browser.version == '7.0')) {
		var selects = document.getElementsByTagName('select');

		for(var i = 0; i < selects.length; i++){
			selects[i].onchange = function(){
				if (this.options[this.selectedIndex].disabled) {
					this.selectedIndex = 0;
				}
			}

			
						
			if (selects[i].selectedIndex > -1){ // selectedIndex = -1 if dropdown is empty, and accessing properties would cause error
				if (selects[i].options[selects[i].selectedIndex].disabled) {
					selects[i].onchange();
				}				
			}
			

			for (var j = 0; j < selects[i].options.length; j++) {
				if (selects[i].options[j].disabled) {
					selects[i].options[j].style.color = '#CCC';
				}
			}
			
		}
	};


	// Search by country or ZIP

	$("#search-location-country").addClass('hide');
	$("a.search-by-country, a.search-by-zip").click(
		function(){
			$("#search-location-zip").toggle();
			$("#search-location-country").toggle();
			return false;
		}
	);


	//url anchor finder
	function getAnchorName(){
		var myFile = document.location.toString();
		if (myFile.match('#')) { // the URL contains an anchor
			 // click the navigation item corresponding to the anchor
		 return myFile.split('#')[1];
		} else {
			return false;
		}
	}


	// Edit Profile accordion

	$( '.edit-profile' ).accordion({
		active: false,
		autoHeight: false, 
		collapsible: true, 
		header: 'a.pane-title', 
		icons: { 'header': 'ui-accordion-hide', 'headerSelected': 'ui-accordion-show' }
	});

	
	// Detailed Search accordion

	$( '.block-detailed-search .pane-title' ).click( function() {
		$( this ).next().slideToggle( 'fast' );
		$( this ).toggleClass( 'selected' );
		return false;
	}).next().hide();

	
	// My Matches sliders

	$('.panel-contents').each(function(){
		var box   = $(this).find('.box');
		var input = $(this).find('input');
		var setText=function(v){
			if (v == 1) box.text( sliderValues[ 0 ] );
			if (v == 2) box.text( sliderValues[ 1 ] );
			if (v == 3) box.text( sliderValues[ 2 ] );
			if (v == 4) box.text( sliderValues[ 3 ] );
			if (v == 5) box.text( sliderValues[ 4 ] );
		}
		var initVal=function(){
			var v = input.val();
			setText(v);
			return v;
		}
		// $(this).find('.slider-bar').slider({
		$(this).find('.ws-slider').slider({
			min: 1,
			max: 5,
			animate: true, 
			value: initVal(),
			slide: function(e,ui){
				setText(ui.value);
				input.val( ui.value );
			}
		});

	});


	// Messages selection

	$('#messages input[type=checkbox].select-all').click(function(){
		var status = this.checked;

		$('#messages input[type=checkbox]').not('.select-all').each(function(){
			this.checked = status;
			if (this.checked == false) $(this).parents('tr').removeClass('selected'); else $(this).parents('tr').addClass('selected');
		});
	});
	$('#messages input[type=checkbox]').not('.select-all').click(function(){
		if (this.checked == false) $(this).parents('tr').removeClass('selected'); else $(this).parents('tr').addClass('selected');
	});


	// Rounded corners

	$('.round').each(function(){
	  $(this).prepend('<b class="rc"><b class="rc1"><b></b></b><b class="rc2"></b><b class="rc3"></b><b class="rc4"></b><b class="rc5"></b></b>');
	  $(this).append('<b class="rc"><b class="rc5"></b><b class="rc4"></b><b class="rc3"></b><b class="rc2"></b><b class="rc1"><b></b></b></b>');
	});
	
	// Odd rows

	$(".pane-activity table tr:nth-child(odd)").addClass("odd");
	$(".pane-favorites ul li:nth-child(odd)").addClass("odd");




	// Sign Up page
		//REMOVED, original file is in global_original.jsp



	// Landing tooltips

	$('.tooltip').tipsy();

	// nave Favorites tooltip
	$( '#nav-favorites' ).tipsy();

	// submit a form on enter
	// since we have no true submit buttons, the enter key didn't seem to trigger a form submit.  this is a workaround - cjg
	$( 'input' ).keypress( function( e ) {
		c = e.which ? e.which : e.keyCode;
		
		if ( c == 13 ) {
			$( this ).parents( 'form' ).submit();
			e.preventDefault();
		}
	});


	$( 'textarea[ maxlength ]' ).keypress( function( e ) {
		//get the limit from maxlength attribute
		var limit = parseInt( $( this ).attr( 'maxlength' ) );
		//get the current text inside the textarea
		var text = $( this ).val();
		//count the number of characters in the text
		var chars = text.length;

		//check if there are more characters then allowed
		if ( chars > limit ) {
			//and if there are use substr to get the text before the limit
			var new_text = text.substr( 0, limit );

			//and change the current text with the new text
			$( this ).val( new_text );
		}
	});

	// assign class of 'singleclick' to an element to prevent double-clicking
	$( '.singleclick' ).click( function( e ) {
		e.preventDefault();

		var linkto = $( this ).attr( 'href' );
		$( this ).attr( 'href', '' );
			
		if ( linkto != "" ) {
			location.href = linkto;
		}
	});
});



/*

//Get the event object (for IE)
var ob = e || event;
//Get the code of key pressed
var keyCode = ob.keyCode;
//Check if it has a selected text
var hasSelection = document.selection? document.selection.createRange().text.length > 0 : this.selectionStart != this.selectionEnd;
//return false if can't write more
return !(this.value.length >= max && (keyCode > 50 || keyCode == 32 || keyCode == 0 || keyCode == 13) && !ob.ctrlKey && !ob.altKey && !hasSelection);
};
*/




// Hide <select>'s before show Facebox in IE6
if ($.browser.msie && $.browser.version=="6.0"){
	$(document).bind("beforeReveal.facebox", function(){$("select").hide();})
	$(document).bind("close.facebox", function(){$("select").show();})
}


// Close Facebox
function CloseFacebox(){
	jQuery(document).trigger('close.facebox');
}

// disable styled a elements
// arguments:
// el = element to disable
// message = message to display (should always be passed in as the translation for "Please Wait...")
doDisable = function( el, message ) {
	var container = $( el ).parent();	// gets the parent/container div (there -should- be one!)
	container.empty();					// empties the parent div
	container.append(					// appends a disabled "button" element where the submit button used to be 
		'<span class="button"><span><cfoutput>' + message + '</cfoutput></span></span>' 
	);
};

