(function($){
   $.extend($.expr[':'],{
	    open: function(a) {
	        return $(a).data('open') === true;
	    }
	});
})(jQuery);

(function($){
	jQuery.fn.delay = function(millis,callBack){
		var object = $(this);
		$.extend(object,{callBack:callBack});
		return window.setTimeout(function() {
			object.callBack();
			return object;
		}, millis);
	}
})(jQuery);

(function($){
	
	$.fn.isOpen = function(){
		if( $(this).is(':open')){
			return elm.data('open');
		}else{
			return false;
		}
	}
	
	
	$.fn.ajaxify = function(){
		return $(this).each(function(){
			var form = $(this);
			var action = $(this).attr('action');
			var method = $(this).attr('method');
			var button = $(this).find(':image, :submit');
			
			
			form.find('input, select, textarea').keypress(function(){
				if($.trim($(this).val()).length > 0){
					$(this).closest('div.text-input').removeClass('focus');	
				}
			});
			
			action += (/\?/.test(action))?'&ajax=true':'?ajax=true';
			
			button.click(function(e){
				e.preventDefault();
				form.find('div.error').remove();
				
				var data = form.serializeArray();
				var respons = $('<div class="message"></div>').appendTo(form).hide();
				
				respons.ajaxError(function(){
					$(this).html($.settings.error.functions.noResponse);
				});
				
				respons.load(
					action,
					data,
					function(responseText, textStatus, XMLHttpRequest){
						if(textStatus == 'error'){
							var statusCode = parseInt(XMLHttpRequest.status);
							if (statusCode == 460) {
								var fieldFocus = $.trim(XMLHttpRequest.getResponseHeader('Ajax-Field-Focus').toString());
								var fieldSelect = $.trim(XMLHttpRequest.getResponseHeader('Ajax-Field-Select').toString());
								respons.html(responseText);
								respons.fadeIn('fast').delay(4000, function(){
									if(fieldFocus.length > 0){
										$(fieldFocus).closest('div.text-input').addClass('focus');
									}
									if(fieldFocus.length > 0){
										$(fieldSelect).get(0).select();
									}
									respons.fadeOut('fast', function(){
										$(this).remove();
									});
								});
							}else {
								respons.fadeIn('fast').delay(4000, function(){
									respons.fadeOut('fast', function(){
										$(this).remove();
									});
								});
							}
						}else{
							respons.html(responseText).fadeIn('fast');	
						}
					}
				);
			});
		});
	}
	
	$.fn.accordion = function(){
		return $(this).each(function(){
			var accordion = $(this);
			var maxHeight = accordion.find('div.open:first').closest('li').height();
			var minHeight = accordion.find('div:not(.open)').closest('li').height();
			
			accordion.find('ul:first>li').data('open',false).height(minHeight);
			accordion.find('ul:first>li div.open').closest('li').data('open',true).height(maxHeight);
			accordion.find('ul:first>li div.content').addClass('open');
			
			accordion.find('li').each(function(){
				var elm = $(this);
				var link = elm.find('>h2>a');
				
				link.click(function(e){
					e.preventDefault();
					if(!elm.isOpen()){
						var open = accordion.find('li:open');
						open.find('h2').removeClass('open');
						open.animate(
							{height:minHeight},
							{
								duration:200, 
								queue:false,
								complete:function(){
									$(this).data('open',false);
								}
							}
						);
						
						elm.find('h2').addClass('open');
						elm.animate(
							{height:maxHeight},
							{
								duration:200, 
								queue:false,
								complete:function(){
									$(this).data('open',true);
								}
							}
						);
					}
				});
			});
			
		});
	}
	
	$(document).ready(function(){
		$('.js-accordion').accordion();
		$('form.js-ajaxify').ajaxify();
		
	});
})(jQuery);
