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();
	});
    

    //bind_ajax_popup_dialog( '.neue_nachricht' , 'news/news_form' , "Neue Nachricht" );
    
    //bind_ajax_popup_dialog( '.neue_nachricht_link' , 'news/news_form_link' ,"Neue Nachricht" );

});


var formfeld="";
var maxlang=1;				
function zaehle()
 	{ 
if (window.document.anwort_form.antwort_text.value.length>0)	
	{
formfeld=window.document.anwort_form.antwort_text.value;
window.document.anwort_form.anzeigen.value=maxlang+window.document.anwort_form.antwort_text.value.length;
return;
	}

}


function bind_ajax_popup_dialog( bind_to_class , content_file , title  , hidden_vars )
{    
    data_array = new Array( 'content_file='+content_file )
    if ( title == undefined ) {title = "Message";}
    
    
    if ( hidden_vars != undefined)
    {
        $.each( hidden_vars , function(index, value) { 
        //alert(index + ': ' + value);
        data_array.push( value ); 
        });
    }
    
    $( bind_to_class ).click(function() {
        if (!$('#common_popup_window').length)
        {
            $('body').append($('<div id="common_popup_window"  title="'+title+'" ></div>'));
    
        }
        
        common_ajax('ajax_functions/load_view' , 'common_popup_window' , data_array );
        
		$( "#common_popup_window" ).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 load_komment_window(t_id)
{
    if (!$('#neue_nachricht_content').length)
    {
        $('body').append($('<div id="neue_nachricht_content"  title="Neuen Kommentar hinterlassen" ></div>'));

    }
    common_ajax('zahnarztforum/ajax_new_komment' , 'neue_nachricht_content' , new Array( 't_id='+t_id )  );
    
	$( "#neue_nachricht_content" ).dialog({ width: 430 });
}


function load_news_komment_window(t_id)
{
    if (!$('#neue_nachricht_content').length)
    {
        $('body').append($('<div id="neue_nachricht_content"  title="Neuen Kommentar hinterlassen" ></div>'));

    }
    common_ajax('zahnarztforum/ajax_new_komment_news' , 'neue_nachricht_content' , new Array( 'id='+t_id )  );
    
	$( "#neue_nachricht_content" ).dialog({ width: 430 });
}

function login_request()
{
    alert('login');
    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 , title )
{
    if (title == undefined) title = 'Message';
    if (!$('#status_message').length)
    {
        $('body').append($('<div id="status_message"  title="'+title+'" ></div>'));
    }
    $('#status_message').html(message);
    $('#status_message').dialog();
    /*
    alert('hier');
    $('#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 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(){ console.log("25:Fehler"); },
    	success: function(ausgabe){
    	   $('.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; }
}
function set_verknuepfung()
{
    elements_to_link = new Array();
    jQuery.each( $('.sys_link_c:checked') , function(index, obj) { elements_to_link.push( obj.value );  } );
    if ( elements_to_link.length == 2)
    {
        common_ajax( 'front_ajax/set_verknuepfung' , 'unit_'+elements_to_link[1] , new Array( 'id1='+elements_to_link[0] , 'id2='+elements_to_link[1] ) , true );
        $('.sys_link_c:checked').attr('checked', false);        
    }

}
function verknupfungsmenu_auf( unit_id , cat_alias )
{
    responseJSON = common_ajax( 'front_ajax/verknupfungsmenu_auf' , 'unit_'+unit_id , new Array( 'unit_id='+unit_id , 'cat_alias='+cat_alias ) , true );
    $( '#unit_' + unit_id ).append( responseJSON.unit_id );
    
    return false;
}
/** ajax Profil upload */
function ajaxProfilFileUpload()
{
    // hide submit button    
    /* 	
        $("#news_f_submit")
    	.ajaxStart(function(){
    		$(this).hide();
    	})
    	.ajaxComplete(function(){
    		$(this).show();
    	});
    */
          
    //starting setting some animation when the ajax starts and completes
 
  
	$("#upload_progress")
        .ajaxStart(function(){	
	           $("#upload_progress").show();
            })
        .ajaxComplete(function(){
               $(this).hide(); 
               location.reload();
        });

	$.ajaxFileUpload
	(
		{
			url: site_url+'ajax_functions/doprofilajaxfileupload', 
			secureuri:false,
			fileElementId:'fileToUpload',
			dataType: 'json',
			success: function (data, status)
			{
				if(typeof(data.error) != 'undefined')
				{
					if(data.error != '')
					{
						alert( data.error );
					}else
					{
                        $('#image_ajax_upload_f').css('display' , 'none');
                        $('#profil_f_image').html('<img src="'+data.uploaded_file_link+'" />');
                        common_ajax('ajax_functions/save_user_profil_bild' , 'profilbild_upload_erfolg' , 
                            new Array('user_profilbild_image_name='+data.uploaded_file_name) , true);
					}
				}

                
			},
			error: function (data, status, e)
			{
				alert( e );
			}
		}
	);
	
	return false;

} 

/** AJAX UPLOAD functions ***/
function ajaxFileUpload()
{
    // hide submit button
    
	
    $("#news_f_submit")
	.ajaxStart(function(){
		$(this).hide();
	})
	.ajaxComplete(function(){
		$(this).show();
	});
    
    //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('blah'+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;

}  
