// Framework Functions
document.loaded = false;

// Setting Global actions to happen on every Ajax Request
global_ajax = {
  onLoaded: function(ajaxObj,xhr,json){
    // catching a redirect call if it happens during an ajax call
    if(json['status'] == '302'){
      window.location = json['url'];
    }
  }
}
Ajax.Responders.register(global_ajax);

if(getCookie('res') != 1){
  setCookie('res',[screen.width,screen.height,screen.colorDepth].join('.'),null,'/');
}

function findPosX(el){
	var curleft = 0;
	if (el.offsetParent){
		while (el.offsetParent){
			curleft += el.offsetLeft
			el = el.offsetParent;
		}
	}
	else if (el.x)
		curleft += el.x;
	return curleft;
}

function findPosY(el){
	var curtop = 0;
	if (el.offsetParent){
		while (el.offsetParent){
			curtop += el.offsetTop;
			el = el.offsetParent;
		}
	}
	else if (el.y)
		curtop += el.y;
	return curtop;
}

function confirm_delete(form_name,msg){
  if(msg == undefined){
    msg = 'Are you sure you want to delete this?';
  }
  if(confirm(msg)){$(form_name).submit();}
  return false;  
}

// generates a form and submits post
function post(linkObj){
  form_name = 'inserted_post_form';
  html = '<form id="'+form_name+'" method="post" action="'+linkObj.href+'"></form>';
  // Insert a form just after the link location then submit via post
  new Insertion.After(linkObj,html);
  $(form_name).submit();
  return false;
}

function del(linkObj,name){
  if(name == undefined){name='this';}
  msg = 'Are you sure you want to delete '+name+'?';
  if(confirm(msg)){post(linkObj);}
  return false;
}


function ajax_submit_w_update(formObj,updateElement,statusElement){
// onsubmit="return ajax_submit_w_update(this, updateElement,statusElement);"
  // hiding action from form spam bots
  action = (formObj.action.length > 0) ? formObj.action : formObj.elements['cmd'].value;
  new Ajax.Updater(updateElement,action,
    {
      asynchronous:true, evalScripts:true,
      onComplete:function(request){Element.hide(statusElement);},
      onLoading:function(request){Element.show(statusElement);},
      parameters:Form.serialize(formObj)
    }
  );
  return false;
}

function hlp_find(el){ // create the help div for this element
  eHlpDiv = $(el.id+'_hlp');
  if(eHlpDiv == undefined){ // doesn't exsits
    fHlpDiv = hlp_f_find(el);
    new Insertion.Bottom(fHlpDiv,hlp_div(el));
    eHlpDiv = $(el.id+'_hlp');
  }
  return eHlpDiv;
}

function hlp_f_find(el){ // find the help div for this form
    fHlpDiv = (el.form.elements['hlp_div'] == undefined) ? $(el.form.id+'_hlp') : $(el.form.elements['hlp_div'].value);
    if(fHlpDiv == undefined){
      //alert('Q Help Setup Error. ('+el.form.id+'_hlp)');
      return false;
    }else{
      return fHlpDiv;
    }
}

function hlp_div(el){
  return '<div id="'+el.id+'_hlp" style="display:none;"></div>';
}

function hlp_sf(el){ // show help
  setTimeout("hlp_sf_now($('"+el.id+"'));",100);
}

function hlp_sf_now(el){ // show help
  hlp = hlp_find(el);
  if (hlp.innerHTML.length < 1){
    hlp.innerHTML = '<div class="ajax_loading">Loading...</div>';
    new Ajax.Updater(hlp,'/help/quick?'+el.id,
      {asynchronous:true, evalScripts:true}
    );
  }
  Element.show(hlp);
}

function hlp_hf(el){ // hide help
  setTimeout("hlp_hf_now($('"+el.id+"'));",100);
}

function hlp_hf_now(el){
  Element.hide(el.id+'_hlp');
}


function hlp_st(el){
  setTimeout("hlp_st_now($('"+el.id+"'));",100);
}

function hlp_st_now(el){
  fHlp = hlp_f_find(el);
  x = findPosX(el);
  tt_y = findPosY(el.parentNode);
  el_dim = Element.getDimensions(el)
  tt_x = x + el_dim.width + 10;
  fHlp.style.top = tt_y+'px';
  fHlp.style.left = tt_x+'px';
  Element.show(fHlp);
  
  hlp = hlp_find(el);
  if (hlp.innerHTML.length < 1){
    hlp.innerHTML = '<div class="ajax_loading">Loading...</div>';
    new Ajax.Updater(hlp,'/help/quick?'+el.id,
      {asynchronous:true, evalScripts:true}
    );
  }
  Element.show(hlp);
}


function hlp_ht(el){
  setTimeout("hlp_ht_now($('"+el.id+"'));",100);
}
function hlp_ht_now(el){
  hlp = hlp_find(el);
  Element.hide(hlp);  
  fHlp = hlp_f_find(el);
  Element.hide(fHlp);
}

function select_form_editor(model_name,attr_name){
  id = model_name+'_'+attr_name;
  f = $F(id);
  if(f != undefined){
    f.focus();
    f.select();
  }
}


function protect_email(who, where, className, doLink){
	var addy = who + String.fromCharCode(64) + where;
	if (doLink){
		document.write("<a href=\"mai" + "lto:" + addy + "\"");
		if (className.length){document.write(" class=\"" + className + "\"");}
		document.write(">");
	}
	document.write(addy);
	if (doLink){document.write("</a>");}
}

