$(document).ready(function(){
    
	$('#account-infobox .access-info a').click(function(){
	    var condition = $(this).parent('.access-info').hasClass('history');
	    if(condition)
	    {
		$('#logout-info').find('.history-box').toggle();
		return false;
	    }
	    else
		return true;
	})
    	    
	$('#logout-info').find('.history-box').live('mouseleave', function(){
		$('#logout-info').find('.history-box').hide();
	});
    
	// old red-box notifications
	$('.red-border-info').confirmBox();
	$('.confirm-box-dialog > .red-border-info').parent().css('position','absolute').css('top','100px');
	
	//old error notifications
	$('.content-error').confirmBox();
	
	$('#payment_survey').find('input[type=text]').focus(function(){
	    $(this).siblings('[type=checkbox]').attr('checked', 'checked');
	}).hint();
	
	$('#payment_survey').confirmBox({
	    okCondition:function(data){
		var f = data.find('form');
		var data = $(f).serialize();
		var url = $(f).attr('action');
				
		$.post(url, data);
		return true;
	    },
	    callback : function() {
		var okbox = $('.survey-ok').clone();
		okbox.confirmBox();
		setTimeout(function(){okbox.dialog('destroy');}, 1000);
	    }
	});
    
	if($('#searchtext').length > 0 ){
		$('#searchtext').unbind('.autocomplete');
		//$('#searchtext').ajaxHinter.unbinder();
		$('#searchtext').unautocomplete();

		$('#searchtext').attr('hint', $('.searchpos.selected').attr('hint')).hint();
		$('#sfor_text').val($('.searchpos.selected').attr('sfor'));
		set_autocomplete();
		
		var searchboxBind = function() {
		    switch($('#sfor_text').val()) {
			case 'colleagues':
			    $('#searchtext').unbind('.autocomplete');
			    $('#searchtext').ajaxHinter({url:'/search/mates_hint'});
			    break;
			case 'agency':
			    $('#searchtext').unbind('.autocomplete');
			    $('#searchtext').ajaxHinter({
				url:'/search/agencies_hint',
				afterResponse : function(response) {
				    $(response).find('.suggest-agency-employing').each(function(){
					$(this).siblings(":first").width(
					    $(this).parent().width() - $(this).width() - 15
					);
				    });
				}
			    });
			    break;
			default:
			    $('#searchtext').ajaxHinterUnbinder();
			    $('#searchtext').attr('hint', $('.searchpos.selected').attr('hint')).hint();
			    set_autocomplete();
			    break;
		    }
		};
		searchboxBind();

		$('.searchpos').click(function(){
			$('.searchpos').removeClass('selected');
			$(this).addClass('selected');
			$('#sfor_text').val($(this).attr('sfor'));
		
			$('#searchtext').unautocomplete();
			$('#searchtext').unbind('.autocomplete');
			$('#searchtext').attr('hint', $(this).attr('hint')).hint();
			$('.ac_results').remove();
			$('.sfor_adv').hide();
			$('.sfor_adv_'+$('#sfor_text').val()).show();
			searchboxBind();
			if($('#sfor_text').val() == "ships" && $('#searchtext').val() != $('#searchtext').attr('hint'))
				$('#searchtext').css('text-transform', 'uppercase');
			else
				$('#searchtext').css('text-transform', 'none');
			$(this).removeClass('red-border');
		});
		
		$('#searchtext').focus(function(){
			if($('#sfor_text').val() == "ships")
				$(this).css('text-transform', 'uppercase');
		});
		
		$('#searchtext').blur(function(){
			if($('#sfor_text').val() != "ships" || $(this).val().length == 0 || $(this).val() == $(this).attr('hint'))
				$(this).css('text-transform', 'none');
			$(this).removeClass('red-border');
		});

		$('#searchfield').find('.result').live('click',function()
		{
		    var name = $(this).find('.profile-name').text();
		    $('#searchtext').val(name);
		    $(this).parents('form').submit();
		})
	}
});

function set_autocomplete()
{
	switch($("#sfor_text").val())
	{
		case "ships":
			$("#searchtext").autocomplete('/search/suggest', {
				   extraParams: {
				       searchfor: function() {return $("#sfor_text").val();}
				   },
				   cacheLength : 0,
				   scroll: true,
				   scrollHeight: 260,
				   max: 10000,
				   minChars: 5,
				   width: 410,
				   submitOnSelect: true
				});
			break;
		/*case 'colleagues':
			$("#searchtext").autocomplete('/search/suggest', {
				   extraParams: {
				       searchfor: function() { return $("#sfor_text").val();}
				   },
				   cacheLength : 0,
				   scroll: true,
				   scrollHeight: 250,
				   width: 410,
				   submitOnSelect: false
				});
			break;*/
		case 'gallery':
			$("#searchtext").autocomplete('/search/suggest', {
				   extraParams: {
				       searchfor: function() {return $("#sfor_text").val();}
				   },
				   cacheLength : 0,
				   scroll: false,
				   width: 410,
				   submitOnSelect: true
				});
			break;
		/*case "agency":
			$("#searchtext").autocomplete('/search/suggest', {
				   extraParams: {
				       searchfor: function() { return $("#sfor_text").val();}
				   },
				   cacheLength : 0,
				   scroll: true,
				   scrollHeight: 260,
				   max: 10000,
				   minChars: 2,
				   width: 410,
				   submitOnSelect: true
				});
			break;*/
	}
}
