/* Global Variables */
var AJAX_REQUEST_MAIN_URL ='http://www.everestuncensored.org/wp-content/plugins/EUTools/eu-xml.php';
var START_OFFSET = 0;
var currentOffset = 0;
var currentOffset1 = 0;
var COUNT = 4;
/* CHANGE THIS */
var catId = 7;
var catId1 = 8;
/* END */
var ONCLICK = 1;
var WO_ONCLICK = 0;
var PREVIOUS = -1;
var NEXT = 1;
/* End of Global Variables */


/**
 * getImageList - retrives the posts by sending AJAX request.
 * offset - last retrived post index.
 * count - number of posts to be retrived
 * isClick - determines if the method is called by onclick event.
 **/
function getImageList(galleryId, offset, count, catid, isClick) {
    ajaxRequestURL = AJAX_REQUEST_MAIN_URL + "?offset="+offset+"&count="+count+"&catid="+catid;
    jQuery.get(ajaxRequestURL,function(xml) {
        var imageLinkList = "";
        var divList = "";
        var firstImage = "";
        var content, postId, postURL, smallImg, thumbImg, label1, label2, postDate;
        var viewCount, commentCount, url;
        var firstDate, firstURL, firstViewCount, firstCommentCount, firstLabel;
        var euImage = new Array();
        var post    = new Array();
        var euCount = new Array();
        //alert("xmldata length: "+xml.length);
		//xml = "<posts><post id='3413' url='http://www.everestuncensored.org/3413/2008/12/03/hunger-n-thirst/'><eu-image small='http://www.everestuncensored.org/wp-content/uploads/2008/12/hunger_n_thirst_small.jpg' thumb='http://www.everestuncensored.org/wp-content/uploads/2008/12/hunger_n_thirst_small.jpg '   label1='306. Hunger &amp; Thirst' label2='Photo By: Samir Pradhananga' date='2008-12-03 06:32:39' /> <eu-count view='96'  comment='2'/></post></posts>";
        if (xml.length > 0) {
            var linkedXMLDoc = createXMLDOM(xml.toString());
            var postCount = linkedXMLDoc.selectNodes('./posts/post').length;

            try{
            for(var i = 0; i < postCount; i++) {  
              post = linkedXMLDoc.selectNodes('./posts/post');       
              postId   = post[i].getAttribute("id");
              postURL = post[i].getAttribute("url");
              
              euImage  = linkedXMLDoc.selectNodes('./posts/post/eu-image');
              smallImg = euImage[i].getAttribute("small");
              thumbImg = euImage[i].getAttribute("thumb");
              label1   = euImage[i].getAttribute("label1");
              label2   = euImage[i].getAttribute("label2");
              postDate = euImage[i].getAttribute("date");
              
              euCount      = linkedXMLDoc.selectNodes('./posts/post/eu-count');
              viewCount    = euCount[i].getAttribute("view");
              commentCount = euCount[i].getAttribute("comment");

              if (WO_ONCLICK == isClick && (i+1) == 1) {
                  firstImage        = thumbImg;
                  firstDate         = postDate;
                  firstURL          = postURL;
                  firstViewCount    = viewCount;
                  firstCommentCount = commentCount;
                  firstLabel        = label1;
              }
               
              imageLinkList += "<a href=\"#\" onclick=\"showPreview("+galleryId+",'"
              +thumbImg+"','"+(i+1)+"','"+postDate+"','"
              +postURL+"','"+viewCount+"','"+commentCount+"','"+firstLabel+"');return false\">"+
              "<img src=\""+smallImg+"\" height='27px' style='border:1px #FFFFFF solid; margin-left:2px; margin-right:2px;' alt=\""+label1+"\"></a>";
              divList += "<div class=\"imageCaption\">"+label1+"</div>"
            }
            } catch(e) {
                //alert("Exception: "+e.message);
            }
			//alert(galleryId+"isCLic: "+isClick);
            var gallery = galleryId == 0?"":galleryId;
            content = imageLinkList + divList + "<div id=\"slideEnd"+gallery+"\"></div>";
            jQuery("#theImages"+gallery).html(content);
            initSlideShow(galleryId);
            (WO_ONCLICK == isClick) ? showPreview(galleryId,firstImage, 1, firstDate, firstURL, 
                                        firstViewCount, firstCommentCount, firstLabel):"";
        } else {
			if (galleryId == 0) {
				currentOffset = currentOffset - COUNT;
			} else {
				currentOffset1 = currentOffset1 - COUNT;
			}
		}
            
    });
}

function getPreviousImages() {
    currentOffset = currentOffset - COUNT;
    if (currentOffset >= START_OFFSET) {
        getImageList(0,currentOffset, COUNT, catId, ONCLICK);
    } else {
			currentOffset = currentOffset + COUNT; //restore to original
	}
}

function getNextImages() {
    currentOffset = currentOffset + COUNT;
    getImageList(0,currentOffset, COUNT, catId, ONCLICK);
}

function getPreviousImages1() {
    currentOffset1 = currentOffset1 - COUNT;
    if (currentOffset1 >= START_OFFSET) {
        getImageList(1,currentOffset1, COUNT, catId1, ONCLICK);
    } else {
			currentOffset1 = currentOffset1 + COUNT; //restore to original
	}
}

function getNextImages1() {
    currentOffset1 = currentOffset1 + COUNT;
    getImageList(1,currentOffset1, COUNT, catId1, ONCLICK);
}


/*===================================================================================================*/




function createXMLDOM(strXML) {
    var xmlDoc;
    try //Internet Explorer
    {
        xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async="false";
        xmlDoc.loadXML(strXML);
    }
    catch(e)
    {
        try //Firefox, Mozilla, Opera, etc.
        {
            parser=new DOMParser();
            xmlDoc=parser.parseFromString(strXML,"text/xml");
        }
        catch(e) {
            //alert("Exception: "+e.message)
        }
    }
    return xmlDoc;
}


/*----------------------------------------------------------------------------*/

// check for XPath implementation
if( document.implementation.hasFeature("XPath", "3.0") )
{
    // prototying the XMLDocument
    XMLDocument.prototype.selectNodes = function(cXPathString, xNode)
    {
        if( !xNode ) {
            xNode = this;
        }
        var oNSResolver = this.createNSResolver(this.documentElement)
        var aItems = this.evaluate(cXPathString, xNode, oNSResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
        var aResult = [];
        for( var i = 0; i < aItems.snapshotLength; i++){
            aResult[i] =  aItems.snapshotItem(i);
        }
        return aResult;
    }
    // prototying the Element
    Element.prototype.selectNodes = function(cXPathString)
    {
        if(this.ownerDocument.selectNodes){
            return this.ownerDocument.selectNodes(cXPathString, this);
        }
        else{
            throw "For XML Elements Only";
        }
    }
    // prototying the XMLDocument
    XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode)
    {
        if( !xNode ){
            xNode = this;
        }
        var xItems = this.selectNodes(cXPathString, xNode);
        if( xItems.length > 0 ){
            return xItems[0];
        }
        else{
            return null;
        }
    }
    // prototying the Element
    Element.prototype.selectSingleNode = function(cXPathString)
    {
        if(this.ownerDocument.selectSingleNode) return this.ownerDocument.selectSingleNode(cXPathString, this);
        else {
            throw "For XML Elements Only";
        }
    }
}


/*** END Helper Functions **/
/*----------------------------------------------------------------------------*/
