/*
 * This Custom ajax.updater use in ProjectVision
 * It want prototype framework
 * Created 22/09/2009 By pree
 */
var url = location.pathname.split('/')[1];
var scr_img = './app/webroot/';
var timeout_message = 0; // set id for flash_message
var check_ie = /MSIE (\d+\.\d+);/.test(navigator.userAgent); //check it ie = true
//CustomAjax.
//var loadshow = 'Loading_Show("LoadingDiv")';
//var loadclose = 'Remove_Show()';
var CustomAjax = Class.create();

 CustomAjax.prototype = {
    initialize:function (){
        //set value
       // this.loadshow = 'Loading_Show("LoadingDiv")';
       // this.loadclose = 'Remove_Show()';
    },
  /*
  * Custom update loading ajax
  * required Div id = LoadingDiv in layout page.
  * @autoloading = 1/0
  * @divid = string of id (No null)
  * @update = string of id went to update (No null)
  * @link = string of url link "controller/action"
  * @params = object of parameter all want to send method post
  * @option = array Option seperate by ^ link "onloading:alert('test')^onloaded:alert('done')" or function(){alert('done');}
  * @nomessage = 0/null set for want flash message and 1 for no flash message.
  */
    loading:function (autoloading , divid, update, link, params, options, nomessage){
    var updateoption = new Object();
    updateoption.asynchronous = true;
    updateoption.evalScripts = true;
    updateoption.parameters = params;
    // add update to flash message
    if(!nomessage){
        update = 'update_message '+update;
    }
    updateoption.requestHeaders = ["X-Update", update];
    var has_loading = 0; // to check it has onloading
    var has_complete = 0;
        if(options){
            for(var i in options){
                    switch (i.toLowerCase()) {
                    case 'oncomplete' :
                        if(autoloading){
                            updateoption[i] = function(){
                                                    options[i]();
                                                    Remove_Show();
                                                };
                        }else{
                             updateoption[i] = function(){options[i]();};
                        }
                        has_complete = 1;
                        break;
                    case 'onloading' :
                        if(autoloading){
                            updateoption[i] = function(){
                                                    options[i]();
                                                    Loading_Show("LoadingDiv");
                                                };
                        }else{
                             updateoption[i] = function(){options[i]();};
                        }
                        has_loading = 1;
                        break;
                    default:
                        updateoption[i] = function(){options[i]();};
                        break;
                }
            }
            
                /*
                 *check all if dont have complete or loading
                 */
                if(autoloading){
                    if(!has_loading){
                        updateoption.onLoading = function(){Loading_Show("LoadingDiv");};
                    }
                    if(!has_complete){
                        updateoption.onComplete = function(){Remove_Show();};
                    }
                }
            }

        else{
            if(autoloading){
                 updateoption.onLoading = function(){Loading_Show("LoadingDiv");};
                 updateoption.onComplete = function(){Remove_Show();};
            }
        }
        new Ajax.Updater(divid,'/'+url+'/'+link, updateoption);
            
        
    },
     /*
      * Custom link ajax
      * required Div id = LoadingDiv in layout page.
      * @divid = string of id (No null)
      * @update = string of id went to update (No null)
      * @link = string of url link "controller/action"
      */
    link:function (divid, update, link){
            if(divid == null){
                new Ajax.Updater(document.createElement('div'),'/'+url+'/'+link, {asynchronous:true, evalScripts:true, onAfter: Loading_Show("LoadingDiv"), onComplete: function(){Remove_Show();}, requestHeaders:['X-Update', update]});
            }
            else{
                new Ajax.Updater(divid,'/'+url+'/'+link, {asynchronous:true, evalScripts:true, onAfter: Loading_Show("LoadingDiv"), onComplete: function(){Remove_Show();}, requestHeaders:['X-Update', update]});
            }
        }
 }

/*
 *this function to check ie compatible
*/
function iecompattest(){
    return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

/*
 *this function all in below use to show div loading and close div loading.
 */
function Remove_Show(){
    Element.hide("LoadingDiv");
    Element.remove(document.getElementById("lightwindow_overlay"));
}
function Loading_Show(name){
    var obj = document.getElementById(name);
    obj.style.visibility='visible';
    obj.style.display='';
    var overlay = Element.extend(document.createElement('div'));
    overlay.setAttribute('id', 'lightwindow_overlay');
    // FF Mac has a problem with putting Flash above a layer without a 100% opacity background, so we need to use a pre-made
    if (Prototype.Browser.Gecko) {
            overlay.setStyle({
                    //backgroundImage: 'url(img/black-70.png)',
                    backgroundRepeat: 'repeat',
                    height: document.documentElement.clientHeight+'px'
            });
    } else {
            overlay.setStyle({
                    opacity: 0.7,
                    //backgroundImage: 'url(img/black.png)',
                    backgroundRepeat: 'repeat',
                    height: document.documentElement.clientHeight+'px'
            });
    }
    obj.parentNode.insertBefore(overlay, obj);
    scroll_auto(obj);
    //setTimeout(Loading_Show, 10);// auto refresh 10ms
}
function scroll_auto(obj){
        if(check_ie){
            obj.style.left = ((iecompattest().clientWidth/2) + iecompattest().scrollLeft) + 'px';
            obj.style.top = ((iecompattest().clientHeight/2) + iecompattest().scrollTop) + 'px';
        }
        else{
            obj.style.left = (window.pageXOffset + (window.innerWidth/2)) + 'px';
            obj.style.top = (window.pageYOffset + (window.innerHeight/2)) + 'px';
        }
        setTimeout( function(){scroll_auto(obj)}, 10);
}
function getElementsByName_iefix(tag, name) {

     var elem = document.getElementsByTagName(tag);
     var arr = new Array();
     for(i = 0,iarr = 0; i < elem.length; i++) {
          att = elem[i].getAttribute("name");
          if(att == name) {
               arr[iarr] = elem[i];
               iarr++;
          }
     }
     return arr;
}
function getElementsByClassName_iefix(tag, classname, document_name) {
     if(document_name){
     var elem = document_name.getElementsByTagName(tag);
     }else{
     var elem = document.getElementsByTagName(tag);
     }
     var arr = new Array();
     for(var i = 0,iarr = 0; i < elem.length; i++) {
          var att = elem[i].className;
          if(att.indexOf(classname) != -1) {
               arr[iarr] = elem[i];
               iarr++;
          }
     }
     return arr;
}
function onKeyPressNumberOnly(e)
{
	var key = window.event ? e.keyCode : e.which;
	var keychar = String.fromCharCode(key);
	return (!/[^0-9.]/.test(keychar) || key == 0 || key == 8);
}
/*
 *end function
*/
