var initUrl = "portfolioInit.php";
var contentUrl = "portfolioUpdateContent.php?designID=";
var thumbsUrl = "portfolioLinkItems.php?";
var linksPage = 0;
var numOfItems = 0;
var itemsPerPage = 0;
var itemsArrayImploded = 0;
var displayItem = 0;


function Portfolio() {
  http.open("GET", initUrl, true);
  http.onreadystatechange = portfolioInitHttpResponse;
  http.send(null);

  instaliseScroller();
}
function portfolioInitHttpResponse() {
  if (http.readyState == 4) {
    results = http.responseText.split(";");
    numOfItems = results[0];
    itemsPerPage = results[1];
    itemsArrayImploded = results[2];
    displayItem = results[3];

    portfolioThumbsInit();
  }
}

// function portfolioThumbsInit() {
//   http.open("GET", thumbsUrl + "linksPage=0&numOfItems=" + numOfItems + "&itemsPerPage=" + itemsPerPage + "&itemsArrayImploded=" + itemsArrayImploded, true);
//   http.onreadystatechange = portfolioThumbsHttpResponse;
//   http.send(null);
// }
function portfolioThumbsHttpResponse() {
  if (http.readyState == 4) {
    results = http.responseText;
    document.getElementById('galleryBarContentIcons').innerHTML = results;
  }
}

function updateGallery(designID) {
  http.open("GET", contentUrl + designID, true);
  http.onreadystatechange = portfolioContentHttpResponse;
  http.send(null);
}
function portfolioContentHttpResponse() {
  if (http.readyState == 4) {
    results = http.responseText.split(";");
    document.getElementById('galleryBarContentScreenshotTitle').innerHTML = results[0];
    document.getElementById('galleryInfoContentInformation').innerHTML = "<p>" + results[1] + "</p>";
    document.getElementById('galleryBarContentScreenshot').style.background = "url("+results[3]+")";
    //document.getElementById('galleryInfoContentInformation').style.background = "url("+results[4]+")";
  }
}
// Tidy 'til here!


function galleryThumbsChange(direction,numOfItems,itemsPerPage,itemsArrayImploded) {
  //alert(direction+" | "+numOfItems+" | "+itemsPerPage+" | "+itemsArrayImploded);

  var itemsArray = new Array();
  itemsArray = itemsArrayImploded.split(",")
  //alert(itemsArray[0]);

  linksPage = linksPage+direction;
  http.open("GET", thumbsUrl + "linksPage=" + linksPage + "&numOfItems=" + numOfItems + "&itemsPerPage=" + itemsPerPage + "&itemsArray=" + itemsArray, true);
  http.onreadystatechange = galleryThumbsHttpResponse;
  http.send(null);
}




function makeRequest(contentUrl) {

  if(window.XMLHttpRequest) {
    request = new XMLHttpRequest();
  }
  else if(window.ActiveXObject) {
    request = new ActiveXObject("MSXML2.XMLHTTP");
  }

  sendRequest(contentUrl);
}

function sendRequest(contentUrl) {

  request.onreadystatechange = handleHttpResponse;
  request.open("GET", contentUrl, true);
  request.send(null)

}

function getHTTPObject() {
  var xmlhttp;
  /*@cc_on 
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/

  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}

var http = getHTTPObject(); // We create the HTTP Object





var scrollAmount = 250;
var scrollStep = 10.0;
var currentlyScrolling = false;

function instaliseScroller() {
  contentIcons = document.getElementById("galleryBarContentIcons");
  contentIconsOffset = 0;
}

function iconsUp() {
  if (currentlyScrolling == false) {
    currentlyScrolling = true;
    contentIconsCurrent = contentIconsOffset;
    startPosition = contentIconsOffset;
    contentIconsOffset += scrollAmount;

    //contentIconsCurrent += scrollStep;
    myInterval = setInterval("iconsScroll(1,startPosition,contentIconsOffset)",50);
  }
}

function iconsDown() {
  if (currentlyScrolling == false) {
    currentlyScrolling = true;
    contentIconsCurrent = contentIconsOffset;
    contentIconsOffset -= scrollAmount;

    //contentIconsCurrent -= scrollStep;
    myInterval = setInterval("iconsScroll(-1,0,contentIconsOffset)",50);
  }
}

function iconsScroll(direction,startPosition,finalPosition) {
  //alert(contentIconsCurrent);

/*  if (direction*contentIconsCurrent > (contentIconsOffset-100)) {
    step = parseInt(scrollStep*0.8);
  } else if (direction*contentIconsCurrent > (contentIconsOffset-800)) {
    step = parseInt(scrollStep*0.5);
  } else if (direction*contentIconsCurrent > (contentIconsOffset-50)) {
    step = 1 //parseInt(scrollStep*0.2);
//   } else if (direction*contentIconsCurrent < (contentIconsOffset-(scrollAmount-50))) {
//     step = parseInt(scrollStep*0.8);
  } else {
    step = parseInt(scrollStep);
  }*/

  contentIconsCurrent += scrollStep*direction;
  contentIcons.style.top = contentIconsCurrent + "px";
  if (direction == 1 && contentIconsCurrent >= finalPosition) {
    clearInterval(myInterval);
    currentlyScrolling = false;
  }
  if (direction == -1 && contentIconsCurrent <= finalPosition) {
    clearInterval(myInterval);
    currentlyScrolling = false;
  }
}
