function loopImages() {
	
	if ( $( '#title_images .images div' ) < 2 ) {
		
		return true;
	}
	
	var current = $( '#title_images .images div:visible' );
	
	if ( current.next().get( 0 ) ) {
		
		current.hide();
		current.next().show();
	} else {
		
		current.hide();
		$( '#title_images .images div:eq(0)' ).show();
	}
	
	looper = setTimeout( loopImages, loop_period );
}

function changeImage( dir ) {
	
	var current = $( '#title_images .images div:visible' );
	
	if ( dir == 1 ) {
		
		if ( current.next().get( 0 ) ) {
			
			current.hide();
			current.next().show();
		} else {
			
			current.hide();
			$( '#title_images .images div:eq(0)' ).show();
		}
	} else {
		
		if ( current.prev().get( 0 ) ) {
			
			current.hide();
			current.prev().show();
		} else {
			
			current.hide();
			$( '#title_images .images div:last' ).show();
		}
	}
	
	clearTimeout( looper );
	looper = setTimeout( loopImages, loop_period );
}

function submitContact( form ) {
	
	form = $( form );
	action = $( form ).attr( 'action' );
	form.find( '.error' ).removeClass( 'error' );
	form.find( '.error_msg' ).hide();
	
	$.ajax( {
		url : action,
		data : form.serializeArray(),
		dataType : 'json',
		type : 'POST',
		success : function( response ) {
			
			if ( !response.result && typeof( response.error_msg ) != 'undefined' ) {
				
				form.find( '.error_msg' ).html( response.error_msg );
				form.find( '.error_msg' ).addClass( 'error' ).show();
				
				return false;
			}
			
			if ( !response.result && typeof( response.error_fields ) != 'undefined' ) {
				
				$.each( response.error_fields, function( index, key ){
					
					$( '#con_' + key ).parent().parent().addClass( 'error' );
				} );
				
				return false;
			}
			
			form.replaceWith( '<div class="success">' + response.success_msg + '</div>' );			
			
			return true;
		}
	} );
}

function subscribe( form ) {
	
	form = $( form );
	action = $( form ).attr( 'action' );
	form.find( '.error' ).removeClass( 'error' );
	form.find( '.error_msg' ).hide();
	
	$.ajax( {
		url : action,
		data : form.serializeArray(),
		dataType : 'json',
		type : 'POST',
		success : function( response ) {
			
			if ( !response.result && typeof( response.error_msg ) != 'undefined' ) {
				
				form.find( '.error_msg' ).html( response.error_msg );
				form.find( '.error_msg' ).addClass( 'error' ).show();
				
				return false;
			}
			
			if ( !response.result && typeof( response.error_cats ) != 'undefined' ) {
				
				form.find( '[type=text]' ).parent().addClass( 'error' );
				
				return false;
			}
			
			if ( !response.result && typeof( response.error_fields ) != 'undefined' ) {
				
				$.each( response.error_fields, function( index, key ){
					
					$( '#sub_' + key ).parent().addClass( 'error' );
				} );
				
				return false;
			}
			
			form.parent().html( '<div class="success">' + response.success_msg + '</div>' );			
			
			return true;
		}
	} );
}

function addImageBorders() {
	
	$( '.text .text_text img' ).addClass( 'image_border' ).wrap( "<table class='img_border'><tr class='image_mid'><td class='bg_5'><\/td><\/tr><\/table>" );
		
	$( '.text .text_text .image_mid' ).prepend( "<td class='bg_4'><img src='" + images_url + "s.gif' alt='' /><\/td>" );
	$( '.text .text_text .image_mid' ).append( "<td class='bg_6'><img src='" + images_url + "s.gif' alt='' /><\/td>" );
	
	$( '.text .text_text .img_border' ).append( "<tr><td class='bg_7'><img src='" + images_url + "s.gif' alt='' /><\/td><td class='bg_8'><img src='" + images_url + "s.gif' alt='' /><\/td><td class='bg_9'><img src='" + images_url + "s.gif' alt='' /><\/td><\/tr>" );
	$( '.text .text_text .img_border' ).prepend( "<tr><td class='bg_1'><img src='" + images_url + "s.gif' alt='' /><\/td><td class='bg_2'><img src='" + images_url + "s.gif' alt='' /><\/td><td class='bg_3'><img src='" + images_url + "s.gif' alt='' /><\/td><\/tr>" );
	
	$( '.text .text_text img.image_border' ).each( function() {
		
		try {
			
			if ( $( this ).attr( 'align' ) != '' ) {
			
				var align = $( this ).attr( 'align' );
				
				switch ( align ) {
					
					case 'left':
						$( this ).parents( 'table.img_border' ).addClass( 'img_border_left' );
						break;
					case 'right':
						$( this ).parents( 'table.img_border' ).addClass( 'img_border_right' );
						break;
				}
				
			}
		} catch( e ) {
		}
	} );
}

$( function() {
	
	$( '#sub_email' ).focus( function(){
		
		if ( this.value == subscribe_email_val ) {
			
			this.value = '';
		}
	} ).blur( function(){
		
		if ( this.value == '' ) {
			
			this.value = subscribe_email_val;
		}
	} );
	
	addImageBorders();
} );