﻿/// <reference path="jquery-1.5.1-vsdoc.js" />
// remap jQuery to $
(function ($) {
    $(function () {

        // active nav links
        $('nav a').each(function () {
            if ($(this).attr('href').length > 1 && document.location.pathname.indexOf($(this).attr('href')) === 0) {
                $(this).addClass('active');
            }
        });

        $("#archivelist").treeview({
            collapsed: true,
            persist: 'location'
        });

        // remove ignore field from contact form
        if ($('#Ig').length) {
            $('#Ig').parent().hide().prev('div').hide();
        }

        $("#archives").treeview({
            collapsed: true,
            persist: 'location'
        });

        // fix bottom content area heights
        function equalHeight(group) {
            var tallest = 0;
            group.each(function () {
                var thisHeight = $(this).height();
                if (thisHeight > tallest) {
                    tallest = thisHeight;
                }
            });
            group.height(tallest);
        }

        equalHeight($('.cpg'));

        $('.sm-tw a').click(function (e) {
            e.preventDefault();
            u = location.href;
            window.open('http://twitter.com/share?url=' + encodeURIComponent(u), 'sharer', 'toolbar=0,status=0,width=626,height=436');
        });

        $('.sm-fb a').click(function (e) {
            e.preventDefault();
            u = location.href;
            t = document.title;
            window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(u) + '&t=' + encodeURIComponent(t), 'sharer', 'toolbar=0,status=0,width=626,height=436');
        });

        var $cookieName = 'pfw-fs';
        var txt = $('article');
        var oFont = txt.css('fontSize');

        if ($.cookie($cookieName)) {
            var $kFont = $.cookie($cookieName);
            txt.css('fontSize', $kFont);
        } else {
            $.cookie($cookieName, oFont);
        }

        $('.sm-fs a').bind('click', function (e) {
            e.preventDefault();
            var cFont = txt.css('fontSize');
            var nSize = parseFloat(cFont, 10);
            var unit = cFont.slice(-2);

            if (this.id == 'large') {
                nSize *= 1.2;
            } else if (this.id == 'small') {
                nSize /= 1.2;
            }
            txt.css('fontSize', nSize + unit);
            $.cookie($cookieName, nSize + unit);
        });

    });
})(jQuery);

/**
* jQuery Cookie plugin
*
* Copyright (c) 2010 Klaus Hartl (stilbuero.de)
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/
jQuery.cookie = function (key, value, options) {

    // key and at least value given, set cookie...
    if (arguments.length > 1 && String(value) !== "[object Object]") {
        options = jQuery.extend({}, options);

        if (value === null || value === undefined) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }

        value = String(value);

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? value : encodeURIComponent(value),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};
