/*
 * Copyright (c) 2011 
 * ==================================
 * original filename: script.js
 * filesize: 9975 Bytes
 * last modified: Wed, 09 Feb 2011 13:50:00 +0100
 *
 */
$(document).ready(function () {
    var g, c, s, v, p, navanchors = $('a', '#nav'),
        nivoslider = $('#nivoslider', '#content'),
        slides = $('.slide', '#content');
    slideropts = {
        directionNav: false,
        controlNav: false,
        keyboardNav: false
    }, init = function () {
        Cufon.replace('h1,h2,h3,h4,h5');
        var current = location.hash.substr(3) || slides[0].id;
        $('#' + current).show();
        slides.css({
            position: 'absolute',
            top: 70,
            left: 0
        });
        $.extend(slideropts, params.slider);
        nivoslider.nivoSlider(slideropts);
        navanchors.bind('click', function () {
            var t = $(this);
            navanchors.removeClass('active');
            t.addClass('active');
            var id = t.attr('href').substr(1);
            if (id != current) {
                $('#' + id).stop().css({
                    top: 10,
                    opacity: 0,
                    'z-index': 3
                }).show();
                $('#' + current).stop().animate({
                    top: 120,
                    opacity: 0
                }, function () {
                    $(this).hide()
                }).css('zIndex', '2');
                $('#' + id).animate({
                    top: 70,
                    opacity: 1
                });
                current = id;
                document.title = document.title.split(' - ')[0] + ' - ' + t.text();
                location.href = '#!/' + current
            }
            if (id != 'home') {
                nivoslider.data('nivoslider').stop()
            } else {
                nivoslider.data('nivoslider').start()
            }
            return false
        });
        if ($('a[href=#' + current + ']', '#nav').length) {
            $('a[href=#' + current + ']', '#nav').click()
        } else {
            navanchors.eq(0).click()
        }
        $('a[href^=#]', '#content').bind('click', function () {
            var id = $(this).attr('href').substr(1);
            $('#nav a[href=#' + id + ']').click();
            return false
        });
        $("[placeholder]").focus(function () {
            var el = $(this);
            if (el.val() == el.attr("placeholder")) {
                el.val("");
                el.removeClass("placeholder").css('color', '#CCCCCC')
            }
        }).blur(function () {
            var el = $(this);
            if (el.val() == "" || el.val() == el.attr("placeholder")) {
                el.addClass("placeholder");
                el.val(el.attr("placeholder")).css('color', '#444444')
            }
        }).blur();
        g = new gallery();
        g.init();
        s = new socialmedia();
        s.init();
        c = new contact();
        c.init()
    };
    var gallery = function () {
        var i = 0,
            g = $('#gallery'),
            pages = g.find('ul'),
            currentid = 0,
            portfoliotitel, galnavanchors;

        function init() {
            g.find('a').prettyPhoto();
            setTitle();
            if (pages.length > 1) {
                navpoints = '';
                pages.each(function (i, e) {
                    navpoints += '<li><a href="#' + i + '">' + (i + 1) + '</a></li>'
                });
                pages.css({
                    position: 'absolute',
                    left: 0,
                    top: 0
                }).hide();
                pages.eq(currentid).show();
                var galnav = $('#gallerynav');
                galnav.html(navpoints);
                galnavanchors = galnav.find('a');
                galnavanchors.eq(currentid).addClass('selected');
                galnavanchors.bind('click', function () {
                    changePage($(this).attr('href').substr(1));
                    return false
                })
            }
        }
        function changePage(id) {
            if (id < currentid) {
                start = -40;
                end = 20
            } else {
                start = 20;
                end = -40
            }
            if (id != currentid) {
                pages.eq(id).stop().css({
                    left: start,
                    opacity: 0,
                    'z-index': 5
                }).show();
                pages.eq(currentid).stop().animate({
                    left: end,
                    opacity: 0
                }, function () {
                    $(this).hide()
                }).css('zIndex', '3');
                pages.eq(id).animate({
                    left: 0,
                    opacity: 1
                });
                currentid = parseInt(id, 10);
                galnavanchors.removeClass('selected').eq(id).addClass('selected')
            }
            setTitle()
        }
        function setTitle() {
            var el = $('#portfolio > h2');
            portfoliotitel = portfoliotitel || el.text();
            var title = pages.eq(currentid).attr('title') || portfoliotitel;
            var current = el.text();
            if (current != title) {
                el.fadeOut('fast', function () {
                    el.text(title).fadeIn('fast');
                    Cufon.replace('#portfolio > h2')
                })
            }
        }
        return {
            init: function () {
                init()
            }
        }
    };
    var socialmedia = function () {
        function init() {
            $('#networks').find('a').fadeTo(1, 0.6).hover(function () {
                $(this).stop().fadeTo(50, 0.99)
            }, function () {
                $(this).clearQueue().delay(100).fadeTo(500, 0.6)
            })
        }
        return {
            init: function () {
                init()
            }
        }
    };
    var contact = function () {
        var timeout, email, nameel = $('#name'),
            emailel = $('#contactemail'),
            msgel = $('#contactmsg'),
            form = $('#contactform'),
            button = $('#contactsubmit'),
            status = $('#contactstatus'),
            valid = false;

        function init() {
            $('.vcardfile').find('img').fadeTo(1, 0.6).hover(function () {
                $(this).stop().fadeTo(50, 1)
            }, function () {
                $(this).clearQueue().delay(100).fadeTo(500, 0.6)
            });
            nameel.bind('keyup', keyupHandler);
            emailel.bind('keyup', keyupHandler);
            msgel.bind('keyup', keyupHandler);
            form.bind('submit', submitForm);
            check()
        }
        function keyupHandler() {
            clearTimeout(timeout);
            timeout = setTimeout(function () {
                check()
            }, 500)
        }
        function setStatus(txt) {
            status.show().html(txt).delay(4000).fadeOut(1000)
        }
        function submitForm() {
            if (valid) {
                nameel.unbind('keyup');
                emailel.unbind('keyup');
                msgel.unbind('keyup');
                button.attr('disabled', 'disabled');
                setStatus(params.texts.contactformwait);
                $.post("contact.php", {
                    name: $.trim(nameel.val()),
                    email: $.trim(emailel.val().toLowerCase()),
                    msg: $.trim(msgel.val())
                }, function (data) {
                    button.removeAttr('disabled');
                    if (data.success) {
                        setStatus(params.texts.contactformadded);
                        nameel.val('').blur();
                        emailel.val('').blur();
                        msgel.val('').blur();
                        valid = false;
                        init()
                    } else {
                        setStatus(params.texts.contactformfail);
                        nameel.bind('keyup', keyupHandler);
                        emailel.bind('keyup', keyupHandler);
                        msgel.bind('keyup', keyupHandler);
                        valid = false
                    }
                }, "json")
            } else {
                setStatus(params.texts.contactforminvalid)
            }
            return false
        }
        function check() {
            email = emailel.val();
            if (verify(email) && nameel.val() != '' && msgel.val() != '' && nameel.val() != nameel.attr('placeholder') && msgel.val() != msgel.attr('placeholder')) {
                button.css('background-color', '#5780cc');
                valid = true
            } else {
                button.css('background-color', '#999999');
                valid = false
            }
        }
        function verify(email) {
            email = $.trim(email.toLowerCase());
            return (email && /^([\w-]+(?:\.[\w-]+)*)\@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$|(\[?(\d{1,3}\.){3}\d{1,3}\]?)$/.test(email))
        }
        return {
            init: function () {
                init()
            }
        }
    };
    init()
});
