// JavaScript Document PS-Producties

// Dom Ready
$(document).ready(function() {
    if ($('#nieuwsbrief').length > 0) {
        ps.swap.init();
        $('#nieuwsbrief').click(function() { ps.nieuwsBrief.show() });
        $('#sluitKnop').click(function() { ps.nieuwsBrief.show() });
    }
    if ($('#googleMaps').length > 0)
        ps.contact.initMap();

    $('#zoeken').click(function() { ps.search($('#zoekenInput')[0].value); });

    var fancy = $('div.content a.newsImg');
    if(fancy.length > 0) {
        fancy.fancybox(); 
    }

    try {
        var pageTracker = _gat._getTracker("UA-7841579-1");
        pageTracker._trackPageview();
    } 
    catch (err) { alert(err); };
});

// ps
this.ps = {
    // SwapBox 	
    swap: {
        normalSrc: 'src/css/img/ps3.png',
        hoverSrc: 'src/css/img/ps2.png',
        timer: null,
        activeEl: null,
        init: function() {
            var anc = $("a.swapLink");
            anc.click(function() {
                var tar = $(this).attr('name');
                $('.caseContent').css('display', 'none');
                ps.swap.loadId(tar);
            });
            ps.swap.loadId($("a.swapLink:first").attr('name'));
            ps.swap.loop();
        },
        loadId: function(a) {
            var el = $('#' + a);
            el.fadeIn("slow");
            $('.swapLink').removeClass('swap-active');
            $('.' + a).addClass('swap-active');
            clearTimeout(ps.swap.timer);
            ps.swap.timer = setTimeout(function() {
                ps.swap.loop(el);
            }, 6000);

        },
        loop: function(el) {
            if (el) {
                var next = el.next().attr('id');
                var first = $('.caseContent:first').attr('id');
                if (next) {
                    $('.caseContent').animate({
                        top: "120px"
                    }, 500, null, function() {
                        el.hide();
                        $('.caseContent').css("top", "0px");
                        ps.swap.loadId(next);
                    });
                }
                else {
                    $('.caseContent').animate({
                        top: "120px"
                    }, 500, null, function() {
                        el.hide();
                        $('.caseContent').css("top", "0px");
                        ps.swap.loadId(first);
                    });
                }
            }
        }
    }
    // NieuwsBrief
    , nieuwsBrief: {
        nieuwsBox: 'nieuwsBox',
        nieuwsbriefBox: 'nieuwsbriefBox',
        nieuwsbriefBoxActive: true,
        show: function() {
            if (ps.nieuwsBrief.nieuwsbriefBoxActive == false) {
                ps.nieuwsBrief.up(ps.nieuwsBrief.nieuwsbriefBox, ps.nieuwsBrief.nieuwsBox);
                ps.nieuwsBrief.nieuwsbriefBoxActive = true;
            }
            else {
                ps.nieuwsBrief.up(ps.nieuwsBrief.nieuwsBox, ps.nieuwsBrief.nieuwsbriefBox);
                ps.nieuwsBrief.nieuwsbriefBoxActive = false;
            }
        },
        up: function(el1, el2) {
            $('#' + el1).animate({
                top: "-170px"
            }, 800, null, function() { ps.nieuwsBrief.down(el2) });
        },
        down: function(el) {
            $('#' + el).animate({
                top: "33px"
            }, 800);
        }
        , send: function(o) {
            $.ajax({
                type: 'POST'
                , url: 'WebService.asmx/VerstuurNieuwsbrief'
                , data: w4u.util.JSON.encode({ validator: o, hash: o.hash })
                , contentType: 'application/json; charset=utf-8'
                , dataType: 'json'
                , success: function(result) {
                    alert(result.d);
                    o.reset();
                    ps.nieuwsBrief.show();

                } .createDelegate(this)
                , error: function(xhr, result) {
                    //TODO: general handler
                    alert('mislukt');
                }
            });
        }
    }
    // Maps
    , contact: {
        initMap: function() {
            if (GBrowserIsCompatible()) {
                var map = new GMap2(document.getElementById("googleMaps"));
                map.addControl(new GSmallMapControl());
                map.setCenter(new GLatLng(53.206752, 5.798150), 13);
                var icon = new GIcon();
                icon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
                icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
                icon.iconSize = new GSize(12, 20);
                icon.shadowSize = new GSize(22, 20);
                icon.iconAnchor = new GPoint(6, 20);
                icon.infoWindowAnchor = new GPoint(5, 1);
                var point = new GLatLng(53.206752, 5.798150);
                map.addOverlay(new GMarker(point, icon));
            }
        }
        , send: function(o) {
            $.ajax({
                type: 'POST'
                , url: 'WebService.asmx/VerstuurEmail'
                , data: w4u.util.JSON.encode({ validator: o, hash: o.hash })
                , contentType: 'application/json; charset=utf-8'
                , dataType: 'json'
                , success: function(result) {
                    alert(result.d);
                    o.reset();

                } .createDelegate(this)
                , error: function(xhr, result) {
                    //TODO: general handler
                    alert('mislukt');
                }
            });
        }
    }
    , search: function(s) {
        var zc = $('#zoeken_contents');
        zc.html('<p>U zocht naar: "' + s + '"<a class="close" href="javascript:;" onclick="$(\'#zoeken_results\').hide();">sluiten</a></p>');
        $('#zoeken_results').show();
        $.ajax({
            type: 'POST'
                , url: 'WebService.asmx/Search'
                , data: w4u.util.JSON.encode({ search: s })
                , contentType: 'application/json; charset=utf-8'
                , dataType: 'json'
                , success: function(result) {
                    var json = eval('(' + result.d + ')');

                    var l = json.results.length;
                    var html = '<p>' + l + ' resultaten:</p>';
                    if (l > 0) {
                        html += '<ul>';
                        for (var i = 0; i < l; i++) {
                            var item = json.results[i];
                            var url = '?id=pages/' + item.pId;
                            if (item.cId && item.rId)
                                url += '&cId=' + item.cId + '&rId=' + item.rId;

                            html += '<li><a href="' + url + '">' + item.name + '</a></li>';
                        }
                        html += '</ul>';
                    }
                    zc.append(html);
                } .createDelegate(this)
                , error: function(xhr, result) {
                    //TODO: general handler
                    alert('mislukt');
                }
        });
    }
    , getRecord: function(collectionId, recordId, type) {
        $.ajax({
            type: 'POST'
            , url: 'WebService.asmx/GetRecord'
            , data: w4u.util.JSON.encode({ collectionId: collectionId, recordId: recordId })
            , contentType: 'application/json; charset=utf-8'
            , dataType: 'json'
            , success: function(result) {
                var json = eval('(' + result.d + ')');
                switch (type) {
                    case 'Cases':
                    case 'Diensten':
                    case 'Opdrachtgevers':
                    case 'Video':
                        var header = $('#contentHeader')[0];
                        header.innerHTML = json.wxr.name;

                        var div = $('#contentDiv')[0];
                        div.innerHTML = json.wxr.algemeen.editor.value.decode()

                        var img = $('#caseFoto')[0];
                        img.src = json.wxr.algemeen.afbeelding.item ? 'Image.axd?f=cms/data/' + json.wxr.algemeen.afbeelding.item.id.value + '&v=1&o=2|false|155' : '';
                        img.alt = json.wxr.name;
                        img.title = json.wxr.name;
                        break;

                        alert('Diensten');
                        break;
                    case 'Team':
                        var header = $('#contentHeader')[0];
                        header.innerHTML = json.wxr.name;

                        var div = $('#contentDiv')[0];
                        div.innerHTML = '"' + json.wxr.algemeen.editor.value.decode() + '"'

                        var span = $('#rechtsHolder')[0];
                        span.innerHTML = '<img class="imgWerknemer" src="Image.axd?f=cms/data/' + json.wxr.algemeen.afbeelding.item.id.value + '&v=1&o=2|false|80" alt="' + json.wxr.name + '" id="werknemerFoto" align="left"><span id="contentRechtsDiv"><p><b><font color="#81106b">' + json.wxr.name + '</font></b></p><p><b><font color="#81106b">Leeftijd:</font> ' + json.wxr.profiel.leeftijd.value + '</b><br><b><font color="#81106b">Functie:</font> ' + json.wxr.profiel.functie.value + '</b><br /><b><font color="#81106b">Email:</font> <a href="mailto:' + json.wxr.profiel.email.value + '">' + json.wxr.profiel.email.value + '</a></b></p></span>';
                        break;
                }

            } .createDelegate(this)
            , error: function(xhr, result) {
                //TODO: general handler
                alert('mislukt');
            }
        });
    }
}






