//        Open the portfolio.xml file
//        and get all the items.
//        Each item makes up the portfolio details
//        i.e. directory, desc, image etc. 
function getportfolio() {

    if (window.XMLHttpRequest) {
        xhttp = new XMLHttpRequest();
    }
    else // Internet Explorer 5/6
    {
        xhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xhttp.open("GET", "portfolio/portfolio.xml", false);
    xhttp.send("");
    if (xhttp.status != 200) {
        document.write("error getting porfolio, Please try later : Status =: " + xhttp.status)
    }
    portfolioDoc = xhttp.responseXML;
    var x = portfolioDoc.getElementsByTagName("item");
    return (x);
}  

// open the XML file under the directory 
// as passed in the paramter portfolioid
// then add each photo & caption to a set of links called image0, image 1 etc
// up to a max of 20 images
/// <reference path="../images/portfolio/2/portfolio.xml" />

function loadimages(portfolioid,portfoliono,callingimage) {
   
    var image;
    var comment;
   

    if (portfolioid != null) { // check to see if we have a valid portfolio id 
        if (window.XMLHttpRequest) {
            xhttp = new XMLHttpRequest();
        }
        else // Internet Explorer 5/6
        {
            xhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xhttp.open("GET", portfolioid + "/" + "images.xml", false);
        xhttp.send("");
        // check if xhttp.status  != 200 ( OK) 

        xmlDoc = xhttp.responseXML;

        
        var xl = xmlDoc.getElementsByTagName("item");
        
                
        for (i = 0; i < xl.length && i < 20 ; i++) {

            image = (xl[i].getElementsByTagName("image")[0].childNodes[0].nodeValue);
            comment = (xl[i].getElementsByTagName("comment")[0].childNodes[0].nodeValue);
            
            // need to make sure we dont have duplicate image 
            if (callingimage != image) {
                imgid = "a#image" + i;
                jq(imgid).attr("rel", "lightbox[portfolio" + portfoliono + "]");
                jq(imgid).attr("href", portfolioid + "/" + image);
                jq(imgid).attr("title", comment);
            }
        }
 
    }
}

function createportfolio(x, id) {
    // id is either "all" for the entire table or the month that sets
    // up the featured project



    if (id == "all") {
        document.write("<table style='width:100%'>");
        for (i = 0; i < x.length; i++) {

            dir = (x[i].getElementsByTagName("dir")[0].childNodes[0].nodeValue);
            image = (x[i].getElementsByTagName("image")[0].childNodes[0].nodeValue);
            type = (x[i].getElementsByTagName("type")[0].childNodes[0].nodeValue);
            loc = (x[i].getElementsByTagName("loc")[0].childNodes[0].nodeValue);
            desc = (x[i].getElementsByTagName("desc")[0].childNodes[0].nodeValue);
            link = (x[i].getElementsByTagName("link")[0].childNodes[0].nodeValue);
            tech = (x[i].getElementsByTagName("tech")[0].childNodes[0].nodeValue);
            

            document.write("<tr>");
            document.write("<td >");
            //setup the bookmark to come back to
            document.write("<h2>" + "Customer: " + "<span class = 'black'>" + loc + "</span></h2>");
            document.write("<h2>" + "Business: " + "<span class = 'black'>" + type + "</span></h2>");
          //  document.write("<h2>" + "Objective: </h2>");
            document.write("<p>" + desc +  "</p>");
            document.write("<h2>Technology: </h2>");
            document.write("<p>" + tech + "</p>");
          
           
        
            //document.write("<p>" + "<b>Objective:</b> " + desc + "</p>");
            document.write("</td>");
            document.write("<td class = 'colimg'>");

            portid = "portfolio/" + dir;
            
            document.write("<a href='" + link + "' target = 'blank'><img  title='" + loc + "' alt='" + loc + "' src='portfolio/" + dir + '/' + image + "'/></a>");

            document.write("</td>");
            document.write("</tr>");

        }
        document.write("</table>");
    }
    else {
        // get the single portfolio item for the required month
        // need to check that the month we have we enough portfolio items
        // else choose a random portfolio item
        // eg if month = 10 and we have only 5 items we need to change to portfolio
        // item 5.

        i = id;
        noofitems = x.length-1;
        if (i > noofitems) {
            c = Math.random() * 10;
            i = noofitems - Math.round(c);
            if (i < 0) {
                i = noofitems;
            }    
            
        }   
                   
        dir = (x[i].getElementsByTagName("dir")[0].childNodes[0].nodeValue);
        image = (x[i].getElementsByTagName("image")[0].childNodes[0].nodeValue);
        loc = (x[i].getElementsByTagName("loc")[0].childNodes[0].nodeValue);
        type = (x[i].getElementsByTagName("type")[0].childNodes[0].nodeValue);
        link = (x[i].getElementsByTagName("link")[0].childNodes[0].nodeValue);
        portid = "portfolio/" + dir;
        
        document.write("<h4>" + loc + "</h4>");
        document.write("<a href='"+ link + "' title = '" + loc + "' target = '_blank'> <img  title='" + loc + "' alt='" + loc + "' src='portfolio/" + dir + '/' + image + "'/></a>");
        document.write("<h4>" + "<a name='item" + i + "'>" + "" + "</a>" + type + "</h4>");
       
       
    }     
    

}
