/**********************************************************
    D O N 'T   M O D I F Y   T H I S   F I L E  !!!
**********************************************************/
var Configuration = new function() {
    this._translations = [],
    this.getTranslation = function (key) {
        if (this.existsTranslation(key)) {
            return this._translations[key];
        } else {
            return key;
        }
    },
    this.existsTranslation = function(key) {
        return (typeof(this._translations[key]) != "undefined");
    },
    this.addTranslation = function (key, value) {
        if (key) {
            this._translations[key] = value;
        }
    },
    this.addTranslations = function(namespace, keyValueArray) {
        if (!namespace) {
            namespace = "";
        }
        if (typeof keyValueArray == "object") {
            jQuery.each(keyValueArray, function(key, value) {
                var newNamespace = (namespace ? "/" : "") + key;
                newNamespace.replace("/0", "");
                
                if (typeof value == "string") {
                    Configuration.addTranslation(newNamespace, value);
                } else {
                    Configuration.addTranslations(newNamespace, value);
                }
            });
        }
    }
}    
    


function getHostnameFromUrl(url) {
    var domainParts = url.match(/:\/\/(.[^/]+)/);
    if (domainParts && domainParts.length > 1) {
        return domainParts[1].replace ("www.", "");
    }
}


function makeCornersRound (item) {
    var cornerRadius = 13;
                
    // iterate over all elments with corner classes and add corner
    $(item).find(".corners, .topcorners, .bottomcorners").each(function(i, element) {
        element = $(element);
        if (element.hasClass("corners")) {
            element.corner(cornerRadius + "px").removeClass ("corners");
        } else { 
            if (element.hasClass("topcorners")) {
                element.corner(cornerRadius + "px top").removeClass ("topcorners");
            } 
            if (element.hasClass("bottomcorners")) {
                element.corner(cornerRadius + "px bottom").removeClass ("bottomcorners");
            }
        }
        
        // shouldn't be needed ---------------------------- I HAVE NO OTHER SOLUTION ------------------------------------------------------------------
        if (element.parent().css("position") == "static") {        // parent elemnt needs to be positioned relative or absolute
        //    element.parent().css("position", "relative");
        }
    });
}


/* this is done with document.write and not with $() to write the html directly and not have a jumping element after the html is loaded completly*/
function addBackButton () {
    if (window.history.length < 1) {
        $(".backLink").hide();
    }
}


function makeMainMenuHovable () {
    $(".main_links > li").hover(function () {$(this).addClass("hover");},function () {$(this).removeClass("hover");});
}




/* 
    Options 
        - closeButton: true/false        (default: true)
        - modal: true/false            (default: true)
        - width: integer            (default: 500)
        - height: integer            (default: 200)
        - content: html code        (default: "")
*/
function showDialog (options) {
    $.modal.close();
    $("#dialog").remove();
    var dialog = $('<div id="dialog"></div>');
    if (options.closeButton === undefined || options.closeButton == true) {
        dialog.append('<a href="#" class="simplemodal-close dialogCloseButton" title="Close">' + Configuration.getTranslation("content/button_close") + '</a>');
    }
    $("body").append(dialog.append(options.content));
    
    
    var containerCss = {};
    if (options.height) {
        $("#dialog").css("height", options.height + "px");
        containerCss.height = (options.height + 10) + "px";
    }
    $("#dialog").css("width", (options.width ? options.width : 470) + "px");
    containerCss.width = ((options.width ? options.width : 500) + 5) + "px";

    // $("#dialog").css("position", "absolute");
    // position : [options.top, options.left],
    
    //http://www.ericmmartin.com/projects/simplemodal/#tests
    $("#dialog").modal({
        containerCss : containerCss,
        closeClass: ('simplemodal-close')
    //    overlayCss : {'background-color' : 'gray'},
    });
    
    makeCornersRound("#dialog");
    
    if (options.modal === false) {            // IE 6 fix   || $.browser.msie && parseInt(jQuery.browser.version.substr(0,1)) < 7
        $("#simplemodal-overlay").css("display", "none");
    } else {
        $("#simplemodal-overlay").click(function () {
            $.modal.close();
        });
    }
    
    // strange hack
    if (options.top) {
        $("#simplemodal-container").css("position", "absolute");
    }
    
    // $("#simplemodal-container").siblings("iframe").remove();        // IE 6 fix - don't know why the lib creates intransparent iframe
}



function openExternalLinkPopUp (linkItem) {
    linkItem = $(linkItem);
    
    showDialog({
        closeButton : false,
        modal    :     true, 
        content :     (Configuration.existsTranslation("content/external_links/headline") ? '<div style="padding: 5px; background-color: #663366; color: white; font-size: 14pt;">' + Configuration.getTranslation("content/external_links/headline") + '</div><br>' : "") +
                    Configuration.getTranslation("content/external_links/text") +
                    '<div style="text-align: right">' +
                        (Configuration.existsTranslation("content/external_links/stay") ? '<a class="arrow" class="simplemodal-close" href="javascript: void(0)" onclick="$.modal.close();">' + Configuration.getTranslation("content/external_links/stay") + '</a> &#160;&#160;&#160;' : '') + 
                        (Configuration.existsTranslation("content/external_links/continue") ? '<a class="button continueButton ' + (linkItem.parents('.shareIcons, .share').length ? "fromShareIcon" : '') + '" class="simplemodal-close" href="' + linkItem.attr('rel') + '" target="_blank">' + Configuration.getTranslation("content/external_links/continue") + '<span></span></a> ' : "") +
                    '</div>'
    });
    
    $(".continueButton").click(function () {
        var continueButton = $(this);
        $.modal.close();
        webTracker.externalLink({
            url            : continueButton.attr("href"),
            domain        : getHostnameFromUrl(continueButton.attr("href")),
            isShareLink    : continueButton.hasClass("fromShareIcon")
        }); 
    });    
}
function addOverlayToExternalLinks(container) {
    var notExternalDomains = ["your-life.com"];
    $(container).find("a[href^='http']:not([rel])").each(function (index, item) {
        item = $(item);
        var domain = getHostnameFromUrl(item.attr("href"));
        
        // seams a bit diffult, but is needed to handle all kinds of subdomains
        var foundNotExternalDomains = jQuery.grep(notExternalDomains, function(notExternalDomain, i){
            var pos = ("." + domain).indexOf("." + notExternalDomain);
            return (pos != -1 && pos + notExternalDomain.length == domain.length);
        });

        if (!foundNotExternalDomains.length) {
            item.attr("rel", item.attr("href"));
            
            item.attr("href", "javascript: void(0);");
            item.attr("target", "");
            
            item.mousemove(function () {window.status = "Open External Link";});                // only works in IE
            item.mouseout(function () {window.status = ""; return true;});
            
            item.click(function () {
                 openExternalLinkPopUp (this);
            });
        }
    }); 
}







function addOverlayToInternalLinks(container) {
    $(container).find("a.internalpageoverlay").each(function (index, item) {
        item = $(item);
        item.attr("rel", item.attr("href"));
        
        item.attr("href", "javascript: void(0);");
        item.click(function () {
            openInternalLinkPopUp (this);
        });
    }); 
}

function openInternalLinkPopUp (linkItem) {
    linkItem = $(linkItem);
    showDialog({
        modal    :     true, 
        content :     '<div id="internalpagecontainer" style="height: 450px">Loading</div>'
    });
    
    $.ajax({
        type: "POST",
        url: "/scripts/include/ajax/index.php",
        data:  {'app' : 'templates/_main',
                'url' : linkItem.attr('rel')},
        success: function(content){
            $("#internalpagecontainer").html(content);
            addOverlayToExternalLinks("#internalpagecontainer");
        }
    });    
}

function toggleElements(container) {
    $(container).find('.toggleElement').each(function (index, item) {
        $(item).click(function() {
            $(item).toggleClass('toggleActive');    
            $('div[id='+$(item).attr('rel')+']').slideToggle();
        });
    });
}

jQuery(function () {
    makeMainMenuHovable();            // for IE 6
    makeCornersRound("#main");
    addOverlayToExternalLinks("#main");
    addOverlayToInternalLinks("#main");
    $(".ytf_item img").pngFix();             // transperent png images for IE 6
    toggleElements("#main");
    
    $("a[rel^='prettyPhoto']").prettyPhoto({
        social_tools:'',
        deeplinking: false,
        show_title: false
    });    
});    

