var pageLoaded = false;
var pageTracker

//sinple version
function goPage(id){
    navigatePage(id, "", scroller.reInitialize.bindAsEventListener(scroller));
}

/* Used to replace all the items in a page */
function navigatePage(id, params, callback){
    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, callback);
    //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, callback){       
    //force the atitude to be JSON
    if(isString(params)){
        if(params != ""){
            params = params + "&";
        }
        params = params + "attitude=json";
    }else{
        params.attitude="json";
    }    
    //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, callback);
            }
        },
        onFailure : function(){
            ajaxReplaceContentFailed(containerName, "Page Failure");
        }
    }
    );
}

/* Replace other content on load */
function ajaxReplaceContentReload(containerName, pageURL, params, callbackURL, callbackParams){
    
}

function ajaxReplaceContentWithResponse(containerName, response, trackGoogle, callback){
    if(trackGoogle == 1){
        //google analytics tracking
        pageTracker._trackPageview('/page/' + response.id);
    }
    Element.replace($(containerName), response.result);
    if(callback != undefined){
        callback();
    }
}

function ajaxReplaceContentFailed(containerName, error){
    //alert("Fail:Unable to complete request" + " " + error);
}

/* 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;
});


function updateBreadcrumbs(){
    var b = $('breadcrumbs');
    var c = b.childElements();
    c.each(function(i){
	    i.remove();
    });
    var p = $('breadcrumbs_pass').remove();
    p.setStyle({'display' : 'block'});
    b.appendChild(p);
}