﻿/// <reference path="jquery-1.3.2.js"/>
/// <reference path="wdcvshared.js"/>

/*
www.webdevelopercv.com
Copyright (c) 2010 Evgeny Neumerzhitskiy.
Please feel free to use any part of my code. If you put a reference to me I would be grateful, but it's not required.*/

jQuery.preloadImages = function() {
    for (var i = 0; i < arguments.length; i++) {
        jQuery("<img>").attr("src", "images/" + arguments[i]);
    }
};

jQuery.preloadImages("gradient_home.png", "gradient_works.png", "gradient_resume.png", "gradient_hireMe.png", "magnifier.gif", "ajax-loader.gif");
$("<img>").load(function() { nosyMe.nosyLoaded(true, false); }).attr("src", "images/evgeny_small.png");
$("<img>").load(function() { nosyMe.nosyLoaded(false, true); }).attr("src", "images/eye_small.png");

var dnDev = {
    $navbar: null,
    $elementToBounce: null,
    $content: null,
    $contentSlider: null,
    $header: null,
    $imageToPreview: null,
    $code: null,
    $outer: null,
    $share: null,
    previewOpened: false,
    previewMouseCoord: null,
    hoverTimout: 50,
    navBounceAmplitude: 30, /*the bounce amplitude in pixels for the navigation elements*/
    hoverTimerID: null,
    bounceSettings: {
        home: 3800, /*the bounce speed for the navigation elements*/
        works: 3000,
        resume: 2000,
        hireMe: 4000
    },
    bounce: function(element) {
        if (element === undefined) {
            element = $(this);
        }

        var vTime = dnDev.bounceSettings[element.attr("id")];
        var topDefault = (dnDev.$navbar.outerHeight() - element.outerHeight()) / 2;
        var bounceTopPosition = topDefault - dnDev.navBounceAmplitude;

        if (element.position().top < topDefault) {
            bounceTopPosition = topDefault + dnDev.navBounceAmplitude;
        }
        var vDistance = Math.abs(element.position().top - bounceTopPosition);
        vTime = Math.ceil(vDistance * vTime / (2 * dnDev.navBounceAmplitude));

        element.animate({ top: bounceTopPosition }, vTime, 'linear', dnDev.bounce);

    },
    onNavHover: function() {
        var $element = this.$elementToBounce;
        this.hoverTimerID = null;
        $element.stop(true);
        dnDev.$navbar.children("div:not(.bounce)").addClass("bounce").each(function() {
            dnDev.bounce($(this));
            dnDev.bindButtonHover($(this));
        });

        $element.removeClass("bounce").unbind('mouseenter mouseleave');
        if ($element.attr('id') == 'home') {
            dnDev.bindPhotoHover();
        } else {
            this.$navbar.children("#home").unbind('mouseleave');
        }
        //now change the content
        this.slideContent($element.attr('id'));
    },
    slideContent: function(pageName, slideTime) {
        var contentPanel = pageName + "Content";

        var $button = this.$navbar.children("#" + pageName);
        this.$header.css("borderColor", $button.css("borderLeftColor"));
        dnDev.$code.css("background-image", "url('images/gradient_" + pageName + ".png')");
        var $headerH1 = this.$header.children("h1");
        if (pageName == 'hireMe') {
            headerMargin = 0;
            this.$navbar.children("#home").removeClass("bounce").stop(true).animate({ top: -30 }, 300, null);
            this.$header.css("background", "Transparent url('images/gradient_hireMe.png') -50 -50 no-repeat ");
            $headerH1.hide();
            this.$header.children("#hireMeHeader").show().find('a').each(wdcv.tvgtulutnjgorkyqcmo);
        } else {
            $headerH1.show();
            this.$header.children("#hireMeHeader").hide();
        }

        if (slideTime === undefined) {
            slideTime = 500;
        }
        dnDev.$share.css("z-index", 0);
        var $contentPanel = this.$contentSlider.children("#" + contentPanel);
        this.updateViewSize($contentPanel.outerHeight(true));
        var $copyrightText = this.$contentSlider.find("#copyright");
        $contentPanel.append($copyrightText.remove());
        $copyrightText.children('.Cmuercehot').mouseenter(function() {
            $(this).unbind('mouseenter').each(wdcv.tvgtulutnjgorkyqcmo);
        });
        this.$contentSlider.stop(true);
        this.$contentSlider.animate({ left: 60 - $contentPanel.position().left + "px" }, slideTime, null, function() { dnDev.$share.css("z-index", 2); });

    },
    updateViewSize: function(contentHeight) {
        var verticalMargin = 65;
        if (contentHeight === undefined) {
            contentHeight = dnDev.$contentSlider.children('#homeContent').outerHeight(true);
        }
        dnDev.$content.height(contentHeight + verticalMargin);
        dnDev.$code.css({ height: contentHeight + dnDev.$content.offset().top + verticalMargin });
    },
    bindPhotoHover: function() {
        this.$navbar.children("#home").hover(function() {
            $(this).stop(true);
            $(this).animate({ top: -50 }, 300, null);
        }, function() {
            $(this).stop(true);
            $(this).animate({ top: -85 }, 300, null);
        }
        );
    },
    bindButtonHover: function($button) {
        $button.hover(function() {
            dnDev.$elementToBounce = $button;
            if (dnDev.hoverTimerID !== null) {
                clearTimeout(dnDev.hoverTimerID);
                dnDev.hoverTimerID = null;
            }
            dnDev.hoverTimerID = setTimeout("dnDev.onNavHover()", dnDev.hoverTimout);
        }, function() {
            if (dnDev.hoverTimerID !== null) {
                clearTimeout(dnDev.hoverTimerID);
                dnDev.hoverTimerID = null;
            }
            dnDev.$elementToBounce = null;
        });
    },
    previewLoaded: function() {
        if (dnDev.$imageToPreview === null) {
            return;
        }
        dnDev.$imageToPreview.find("~ .ajaxLoaderGif").remove();
        var originalWidth = $(this).width();
        var originalHeight = $(this).height();
        var originalWidthOuter = $(this).outerWidth(true);
        var originalHeightOuter = $(this).outerHeight(true);

        var resizeProportion = 1;
        if (originalHeightOuter > $(window).height() * 0.95) {
            resizeProportion = $(window).height() * 0.95 / originalHeightOuter;
        }

        originalHeight *= resizeProportion;
        originalWidth *= resizeProportion;
        originalHeightOuter *= resizeProportion;
        originalWidthOuter *= resizeProportion;

        var previewTop = Math.floor(($(window).height() - originalHeightOuter) / 2 + $(window).scrollTop());
        var previewLeft = Math.floor(($(window).width() - originalWidthOuter) / 2 + $(window).scrollLeft());

        $(this).css({ left: dnDev.$imageToPreview.offset().left, top: dnDev.$imageToPreview.offset().top,
            width: dnDev.$imageToPreview.width(), height: dnDev.$imageToPreview.height()
        }).show().animate({ width: originalWidth, height: originalHeight, left: previewLeft, top: previewTop }, 500, null, function() { setTimeout("dnDev.previewOpened = true;", 500); });
    },
    hidePreview: function(e) {
        if (!dnDev.previewOpened) {
            return;
        }

        if (e !== undefined) {
            if (dnDev.previewMouseCoord === null) {
                dnDev.previewMouseCoord = e.pageX;
                return;
            }
            if (Math.abs(dnDev.previewMouseCoord - e.pageX) < 5) {
                return;
            }
        }
        dnDev.previewOpened = false;
        dnDev.previewMouseCoord = null;
        $("#previewImage").animate({ left: dnDev.$imageToPreview.offset().left,
            top: dnDev.$imageToPreview.offset().top,
            width: dnDev.$imageToPreview.width(),
            height: dnDev.$imageToPreview.height()
        }, 500, null, function() { $("#previewImage").hide(); });
    },
    showPreview: function() {
        dnDev.$imageToPreview = $(this).children("img");
        dnDev.showAjaxLoader($(this));

        var previewImageURL = dnDev.$imageToPreview.attr('src').replace('_small.jpg', '.png');
        $("#previewImage").attr('alt', dnDev.$imageToPreview.attr('alt')).removeAttr('src').css({ width: '', height: '' }).attr('src', previewImageURL);
    },
    showAjaxLoader: function($container, position) {
        var $ajaxLoaderGif = $container.children(".ajaxLoaderGif");
        if ($ajaxLoaderGif.length === 0) {
            $ajaxLoaderGif = $("<div class='ajaxLoaderGif'/>");
            $container.append($ajaxLoaderGif);
        }
        if (position === undefined) {
            position = { right: 10, top: 10 };
        }
        $ajaxLoaderGif.css(position).show();
    },
    showMagnifier: function($image, bShow) {
        var $magnifier = $image.children(".magnifier");
        if (bShow) {
            if ($magnifier.length === 0) {
                $magnifier = $("<div class='magnifier'/>");
                $image.append($magnifier);
            }
            $magnifier.css({ left: 10, top: 10 }).show();
        } else {
            if ($magnifier.length > 0) {
                $magnifier.remove();
            }
        }
    },
    colorBracket: function(text) {
        return "<span class='bracket'>" + text + "</span>";
    },
    getLT: function() {
        return dnDev.colorBracket("&lt;");
    },
    getLTEnd: function() {
        return dnDev.colorBracket("&lt;/");
    },
    getGT: function() {
        return dnDev.colorBracket("&gt;");
    },
    getGTEnd: function() {
        return dnDev.colorBracket(" /&gt;");
    },
    lineBreak: function() {
        dnDev.formatHTMLLineNumber++;
        var lineNumber = 3 - (dnDev.formatHTMLLineNumber + "").length;
        if (lineNumber < 0) {
            lineNumber = 0;
        }
        return "<br /><span class='line'>" + "&nbsp;".repeat(lineNumber) + dnDev.formatHTMLLineNumber + "</span>";
    },
    formatHTMLLineNumber: 0,
    encodeHTML: function(text) {
        var textneu = text.replace(/&/g, "&amp;");
        textneu = textneu.replace(/</g, "&lt;");
        textneu = textneu.replace(/>/g, "&gt;");
        return (textneu);
    },
    formatHTML: function(element, indent) {
        if (indent == -1) {
            return dnDev.lineBreak() + dnDev.colorBracket("&lt;!") + "<span class='element'>DOCTYPE</span><span class='attrName'> html PUBLIC</span>" + "<span class='attrValue'> &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;</span>" + dnDev.getGT() + dnDev.formatHTML(element, 0);
        }
        var indentString = "&nbsp;&nbsp;";
        if (element.nodeType == 3 && jQuery.trim(element.nodeValue).length === 0) {
            return '';
        }
        var vCodeFormatted = dnDev.lineBreak() + indentString.repeat(indent);
        var nodeName = "<span class='element'>" + element.nodeName.toLowerCase() + "</span>";
        if (element.nodeType == 3 || element.nodeType == 8) {//textNode            
            if (element.nodeType == 8) {//comment
                vCodeFormatted += "<span class='comment'>&lt;!--";
            }
            vCodeFormatted += dnDev.encodeHTML(element.nodeValue);
            if (element.nodeType == 8) {
                vCodeFormatted += "--&gt;</span>";
            }
            return vCodeFormatted;
        }
        vCodeFormatted += dnDev.getLT() + nodeName;

        //attribures
        if (element.attributes !== undefined) {
            for (var iAttr = 0; iAttr < element.attributes.length; iAttr++) {
                var attributes = element.attributes[iAttr];
                if (attributes.specified) {
                    if (attributes.name != "complete" && attributes.name != "shape") {
                        vCodeFormatted += "&nbsp;<span class='attrName'>" + attributes.name + "</span><span class='attrValue'>=&quot;" + attributes.value.replace(/&/g, "&amp;amp;") + "&quot;</span>";
                    }
                }
            }
        }

        if (element.childNodes.length > 0) {
            //child elements
            vCodeFormatted += dnDev.getGT();
            var containsTextOnly = false;
            for (var i = 0; i < element.childNodes.length; i++) {
                var childNode = element.childNodes[i];
                if (childNode.nodeType == 3 && element.childNodes.length == 1) {
                    vCodeFormatted += childNode.nodeValue;
                    containsTextOnly = true;
                } else {
                    vCodeFormatted += dnDev.formatHTML(childNode, indent + 1);
                }
            }

            if (!containsTextOnly) {
                vCodeFormatted += dnDev.lineBreak() + indentString.repeat(indent);
            }
            vCodeFormatted += dnDev.getLTEnd() + nodeName + dnDev.getGT();
        } else {
            if (element.innerHTML !== undefined && element.innerHTML.length > 0) {
                vCodeFormatted += dnDev.getGT();
                vCodeFormatted += element.innerHTML;
                vCodeFormatted += dnDev.getLTEnd() + nodeName + dnDev.getGT();

            } else {
                vCodeFormatted += dnDev.getGTEnd();
            }
        }

        return vCodeFormatted;
    },
    Torkyhot: function Torkyhot() {
        dnDev.$contentSlider.find(".Cmuercehot").mouseenter(function() {
            $(this).unbind('mouseenter').each(wdcv.tvgtulutnjgorkyqcmo);
        });
    },
    init: function() {
        this.$outer = $("#outer");
        this.$navbar = this.$outer.children("#navbar");
        this.$contentSlider = this.$outer.find("#contentSlider");
        this.$content = this.$contentSlider.parent();
        this.$header = this.$outer.children("#header");
        this.$code = $("#code");
        this.$share = this.$outer.find("#share");

        String.prototype.repeat = function(num) {
            return new Array(num + 1).join(this);
        };

        nosyMe.myInit();
        dnDev.updateViewSize();
        this.$code[0].innerHTML = dnDev.formatHTML($('html')[0], -1);
        this.bindPhotoHover();
        $('a.newWindow, area.newWindow').click(function() {
            window.open(this.href);
            return false;
        });

        this.$contentSlider.find(".worksImage").click(dnDev.showPreview).hover(function() { dnDev.showMagnifier($(this), true); }, function(e) { dnDev.showMagnifier($(this), false); });
        this.$outer.children("#previewImage").load(dnDev.previewLoaded).click(function() { dnDev.hidePreview(); }).mousemove(dnDev.hidePreview);
        this.$contentSlider.find("#dice4fun").mouseenter(function() {
            var $diceSection = $(this);
            dnDev.showAjaxLoader($diceSection, { right: 10, top: 50 });
            $diceSection.unbind('mouseenter');
            $.getScript("./js/dice4fun.js", function(data, textStatus) {
                setTimeout('$("#dice4fun .ajaxLoaderGif").remove(); diceGameEvg.init();', 500);
            });
        });
        dnDev.Torkyhot();

        this.$contentSlider.children("#resumeContent").find(".resumePrintLink").click(function() {
            var $resumePrint = $('body').children('#resumePrint');
            var $printContents = $resumePrint.children("#printContents");
            var textToPrint = dnDev.$contentSlider.children("#resumeContent").html().replace(/<h2>Resume<\/h2>/i, "");
            $printContents.html(textToPrint);
            $printContents.find('.Cmuercehot').each(wdcv.tvgtulutnjgorkyqcmo);
            $resumePrint.css("width", $(window).width() + 'px').css({ left: $(window).width() + $(window).scrollLeft(), top: $(window).height() + $(window).scrollTop() }).show().animate({ left: 0, top: 0 }, 700, null, function() {
                $(this).css("width", null);
                $(document).scrollTop(0);
                $(document).scrollLeft(0);
                dnDev.$code.hide();
                dnDev.$outer.hide();
                window.print();
            });

            $resumePrint.children("#printGoBack").click(function() {
                dnDev.$code.show();
                dnDev.$outer.show();
                $('body').children('#resumePrint').animate({ left: $(window).width(), top: $(window).height() }, 700, null, function() {
                    $(this).hide().children("#printContents").html('');
                });
                return false;
            });

            return false;
        });

        this.$contentSlider.find("#homepageResumeLink").click(function() {
            $('html,body').animate({ scrollTop: 0 }, 1000);
            dnDev.$elementToBounce = dnDev.$navbar.find("#resume");
            dnDev.onNavHover();
            return false;
        });
        this.$contentSlider.find("#homepageProjectsLink").click(function() {
            $('html,body').animate({ scrollTop: 0 }, 1000);
            dnDev.$elementToBounce = dnDev.$navbar.find("#works");
            dnDev.onNavHover();
            return false;
        });

        wdcv.shareMe(this.$share);

        this.$navbar.children(".bounce").each(function() {
            dnDev.bounce($(this));
            dnDev.bindButtonHover($(this));
        });
    }
};

$(function() {
    dnDev.init();
});
