/**
 * @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;
}
/* }}} */

// 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;
};
// }}}

// 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() {
                            //$("#info").append("<p>defer call of " + func + " - exception: " + e + "</p>");
                            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
    var lang = $("meta[name = 'Content-Language']")[0].content;
    // }}}
    // {{{ add click event for teaser
    $(".teaser").click( function() {
        document.location = $("a", this)[0].href;
    });
    // }}}
}
// }}}
// {{{ debug()
function debug(msg) {
    /*
    $("#debug").each( function() {
        $("<p></p>").text(msg).appendTo(this);
    });
    */
}
// }}}

// fix browser behaviours
// {{{ fixHeightIE6()
function fixHeightIE6() {
    var body = $("body");
    var content = $("content");

    if (body.height() > content.height()) {
	content.height(body.height());
    }
}
// }}}
// {{{ fixFlashDisplayOpera()
function fixFlashDisplayOpera(numcall) {
    numcall++;
    if (numcall < 20) {
	setTimeout("fixFlashDisplayOpera(" + numcall + ")", 200);
    }

    if (numcall % 2 == 0) {
	$("object").css({ border: "0px solid" });
    } else {
	$("object").css({ border: "none" });
    }
}
// }}}

// {{{ register events
$(document).ready(function() {
    // init global vars
    // {{{ get language from content tag in header
    window.lang = $("meta[name = 'Content-Language']")[0].content;
    // }}}
    // {{{ get script path for relative links
    window.scriptPath = $("script[src *= 'global.js']")[0].src.match(/^.*\//).toString();
    // }}}
    
    // replace content
    replaceEmailRefs();
    replaceInteractiveContent();

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

	$("body").addClass("flash");
    }

    // fix browser bugs
    if ($.browser.msie) {
	fixHeightIE6();
	$(window).resize( fixHeightIE6 );
    }
    if ($.browser.opera) {
	fixFlashDisplayOpera(0);
    }
});
// }}}
    
/* vim:set ft=javascript sw=4 sts=4 fdm=marker : */
