/**
 * @file    js/global.js
 *
 * copyright (c) 2006-2009 Frank Hellenkamp [jonas@depagecms.net]
 *
 * @author    Frank Hellenkamp [jonas@depagecms.net]
 */

// global helpers
// {{{ getHexColorFromString()
function getHexColorFromString(colorString) {
    if (colorString == "transparent") {
	var hexCode = "000000";
    } else if (colorString.substr(0, 3) == "rgb") {
        var components = colorString.match(/[0-9]+/g);
        var r = parseInt(components[0]).toString(16);
        var g = parseInt(components[1]).toString(16);
        var b = parseInt(components[2]).toString(16);

        if (r.length < 2) r = "0" + r;
        if (g.length < 2) g = "0" + g;
        if (b.length < 2) b = "0" + b;

        var hexCode = r + g + b;
    } else if (colorString.charAt(0) == "#") {
        var hexCode = colorString.substring(1);
    }

    return "0x" + hexCode;
}
/* }}} */

// replace content, depending on reader capabilities
// {{{ replaceEmailChars()
function replaceEmailChars(mail) {
    mail = unescape(mail);
    mail = mail.replace(/ \*at\* /g, "@");
    mail = mail.replace(/ \*dot\* /g, ".");
    mail = mail.replace(/ \*punkt\* /g, ".");
    mail = mail.replace(/ \*underscore\* /g, "_");
    mail = mail.replace(/ \*unterstrich\* /g, "_");
    mail = mail.replace(/ \*minus\* /g, "-");
    mail = mail.replace(/mailto: /, "mailto:");

    return mail;
}
// }}}
// {{{ replaceEmailRefs()
function replaceEmailRefs() {
    $("a[href*='mailto:']").each(function() {
        // replace attribute
        $(this).attr("href", replaceEmailChars($(this).attr("href")));
        
        //replace content if necessary
        if ($(this).text().indexOf(" *at* ") > 0) {
            $(this).text(replaceEmailChars($(this).text()));
        }
    });
}
// }}}

// {{{ addAnimation()
function addAnimation() {
    var $animation = $("#animation");

    if ($animation.length > 0) {
        var $tohide = $("#mainheadline, .mk1, .mk2");
        var defaultSpeed = 1500;
        var speed = defaultSpeed;
        var $currentStep;
        var $minikitop = $(".miniki.interactive .top");
        var $minikitopimg = $("img", $minikitop);
        var minikitopsrc = $minikitopimg[0].src;
        var images = [];

        $tohide.hide();
        $animation.show();

        // preload images
        var i = 0;
        $("#animation > div").each(function() {
            if ($(this).attr("data-top-image") != null) {
                img = new Image();
                img.src = $(this).attr("data-top-image");
                images[i] = img;
            }

            i++;
        });

        var $steps = $("#animation > div").hide();
        var step = -1;
        var nextStep = function() {
            if (step < $steps.length - 1) {
                if ($currentStep) {
                    $currentStep.hide();
                }
                step++;
                $currentStep = $($steps[step]);

                // get speed from attribute
                if ($currentStep.attr("data-speed") != null) {
                    speed = parseInt($currentStep.attr("data-speed"));
                } else {
                    speed = defaultSpeed;
                }

                // get top-image from attribute
                if ($currentStep.attr("data-top-image") != null) {
                    // show top image
                    $minikitopimg[0].src = $currentStep.attr("data-top-image");
                    $minikitop.addClass("open");
                } else {
                    $minikitop.removeClass("open");
                }

                // animate
                $currentStep.show().animate({
                    top: 0
                }, {
                    duration: speed,
                    complete: function() {
                        if (images[step + 1] && !images[step + 1].complete) {
                            $(images[step + 1]).load(function() {
                                nextStep();
                            });
                        } else {
                            nextStep();
                        }
                    }
                });
            } else if (step <= 10000) {
                step = 10001;
                $animation.hide();
                $tohide.show();
                $skip.remove();
                $minikitopimg[0].src = minikitopsrc;

                replaceInteractiveContent();
            }
        }
        
        // add skip-link
        var $skip = $("<a href=\"#skip\" id=\"skip\"><span>skip &gt;&gt;</span></a>").appendTo($animation).click(function() {
            step = 10000;
            nextStep();

            return false;
        });

        // start animation
        nextStep();
    } else {
        replaceInteractiveContent();
    }
}
// }}}
// {{{ replaceInteractiveContent()
function replaceInteractiveContent() {
    // {{{ get language from html tag
    var lang = $("html").attr("lang");

    if (lang == "de") {
        var textOpen = "öffnen";
        var textClose = "schließen";
    } else {
        var textOpen = "open";
        var textClose = "close";
    }
    // }}}
    // {{{ add click event for teaser
    $(".teaser").click( function() {
        document.location = $("a", this)[0].href;
    });
    // }}}
    // {{{ add click event for miniki
    $(".miniki.interactive").each( function() {
        $(".left, .top, .right", this).each( function() {
            var $img = $("img", this);
            var $opener = $("<a class=\"opener\" href=\"#\"><span>" + textOpen + "</span></a>").appendTo(this);

            $(this).toggle(
                function() {
                    $(this).addClass("open");
                    $("span", this).text(textClose);
                    $opener.focus().blur();
                    if (!isopen) {
                        window.location.hash = "#open";
                        isopen = true;
                    }
                }, function() {
                    $(this).removeClass("open");
                    $("span", this).text(textOpen);
                    $opener.focus().blur();
                    if (isopen) {
                        window.location.hash = "#";
                        isopen = false;
                    }
                }
            );
        });
    });
    if (isopen) {
        $(".miniki.interactive .top").animate({
            marginTop: 0
        }, {
            speed: 400,
            complete: function() {
                $(this).click();
            }
        });
    }
    // }}}
    // {{{ open status to colorlinks
    $(".colorlink").click( function() {
        if ($(".miniki.interactive .top").hasClass("open")) {
            this.href += "#open";
        }

        return true;
    });
    // }}}
}
// }}}
/* {{{ addTouchEvents() */
function addTouchEvents() {
    if (!$.browser.msie) {
        if ($(".prevnext").length > 0) {
            var $prevlink = $("a[rel='prev']");
            var $nextlink = $("a[rel='next']");

            $("body").touchwipe({
                wipeLeft: function() { 
                    document.location = $nextlink[0].href + (isopen ? "#open" : "");
                },
                wipeRight: function() { 
                    document.location = $prevlink[0].href + (isopen ? "#open" : "");
                },
                min_move_x: 100,
                preventDefaultEvents: true
            });
        }
    }
}
/* }}} */
// {{{ resizeLayout()
function resizeLayout() {
    var $view = $("body");

    var areaH = $view.height();
    var areaW = $view.width();

    if (areaW >= 1240) {
        $view.addClass("zoom125");
    } else {
        $view.removeClass("zoom125");
    }
}
// }}}

var baselocation = "";
var isopen = false;

// {{{ register events
$(document).ready(function() {
    $("body").addClass("javascript");
    baselocation = window.location + "";
    if (baselocation.substr(baselocation.length - 5) == "#open") {
        isopen = true;
    }
    baselocation = baselocation.replace(/#.*/, "");

    // replace content
    replaceEmailRefs();
    addTouchEvents();

    $(window).resize( resizeLayout );
    resizeLayout();
});
$(window).load(function() {
    addAnimation();
});
// }}}
    
/* vim:set ft=javascript sw=4 sts=4 fdm=marker : */

