// JavaScript Document

function showFlashMessage( title, message, message_options )
{	
	var sufix = Math.round( new Date().getTime() / 1000 );
	var message_id = "flash_message_" + sufix;
	
	var html_code = '<div id="' + message_id + '" title="' + title + '">';
	html_code += '<p>';
	
	if ( typeof( message_options ) == 'object' && message_options.type != '' )
	{
		html_code += '<span class="ui-icon ui-icon-info" style="float:left; margin:0 7px 50px 0;"></span>';
	}
	html_code += message;
	html_code += '</p>';
	html_code += '</div>';
	
	var width = "auto";
	var height = "auto";
	var modal = true;
	var show_title = true;
	var show_button = true;
	var auto_hide = 0;
	if ( typeof( message_options ) == 'object' )
	{
		if ( typeof( message_options.width ) != "undefined" )
			width = parseInt( message_options.width );
		if ( typeof( message_options.height ) != "undefined" )
			height = parseInt( message_options.height );
		if ( typeof( message_options.modal ) != "undefined" )
			modal = message_options.modal;
		if ( typeof( message_options.show_title ) != "undefined" )
			show_title = message_options.show_title;
		if ( typeof( message_options.show_button ) != "undefined" )
			show_button = message_options.show_button;
		if ( typeof( message_options.auto_hide ) != "undefined" )
			auto_hide = parseInt( message_options.auto_hide );
	}
	
	var buttons = {
				"  OK  ": function() {
					$( this ).dialog( 'close' );
				}
			};
	if ( !show_button )
		buttons = null;
	$( "body" ).append( html_code );
	$( "#" + message_id ).dialog( 
		{
			bgiframe: true,
			modal: modal,
			width: width,
			height: height,
			buttons: buttons,
			zIndex: 2000
		}
	);
	
	if ( show_title == false )
		$( "div.ui-dialog-titlebar" ).remove();
	if ( auto_hide > 0 )
	{
		setTimeout( "hideFlashMessage( '" + message_id + "' )", auto_hide );
	}

}

function hideFlashMessage( message_id )
{
	$( "#" + message_id ).dialog( 'close' );
}

var current_header = 1;
function showNextHeader( headers_checked )
{
	$("img#header_image_" + current_header).fadeOut( 1000 );
	
	current_header++;
	if ( current_header > headers_checked )
		current_header = 1;
		
	$("img#header_image_" + current_header).fadeIn( 1000 );
}

var current_news_image = 1;
function changeNewsImage( num, cnt_images )
{
	if ( num == -1 ) // previous page
	{
		current_news_image -= 1;
		if ( current_news_image < 1 )
			current_news_image = cnt_images;
	}
	else if ( num == -2 ) // next page
	{
		current_news_image += 1;
		if ( current_news_image > cnt_images )
			current_news_image = 1;		
	}
	else
	{
		current_news_image = num;
	}
	
	$( "img.news-image" ).hide();
	$( "img#news_image_"+current_news_image ).css("display","inline-block");
	$( "a.news-link" ).removeClass( "selected" );
	$( "a#news_link_"+current_news_image ).addClass( "selected" );
}

function postGalleryComment()
{
	
	var params = $("form#comment_form").serialize();
	$.post("/gallery/ajax/post_comment", params, function(data) {
		
		showFlashMessage( data.title, data.message, data.popup_options );
		
		$("a#refresh_code").trigger("click");
		
		if( data.popup_options.auto_hide > 0 )
		{
			var gallery_image_id = $("input[name=gallery_images_id]").val();
			$("a#gallery_image_" + gallery_image_id).trigger("click");
		//	$("form#comment_form input[type=text],form#comment_form textarea").val("");
		}

	}, "json");
}










