var my_input_temp_value = '';
var responseJSON = '';

$(document).ready(function() {

    $('.my_input').focus(
        function() {
            my_input_temp_value = $(this).val();
            $(this).val('');
        }
    
    );
    $('.my_input').blur(        
        function() {
            if ( $(this).val() == '' ) $(this).val(my_input_temp_value);           
        }
    
    );
    
    $(function() {
		$( ".button"  ).button();
        // $( ".button , input: submit, a"  ).button();
		
	});
    
    $('.neue_nachricht').click(function() {
        if (!$('#neue_nachricht_content').length)
        {
            $('body').append($('<div id="neue_nachricht_content"  title="Neue Nachricht" ></div>'));
    
        }
        common_ajax('forum/ajax_new_thread' , 'neue_nachricht_content' , new Array('content_file=forum/fast_post') );
        
		$( "#neue_nachricht_content" ).dialog({ width: 430 });
           
        
    });    
    

    /*
    $('#login_form').submit(function() {
              return login_request();
            });
    */
});

function load_komment_window(t_id)
{
    if (!$('#neue_nachricht_content').length)
    {
        $('body').append($('<div id="neue_nachricht_content"  title="Neuen Kommentar hinterlassen" ></div>'));

    }
    common_ajax('forum/ajax_new_komment' , 'neue_nachricht_content' , new Array( 't_id='+t_id )  );
    
	$( "#neue_nachricht_content" ).dialog({ width: 430 });
}
function load_login_window( ajax_login_controller )
{
    if (!$('#login_window_content').length)
    {
        $('body').append($('<div id="login_window_content"  title="Login" ></div>'));

    }
    common_ajax( 'login/load_login_window' , 'login_window_content' , new Array('ajax_login_controller='+ajax_login_controller) );
    
    $(function() {
		$( "#login_window_content" ).dialog();
         /*
         $('#ajax_login_f').submit(function() {
              return login_request();
            });
       
        $( "#login_window_content" ).position({
				of: $( "#main_container" ),
                my: 'top',
				at: 'center',
							
			});
       */

	});
    return false;
}



function do_search()
{
    //alert(site_url);
    $('#frontend_search_form').attr("action", site_url+'suche/nach/'+$('#search_request').val() );
    return true;
}
function show_status_message( message )
{
    if (!$('#status_message').length)
    {
        $('body').append($('<div id="status_message"  title="Message" ><p>'+message+'</p></div>'));
    }
    
    $( "#status_message" ).dialog();
  

    
    /*

    $('#status_message').html(message);
    $('#status_message').css('display' , 'block');
    topx = parseInt(((document.all)?document.body.scrollTop:window.pageYOffset));  
    $('#status_message').css('top', topx ); 
    $('div#status_message').slideDown("slow", function(){
      hTimer = window.setTimeout(function(){$('div#status_message').slideUp("slow");}, 3000);
    });
    */
 
}
function myErrorCallback()
{
    
}
function common_ajax( source , ausgabe_element_id , form_id_or_data , json )
{
    $('#'+ausgabe_element_id).append('<img src="'+site_url+'images/ajax_loader.gif" class="ajax_loader" title="loading" />');
    if ( typeof( form_id_or_data ) == "object" ) { var post_data = form_id_or_data.join('&'); } else { var post_data = $('#'+form_id_or_data).serialize(); }
      
    jQuery.ajax({
    	url: site_url + source, 
    	data: post_data,
    	type: "POST",
    	timeout: 4000,
    	error: function (xhr, ajaxOptions, thrownError){
                    alert(xhr.status);
                    alert(thrownError);
                }    
        //myErrorCallback 
           // function(){ console.log("25:Fehler");}
           ,
    	success: function(ausgabe , textS, xhr){
    	   
    	   $('.ajax_loader').remove();    
    	    if (json){ 
    	       responseJSON = jQuery.parseJSON(ausgabe);               
               if ( responseJSON.message ) { $( '#' + ausgabe_element_id ).html( responseJSON.message ); }
               if ( responseJSON.sys_message ) { show_status_message( responseJSON.sys_message ); }
               if ( responseJSON.redirect) { window.location.replace( responseJSON.redirect );   }
                
            }
            else
            {
                $( '#' + ausgabe_element_id ).html( ausgabe );
            }
   		}
    });
    
    if (json) { return responseJSON; }
    else
    { return false; }
}


/** AJAX UPLOAD functions ***/

function ajaxFileUpload()
{
	//starting setting some animation when the ajax starts and completes
	$("#upload_progress")
	.ajaxStart(function(){
		$(this).show();
	})
	.ajaxComplete(function(){
		$(this).hide();
	});
	
	/*
		prepareing ajax file upload
		url: the url of script file handling the uploaded files
                    fileElementId: the file type of input element id and it will be the index of  $_FILES Array()
		dataType: it support json, xml
		secureuri:use secure protocol
		success: call back function when the ajax complete
		error: callback function when the ajax failed
		
            */
	$.ajaxFileUpload
	(
		{
			url: site_url+'ajax_functions/doajaxfileupload', 
			secureuri:false,
			fileElementId:'fileToUpload',
			dataType: 'json',
			success: function (data, status)
			{
				if(typeof(data.error) != 'undefined')
				{
					if(data.error != '')
					{
						alert(data.error);
					}else
					{
						//alert(data.msg);
                        $('#news_f_image').val(data.uploaded_file_name);
                        $('#image_ajax_upload_f').css('display' , 'none');
                        $('#uploadedFileView').html('<img src="'+data.uploaded_file_link+'" />');
					}
				}
			},
			error: function (data, status, e)
			{
				alert(e);
			}
		}
	)
	
	return false;

}  
