/**
 * @file    js/global.js
 *
 * copyright (c) 2006-2010 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;
}
/* }}} */

// javascript flash detection
// {{{ jquery.browser.flash
jQuery.extend(jQuery.browser, {
    flash: (function (neededVersion) {
        var found = false;
	var version = "0,0,0";

	try {
	    // get ActiveX Object for Internet Explorer
	    version = new ActiveXObject("ShockwaveFlash.ShockwaveFlash").GetVariable('$version').replace(/\D+/g, ',').match(/^,?(.+),?$/)[1];
	} catch(e) {
	    // check plugins for Firefox, Safari, Opera etc.
	    try {
		if (navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin) {
		    version = (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]).description.replace(/\D+/g, ",").match(/^,?(.+),?$/)[1];
		}
	    } catch(e) {
		return false;
	    }		
	}

	var pv = version.match(/\d+/g);
	var rv = neededVersion.match(/\d+/g);

	for (var i = 0; i < 3; i++) {
	    pv[i] = parseInt(pv[i] || 0);
	    rv[i] = parseInt(rv[i] || 0);

	    if (pv[i] < rv[i]) {
		// player is less than required
	       	return false;
	    } else if (pv[i] > rv[i]) {
		// player is greater than required
		return true;
	    }
	}
	// major version, minor version and revision match exactly
	return true;
    })
});
// }}}
// {{{ jquery.flash
jQuery.fn.flash = function(params) {
    var html1 = "";
    var html2 = "";
    var flashParam = [];

    for (var p in params.params) {
	flashParam.push(p + "=" + encodeURI(params.params[p]));
    }
    var src = params.src;
    if (flashParam.length > 0) {
        src += "?" + flashParam.join("&amp;");
    }

    //object part
    html1 += "<object type=\"application/x-shockwave-flash\" ";
    html1 += "data=\"" + src+ "\" ";
    if (params.width !== undefined) {
	html1 += "width=\"" + params.width + "\" ";
    }
    if (params.height !== undefined) {
	html1 += "height=\"" + params.height + "\" ";
    }
    if (params.className !== undefined) {
	html1 += "class=\"" + params.className + "\" ";
    }
    if (params.id !== undefined) {
	html1 += "id=\"" + params.id + "\" ";
    }
    html1 +="allowFullScreen=\"true\" ";

    //param part
    html2 += "<param name=\"movie\" value=\"" + src + "\" />";
    html2 += "<param name=\"allowFullScreen\" value=\"true\" />";

    if (params.transparent === true) {
	html1 += "mwmode=\"transparent\"";
	html2 += "<param name=\"wmode\" value=\"transparent\" />";
    }
    html1 += ">";

    var value = $( html1 + html2 + "</object>");
    value.plainhtml = html1 + html2 + "</object>";

    return value;
};
// }}}

// {{{ removeTextSelection()
function removeTextSelection() {
    if (window.getSelection) {
        window.getSelection().removeAllRanges();
    } else if (document.selection) {
        document.selection.empty();
    }
}
// }}}

// 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()));
        }
    });
}
// }}}
// {{{ replaceFlashContent()
function replaceFlashContent() {
    /* {{{ replace flash images */
    $("img.flash_repl").each(function() {
	var parent = $(this).parent().html( $().flash({
            src:		this.src.replace(/\.jpg|\.gif|\.png/, ".swf").replace(/\&/, "&amp;"),
            width:		$(this).width(),
            height:		$(this).height(),
            className:	"flash",
            id:		this.id ? this.id + "_flash" : null,
            transparent:    $(this).hasClass("trans")
        }).plainhtml + $(this).parent().html());
        
	if (parent[0].nodeName == "A") {
	    // deactivate link for surrounding a-node in safari
	    parent[0].href = "javascript:return false;";
	}
    });
    /* }}} */
    /* {{{ replace flash videos */
    var vidIdCount = 0;

    $(".video").each(function() {
        var videoDiv = this;

        $(videoDiv).wrapInner("<div class=\"wrapper\"></div>");

        var placeholder = $("a img", this);
        var legend = $("p.legend", this);
        var requirements = $("p.requirements", this);
        var videoURL = $("<a href=\"" + $("a", this)[0].href + "\"></a>")[0].toString();
        var playerId = "dpPlayer" + vidIdCount++;
        var flashPlayer;

        if ($.browser.msie && parseInt($.browser.version) < 7) {
            var imgSuffix = ".gif";
        } else {
            var imgSuffix = ".png";
        }

        // {{{ click-event on placeholder
        $("a", videoDiv).click(function() {
            videoDiv.data.player.play();

            return false;
        });
        // }}}
        // {{{ floatToTime
        function floatToTime(value) {
            var min = Math.floor(value / 60);
            var sec = Math.floor(value) % 60; 
            if (min < 10) {
                min = "0" + min;
            }
            if (sec < 10) {
                sec = "0" + sec;
            }
            return min + ":" + sec;
        }
        // }}}
        // {{{ inititalize dummy function until flash player is loaded
        requirements.hide();

        videoDiv.data = {};
        videoDiv.data.player = {
            initialized: false
        };

        var apifuncs = ["load", "play", "pause", "seek"];
        
        for (var i = 0; i < apifuncs.length; i++) {
            videoDiv.data.player[apifuncs[i]] = function(func) {
                return function() {
                    var args = arguments;
                    var code = "";

                    if (!videoDiv.data.player.initialized) {
                        insertPlayer();
                    }

                    code += func + "(";
                    for (j = 0; j < args.length; j++) {
                        if (j > 0) {
                            code += ",";
                        }
                        code += "\"" + args[j] + "\"";
                    }
                    code += ");";

                    var call_successful = false;
                    try {
                        // try on flash player
                        if ($.browser.msie) {
                            var val = eval("window['" + playerId + "'].f" + code);
                        } else {
                            var val = eval("document['" + playerId + "'].f" + code);
                        }
                        call_successful = val;
                    } catch (e) {
                        call_successful = false;
                    }
                    if (!call_successful) {
                        debug("repeat call of " + func);
                        // defer call
                        setTimeout(function() {
                            eval("videoDiv.data.player." + code);
                        }, 300);
                    }
                }
            }(apifuncs[i]);
        }
        debug("initialized dummy player");

        // }}}
        // {{{ insertPlayer()
        function insertPlayer() {
            videoDiv.data.player.initialized = true;

            debug("adding flash player");
            debug("- player url: " + scriptPath + "/depage_player/depage_player.swf");
            debug("- width: " + placeholder.width());
            debug("- height: " + placeholder.height());
            debug("- id: " + playerId);

            var html = $().flash({
                src:		scriptPath + "depage_player/depage_player.swf",
                width:		placeholder.width(),
                height:		placeholder.height(),
                id:             playerId,
                params: {
                    id: playerId
                }
            });
            $("div:first", videoDiv)[0].innerHTML = html.plainhtml;

            placeholder.parent().hide();

            // {{{ setPlayerVar
            window.setPlayerVar = function(playerId, name, value) {
                var flashPlayer = $("#" + playerId)[0];
                var videoDiv = $("#" + playerId).parent().parent()[0];

                var player = videoDiv.data.player;

                player[name] = value;
                //$("#info").append("<p>player[" + name + "] = " + value + "</p>");

                // {{{ paused
                if (name == "paused") {
                    if (player.paused) {
                        $(".play", videoDiv).show();
                        $(".pause", videoDiv).hide();
                        $(".rewind", videoDiv).show();
                    } else {
                        $(".play", videoDiv).hide();
                        $(".pause", videoDiv).show();
                        $(".rewind", videoDiv).show();
                    }
                // }}}
                // {{{ currentTime
                } else if (name == "currentTime") {
                    $(".current", videoDiv).html(floatToTime(player.currentTime) + "/");
                    $(".progress .position", videoDiv).width(player.currentTime / duration * 100 + "%");
                // }}}
                // {{{ percentLoaded
                } else if (name == "percentLoaded") {
                    $(".progress .buffer", videoDiv).width(player.percentLoaded * 100 + "%");
                // }}}
                // {{{ duration
                } else if (name == "duration") {
                    $(".duration", videoDiv).html(floatToTime(player.duration));
                }
                // }}}
                debug("setting player var on '" + playerId + "': " + name + " = " + value);
            }
            /* }}} */
            
            debug("trying to load " + videoURL);

            videoDiv.data.player.load(videoURL);
        }
        // }}}
        // {{{ add controls
        var duration = $("a", videoDiv).attr("data-video-duration");
        var size = $("a", videoDiv).attr("data-video-size");

        var controls = $("<div class=\"controls\"></div>");
        $("<a class=\"play\"><img src=\"" + scriptPath + "/depage_player/play_button" + imgSuffix + "\" alt=\"play\"></a>").appendTo(controls).click( function() {
            videoDiv.data.player.play();

            return false;
        });
        $("<a class=\"pause\" style=\"display: none\"><img src=\"" + scriptPath + "/depage_player/pause_button" + imgSuffix + "\" alt=\"pause\"></a>").appendTo(controls).click( function() {
            videoDiv.data.player.pause();

            return false;
        });
        $("<a class=\"rewind\"><img src=\"" + scriptPath + "/depage_player/rewind_button" + imgSuffix + "\" alt=\"rewind\"></a>").appendTo(controls).click( function() {
            videoDiv.data.player.seek(0);

            return false;
        });
        $("<span class=\"progress\"><span class=\"buffer\"></span><span class=\"position\"></span></span>").appendTo(controls).mouseup( function(e) {
            var offset = (e.pageX - $(this).offset().left) / $(this).width() * duration;

            videoDiv.data.player.seek(offset);
        });
        $("<span class=\"time\"><span class=\"current\">00:00/</span><span class=\"duration\">" + floatToTime(duration) + "</span></span>").appendTo(controls);
        $("<p class=\"legend\"><span>" + legend.text() + "</span></p>").appendTo(controls);

        controls.appendTo(videoDiv);
        legend.hide();
        // }}}

        // call autoload/-start
        if ($(this).hasClass("autostart")) {
            if (placeholder[0].complete) {
                insertPlayer();
            } else {
                placeholder.load(function() {
                    insertPlayer();
                });
            }
        }
    });
    /* }}} */
}
// }}}
// {{{ replaceInteractiveContent()
function replaceInteractiveContent() {
    // {{{ get language from content tag in header
    if (lang == "de") {
        var locale = {
            prev: 'zurück',
            next: 'weiter',
            video: 'video'
        };
    } else {
        var locale = {
            prev: 'prev',
            next: 'next',
            video: 'video'
        };
    }
    // }}}

    /* {{{ add blur to links */
    $("a").click(function() {
        this.blur();
    });
    /* }}} */
    // {{{ add image-list handlers
    var $images = $(".section .images li");
    var activeImg = -1;

    // {{{ showimage()
    function showimage(n) {
        if (isNaN(n) || n <= 0) {
            n = 0;
        } else if (n >= $images.length) {
           n = $images.length - 1;
        }

        if (activeImg != n) {
            activeImg = n;

            if (activeImg == 0) {
                $(".imagenav .prev").addClass("inactive");
            } else {
                $(".imagenav .prev").removeClass("inactive");
            }
            if (activeImg == $images.length - 1) {
                $(".imagenav .next").addClass("inactive");
            } else {
                $(".imagenav .next").removeClass("inactive");
            }

            n++;
            $(".imagenav li a.active").removeClass("active");
            $images.filter(".active").removeClass("active").css({
                visibility: "hidden"
            }).children(".video").each( function() {
                // pause video if playing
                if (this.data.player.initialized) {
                    this.data.player.pause();
                }
            });
            if (n !== undefined) {
                $(".imagenav li:nth-child(" + n + ") a").addClass("active");
                $images.filter(":nth-child(" + n + ")").addClass("active").css({
                    visibility: "visible",
                    opacity: 0.1
                }).fadeTo("fast", 1);
            }

            var baselocation = document.location.toString();
            if (n > 1 || baselocation.split("#i")[0] != baselocation) {
                window.location = baselocation.split("#i")[0] + "#i" + n;
            }
        }
        if (window.getSelection) {
            if (window.getSelection().empty) {  // Chrome
                window.getSelection().empty();
            } else if (window.getSelection().removeAllRanges) {  // Firefox
                window.getSelection().removeAllRanges();
            }
        } else if (document.selection) {  // IE?
            document.selection.empty();
        }
        
        return false;
    }
    // }}}
    
    $(".project .images").each( function() {
        var imagenav = "";

        // hide all images
        $images.css({
            visibility: "hidden"
        });

        if ($images.length > 1) {
            for (var i = 0; i < $images.length; i++) {
                isvideo = $(".video", $images[i]).length > 0;
                var text = i + 1;

                if (isvideo) {
                    var lclass = "class=\"isvideo\"";
                    text = locale.video;
                } else if (i >= 9) {
                    var lclass="class=\"doubledigit\"";
                }
                imagenav += "<li><a " + lclass + " data-imgnum=\"" + i + "\" href=\"#i" + (i + 1) + "\">" + text + "</a></li>";
            }
            imagenav += "<li><a class=\"prev\">" + locale.prev + "</a></li>";
            imagenav += "<li><a class=\"next\">" + locale.next + "</a></li>";

            $imagenav = $("<ul class=\"imagenav\">" + imagenav + "</ul>").prependTo(this);
            $("a[data-imgnum]", $imagenav).click(function() {
                return showimage(parseInt($(this).attr("data-imgnum")));
            });
            $("a.prev", $imagenav).click(function() {
                return showimage(activeImg - 1);
            });
            $("a.next", $imagenav).click(function() {
                return showimage(activeImg + 1);
            });

            $images.click(function() {
                return showimage(activeImg + 1);
            });
        }

        var baselocation = document.location.toString();
        showimage(parseInt(baselocation.split("#i")[1]) - 1);
    });
    // }}}
    // {{{ add click event for teaser
    $(".teaser").click( function() {
        document.location = $("a", this)[0].href;
        this.blur();
    });
    // }}}
    // {{{ add detail-handlers
    $(".details h2").each( function() {
        var $h2 = $(this);
        var $detail = $h2.next(".detail");

        $h2.toggle( function() {
            $h2.addClass("active");
            $detail.show();
            
            removeTextSelection();
        }, function() {
            $h2.removeClass("active");
            $detail.hide();
            
            removeTextSelection();
        });
    });
    // }}}
}
// }}}

// {{{ debug()
function debug(msg) {
    return false;

    $("#debug").each( function() {
        $("<p></p>").text(msg).appendTo(this);
    });
}
// }}}

// {{{ register events
$(document).ready(function() {
    // init global vars
    // {{{ get language from content tag in header
    window.lang = $("html").attr("lang");
    // }}}
    // {{{ get script path for relative links
    window.scriptPath = $("script[src *= 'global.js']")[0].src.match(/^.*\//).toString();
    // }}}
    
    $("body").addClass("javascript");

    // replace content
    replaceEmailRefs();
    replaceInteractiveContent();

    // add flash content
    if ($.browser.flash("9,0,115")) {
        replaceFlashContent();

	$("body").addClass("flash");
    }
});
// }}}
    
/* vim:set ft=javascript sw=4 sts=4 fdm=marker : */

