﻿/// <reference path="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.4-vsdoc.js" />

$(document).ready(function () {
    if (window.location.hash.indexOf(".shtml") > -1) History.replaceState(null, null, window.location.hash.replace(/^.*#/, ''));
    $("#loader").css('visibility', 'visible');
    for (var i = 2; i < 5; i++)
        $('#imgrotator .images').append('<img src="../photos/rotator/0' + i + '.jpg" />');
    for (var i = 1; i < 3; i++)
        $('#imgrotator .images').append('<img src="../photos/rotator/s0' + i + '.jpg" />');
    for (var i = 1; i < 4; i++)
        $('#imgrotator .images').append('<img src="../photos/rotator/e0' + i + '.jpg" />');
    $(".gallery .images a,.thumbnails a, .thumbnail").slimbox();
});

$(window).load(function () {

    $('#imgrotator').fadeIn(750, function () {

        $('#imgrotator .images').fadeIn(2500, function () {

            $(this).cycle({
                fx: 'fade',
                speed: 2500,
                sync: 1,
                timeout: 6000,
                random: 1
            });
            $('#imgrotator .loading').hide();
        });

    });

    $(".gallery").animate({ height: 350 }, 500, function () {
        $('.gallery .images,.gallery .button').fadeIn(250);
        bindGallery();
    });
    $("#loader").css('visibility', '');

});

function bindGallery() {
    $('.gallery .button').click(function () {

        $('.gallery .button').removeClass("disabled");
        var v = $(this).hasClass('top') ? "-=" : "+=";
        var $bt = $('.gallery .button.top'), $bb = $('.gallery .button.bottom');

        $('.gallery .images').scrollTo(v + "335px", 450,
            { easing: 'swing',
                onAfter: function () {

                    var h = $(this)[0].scrollHeight, t = $(this).scrollTop(), o = $(this).outerHeight();

                    if (t == 0) $bt.addClass("disabled");
                    if (h - t == o) $bb.addClass("disabled");
                }
            });

    });
}

function loadingComplete() {
    $(".gallery .images a,.thumbnails a,.thumbnail").slimbox();
    bindGallery();
    $("html").removeClass("js");
}

(function (window, undefined) {

    var 
		History = window.History,
		$ = window.jQuery,
		document = window.document;

    if (!History.enabled) {
        return false;
    }

    $(function () {
        var 
			contentSelector = '#content,article:first,.article:first,.post:first',
			$content = $(contentSelector).filter(':first'),
			contentNode = $content.get(0),
			$menu = $('#menu,#nav,nav:first,.nav:first').filter(':first'),
			activeClass = 'current',
			activeSelector = '.current',
			menuChildrenSelector = '> a,> li,> ul > li',

			$body = $(document.body),
			rootUrl = History.getBaseUrl(),
			scrollOptions = {
			    duration: 800,
			    easing: 'swing'
			};


        if ($content.length === 0) {
            $content = $body;
        }


        $.expr[':'].internal = function (obj, index, meta, stack) {

            var $this = $(obj),
				url = $this.attr('href') || '',
				isInternalLink;


            isInternalLink = url.substring(0, rootUrl.length) === rootUrl || url.indexOf(':') === -1;

            return isInternalLink;
        };

        var documentHtml = function (html) {
            var result = String(html)
				.replace(/<\!DOCTYPE[^>]*>/i, '')
				.replace(/<(html|head|body|title|meta|script)([\s\>])/gi, '<div class="document-$1"$2')
				.replace(/<\/(html|head|body|title|meta|script)\>/gi, '</div>')
			;

            return result;
        };

        $.fn.ajaxify = function () {
            var $this = $(this);
            $this.find('a:internal:not(.no-ajaxy)[href*=".shtml"]').click(function (event) {
                var 
					$this = $(this),
					url = $this.attr('href'),
					title = $this.attr('title') || null;

                if (event.which == 2 || event.metaKey) { return true; }

                History.pushState(null, title, url);
                event.preventDefault();
                return false;
            });
            return $this;
        };

        $body.ajaxify();

        $(window).bind('statechange', function () {
            var 
				State = History.getState(),
				url = State.url,
				relativeUrl = url.replace(rootUrl, '');

            $body.addClass('loading');

            $content.animate({ opacity: 0 }, 250);

            $.ajax({
                url: url,
                success: function (data, textStatus, jqXHR) {
                    var 
						$data = $(documentHtml(data)),
						$dataBody = $data.find('.document-body:first'),
						$dataContent = $dataBody.find(contentSelector).filter(':first'),
						$menuChildren, contentHtml, $scripts;

                    $scripts = $dataContent.find('.document-script');
                    if ($scripts.length) {
                        $scripts.detach();
                    }

                    contentHtml = $dataContent.html() || $data.html();
                    if (!contentHtml) {
                        document.location.href = url;
                        return false;
                    }

                    $menuChildren = $menu.find(menuChildrenSelector);
                    $menuChildren.filter(activeSelector).removeClass(activeClass);
                    $menuChildren = $menuChildren.filter('a[href^="' + relativeUrl + '"],a[href^="/' + relativeUrl + '"],a[href^="' + url + '"]');
                    if ($menuChildren.length === 1) { $menuChildren.addClass(activeClass); }

                    $content.stop(false, false);
                    $content.html(contentHtml).ajaxify().animate({ opacity: 1 }, 125); /* you could fade in here if you'd like */
                    loadingComplete();

                    document.title = $data.find('.document-title:first').text();
                    try {
                        document.getElementsByTagName('title')[0].innerHTML = document.title.replace('<', '&lt;').replace('>', '&gt;').replace(' & ', ' &amp; ');
                    }
                    catch (Exception) { }

                    $scripts.each(function () {
                        var $script = $(this), scriptText = $script.html(), scriptNode = document.createElement('script');
                        scriptNode.appendChild(document.createTextNode(scriptText));
                        contentNode.appendChild(scriptNode);
                    });

                    if ($body.ScrollTo || false) { $body.ScrollTo(scrollOptions); } /* http://balupton.com/projects/jquery-scrollto */
                    $body.removeClass('loading');

                    if (typeof window.pageTracker !== 'undefined') {
                        window.pageTracker._trackPageview(relativeUrl);
                    }

                    if (typeof window.reinvigorate !== 'undefined' && typeof window.reinvigorate.ajax_track !== 'undefined') {
                        reinvigorate.ajax_track(url);
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    document.location.href = url;
                    return false;
                }
            }); // end ajax

        }); // end onStateChange

    }); // end onDomLoad

})(window); // end closure
