/**
 * effects.js
 */

var nav  = new Array();
nav.push('me', 'code', 'mac', 'references', 'blog');

function processCall(next) {
    var url  = '/' + next;
    var helper = 'Article';
    if (next == 'blog') {
        helper = 'Articlelist';
        $('head').append('<link href="/blog/rss" rel="alternate" type="application/rss+xml" title="sch0ll RSS Feed" >');
    } else {
        $('head link[title]').remove();
    }
    var post = {
        'useHelper': helper,
        'cmsBox': '2'
    };
    $('header h1 img').removeClass('hide');
    var title = document.title.split('|');
    document.title = next + ' | ' + title[1];
    $.post(url, post, function(data, textStatus) {
        if (textStatus == 'success' && data['error'] == 0) {
            $('#contentArea').hide().html(data['response']).fadeIn();
            $('nav ul li').removeClass('active');
            $('nav ul.first_nav li:contains('+next+')').addClass('active');
        }
    });
}

function in_array(item,arr) {
    for (p=0;p<arr.length;p++) if (item == arr[p]) return true;
    return false;
}

function locationHashChanged() {
    var hash = $(location).attr('hash').replace(/#/, '');
    if (in_array(hash, nav)) {
        processCall(hash);
    }
}

function doHashChange(hash) {
    if ($(location).attr('pathname') != '/') {
        window.location = '/#' + hash;
        return false;
    }
    if (hash == 'blog') {
        window.location = '/' + hash;
        return false;
    }
    history.pushState(null, null, '/#' + hash);
    locationHashChanged();
}

window.onhashchange = locationHashChanged;

$(document).ready(function() {

    // Hide Start Logo
    $('img.homepic').hide();

    // Load hash / show iphone welcome (for mobile browsers)
    if ($(location).attr('pathname') == '/' && $(location).attr('hash')) {
        locationHashChanged();
    } else {
        $('#iphone_welcome').css('opacity', 1);
    }

    // Show Start Logo
    if ($(location).attr('pathname') == '/' && !$(location).attr('hash')) {
        $('img.homepic').fadeIn();
    }

    // Contact - change form to stacked form for iphones
    if ($(location).attr('pathname') == '/contact') {
        if ($(window).width() <= 320) {
            $('form#contactForm').addClass('form-stacked');
        }
        $(window).resize(function() {
            if ($(window).width() <= 450) {
                $('form#contactForm').addClass('form-stacked');
            } else {
                $('form#contactForm').removeClass('form-stacked');
            }
        });
    }

    // Arrow Navigation
    var sel  = null;
    var next = '';
    $('#arrowLeft, #arrowRight').click(function() {
        //$('#contentArea').fadeOut('fast');
        sel = $('nav ul li.active a').html();
        if (sel == null) {
            if ($(this).attr('id') == 'arrowLeft') {
                next = nav[4];   
            } else if ($(this).attr('id') == 'arrowRight') {
                next = nav[0];
            }
        } else {
            var pos = nav.indexOf(sel);
            if (pos == -1) {
                next = nav[0]
            } else {
                if ($(this).attr('id') == 'arrowLeft') {
                    pos--; 
                    if (pos < 0) {
                        next = nav[4];
                    } else {
                        next = nav[pos]; 
                    }
                } else if ($(this).attr('id') == 'arrowRight') {
                    pos++; 
                    if (pos > 4) {
                        next = nav[0];
                    } else {
                        next = nav[pos]; 
                    }
                }
            }
        }
        doHashChange(next);
        return false;
    });

    // Top Navigation
    $('nav ul.first_nav li a').click(function() {
        var next = $(this).html();
        if ($('nav ul li.active a').html() == next) {
            return false;
        }
        doHashChange(next);
        return false;
    });

});

