var sounds = {
    down: 2,
    up: 3,
    left: 4,
    right: 5,
    error: 14,
    phone: 17
}

$(function() {
    if ($('#featurebuttons').length > 0) {
        var animateF = function() {
            var $p = $('#featurebuttons img');
            $p.animate({marginLeft: '4px'}, 50, function() {
                $p.animate({marginLeft: '-4px'}, 50, function() {
                    $p.animate({marginLeft: '4px'}, 50, function() {
                        $p.animate({marginLeft: '-4px'}, 50, function() {
                            $p.animate({marginLeft: '2px'}, 50, function() {
                                $p.animate({marginLeft: '-2px'}, 50, function() {
                                    $p.animate({marginLeft: '0px'}, 100, function() {
                                    });
                                });
                            });
                        });
                    });
                });
            });
        }
        window.setTimeout(function() {
            puzzle.playSound(sounds.error);
            animateF();
            //window.setTimeout(animateF, 500);
        }, 2000);
    }

    if ($('#puzzle').length > 0) {
        puzzle.init();
        $('.biozoom').fancybox({
            padding: 5,
            margin: 5, 
            cyclic: true, 
            transitionIn: 'elastic', 
            transitionOut: 'elastic',
            easingIn: 'easeOutBack',
            overlayOpacity: 0.8,
            overlayColor: '#000'
        });
    }

    if ($('#contentBlog').length > 0) {
        blog.init();
    }

    if ($.prototype.fancybox) {
        $('.ContentItemGalleryImage .thumb').fancybox({
            padding: 5,
            margin: 5, 
            cyclic: true, 
            transitionIn: 'elastic', 
            transitionOut: 'elastic',
            easingIn: 'easeOutBack',
            overlayOpacity: 0.8,
            overlayColor: '#000',
            titlePosition: 'over',
            titleFormat: function(title, currentArray, currentIndex, currentOpts) {
                var html = $('#' + title).html();
                if (html.length > 10) return '<div id="fancybox-title-over">' + html + '</div>'; else return '';
            }
        });
    }

    $('.ContentItemContentGroupAppointment .FormFieldBody a').attr('target', '_blank');
/*
    if ($('.ContentItemContentGroupAppointment').length > 0) {
        $('.ContentItemContentGroupAppointment').css('display', 'block');
        var $items = $('.ContentItemContentGroupAppointment .ContentContainerGroupTitle, .ContentItemAppointment');
        $items.css('display', 'none')
        var i = $items.length - 1;
        var interval = (3000 / $items.length);
        window.setInterval(function() {
            $items.eq(i).show();
            i--;
        }, interval);
    }
*/
});

var blog = {
    setMargins: function() {
        var count = 1;
        $('.ContentItemWeblogItem').each(function() {
            switch (count) {
                case 2:
                    $(this).animate({marginTop: '38px'}, 1000);
                    break;
                case 3:
                    $(this).animate({marginTop: '153px'}, 1000);
                    break;
                case 4:
                    $(this).animate({marginTop: '10px'}, 1000);
                    break;
            }
            count++;
        });
    },
    
    collapse: function(onReady) {
        $('.ContentItemWeblogItem').animate({marginTop: '0px'}, 500);
        $('.ContentItemWeblogItem:not(.Active)').slideUp(500, onReady);
    },
    
    restore: function() {
        $('.ContentItemWeblogItem:not(.Active)').slideDown(500, function() {
            blog.setMargins();
        });
    },
    
    init: function() {
        blog.setMargins();
        $('.ContentItemWeblogItem .BlogSummary, .ContentItemWeblogItem .BlogDate').click(function() {
            var $item = $(this).closest('.ContentItem').addClass('Active').css('visibility', 'hidden');
            window.setTimeout(function() { $item.css('visibility', ''); }, 50);
            blog.collapse(function() {
                $item.find('.BlogSummary').css('display', 'none');
                $item.find('.BlogDate').css('display', 'none');
                $item.animate({width: '697px'}, 200).css('height', 'auto');
                $item.find('.BlogDetail').show(200);
            });
            return false;
        });
        $('.ContentItemWeblogItem .FormFieldTerug a').click(function() {
            var $item = $(this).closest('.ContentItem').removeClass('Active');
            $item.animate({width: '146px', height: '156px'}, 200, function() {
                $item.find('.BlogDetail').css('display', 'none');
                $item.find('.BlogDate').css('display', 'block');
                $item.find('.BlogSummary').css('display', 'block');
                blog.restore();
            });
            return false;
        });
    }
}


var puzzle = {
    active: '',
    empty: 9,
    pieces: [],
    sound: null,

    siblings: function(num) {
        var result = [];
        // Horizontal
        if (num == 1 || num == 2 || num == 4 || num == 5 | num == 7 || num == 8) result[result.length] = (num + 1);
        if (num == 2 || num == 3 || num == 5 || num == 6 | num == 8 || num == 9) result[result.length] = (num - 1);
        // Vertical
        if (num == 1 || num == 2 || num == 3 || num == 4 | num == 5 || num == 6) result[result.length] = (num + 3);
        if (num == 4 || num == 5 || num == 6 || num == 7 | num == 8 || num == 9) result[result.length] = (num - 3);
        return result;
    },

    getx: function(num) {
        if (num == 1 || num == 4 || num == 7) return 0;
        if (num == 2 || num == 5 || num == 8) return 166;
        if (num == 3 || num == 6 || num == 9) return 332;
    },

    gety: function(num) {
        if (num == 1 || num == 2 || num == 3) return 0;
        if (num == 4 || num == 5 || num == 6) return 166;
        if (num == 7 || num == 8 || num == 9) return 332;
    },
    
    getDirection: function(oldPosition, newPosition) {
        var old = puzzle.getx(oldPosition);
        var nw = puzzle.getx(newPosition);
        if (old == nw) {
            old = puzzle.gety(oldPosition);
            nw = puzzle.gety(newPosition);
            if (nw < old) return 'up'; else return 'down';
        } else {
            if (nw < old) return 'left'; else return 'right';
        }
    },

    contains: function(items, num) {
        var result = false;
        $.each(items, function() { if (this == num) result = true; });
        return result;
    },

    canmove: function(num) {
        return puzzle.contains(puzzle.siblings(num), puzzle.empty);
    },

    getpiece: function(num) {
        var result;
        $('#puzzle .piece').each(function() { if ($(this).data('piece').position == num) result = $(this).data('piece'); });
        return result;
    },

    move: function(piece) {
        if (puzzle.canmove(piece.position)) {
            puzzle.playSound(sounds[puzzle.getDirection(piece.position, puzzle.empty)]);
            $(piece.piece).animate({left: puzzle.getx(puzzle.empty) + 'px', top: puzzle.gety(puzzle.empty) + 'px'}, 100);
            var tmp = piece.position;
            piece.position = puzzle.empty;
            puzzle.empty = tmp;
        } else {
            puzzle.playSound(sounds.error);
            var $p = $(piece.piece);
            var x = puzzle.getx(piece.position);
            $p.animate({left: (x+2) + 'px'}, 50, function() {
                $p.animate({left: (x-2) + 'px'}, 50, function() {
                    $p.animate({left: (x+1) + 'px'}, 50, function() {
                        $p.animate({left: (x-1) + 'px'}, 50, function() {
                            $p.animate({left: x + 'px'}, 100, function() {
                            });
                        });
                    });
                });
            });
        }
    },

    lastRandom: 0,
    randomMove: function() {
        var siblings = puzzle.siblings(puzzle.empty);
        var r = siblings[Math.floor(siblings.length * Math.random())];
        while (r == puzzle.lastRandom) r = siblings[Math.floor(siblings.length * Math.random())];
        puzzle.lastRandom = puzzle.empty;
        puzzle.move(puzzle.getpiece(r));
    },
    
    showDetail: function(newId) {
        if (newId != puzzle.active) {
            puzzle.active = newId;
            var reverse = (newId == 'detail0');
            var toPx = (reverse ? '-499px' : '499px');
            var fromPx = (reverse ? '499px' : '-499px');
            $('.puzzledetailactive')
                .animate({left: toPx}, 200)
                .removeClass('puzzledetailactive');
            $('#' + newId)
                .css('display', 'block')
                .css('left', fromPx)
                .animate({left: '0px'}, 200)
                .addClass('puzzledetailactive');
        }
    },

    playSound: function(frame) {
        var m = (document['schuifpuzzel'] ? document['schuifpuzzel'] : window['schuifpuzzel']);
        if (m) { if (m.playSound) { m.playSound(0); m.playSound(frame); } }
    },

    init: function() {
        var i = 1; var piece = null;
        $('#puzzle .piece').each(function() {
            piece = puzzle.pieces[puzzle.pieces.length] = { piece: this, position: i };
            $(this).data('piece', piece).css('left', puzzle.getx(i) + 'px').css('top', puzzle.gety(i) + 'px').css('position', 'absolute');
            i++;
        }).click(function() {
            puzzle.showDetail(this.id.replace('puzzle', 'detail'));
            puzzle.move($(this).data('piece'));
            return false;
        });
        i = 0;
        var interval = window.setInterval(function() {
            puzzle.randomMove();
            i += 1;
            if (i > 20) window.clearInterval(interval);
        }, 150);
        puzzle.showDetail('detail0');
    }
}

