// JavaScript Document    
var ids = new Array();           
jQuery(document).ready(function(){       
        init_ajax_viewer();      
        init_movers();       
    jQuery("a#helper_close").click(function(){
        close_viewer(null,null);
        return false;
    });        
    jQuery("a.helper_close").livequery("click",function(event){
        close_viewer(null,null);
        return false;
    });                        
});  

function init_ajax_viewer(element)
{                                               
    wh=jQuery(window).height();
    h=jQuery("html").height();
    w=jQuery("body").width();
    if(h<wh)
    {
      ah=wh;     
    }
    else ah=h;
    if(jQuery("a.open_viewer").size())
    {
        jQuery("a.open_viewer").live("click",function(event){
            h_w=jQuery("#ajax_helper").width();
            h_h=jQuery("#ajax_helper").height();
            l_margin=(w-640)/2;
            link_id=this.id;
            jQuery("#ajax_helper_container").css({"height":ah+"px","width":"100%"}).fadeIn(1000,function(){
                jQuery("#ajax_helper").css({"left":l_margin+"px","max-height":(wh-220)+"px"}).fadeIn(1000,function(){   
                    jQuery.getJSON("/index.php", {type:"ajaxViewer", region:"ajax", id:"0", s:link_id}, function(data){
                        if(data)
                        {
                            jQuery("#ajax_helper_inner").fadeOut(200, function(){
                                jQuery("#ajax_viewer_title").html(data.title).fadeIn(200);
                                jQuery("#ajax_helper_inner").css({"max-height":(wh-320)+"px"}).removeClass("loading").html(data.content).fadeIn(200, function(){
                                    ahi_h = jQuery("#ajax_helper_inner").height();
                                    jQuery('#ajax_helper_message_container').css({"height":(ahi_h+40)+"px"});  
                                    jQuery('#helper_form').livequery(function(){ 
                                        $(this).ajaxForm({           
                                            dataType:  'json', 
                                            beforeSubmit: beforeSubmit,
                                            success:   processJson        
                                        });      
                                    }); 
                                });
                            });
                        }
                    });
                });
            });
            return false;   
        });
    }
}

function init_movers(element)
{   
    isEnabled=true;                                              
    wh=jQuery(window).height();
    h=jQuery("html").height();
    w=jQuery("body").width();
    if(jQuery("a.move").size())
    {
        jQuery("a.move").livequery("click",function(event){
            if (isEnabled == true) { 
                isEnabled=false    
                var ele=jQuery(this);
                jQuery(ele).switchClass("move","move_disabled");        
                h_w=jQuery("#ajax_helper").width();
                h_h=jQuery("#ajax_helper").height();
                l_margin=(w-640)/2;
                link_id=this.id;    
                bits=link_id.split(":");                     
                jQuery.getJSON("/index.php", {type:bits[0], region:"ajax", id:bits[1]}, function(data){
                    if(data)
                    {              
                        if(data.status=='success')
                        {                              
                            if(typeof window[data.callback] == 'function') { 
                                window[data.callback](data.params);   
                            }                       
                        }
                        else
                        {
                            alert(data.message);   
                        }
                    }    
                    setTimeout(function()
                    {
                        isEnabled=true;
                    }, 1000);
                });
            }
            return false;   
        }); 
    }
}
             
function processJson(data)
{                                            
    if(data.status=='success')
    {                                                                                                       
        jQuery('#ajax_helper_message_text').html(data.message).fadeIn(500, function(){    
            jQuery("#ajax_helper_message_text").switchClass("fixed_height","",300);
            jQuery("#ajax_helper_message").removeClass("loading");
            setTimeout(function(){close_viewer(data.callback,data.params)},2000);  
        });           
    }
    else
    {                                              
        jQuery('#ajax_helper_message_text').html(data.message).fadeIn(500, function(){    
            jQuery("#ajax_helper_message_text").switchClass("fixed_height","",300);
            jQuery("#ajax_helper_message").switchClass("loading","",300);
            setTimeout("close_message_pane()",2000); 
            if(typeof window[data.callback] == 'function') { 
                window[data.callback](data.params);   
            }                                                  
        });
    }
}

function beforeSubmit()
{                             
    jQuery("#ajax_helper_message_container").fadeIn(200, function(){
        jQuery("#ajax_helper_message").addClass("loading").fadeIn(200);
    });
}

function close_message_pane()
{
    jQuery("#ajax_helper_message").fadeOut(400,function(){
        jQuery("#ajax_helper_message_text").html(' ');
        jQuery("#ajax_helper_message_container").fadeOut();
        jQuery("#ajax_helper_message_text").addClass("fixed_height").fadeOut();
        jQuery("#ajax_helper_message").addClass("loading");
    });
}

function close_viewer(callback, param)
{                                                
    close_message_pane();     
    jQuery("#ajax_helper_container").fadeOut(1000,function(){
        jQuery("#ajax_helper_inner").html(' ').addClass("loading");
        jQuery("#ajax_viewer_title").html(' ');    
        if(typeof window[callback] == 'function') {    
            window[callback](param);   
        }                  
    });
}        

function update_row_to_table(str)
{                            
    params=str.split(":");              
    jQuery.get("/index.php", {type:"get_single_pm_row", region:"ajax", id:params[1]}, function(data){ 
        jQuery.scrollTo("#page_"+params[0],200,{
            onAfter:function(){ 
                jQuery("#page_"+params[0]).after(data); 
                row_alternate();     
            }
        });   
    });
}

function updated_row_refresh(id)
{                                      
    jQuery.get("/index.php", {type:"get_single_pm_row", region:"ajax", id:id}, function(data){ 
        jQuery.scrollTo("#page_"+id,200,{
            onAfter:function(){ 
                jQuery("#page_"+id).fadeOut(500).before(data).remove(); 
                row_alternate();          
            }
        });   
    });
}           

function update_deleted_row(str)
{
    params=str.split(":"); 
    for ( var i in params )
    {
        if(i==0)
        {               
            jQuery("#page_"+params[i]).remove();  
        }
        else 
        {   
            id=params[i];
            update_single_pm_row(id);      
        }
    }     
    row_alternate();            
}       

function update_rows(str)
{
    params=str.split(":");
    for ( var i in params )
    {         
        id=params[i];
        update_single_pm_row(id);
    }     
    row_alternate();            
}

function update_rows_by_listorder(str)
{    
    //alert(str);                 
    params=str.split("|");
    n=params.length;
    for ( var i in params )
    {                         
        param_bits=params[i].split(":");      
        id=param_bits[0];              
        list_order=param_bits[1];    
        update_single_pm_row_by_listorder(id,list_order,n,i);
    }       
    row_alternate();                                                             
}       

function update_single_pm_row(id)
{                              
        jQuery.get("/index.php", {type:"get_single_pm_row", region:"ajax", id:id}, function(data){ 
            jQuery("#page_"+id).fadeOut(500).before(data).remove(); 
            row_alternate();    
        });      
}

function update_single_pm_row_by_listorder(id,list_order,n,i)
{   
    n=n-1;      
    ids.push(id);                        
    jQuery.get("/index.php", {type:"get_single_pm_row_by_listorder", region:"ajax", id:id, s:list_order}, function(data){ 
        jQuery("#page_"+id).fadeOut(500).before(data).remove(); 
        row_alternate();
        reset_id_runner(n,i,ids)      
    });                               
}

function reset_id_runner(n,i,ids)
{                                  
    if(n==i)
    {                                                                         
        setTimeout(function(){reset_moving_id()},500); 
    }
}

function reset_moving_id()
{                               
    for ( var i in ids )
    {   
        id=ids[i];                    
        jQuery("#moving_page_"+id).attr("id","page_"+id).removeClass("moving"); 
    }                                                                           
    ids.length=0;
}

function update_table(str,div_id,id,type)
{                                                              
    params=str.split("|");                    
    div_id=params[0];              
    id=params[1];                  
    type=params[2];                             
    jQuery.get("/index.php", {type:type, region:"ajax"}, function(data){ 
        jQuery(div_id).slideUp(500, function(){ 
            jQuery(div_id).html(data).slideDown(500);
        }); 
        row_alternate();                                             
    });           
}
