preloadImg = function(image){
	var img = new Image();
	img.src = image;
}
preloadImg('media/img/overlay/blue_login.png');
preloadImg('media/img/overlay/logo.png');
preloadImg('media/img/overlay/blue_register.png');
preloadImg('media/img/overlay/close.png');

window.iact = window.iact || {};


iact.log = function( text ){

	( ( window.console && window.console.log ) || ( window.opera && window.opera.postError ) ||  ( function(){} ) )( text )

}


$.fx.speeds._default = 200;


iact.notify = function( message, type ){

	if( ! $.jGrowl ){
		alert( message );
		return;
	}

	type = type || 'message';

	if( ! iact.notify.jgrowlSetup ){
		$.jGrowl.defaults.position = 'center';
		iact.notify.jgrowlSetup = true;
	}


	$.jGrowl( $( '<div/>' ).append( $( '<img/>' ).attr( {
		src : iact.base + iact.notify.ico[ type ]
	} ) ).append( $( '<div class="actual-message"/>' ).html( message ) ).html(), {
		sticky : type == 'alert'
	})

}


iact.notify.ico = {

	alert : 'media/img/ico-alert.png',
	message : 'media/img/ico-message.png'

}

iact.humanize = function ( string ){

	return $.trim( string ).replace( /[\/\\_-]+/g, ' ' ).replace( /\s{2,}/, ' ' );
}

iact.string2slug = function( string ) {

	if ( ( /\S/ ).test( string ) ) {
		return string.toLowerCase().replace( /[^a-z0-9_]/gm, '-' ).replace( /^-|-{2,}|-$/gm, '' );
	} else {
		return '';
	}
}

$.fn.slugify = function ( string ){

	if(! arguments.length ){
		string = null;
	}

	return this.each( function (){
		var $t = $( this );
		$t.val( iact.string2slug( null === string ? $t.val() : string  ) );
	} );

}
iact.equalHeight = function ( lot ){
	var m = 0;
	lot.each(function() {
		t = $(this).height();
		t > m  && ( m = t );
	});
	return lot.css( 'min-height', m );
}

$.fn.equalHeight = function(){

	return iact.equalHeight( this );
}

iact.loadingMessage = function( loadingWhat ){

	loadingWhat = loadingWhat || 'Loading';

	return $( '<div class="loading-message">' + loadingWhat + '. Please Wait.</div>'  );
}

iact.yosExpander = function( element ){
	$( element ).click( function ( ev ){
		var $this = $( this ),
			$eventName = $this.hasClass( 'on' ) ? 'collapsed' : 'expanded';
		( $this.hasClass( 'on' ) ? $this.removeClass( 'on' ).text( 'show' ) :
									$this.addClass( 'on' ).text( 'hide' ) ).parent().nextAll( '.expand-me:first' )
		.slideToggle( 'fast', function(){
			$( this ).triggerHandler( $eventName )
		} )

		;



		ev.preventDefault();
	} )
}


$.fn.yosExpander = function(){

	return this.each( function (){
		iact.yosExpander( this );
	} );

}


iact.cuteLabel = function( elem ){
	var $elem = $( elem );

	$elem.find( 'input[type=radio], input[type=checkbox]' )
		.change( function ( ev ){
			if( $( this ).is( 'input[type=radio]' ) && $( this ).is( ':checked' ) ){
				$elem.siblings( 'label, .label' ).removeClass( 'selected' );
			}
			$( elem ).toggleClass( 'selected', $( this ).is( ':checked' ) );
		} );

	if( ! $elem.is( 'label' ) || $.browser.msie ){
		$elem.click( function ( ev ){
			$input = $elem.find( 'input[type=radio], input[type=checkbox]' );
			if( $input.is( 'checked' ) )
				$input.removeAttr( 'checked' );
			else
				$input.attr( 'checked', 'checked' );

			$input.change();
		} );
	}

	if( $elem.find( 'input[type=radio]:checked, input[type=checkbox]:checked' ).size() ){

		$elem.addClass( 'selected' );
	}

	return $elem;
}

$.fn.cuteLabel = function(){
	return this.each( function ( i, el ){
		iact.cuteLabel( el );
	} );

}

$( function(){

	$( '#left, #right' ).equalHeight();
	$( '.pretty-date' ).prettyDate();

	$( '.labels>label, .labels>.label' ).cuteLabel();

	$( '#latest-battles-cheese .navigation' ).click( function ( ev ){

		var $a = $( ev.target ),
			$this = $( this ).find( 'a' ).toggleClass( 'on' ).end().parent();


		if( ! $a.is( 'a' ) )
			$a = $a.parents( 'a:first' );

		$this.children( '.battle_bcg' ).hide()
			.removeClass( 'hide' )
			.filter( '.' + ( $a.is( '.r' ) ? 'r' : 'l' ) ).show()


		ev.preventDefault();

	} );

	$( '.select-me' ).focus( function ( ev ){
		this.select();
	} );

	$( '#videos-search-form' ).submit( function ( ev ){

		var searchTerm = $( this ).children( 'input[name=s]' ).val();

		if( ( ! $.trim( searchTerm ).length ) || searchTerm == 'Seach Our Smarty Videos' )
			ev.preventDefault();

	} );

} )



