var pageLoaded = false;
var pageTracker
/* Used to replace all the items in a page */
function navigatePage(id, params){
    if(params == undefined){
        params = "";
    }
    if(isString(params)){
        if(params != ""){
            params = params + "&";
        }
        params = params + "id=" + id;
    }else{
        params.id=id;
    } 
    
        
    //replace the content
    ajaxReplaceContent("contentArea", "/page.php", params);
    //replace the header
    //params = "mode=header&id="+id;
    //ajaxReplaceContent("preNavFlash", "/page.php", params);
}

function insertFlash(t){
    if(pageLoaded == true){
        flash = $('flashContainer');
        flash.replace('<div id="flashContainer">' + t + "</div>");
    }else{
        setTimeout(function(){insertFlash(t)}, 1000);
    }
}
/* Call an AJAX call and replace the target element with the response data */
function ajaxReplaceContent(containerName, pageURL, params){      
    //force the atitude to be JSON
    if(isString(params)){
        if(params != ""){
            params = params + "&";
        }
        params = params + "attitude=json";
    }else{
        params.attitude="json";
    }   
    if(containerName != undefined && containerName != ""){
        waitingOverlay(containerName);
    }
    //track google
    trackGoogle = 1;
    if(pageTracker == null){
         trackGoogle = 0;
    }
    //make the request
    new Ajax.Request(pageURL, {
        method:'post',
        parameters : params,
        onSuccess: function(transport){
            var response = transport.responseJSON || "";
            if(response == ""){      
                ajaxReplaceContentFailed(containerName, "No Data");
            }else if(response.error == 1){
                ajaxReplaceContentFailed(containerName, "Error");
            }else{
                //Success
                ajaxReplaceContentWithResponse(containerName, response, trackGoogle);
            }
        },
        onFailure : function(){
            ajaxReplaceContentFailed(containerName, "Page Failure");
        }
    }
    );
}


/* Waiting overlay */
function waitingOverlay(containerName){
      var  c = $(containerName);
      if(c != undefined){
         if(containerName != ""){
            c.absolutize();
            c.relativize();
            d = c.getDimensions();           
            Effect.Fade(c, { from: 1.0, to: 0.5, duration: .1 });
            c.insert(Builder.node('img',{'id':'ajaxGraphic', 'src':'/images/core/ajax-loader.gif', 'style':'z-index:901;position:absolute;left:'+(Math.round(d.width/2) - 30)+'px;top:'+(Math.round(d.height/2) - 20)+'px;'}))
        }
    }
}

/* Hide waiting overlay */
function removeWaitingOverlay(containerName){
    ajaxImage = $('ajaxGraphic');
    if(ajaxImage != undefined){
        ajaxImage.remove();
    }
    c = $(containerName);
    if(c != undefined){
         Effect.Fade(c, { from: .5, to: 1, duration: .1 });
    }
}

/* Replace other content on load */
function ajaxReplaceContentReload(containerName, pageURL, params, callbackURL, callbackParams){

}

function ajaxReplaceContentWithResponse(containerName, response, trackGoogle){
    if(trackGoogle == 1){
        //google analytics tracking
        pageTracker._trackPageview('/' + response.mode + '/' + response.name);
    }
    Element.replace($(containerName), response.result);
}

function ajaxReplaceContentFailed(containerName, error){
    //alert("Fail:Unable to complete request" + " " + error);
     removeWaitingOverlay(containerName);
}

/* Post a form via AJAX */
function ajaxPostForm(formName, containerName){
    //get the form
    f = $(formName);
    if(f == undefined){
        alert('invalid form');
        return;
    }
    //get the values
    params = f.serialize(true);
    //get the port action
    action = f.readAttribute('action');
    //call it
    ajaxReplaceContent(containerName, action, params)
}

/* Utilities */
function isArray(obj) {
   if (obj.constructor.toString().indexOf("Array") == -1)
      return false;
   else
      return true;
}
function isObject(obj) {
   if (obj.constructor.toString().indexOf("Object") == -1 || obj == null)
      return false;
   else
      return true;
}
function isString(obj) {
   if (obj.constructor.toString().indexOf("String") == -1 || obj == null)
      return false;
   else
      return true;
}

function openFAQ(id){
    var itm = $('faq_answer_' + id);
    if(itm.visible()){
        Effect.BlindUp('faq_answer_' + id);
    }else{     
        closeFAQs();
        if(itm != undefined){
            Effect.BlindDown('faq_answer_' + id);
        }
    }
 }

function closeFAQs(){
    var items = $$('.faq_answer');
    if(items.length > 0){
        items.each(function(itm){
            if(itm.visible()){
                Effect.BlindUp(itm.id);
            }
        });
    }
}

Event.observe(window, "load", function() {
  pageLoaded = true;
});

