var fade = 
{
    toBlack : function(onComplete)
    {
        /* disable overflow, will work better here. */
        scroll(0,0);
        document.body.style['overflow'] = 'hidden';

        var d = document.createElement('div');
        d.id = 'blackout';
        document.body.appendChild(d);

        var a = new YAHOO.util.ColorAnim('blackout', { backgroundColor: { to: '#000' } }, .5);
        a.onComplete.subscribe(onComplete);
        a.animate();
        
        
    },
    
    out : function()
    {
        var d = document.getElementById('blackout');
        d.parentNode.removeChild(d);
        document.body.style['overflow'] = 'auto';
    },
    
    setCloseButton : function()
    {
        var d = document.createElement('div');
        d.className = 'actionbar';
        d.id = 'close';
        d.innerHTML = '<a href="javascript:fade.out()">CLOSE THIS WINDOW</a>';
        document.getElementById('blackout').appendChild(d);
    }
}

var stream = 
{
    filename : null,
    
    open : function(filename)
    {
        stream.filename = filename;
        fade.toBlack(stream.load);        
    },
    
    load : function()
    {
        /* stream */
        var s = document.createElement('div');
        s.style['top'] = '31px';
        s.style['left'] = '50%'; 
        s.style['width'] = '800px'
        s.style['height'] = '700px'; 
        s.style['margin'] = '0 0 0 -400px';
        s.innerHTML = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="800" height="685" id="StreamingVideoWidget" align="middle">'
                    + '<param name="allowScriptAccess" value="sameDomain" />'
                    + '<param name="movie" value="/gfx/swf/StreamingVideoWidget.swf?filename=' + stream.filename + '" />'
                    + '<param name="quality" value="high" />'
                    + '<param name="bgcolor" value="#000000" />'
                    + '<embed src="/gfx/swf/StreamingVideoWidget.swf?filename=' + stream.filename + '" quality="high" bgcolor="#000000" width="800" height="685" name="StreamingVideoWidget" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />'
                    + '</object>';
        document.getElementById('blackout').appendChild(s);
        fade.setCloseButton();
        comment.init();
    }
}


var gallery = {

    imgNum : null,
    
    open : function(startImg)
    {
        gallery.imgNum = startImg;
        fade.toBlack(gallery.load);    
    },
    
    load : function()
    {
        var o = document.getElementById('blackout');
        
        fade.setCloseButton();
        comment.init();
        
        /* next and previous */
        var i = gallery.imgNum - 1;
        if (i >= 0) {
            var p = document.createElement('div');
            p.className = 'pagination';
            p.style['left'] = '10px';
            p.style['marginTop'] = '-15px';
            p.innerHTML = '<a href="javascript:gallery.shift(' + i + ')">&lt;&lt;<br/><span>PREVIOUS</span></a>';
            o.appendChild(p);
        }
        
        var i = gallery.imgNum + 1;
        if (i < document.getElementById('stills').getElementsByTagName('img').length) {
            var n = document.createElement('div');
            n.className = 'pagination';
            n.style['right'] = '10px';
            n.style['marginTop'] = '-15px';
            n.innerHTML = '<a href="javascript:gallery.shift(' + i + ')">&gt;&gt;<br/><span>NEXT</span></a>';
            o.appendChild(n);
        }
          /* show loading */
        var d = document.createElement('div');
        d.id = 'still_image';
        d.style['top'] = '50%';
        d.style['left'] = '50%';
        d.style['margin'] = '-16px 0 0 -16px';
        d.innerHTML = '<img src="/gfx/loading.gif" alt="Loading..."/>';
        o.appendChild(d);
        
        /* load image */
        var i = new Image();
        i.src = document.getElementById('stills').getElementsByTagName('img')[gallery.imgNum].src.replace('thumb', 'still');
        i.src += '?' + Math.floor(Math.random()*111);
        YAHOO.util.Event.addListener(i, 'load', gallery.scale);
    },
    
    shift : function(imgNum) 
    {
        document.getElementById('blackout').innerHTML = '';
        gallery.imgNum = imgNum;
        gallery.load();
    },
    
    scale : function(e)
    {
        var o = document.getElementById('still_image');
        o.innerHTML = '';
        
        /* scale this image */
        var w = 800;
        var h = 600;
        var x = this.width;
        var y = this.height;
        if (x > 800) {
            w = 800;
            h = Math.round((y * w) / x);
        }
        
        if (y > 580 || h > 580) {
            h = 580;
            w = Math.round((x * h) / y); 
        }
        
        this.style['width'] = w + 'px';
        this.style['height'] = h + 'px';
        var a = document.createElement('a');
        a.href = this.src;
        a.appendChild(this);
        
        h = h / 2;
        
        if (h >= 380) {
            h = h - 80;
        }
        
        o.style['marginTop'] = '-' + h + 'px'
        o.style['marginLeft'] = '-' + (w / 2) + 'px'
        o.appendChild(a);
    }
}


var comment = {

    slide : null,
    pid : null,
    timer : null,

    init : function()
    {
        if (document.getElementById('comment_on')) {
            
           comment.pid = document.getElementById('comment_on').value
           var d = document.createElement('div');
           d.className = 'actionbar';
           d.id = 'comment_slide';
           d.innerHTML = '<a href="javascript:comment.toggle()">COMMENT ON THIS PROJECT</a>';
           document.getElementById('blackout').appendChild(d);
        }
        
      
    },
    
    toggle : function()
    {
        comment.slide = document.getElementById('comment_slide');
        var slideCurrentHeight = comment.slide.style['height'];
                
        
        if (slideCurrentHeight == '' || slideCurrentHeight == '30px') {
            comment.slide.innerHTML = '<a href="javascript:comment.toggle()">CLOSE COMMENTS</a>';
            
            var a = new YAHOO.util.Anim('comment_slide', { height: { to: YAHOO.util.Dom.getViewportHeight()} }, .3);
            a.onComplete.subscribe(comment.loadInterface);
            
            if (s = document.getElementById('StreamingVideoWidget')) {
                s.style['display'] = 'none';
            }
        } else {
            comment.slide.innerHTML = '<a href="javascript:comment.toggle()">COMMENT ON THIS PROJECT</a>';
            
            var a = new YAHOO.util.Anim('comment_slide', { height: { to: 30} }, .3);
            
            if (s = document.getElementById('StreamingVideoWidget')) {
                s.style['display'] = 'block';
            }
            
            clearTimeout(comment.timer);     
        }
        
        a.animate();
    },
    
    loadInterface : function()
    {
        var interface = document.createElement('div');
        
        interface.innerHTML = '<div id="list">'
                            + '<img src="/gfx/loading.gif" alt="Fetching Comments..." /><br/>Fetching Comments...'
                            + '</div>'
                            + '<div id="insert"><table><tr>'
                            + '<td style="width: 250px;"><p id="comment_help">To comment on this project type it in the text box to the right and click the "comment" button.</p></td>'
                            + '<td>'
                            + '<form method="post" id="comment_add" action="javascript:comment.add()"><textarea name="comment" id="comment_text"></textarea><br/><input type="submit" value="Comment"/></form>'
                            + '</td>'
                            + '</tr></table></div>';
        
        comment.slide.appendChild(interface);
        comment.get();
    },
    
    get : function()
    {
        comment.timer = setTimeout("comment.get()", 15000);
    
        var list = document.getElementById('list');
        pass = function(o) {
        
            var list = document.getElementById('list');
            if (comments = o.responseText.parseJSON()) {
                var table = document.createElement('table');
                for (var i = 0, l = comments.length; i < l; i++) {
                    var row = table.insertRow(-1);
                    row.style['backgroundColor'] = i % 2 ? '#111' : '#333';

                    var column = row.insertCell(-1);
                    column.style['width'] = '120px';
                    column.style['textAlign'] = 'center';
                    column.innerHTML = comments[i].date;
                    var column = row.insertCell(-1);
                    column.style['width'] = '110px';
                    column.innerHTML = comments[i].name;
                    var column = row.insertCell(-1);
                    column.innerHTML = comments[i].comment;
                }
                list.innerHTML = '';
                list.appendChild(table);
            } else {
                list.innerHTML = 'An error occured whilst trying to decoded the comments.';
            }
        }
        
        fail = function(o) {
            document.getElementById('list').innerHTML = 'An error occured whilst trying to fetch the comments, possibly a network connection problem?';
        }
        
        var callback = { success: pass, failure: fail, scope: this };
        YAHOO.util.Connect.asyncRequest('GET', '/comment/get/' + comment.pid, callback);
    },
    
    add : function()
    {
        pass = function(o) {
            var help = document.getElementById('comment_help');
            if (o.responseText == '') {
                document.getElementById('comment_text').value = '';
                help.innerHTML = 'Thank you! Your comment has been added.';
                comment.get();            
            } else {
                help.innerHTML = 'An error occured whilst trying to add your comment';
            }
        }
        fail = function(o) {
            document.getElementById('comment_help').innerHTML = 'An error occured whilst trying to add the comment, possibly a network connection problem?';
        }
        
        if (document.getElementById('comment_text').value.length > 0) {
        
            var callback = { success: pass, failure: fail, scope: this };
            YAHOO.util.Connect.setForm('comment_add');   
            YAHOO.util.Connect.asyncRequest('POST', '/comment/add/' + comment.pid, callback);
        } else {
            document.getElementById('comment_help').innerHTML = 'You cannot add an empty comment, please type your comment in the textbox to the <strong>right</strong> and then click the "comment" button.';
        }
    }
    
 
}

String.prototype.parseJSON = function ()
{
	try {
 		return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
 	    this.replace(/"(\\.|[^"\\])*"/g, ''))) &&
 	            eval('(' + this + ')');
 	} catch (e) {
 		return false;
 	}
};
