/*
Photos.js
Jeremy Turpin
August 1, 2008
*/

 function bsearch(arr,value,lo,hi)
    {
        if (hi <= lo)
        {
            return ((value == arr[lo])?lo:-1);
        }        
        
        var mid = Math.floor((hi + lo) / 2);
        if (value < arr[mid])
        {
            return bsearch(arr,value,lo,mid-1)
        }
        else if (value > arr[mid])
        {
            return bsearch(arr,value,mid+1,hi);
        }
        else
        {
            return mid;
        }
    }
    function enumerateTags(pl)
    {
        var tags = [];
        for (i in pl)
        {
            l = pl[i];
            if (l.tags)
            {
                for (j = 0; j < l.tags.length; j++)
                {
                    if (bsearch(tags,l.tags[j],0,tags.length) < 0)
                    {
                       tags[tags.length] = l.tags[j];
                       tags.sort();
                    }
                }
            }
        }
        return tags; 
    }       
    function initPhotos(container,path,pl)
    {
        container = $(container);    
        var i;
        for (i = 0; i<pl.length;i++)
        {
            var l = pl[i];
            
            var img = path+(l.img||l);
            var title = l.title||"";
            var desc = l.desc||"";
            
            var thumb = path + "Thumb/" + (l.img||l) + ".jpg";
            var p = document.createElement("li");
            p.img = img;
            p.title= title;
            p.desc = desc;
            
            var s = p.appendChild(document.createElement('span'))
            var im = document.createElement("img");
            
            im.setAttribute('src',thumb);            
            im.setAttribute('title', title);
            im.setAttribute('alt', title);
            im.setAttribute('border','0');
            s.appendChild(im);  
                        
            p.onclick = function(e){e = e||window.event;ShowImage(e,this.img,this.title,this.desc,this);};
            
            container.appendChild(p);        
        }       
    }
    
    function absTop(o)
    {
        var y = 0;
        while (o.offsetTop)
        {
            y = y + o.offsetTop;
            o = o.offsetParent;
        }
        return y;
    }
    
    var imgwindow;
    function ShowImage(e,img,title,desc,obj)
    {
                
        //Open a 'window' on top of the page to show the picture. 
        if (imgwindow)
        {if (imgwindow.close){imgwindow.close()}}
                
        //Get the absolute height that the window should be displayed in.         
        var y = absTop(obj)+30-e.clientY;       
               
        imgwindow = document.createElement('div');
        imgwindow.className = 'dialog';
        imgwindow.close = function(){imgwindow.parentNode.removeChild(imgwindow);imgwindow = null;};
        imgwindow.style.width = '650px';
        imgwindow.style.left = '100px';
        imgwindow.style.top = ((y < 10) ? 10 : y ).toString()+'px';
        
        
        //Add close bar
        var t = imgwindow.appendChild(document.createElement('span'));
        t.className = 'close'       
        var a = t.appendChild(document.createElement('A'))
        a.setAttribute('href','javascript:void(0);')
        a.onclick = imgwindow.close;
        a.appendChild(document.createTextNode('Close ( X )'))
        //Add title element
        var titleEl = document.createElement('div')
        titleEl.appendChild(document.createTextNode(title))
        titleEl.className = 'title'
        imgwindow.appendChild(titleEl);
        
        //Image 
        var ibox = imgwindow.appendChild(document.createElement('img'));
        ibox.src = img;
        ibox.alt = title;
        ibox.title = title;
        
        //Description box       
        var dbox = imgwindow.appendChild(document.createElement('div'));
        dbox.className = 'desc';        
        dbox.appendChild(document.createTextNode(desc));
               
        //Bottom close box
        var t = imgwindow.appendChild(document.createElement('span'));
        t.className = 'close'       
        var a = t.appendChild(document.createElement('A'))
        a.setAttribute('href','javascript:void(0);')
        a.onclick = imgwindow.close;
        a.appendChild(document.createTextNode('Close ( X )'))
        
        //Add Drag and Drop to title header
        imgwindow.handle = titleEl;
        titleEl.parent = imgwindow;
        DragHandler.attach(imgwindow)
        
        //Add to window
        document.body.appendChild(imgwindow);        
    }
    function getTagPictures(data,tag)
    {
        if (tag == '')
        {
            return data;
        }
        else
        {
            var pictures = new Array();
            for (key in data)
            {            
                if (data[key].tags)
                {
                    for (ktag in data[key].tags)
                    {
                        if (data[key].tags[ktag] == tag)
                        {
                            //This picture fits the tag
                            pictures.push(data[key]);
                            break;
                        }
                    }
                }
            }
            return pictures;
        }
    }
    function showTags(data)
    {
        tagcontainer = $(tagcontainer);
        if (tagcontainer)
        {
            //empty the container
            //while (tagcontainer.firstChild){tagcontainer.removeChild(tagcontainer.firstChild);}
            
            var tags = enumerateTags(data);
            //Generate tag to show all photos.
            var li = tagcontainer.appendChild(document.createElement('li'));
            var a = li.appendChild(document.createElement('a'));
            a.setAttribute('href','javascript:void(0);');
            a.appendChild(document.createTextNode('All Pictures'));
            a.onclick = function(){showTagPictures(data,'');};
            
            for (key in tags)
            {
                var li = tagcontainer.appendChild(document.createElement('li'));
                var a = li.appendChild(document.createElement('a'));
                a.setAttribute('href','javascript:void(0);')
                a.appendChild(document.createTextNode(tags[key])) 
                a._text = tags[key];              
                a.onclick = function(){showTagPictures(data,this._text);};                
            }
        }
    }
    function showTagPictures(data,tag)
    {
        container = $(container);
        if (container){
            //Clear the container
            while (container.firstChild){container.removeChild(container.firstChild);}
                     
            //Generate the thumbnails
            initPhotos(container,imgpath,getTagPictures(data,tag));
        }
    }
