$(function(){
    Spotlight.initialise();
    Portfolio.initialise();
    Core.initialise();
});

var Core = {
    
    currentPage : 'home',
    
    initialise : function() {
        $('a').unbind();
        $('a').click(function(e) {
            
            if(e.srcElement.target == 'blanc') {
                window.open(e.srcElement.href)
            }else{
                Core.navigate(e.srcElement.pathname);
            }
            return false;
        });
        
        $('#contentbar').mouseover(function(){
            if(Portfolio.blockBig == false) {
                $('#fade').fadeOut(200);
            }
        });

        $('#contentbar').mouseleave(function(){
            if(Portfolio.blockBig == false) {
                $('#fade').fadeIn(200);
            }
        });
        
        var hash = window.location.hash;
        if(hash.substr(0, 1) == '#') {
            hash = hash.substr(1);
        }
        if(hash != '' && hash != Core.currentPage) {
             Core.navigate('/' + hash + '.html');
        }
    },
    
    navigate : function(url) {
        
        if(url.substring(0, 1) == '/') {
            url = url.substring(1);
        } 
        
        cmd = '';
        url = url.split('/');        
        
        if(url[1]) {
            cmd = url[1].split('.html');
            cmd = cmd[0];
        }
        
        url = url[0].split('.html');
        
        url = url[0];

        window.location.hash = url;
        document.body.scrollTop = 0;

        if(Core.currentPage == 'home') {
            Spotlight.slimView();
        }

        Core.focusMenuItem(url);
        Core.currentPage = url;

        var pageH = $('#page_content').height();
        $('#page_content').css({'height' : pageH + 'px'});

        $('#page_content').fadeTo(300, 0, function() {
            $.post('/pages/' + url + '.php',
                {
                    cmd : cmd
                },
                function(html) {
                    html = '<div id="page-' + url + '">' + html + '</div>';
                    $('#page_content').html(html);

                    var contentH = $('#page-' + url).height();

                    $('#page_content').animate({'height' : contentH + 'px'}, 500, function() {
                        $('#page_content').css({'height' : 'auto'});
                    });
                    $('#page_content').fadeTo(300, 1);
                    Core.initialise();
                }
            );
        });
    },
    
    focusMenuItem : function(url) {
        $('#header #menu .item').removeClass('active');
        $('#header #menu #' + url).addClass('active');
    }
}

var Diensten = {
    currPage : 'website-ontwikkeling',
    
    navigate : function(id, e) {
        var newH = $('#page-diensten #page-' + id).outerHeight();
        newH += 35;
        
        $('#page-diensten #page-' + Diensten.currPage).fadeOut();
        $('#page-diensten').animate({'height' : newH + 'px'}, function() {
            $('#page-diensten #page-' + id).fadeIn();
        });
        
        Diensten.currPage = id;
        
        $.each($('#page-diensten #menu-center .item'), function() {
            $(this).removeClass('active');
        })
        
        $(e).addClass('active');
    }
}

var Route = {
    plan : function() {
        var street = $('#page-contact #route #street').val();
        var city = $('#page-contact #route #city').val();
        
        var contentH = $('#page-contact').height();
        $('#page_content').css({'height' : contentH + 'px'});
        $('#page-contact').css({'height' : contentH + 'px'});
        
        
        $('#page-contact #content').fadeOut(function() {
            map = new GMap2(document.getElementById("page-contact"));
            directionsPanel = document.getElementById("directions");
            map.setCenter(new GLatLng(49.496675,-102.65625), 3);
            directions = new GDirections(map, directionsPanel);
            directions.load("from: " + street + ", " + city + ", Netherlands to: Koningin Julianaweg 36, Maasland, Netherlands");
        });
    }
}

var Email = {
    send : function() {
        var company = $('#page-contact #company').val();
        var name    = $('#page-contact #name').val();
        var email   = $('#page-contact #email').val();
        var message = $('#page-contact #message').val();
        var phone   = $('#page-contact #phone').val();
        
        if(company != '' && name != '' && email != '' && message != '') {
            $.post('/inc/php/sent_mail.php',
                {
                    company : company,
                    name : name,
                    email : email,
                    message : message,
                    phone : phone
                }
            );
            alert('Bedankt voor uw e-mail. Wij zullen deze zo spoedig mogelijk beantwoorden.')
        }else{
            alert('Wij verzoeken u alle velden in te vullen.');
        }
    }
}

var Offerte = {
    send : function() {
        var company = $('#page-offerte #company').val();
        var name    = $('#page-offerte #name').val();
        var email   = $('#page-offerte #email').val();
        var message = $('#page-offerte #message').val();
        var phone   = $('#page-offerte #phone').val();
        var website = $('#page-offerte #website').val();
        var budget  = $('#page-offerte #budget').val();
        
        if(company != '' && name != '' && email != '' && message != '') {
            $.post('/inc/php/sent_offerte.php',
                {
                    company : company,
                    name : name,
                    email : email,
                    message : message,
                    phone : phone,
					budget : budget,
					website : website
                }
            );
            alert('Bedankt voor uw aanvraag. Wij zullen deze zo spoedig mogelijk beantwoorden.')
        }else{
            alert('Wij verzoeken u de verplichte velden in te vullen.');
        }
    }
}
