String.prototype.rot = function (ct) {
    if (typeof ct=='undefined') ct=13;
   var ret = '',ofs,rot,chr;
   for (var i=0; i<this.length; i++) {
       chr=this.charCodeAt(i);
       ofs=0;
       rot=0;
       if (chr >= 65 && chr <= 90) { rot=ct; ofs=65; }
       if (chr >= 97 && chr <= 122) { rot=ct; ofs=97; }
       ret+=String.fromCharCode((chr-ofs+rot)%26+ofs);
   }
   return ret;
};
(function ($) {
    var 
        gallery = $('ul.gallery:first'),
        list = gallery.find('li'),
        items = [],
        pos = 0,
        origin = parseInt(gallery.css('left')),
        desc = $('div.control-bar h2'),
        orig = desc.html(),
        title = $('div.upper-content>h1'),
        animSpeed=750;
    
    list.each(function () {
        items.push($(this).width());
    });
    function update(i) {
        if (isNaN(i)) {
            pos = list.index($(i)[0]);
        } else pos+=i;
        if (pos == items.length) pos --;
        if (pos == -1) pos ++;
        var sum = 0;
        for (i=0; i<pos; i++) sum += items[pos];
        gallery.animate({ left:  origin - sum }, animSpeed);
        desc.html(list.eq(pos).find('p').html());
        title.html(list.eq(pos).find('img').attr('alt'));
        window.location.hash='#folio/'+list.eq(i).attr('id');
        return false;
    }    
    $('div.upper-content fieldset a[href="#next"]').click(function () {
        return update(1);
    });
    $('div.upper-content fieldset a[href="#previous"]').click(function () {
        return update(-1);
    });
    
    $('ul.navigation a[href="#about"]').click(function () {
        $('div.bottom').animateClass('top','bottom', animSpeed);
        $('div.upper-content').slideUp(animSpeed);
        $('div.lower-content').slideDown(animSpeed);
        desc.html(orig);
    });
    $('ul.navigation a[href="#folio"]').click(function () {
        $('div.top').animateClass('bottom','top', animSpeed);
        $('div.upper-content').slideDown(animSpeed);
        $('div.lower-content').slideUp(animSpeed);
        update(0);
    });
    $('ul.about-nav a[href^="#about/"]').click(function () {
        var id = $(this).attr('href').replace(/^.*#about\//,'');
        $(this).parents('ul.about-nav').attr('className', 'about-nav').addClass(id);
        $('div.about-content:not(#'+id+')').hide();
        $('#'+id).fadeIn(animSpeed);
    });
    
    if (window.location.hash) {
        var 
            hash = window.location.hash.toString().replace('/^.*#/', '#'),
            set = hash.replace(/^#/,'').split('/');
        animSpeed=0;
        for (i=set.length; i>=1; i--) {
            var link = $('a[href$="'+set.slice(0,i).join('/')+'"]');
            if (link.length>0) link.click();
            else {
                update('#'+set[i-1]);
            }
        }
        animSpeed=750;
    } else {
        update('#toysrus');
    }
    
    $('a[href*="contact/"]').each(function () {
        this.href = 'mailto:'+this.href.replace(/^.*contact\//,'').rot()+'@codemonkeybryan.com';
    });
})(jQuery);