var currentMenuItem = null;
var currentPortfolioSort = 1;
var ajaxObj = [];

function createRequestObject() { 
    var ajaxObj;
    var browser = navigator.appName;
    if(browser.indexOf('Microsoft') >= 0)
        ajaxObj = new ActiveXObject("Microsoft.XMLHTTP");
    else
        ajaxObj = new XMLHttpRequest();
   
    return ajaxObj;
}

function applyBehaviour() {
   Behaviour.apply();
}

function getSalt() {
   return Math.floor(Math.random()*1000);
}

function evalJSON(responseText) {
   var json = eval('(' + responseText + ')');

   return json;
}

function test(text) {
   text = document.getElementById("test").value + "\n" +  text;
   document.getElementById("test").value = text;
}

function updateOpacity(objectName, opacity) {
   var objectElement = document.getElementById(objectName);
   objectElement.style.opacity = opacity;
   if (navigator.appName.indexOf("Microsoft")!=-1&&parseInt(navigator.appVersion)>=4)
      objectElement.filters.alpha.opacity = (opacity * 100);

   objectElement.style.MozOpacity = opacity;
}

// Fading out- assuming opacity is at 100 already
function fadeOut(objectName, currentOpacity) {
   currentOpacity = parseFloat(currentOpacity) - 0.05;

   currentOpacity = currentOpacity.toFixed(2);

   // Set the new opacity
   if (currentOpacity <= 0.1)
      currentOpacity = 0.1;

   updateOpacity(objectName, currentOpacity);

   if (currentOpacity > 0.1) {
      //setTimeout("fadeOut('" + objectName + "', " + currentOpacity + ")", 20);
      setTimeout(function() {
         fadeOut(objectName, currentOpacity);
      }, 10);
   } else
      updateOpacity(objectName, 0);
}

// Fading in- assuming opacity is at 0 already
function fadeIn(objectName, currentOpacity) {
   currentOpacity = parseFloat(currentOpacity) + 0.15;
   //currentOpacity += 0.1;

   currentOpacity = currentOpacity.toFixed(2);

   // Set the new opacity
   if (currentOpacity > 0.95)
      currentOpacity = 0.95;


   updateOpacity(objectName, currentOpacity);

   if (currentOpacity < 0.95) {
      //setTimeout("fadeIn('" + objectName + "', " + currentOpacity + ")", 20);
      setTimeout(function() {
         fadeIn(objectName, currentOpacity);
      }, 20);
   } else
      updateOpacity(objectName, 0.99);
}

function hide(objectName) {
   document.getElementById(objectName).style.display = 'none';
}

function show(objectName) {
   document.getElementById(objectName).style.display = '';
}

function updateInnerHtml(objectName, value) {
   document.getElementById(objectName).innerHTML = value;
}



function activateMenu(name) {
   if (currentMenuItem)
      deactivateMenu(currentMenuItem);

   currentMenuItem = name;

   document.getElementById(name + "on").style.display = ""; 
   document.getElementById(name).style.display = "none"; 
}

function deactivateMenu(name) {
   document.getElementById(name).style.display = ""; 
   document.getElementById(name + "on").style.display = "none"; 
}

function activateX(name) {
   document.getElementById("x" + name).style.display = "none";
   document.getElementById("x" + name + "On").style.display = "";
}

function deactivateX(name) {
   document.getElementById("x" + name + "On").style.display = "none";
   document.getElementById("x" + name).style.display = "";
}

//-----------------
function loadPortfolioCategory(categoryid) {
   loadPortfolioWork(categoryid, 1);
}

function loadPortfolioWork(categoryid, sortorder) {
   // Now initialize the ajax
   var salt = getSalt();
   ajaxObj['loadportfoliocategory' + "_" + salt] = createRequestObject();
   var subAjaxObj = ajaxObj['loadportfoliocategory' + "_" + salt];

   var url = '/loadportfoliowork/' + categoryid + '/' + sortorder + '/' + salt + '/';

   subAjaxObj.onreadystatechange = function() {
      if (subAjaxObj.readyState == 4) {
         var json = evalJSON(subAjaxObj.responseText);
         var result = parseInt(json.result);

         if (result == 1) {
            updateInnerHtml("portfolioPaging", json.paging);
            updateInnerHtml("portfolioArrowLeft", json.arrowleft);
            updateInnerHtml("portfolioArrowRight", json.arrowright);
            updateInnerHtml("portfolioCategories", json.categories);
            updateInnerHtml("portfolioDescription", json.description);

            //updateOpacity("portfolioWork", 0.1);
            updateInnerHtml("portfolioWork", json.work);
            fadeIn("portfolioWork", 0.1);

            currentPortfolioSort = sortorder;
            applyBehaviour();
         }
      }
   }
   subAjaxObj.open("GET", url);
   subAjaxObj.send(null);
}

