/**
 *
 * @desc        Emit Design / Consulting - JS Engine
 * @author      Christopher Sanford
 * @version     1.0.0
 * @copyright   (CC) 2006 Christopher Sanford
 * @url         http://www.christophersanford.com/
 * @url         http://www.inkandcode.com/
 * 
 * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General 
 * Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) 
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied 
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to 
 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */

    function loadPortfolio()
    {
        /**
         *  FX Object (width) for portfolio menu
         */

        new FX.Width({
            // trigger element
            trig: 'port-btn',
            // target element
            targ: 'port-outer',
            // maximum width
            max: 460,
            // minimum width
            min: 0,
            // toggle event
            evt: 'onclick',
            // initialization state
            init: 'min',
            // called before animation
            onStart: function()
            {
                var logo = ($('logo')) ? $('logo') : $('index-logo');
                logo.style.zIndex = 0;
                window.scrollTo(0,0);
                Element.show('port-inner');
            },
            // called after animation
            onStop: function()
            {
                // if minimized
                if (!this.sized) {

                    // swap logo depths
                    var logo = ($('logo')) ? $('logo') : $('index-logo');
                    logo.style.zIndex = 1;
                    Element.removeClassName($('port-btn'), 'opened');
                    Element.hide('port-inner');
                } else {
                    Element.addClassName($('port-btn'), 'opened');
                }
            },
            // tween amount
            tween: 5
        });

        /**
         *  Portfolio Slider Height
         */

        // position span at the bottom of content
        var content_span = document.createElement('span');
        content_span.id = 'content-height';
        $('content').appendChild(content_span);
        content_offset = parseInt(content_span.offsetTop);

        // position span at the bottom of col-a
        var col_a_span = document.createElement('span');
        col_a_span.id = 'col-a-height';
        $('col-a').appendChild(col_a_span);
        col_a_offset = parseInt(col_a_span.offsetTop);

        var footer_offset = parseInt($('footer').offsetHeight);

        // use content offset
        if (content_offset >= col_a_offset) {
            $('port-inner').style.height = (content_offset+footer_offset)+'px';
        // use col-a offset
        } else {
            $('port-inner').style.height = (col_a_offset+footer_offset)+'px';
        }

        var contact = document.getElementById('contact-form');
        if (contact !== null) {
            $('submit').onclick = function()
            {
                contact.submit();
                return false;
            };
        }

        /**
         *  About Bio Sliders
         */

        var bios = document.getElementsByClassName('bio');
        if (bios.length) {
            var objs = [];
            for (b = 0, i = 1, c = bios.length; b < c; b++, i++) {
                objs.push({
                    trig: 'bio-btn-'+i,
                    targ: 'bio-wrap-'+i,
                    max: $('bio-wrap-'+i).offsetHeight,
                    min: 0,
                    evt: 'onclick',
                    tween: 3
                });
            }

            new FX.Combo('h', objs);
        }

        /**
         *  Relative Anchors
         */

         var anks = document.getElementsByTagName('A');
         for (var a = 0, c = anks.length; a < c; a++) {
            if (anks[a].rel && anks[a].rel.toLowerCase() == 'external') {
                anks[a].target = 'blank';
            }
         }

    };

/**
 *  Portfolio Logic
 */

if (FX.Event) {

    (new FX.Event()).setEvent('window', 'onload', function() {

        var files = {};
        files['id-1a'] = {};
        files['id-1a']['cli'] = "0";
        files['id-1a']['desc'] = " Description ";
        files['id-2a'] = {};
        files['id-2a']['cli'] = "1";
        files['id-2a']['desc'] = " Description ";
        files['id-2b'] = {};
        files['id-2b']['cli'] = "1";
        files['id-2b']['desc'] = " Description ";
        files['id-3a'] = {};
        files['id-3a']['cli'] = "2";
        files['id-3a']['desc'] = "";
        files['id-4a'] = {};
        files['id-4a']['cli'] = "3";
        files['id-4a']['desc'] = "";
        files['id-5a'] = {};
        files['id-5a']['cli'] = "4";
        files['id-5a']['desc'] = "";
        files['id-6a'] = {};
        files['id-6a']['cli'] = "5";
        files['id-6a']['desc'] = "";
        files['id-6b'] = {};
        files['id-6b']['cli'] = "5";
        files['id-6b']['desc'] = "";
        files['id-7a'] = {};
        files['id-7a']['cli'] = "6";
        files['id-7a']['desc'] = "";
        files['pri-1a'] = {};
        files['pri-1a']['cli'] = "16";
        files['pri-1a']['desc'] = "";
        files['pri-1b'] = {};
        files['pri-1b']['cli'] = "16";
        files['pri-1b']['desc'] = "";
        files['pri-1c'] = {};
        files['pri-1c']['cli'] = "16";
        files['pri-1c']['desc'] = "";
        files['pri-2a'] = {};
        files['pri-2a']['cli'] = "17";
        files['pri-2a']['desc'] = "";
        files['pri-2b'] = {};
        files['pri-2b']['cli'] = "17";
        files['pri-2b']['desc'] = "";
        files['pri-2c'] = {};
        files['pri-2c']['cli'] = "17";
        files['pri-2c']['desc'] = "";
        files['pri-3a'] = {};
        files['pri-3a']['cli'] = "18";
        files['pri-3a']['desc'] = "";
        files['pri-3b'] = {};
        files['pri-3b']['cli'] = "18";
        files['pri-3b']['desc'] = "";
        files['pri-3c'] = {};
        files['pri-3c']['cli'] = "18";
        files['pri-3c']['desc'] = "";
        files['pri-3d'] = {};
        files['pri-3d']['cli'] = "18";
        files['pri-3d']['desc'] = "";
        files['pri-4a'] = {};
        files['pri-4a']['cli'] = "12";
        files['pri-4a']['desc'] = "";
        files['pri-4b'] = {};
        files['pri-4b']['cli'] = "12";
        files['pri-4b']['desc'] = "";
        files['pri-4c'] = {};
        files['pri-4c']['cli'] = "12";
        files['pri-4c']['desc'] = "";
        files['pri-4d'] = {};
        files['pri-4d']['cli'] = "12";
        files['pri-4d']['desc'] = "";
        files['pri-5a'] = {};
        files['pri-5a']['cli'] = "12";
        files['pri-5a']['desc'] = "";
        files['pri-5b'] = {};
        files['pri-5b']['cli'] = "12";
        files['pri-5b']['desc'] = "";
        files['pri-6a'] = {};
        files['pri-6a']['cli'] = "19";
        files['pri-6a']['desc'] = "";
        files['pri-6b'] = {};
        files['pri-6b']['cli'] = "19";
        files['pri-6b']['desc'] = "";
        files['pri-7a'] = {};
        files['pri-7a']['cli'] = "20";
        files['pri-7a']['desc'] = "";
        files['pri-7b'] = {};
        files['pri-7b']['cli'] = "20";
        files['pri-7b']['desc'] = "";
        files['pri-7c'] = {};
        files['pri-7c']['cli'] = "20";
        files['pri-7c']['desc'] = "";
        files['pri-8a'] = {};
        files['pri-8a']['cli'] = "5";
        files['pri-8a']['desc'] = "";
        files['pri-8b'] = {};
        files['pri-8b']['cli'] = "5";
        files['pri-8b']['desc'] = "";
        files['pri-8c'] = {};
        files['pri-8c']['cli'] = "5";
        files['pri-8c']['desc'] = "";
        files['web-1a'] = {};
        files['web-1a']['cli'] = "21";
        files['web-1a']['desc'] = "";
        files['web-2a'] = {};
        files['web-2a']['cli'] = "12";
        files['web-2a']['desc'] = "";
        files['web-2b'] = {};
        files['web-2b']['cli'] = "12";
        files['web-2b']['desc'] = "";
        files['web-2c'] = {};
        files['web-2c']['cli'] = "12";
        files['web-2c']['desc'] = "";
        files['web-3a'] = {};
        files['web-3a']['cli'] = "4";
        files['web-3a']['desc'] = "";
        files['web-3b'] = {};
        files['web-3b']['cli'] = "4";
        files['web-3b']['desc'] = "";
        files['web-3c'] = {};
        files['web-3c']['cli'] = "4";
        files['web-3c']['desc'] = "";
        files['web-4a'] = {};
        files['web-4a']['cli'] = "22";
        files['web-4a']['desc'] = "";
        files['web-4b'] = {};
        files['web-4b']['cli'] = "22";
        files['web-4b']['desc'] = "";
        files['web-4c'] = {};
        files['web-4c']['cli'] = "22";
        files['web-4c']['desc'] = "";
        files['web-5a'] = {};
        files['web-5a']['cli'] = "23";
        files['web-5a']['desc'] = "";
        files['web-5b'] = {};
        files['web-5b']['cli'] = "23";
        files['web-5b']['desc'] = "";
        files['web-5c'] = {};
        files['web-5c']['cli'] = "23";
        files['web-5c']['desc'] = "";
        files['web-5d'] = {};
        files['web-5d']['cli'] = "23";
        files['web-5d']['desc'] = "";
        files['web-6a'] = {};
        files['web-6a']['cli'] = "22";
        files['web-6a']['desc'] = "";
        files['web-6b'] = {};
        files['web-6b']['cli'] = "22";
        files['web-6b']['desc'] = "";
        files['web-6c'] = {};
        files['web-6c']['cli'] = "22";
        files['web-6c']['desc'] = "";
        files['web-6d'] = {};
        files['web-6d']['cli'] = "22";
        files['web-6d']['desc'] = "";
        files['web-7a'] = {};
        files['web-7a']['cli'] = "22";
        files['web-7a']['desc'] = "";
        files['web-7b'] = {};
        files['web-7b']['cli'] = "22";
        files['web-7b']['desc'] = "";
        files['web-7c'] = {};
        files['web-7c']['cli'] = "22";
        files['web-7c']['desc'] = "";
        files['pak-1a'] = {};
        files['pak-1a']['cli'] = "7";
        files['pak-1a']['desc'] = "";
        files['pak-1b'] = {};
        files['pak-1b']['cli'] = "7";
        files['pak-1b']['desc'] = "";
        files['pak-2a'] = {};
        files['pak-2a']['cli'] = "8";
        files['pak-2a']['desc'] = "";
        files['pak-3a'] = {};
        files['pak-3a']['cli'] = "9";
        files['pak-3a']['desc'] = "";
        files['pak-3b'] = {};
        files['pak-3b']['cli'] = "9";
        files['pak-3b']['desc'] = "";
        files['pak-4a'] = {};
        files['pak-4a']['cli'] = "11";
        files['pak-4a']['desc'] = "";
        files['pak-4b'] = {};
        files['pak-4b']['cli'] = "11";
        files['pak-4b']['desc'] = "";
        files['pak-4c'] = {};
        files['pak-4c']['cli'] = "11";
        files['pak-4c']['desc'] = "";
        files['pho-1a'] = {};
        files['pho-1a']['cli'] = "10";
        files['pho-1a']['desc'] = "";
        files['pho-1b'] = {};
        files['pho-1b']['cli'] = "10";
        files['pho-1b']['desc'] = "";
        files['pho-2a'] = {};
        files['pho-2a']['cli'] = "12";
        files['pho-2a']['desc'] = "";
        files['pho-2b'] = {};
        files['pho-2b']['cli'] = "12";
        files['pho-2b']['desc'] = "";
        files['pho-3a'] = {};
        files['pho-3a']['cli'] = "13";
        files['pho-3a']['desc'] = "";
        files['pho-3b'] = {};
        files['pho-3b']['cli'] = "13";
        files['pho-3b']['desc'] = "";
        files['pho-3c'] = {};
        files['pho-3c']['cli'] = "13";
        files['pho-3c']['desc'] = "";
        files['pho-4a'] = {};
        files['pho-4a']['cli'] = "14";
        files['pho-4a']['desc'] = "";
        files['pho-4b'] = {};
        files['pho-4b']['cli'] = "14";
        files['pho-4b']['desc'] = "";
        files['pho-5a'] = {};
        files['pho-5a']['cli'] = "12";
        files['pho-5a']['desc'] = "";
        files['pho-5b'] = {};
        files['pho-5b']['cli'] = "12";
        files['pho-5b']['desc'] = "";
        files['pho-5c'] = {};
        files['pho-5c']['cli'] = "12";
        files['pho-5c']['desc'] = "";
        files['pho-6a'] = {};
        files['pho-6a']['cli'] = "15";
        files['pho-6a']['desc'] = "";
        files['pho-6b'] = {};
        files['pho-6b']['cli'] = "15";
        files['pho-6b']['desc'] = "";
        files['pho-7a'] = {};
        files['pho-7a']['cli'] = "15";
        files['pho-7a']['desc'] = "";
        files['pho-7b'] = {};
        files['pho-7b']['cli'] = "15";
        files['pho-7b']['desc'] = "";
        files['pho-8a'] = {};
        files['pho-8a']['cli'] = "15";
        files['pho-8a']['desc'] = "";
        files['pho-8b'] = {};
        files['pho-8b']['cli'] = "15";
        files['pho-8b']['desc'] = "";
        files['pho-9a'] = {};
        files['pho-9a']['cli'] = "15";
        files['pho-9a']['desc'] = "";
        files['pho-9b'] = {};
        files['pho-9b']['cli'] = "15";
        files['pho-9b']['desc'] = "";

        var preload = [];
        var port = [];
        var types = [];
        types.push('id');
        types.push('pri');
        types.push('web');
        types.push('pak');
        types.push('pho');
        for (var f in files) {
            if (f.match(/^([^\-]+\-\d+)(.+)/))  {
                port.push(f);
                //var img = new Image();
                //img.src = './i/port/'+f+'.jpg';
                //preload.push(img);
            }
        }

        var cli = null;
        var cls = 'selected';
        var prev = $('prev');
        var next = $('next');
        var img = $('port-img');

        for (var p = 0, c = port.length; p < c; p++) {
            var id = port[p].replace(/^([^\-]+\-\d+)(.+)/, '$1');
            $(port[p]).onclick = function()
            {
                setPrevNext(this.id);
                return false;
            };
            $(id).onclick = function()
            {
                setPrevNext(this.id+'a');
                return false;
            };
        };

        for (var t = 0, c = types.length; t < c; t++) {
            
            var id = types[t];
            $(id).onclick = function()
            {
                setPrevNext(this.id+'-1a');
                return false;
            };
        };

        var setPrevNext = function(id)
        {
            for (var p = 0, c = port.length; p < c; p++) {

                if (p == 0) {
                    next.nid = port[p+1];
                    prev.pid = port[c-1];
                } else if (p==(c-1)) {
                    next.nid = port[0];
                    prev.pid = port[c-2];
                } else {
                    next.nid = port[p+1];
                    prev.pid = port[p-1];
                }

                if (port[p]==id) break;
            }
            
            toggleNav(id);
            toggleCli(id);
            toggleType(id);

        };

        next.onclick = function()
        {
            setPrevNext(this.nid);
            return false;
        };

        prev.onclick = function()
        {
            setPrevNext(this.pid);
            return false;
        };

        var toggleNav = function(id)
        {
            Element.addClassName(id, cls);
            var cur = id.replace(/^([^\-]+\-\d+)(.+)/, '$1');
            Element.addClassName(cur, cls);

            Element.removeClassName('port-img-wrap', 'loading');
            Element.addClassName('port-img-wrap', 'loading');

            img.style.visibility = 'hidden';
            img.src = './i/port/'+id+'.jpg';
            img.onload = function()
            {
                img.style.visibility = 'visible';
            };

            for (var p = 0, c = port.length, nav = null; p < c; p++) {
                nav = port[p].replace(/^([^\-]+\-\d+)(.+)/, '$1');
                if (cur != nav) {
                    Element.removeClassName(nav, cls);
                    Element.hide(nav+'-nav');
                    Element.removeClassName(port[p], cls);
                } else if (id != port[p]) {
                    Element.removeClassName(port[p], cls);
                } else {
                    Element.show(nav+'-nav');
                }
            }

        };

        var toggleCli = function(id)
        {
            if (cli === files[id]['cli']) {
                return;
            }
            for (var f in files) {
                if (f.match(/^([^\-]+\-\d+)(.+)/)) {
                    if (files[id]['cli'] != files[f]['cli']) {
                        Element.hide('cli-'+files[f]['cli']+'-name');
                        Element.hide('cli-'+files[f]['cli']+'-det');
                    } else {
                        Element.show('cli-'+files[f]['cli']+'-det');
                        Element.show('cli-'+files[f]['cli']+'-name');
                        $('cli-'+files[f]['cli']+'-det').getElementsByTagName('DIV')[1].innerHTML = files[f]['desc'];
                    }
                }
            }
            cli = files[id]['cli'];
        };

        var toggleType = function(id)
        {
            var type = id.replace(/^([^\-]+)(\-.+)/, '$1');
            for (var t = 0, c = types.length; t < c; t++) {
                if (types[t] == type) {
                    Element.addClassName($(type), cls);
                } else {
                    Element.removeClassName($(types[t]), cls);
                }
            }
        };

        var rand = Math.floor(Math.random()*port.length);
        setPrevNext(port[rand]);

    });
}
