/*<?PHP
Header("content-type: application/x-javascript");
?>*/
        

	jQuery(function($) {
		init();      
	});
   

function dragdrop(script,action,drag,drop,div){
    if(confirm('Proceed with copy to this location?')){
        new Ajax.Request(script+'?event='+action+'&drag='+drag.id+'&drop='+drop.id,
                {onComplete:function(){dom_drop(drop);}});
    }
}        
   
	var prev_hash;
	var var_this;
	var nav = false;
	
	function pageload(hash) {
		// hash doesn't contain the first # character.
		// check if prev_hash already set
		// if so, check if its a back or forward, and update prev_hash
		// if not, expand desired rows
		
		if (hash || prev_hash) {
			ids = splits(hash);
			prev_ids = splits(prev_hash);
			one = ids[ids.length-1];
						
			if (prev_ids) {
				if (ids.length==0)
					ajaxint(jQuery("#"+prev_ids[0]));
				else {
					difference = diff(ids,prev_ids);
					difference2 = diff(prev_ids,ids);
				
					if (difference.length==0 && difference2.length==0)
						ajaxint(jQuery("#"+ids[0]));
					else if (difference.length>0)
						ajaxint(jQuery("#"+difference));
					else
						ajaxint(jQuery("#"+difference2));
				}
				
			} else {
				if (var_this!=null) ajaxint(var_this);
			}
			
			prev_hash = hash;
		} else {
			// start page
			prev_hash = '';
		}
	}
	
	function splits(v, s) {
		var s = (s==null) ? "," : s;
		if (v && v.length>0) {
			return (v.indexOf(",")<0) ? [v] : v.split(s);
		} return [];
	}
	
	function diff(v, c, m)
	{
	    var d = [], e = -1, h, i, j, k;
	    for(i = c.length, k = v.length; i--;){
	        for(j = k; j && (h = c[i] !== v[--j]););
	        h && (d[++e] = m ? i : c[i]);
	    }
	    return d;
	}
	
	function scrollto(row) {
			if (row=='#') offset = 1;
			else offset = jQuery(row).offset().top;
			jQuery("html,body").animate({scrollTop: offset},1000);
	}
	
	function expandit() {
		jQuery(".expandme").click().removeClass("expandme");
	}
	                                       
    function hideit(thiss){   
        var tr = jQuery(thiss).parent().parent();
        var trn = tr.next();
        if (trn.is(".table")) {
            trn.remove();
        }
        tr.remove();                                                    
    }

    function cancelit(thiss){   
        jQuery("span#"+thiss).html("cancelled");
                                                          
    }
   
    //  level D__t is the name of the div to collapse and refresh                 
    function dom_insert_edit(container){
      var params = jQuery('div#P_'+container).text(); 
      ajaxload('div#D_'+container,'?'+params,0);
    }   //  level D__t is the name of the div to collapse and refresh                 
    function dom_insert_add(container){
      var params = jQuery('div#P_'+container).text(); 
      ajaxload('div#D_'+container,'?'+params,1);
    }   
    //  level D__t is the name of the div to collapse and refresh                 
    function dom_insert_dupl(container){
      var params = jQuery('div#P_'+container).text(); 
      ajaxload('div#D_'+container,'?'+params,1);
    }                  
    
    // drop into a row R__t - row could be
    //     unfilled (do nothing!), 
    //     filled but collapsed (unfill and reload).
    //      filled and open (reload)            
    function dom_drop(row){
         rown = row.id;
         p = rown.split('_drag');
         rown = p[0];
         row = rown.replace("drop","");
         
         var link = jQuery("a#A_"+row);   
         var params = link.attr("rel");
         if(jQuery("div#D_"+row).length>0){
            ajaxload("div#D_"+row,params,0);
         }else{
            row = link.parent().parent();
            jQuery(link).toggleClass("expand").toggleClass("collapse");
            jQuery("<tr class='table'><td></td><td colspan='20'><img src='pix/ajaxloader.gif' /></td></tr>").insertAfter(row);
        
            jQuery(".ajax").unbind("click");
            jQuery.get(params,
                function(responseHtml){
                    row.next().children().eq(1).empty().append(responseHtml);
                    scrollto(row);
                    init();
                }
            ); 
         }
    } 
    
    function ajaxload(r,params,rld){
        jQuery(r).replaceWith("<div id='a__loader'><img src='pix/ajaxloader.gif' /></div>");
        jQuery.get(params,
             function(responseHtml){
                    var row = jQuery("div#a__loader");
                    //alert(responseHtml);
                    row.replaceWith(responseHtml);
                    if(rld==1){
                        scrollto(row); 
                    }
                    init();
                }
             )    
    } 
                                         
    function insertit(thiss){        
        var row = jQuery('div#R_'+thiss);   
        if(!jQuery('div#P_'+thiss).empty()){
            var variables = '?'+jQuery('div#P_'+thiss).text(); 
            row.replaceWith("<div id='a__loader'><img src='pix/ajaxloader.gif' /></div>");
            jQuery.get(variables,
             function(responseHtml){
                    var row = jQuery("div#a__loader");
                    //alert(responseHtml);
                    row.replaceWith(responseHtml);
                    scrollto(row); 
                    init();
                }
             )
        }
    }
      
      
      
	function ajaxint(thiss) {        
        var row = jQuery(thiss).parent().parent();
        var variables = jQuery(thiss).attr("rel");
        var expand = (jQuery(thiss).attr("class").indexOf("expand")>0);
        jQuery(thiss).toggleClass("expand").toggleClass("collapse");
        //check if already clicked before
        if (row.next().is(".table")) {
            if (!expand) {
                prev = row.parent().parent().parent().parent().parent().prev()
                if (prev.is("tr")) {
                    scro = prev.children().eq(0).children().eq(0).attr("rev");
                    scrollto(scro);
                }
            } else {
                scrollto(row);
            }
            
            div = row.next().children().eq(1).children().eq(0);
            row.next().show();
            (div.css("display")=="none") ? div.slideDown("normal") : div.slideUp("normal",function(){row.next().hide()});
        } else {
            
        
            jQuery("<tr class='table'><td></td><td colspan='20'><img src='pix/ajaxloader.gif' /></td></tr>").insertAfter(row);
        
            jQuery(".ajax").unbind("click");
            jQuery.get(variables,
                function(responseHtml){
                    row.next().children().eq(1).empty().append(responseHtml);
                    scrollto(row);
                    init();
                }
            );
        }
	}
	
	var hash;

    function forminit(){
         //tooltip();
    }
    
	function init() {
    
        rehelptip();
        //jQuery('#dialog').jqm({modal:true, ajax:'@href', ajaxText: '<img src=\"pix/ajaxloader.gif"\" alt=\"Loading.." />'});
        //jQuery('#dialog').jqmAddTrigger('.jqModal'); 
        //jQuery('#dialog').jqmAddClose('#jqHide');
        		
		//jQuery('#dialog').jqm({onLoad:forminit}); 
        jQuery(".sort,.ajax").unbind("click");
		jQuery(".ajax").click(function() {
			ajaxint(this);
			return false;
		});
		
		expandit();
		                          
		// .filter("a[@href^=#]")
		jQuery(".sort").click(function() {
			
			var $table = jQuery(this).parent().parent().parent().parent();
			if (jQuery.browser.msie) $table = $table.parent();
			var $id = jQuery(this).attr('id');
			var firstsort = ((jQuery(this).attr("class").indexOf("sortup")<0) && (jQuery(this).attr("class").indexOf("sortdown")<0)); 
			var sortup = (jQuery(this).attr("class").indexOf("sortup")>0);
			
			variables = jQuery(this).attr("rel");
			
			if (sortup) {
				 variables = variables.concat("%20DESC");
			}
			
			
			var previous = jQuery(".sortup, .sortdown", $table.children("thead").children());
			if ((jQuery(this).attr('id') != previous.attr('id')) && previous.is('a')) {
				sort = (previous.attr('class')!="sortup") ? "%20ASC" : "%20DESC";
				prev_id = previous.attr('id').slice(previous.attr('id').indexOf('__')+2);
				variables = variables.concat(","+prev_id+sort);
			}
			
			 jQuery(".sort").unbind("click");
			 jQuery.get(variables,
					function(responseHtml){	
					   $table.before(responseHtml).remove();
						(sortup || !firstsort) ? jQuery("#"+$id).removeClass("sortup").addClass("sortdown"): jQuery("#"+$id).removeClass("sortdown").addClass("sortup");
						init();
					}
				);
				return false;
		});
	}
	