// code ripped from AIM
jform = {
    frame : function(c) {

        var n = 'f' + Math.floor(Math.random() * 99999);
        var d = document.createElement('DIV');
        d.innerHTML = '<iframe style="display: none" src="about:blank" id="'+n+'" name="'+n+'" onload="jform.loaded(\''+n+'\')"></iframe>';
        document.body.appendChild(d);
        
        var i = document.getElementById(n);
        if (c && typeof(c.onComplete) == 'function') {
            i.onComplete = c.onComplete;
        }

        return n;
    },

    form : function(f, name) {
        f.setAttribute('target', name);
    },

    submit : function(f, c) {
        jform.form(f, jform.frame(c));
        if (c && typeof(c.onStart) == 'function') {
            return c.onStart();
        } else {
            return true;
        }
    },

    loaded : function(id) {
        var i = document.getElementById(id);
        if (i.contentDocument) {
            var d = i.contentDocument;
        } else if (i.contentWindow) {
            var d = i.contentWindow.document;
        } else {
            var d = window.frames[id].document;
        }
        if (d.location.href == "about:blank") {
            return;
        }

        if (typeof(i.onComplete) == 'function') {
            i.onComplete(d.body.innerHTML);
        }
    }
}

var errors;

$(document).ready( function() {
	var height = parseFloat($(document).height());
	

	$('#content').css({'min-height': (height - 154 - 76 - 86)});

	$('.loading').hide();

	$("form#contact-form, form#search-form, form#recommendations-form").submit( function() {
		errors = false;

		$('.loading').show();

		$( '#' + this.id + ' .validate' ).each( function() {
			if( $( this ).val().trim() == '' ) {
				errors = true;

				$( this ).addClass( 'err' );
				$( this ).parent( ).prev( ).children( 'span' ).show( );
			} else {
				$( this ).removeClass( 'err' );
				$( this ).parent( ).prev( ).children( 'span' ).hide( );
			}
		});

		if( errors ) {
			$('.loading').hide();

			return false;
		}

		return jform.submit(this, {
			onComplete: function( data ) {
				if( !data.match( /Error:.*/ ) )
					ddaccordion.expandone('main_links', 6);
				else {
					
				}

				$('.loading').hide();
			}
		});

		return false;
	});

	$("form#tag-form").submit( function() {
		$('.loading').show();

		return jform.submit(this, {
			onComplete: function(d) {
				$('#tags').show();
				if(d.length > 0)
					$('#tags div').html(d);
				else
					$('#tags div').html('No tags found');

				$('.loading').hide();
			}
		});

		return false;
	});
});

String.prototype.trim = function () {
	return this.replace(/^\s*|\s*$/,"");
};