﻿var dd = {
    init: function () {
        this.tools.originalFontSize = $('#content').css('font-size');
        $('#text-reset').click(function () {
            return dd.tools.resetFont();
        });
        $('#text-increase').click(function () {
            return dd.tools.increaseFont();
        });
        $('#text-decrease').click(function () {
            return dd.tools.decreaseFont();
        });
        $('#text-print').click(function () {
            return dd.tools.printPage();
        });
        $('#text-facebook').click(function () {
            return dd.tools.facebookShare();
        });
        if (!window.clipboardData) {
            $('#text-copy').parent().remove();
        }
        $('#text-mail').click(function () {
            if ($('#form-mail').is(':hidden')) {
                var totalHeight = $(this).height() + $('#form-mail').height() + 30;
                $(this).parent().animate({
                    height: totalHeight,
                    width: ($('#form-mail').width() - 20)
                },
                1500,
                function () {
                    $('#form-mail').fadeIn('1000');
                });
            }
            else {
                $(this).parent().animate({
                    height: $(this).height(),
                    width: ($(this).width() + 20)
                },
                2000,
                function () {
                    $('#form-mail').fadeOut('1000');
                    $('#form-mail input[type="text"], #form-mail textarea').val('');
                });
            }
            return false;
        });
    },
    slideShow: {
        images: new Array(),
        imageCache: new Array(),
        currentIndex: 0,
        maxIndex: function () {
            return (this.images.length - 1);
        },
        addImage: function (u, t) {
            this.images.push({ url: u, text: t });
            var cacheImage = document.createElement('img');
            cacheImage.src = u;
            this.imageCache.push(cacheImage);
        },
        navigate: function (direction) {
            if (direction === 'last') {
                this.navigateTo(this.maxIndex());
                return;
            }
            if (direction === 'first') {
                this.navigateTo(0);
                return;
            }
            if (direction === 'next') {
                if (this.currentIndex === this.maxIndex()) {
                    this.navigateTo(0);
                    return;
                } else {
                    this.navigateTo(this.currentIndex + 1);
                    return;
                }
            }
            if (direction === 'previous') {
                if (dd.slideShow.currentIndex === 0) {
                    this.navigateTo(this.maxIndex());
                    return;
                } else {
                    this.navigateTo(this.currentIndex - 1);
                    return;
                }
            }
        },
        navigateTo: function (index) {
            if (index >= 0 && index <= (this.maxIndex())) {
                var _slideShowCurrent = $('#slideShowCurrent');
                var _slideShowImage = $('#slideShowImage');
                var _slideShowText = $('#slideShowText');
                this.currentIndex = index;
                _slideShowCurrent.html(index + 1);
                _slideShowText.html(this.images[index].text);
                var text = _slideShowText.html();
                _slideShowImage.attr('src', this.images[index].url);
                _slideShowImage.attr('title', text);
                _slideShowImage.attr('alt', text);
            }
            return;
        }
    },
    tools: {
        originalFontSize: 0,
        popup: function (id, status, toolbar, location, menubar, directories, resizable, scrollbars, width, height, url) {
            var features;
            features += ((status === true) ? 'status=1,' : 'status=0,');
            features += ((toolbar === true) ? 'toolbar=1,' : 'toolbar=0,');
            features += ((location === true) ? 'location=1,' : 'location=0,');
            features += ((menubar === true) ? 'menubar=1,' : 'menubar=0,');
            features += ((directories === true) ? 'directories=1,' : 'directories=0,');
            features += ((resizable === true) ? 'resizable=1,' : 'resizable=0,');
            features += ((scrollbars === true) ? 'scrollbars=1,' : 'scrollbars=0,');
            features += 'width=' + width + ',';
            features += 'height=' + height;
            return window.open(url, id, features);
        },
        facebookShare: function () {
            this.popup('facebook', false, false, false, true, false, true, true, 800, 800, 'http://www.facebook.com/share.php?u=' + escape(window.location.href));
            return false;
        },
        babyImage: function (image) {
            this.popup('baby', false, false, false, false, false, false, false, 475, 323, image);
            return false;
        },
        increaseFont: function () {
            /*var currentFontSize = $('#content').css('font-size');
            var currentFontSizeNumeric = parseFloat(currentFontSize, 10) * 1.2;*/
            $('#content').css('font-size', 16);
            return false;
        },
        decreaseFont: function () {
            /*var currentFontSize = $('#content').css('font-size');
            var currentFontSizeNumeric = parseFloat(currentFontSize, 10) * 0.8;*/
            $('#content').css('font-size', 9);
            return false;
        },
        resetFont: function () {
            $('#content').css('font-size', 12);
            return false;
        },
        copyLink: function (successMessage) {
            window.clipboardData.setData('Text', window.location.href);
            alert(successMessage);
            return false;
        },
        printPage: function () {
            this.popup('print', false, false, false, false, false, false, false, 800, 600, window.location.href + '?print=true');
            return false;
        },
        openContent: function (url, width, height) {
            this.popup('partial', false, false, false, false, false, false, true, width, height, url + '?partial=true');
            return false;
        }
    }
};

$(document).ready(function() {
    dd.init();
    $('.tabset a').click(function() {
        var _this = $(this);
        var _rel = $('#' + _this.attr('rel'));
        _this.addClass('active').parent().siblings().find('.active').removeClass('active');
        _rel.show().siblings('.tab-content').hide();
        return false;
    });
});

