﻿/// <reference path="/germany/dynamics/_global/components/infocenter/js/jquery-1.3.2-vsdoc2.js" />
/// <reference path="/germany/dynamics/_global/components/infocenter/js/extend/urlEncodeDecode.js" />
/// <reference path="/germany/dynamics/_global/components/infocenter/js/extend/getUrlParameters.js" />
/// <reference path="/germany/dynamics/_global/components/infocenter/js/extend/uniqueArray.js" />
/// <reference path="/germany/dynamics/_global/components/infocenter/js/extend/sortArray.js" />
/// <reference path="/germany/dynamics/_global/components/infocenter/js/plugins/addCategoryId.js" />
/// <reference path="/germany/dynamics/_global/components/infocenter/js/plugins/setWidthEqual.js" />
/// <reference path="/germany/dynamics/_global/components/infocenter/js/plugins/resetSelect.js" />
/// <reference path="/germany/dynamics/_global/components/infocenter/js/plugins/addCategoryToBasket.js" />
var zzz = "test";
(function($) {
    $.fn.createOverlay = function(options) {
        var defaults = {
            'overlayContainer': $('#myoverlay'),
            'defaultWindowHeight': 'auto',
            'defaultWindowWidth': 'auto',
            'windowWidth': 700,
            'windowHeight': 500,
            'headlineObj': '.e .c .mbcontainercontent .content .headline',
            'headlineActive': true,
            'headlineHtml': '',
            'descriptionObj': '.e .c .mbcontainercontent .content .description',
            'descriptionActive': true,
            'descriptionHtml': '',
            'menuHtml': '<select style="margin:0 20px 0 150px;float:right;"><option>Menu</option><option>E-Mail</option></select>',
            'targetContentHtml': '',
            'menuActive': true,
            'cta': null,
            'ctaType': null,
            'imageWidth': 0,
            'imageHeight': 0,
            'topOffset': $(document).scrollTop() + 50,
            'leftOffset': null
        };
        var options = $.extend(defaults, options);
        //clear older OverlayContent
        options['overlayContainer'].html('');
        //set leftOffset
        options['leftOffset'] = (($('#core_content').outerWidth() - options['windowWidth']) / 2) + $('#core_content').position().left;


        //set headline Html
        if (options['headlineActive']) {
            options['headlineHtml'] = options['cta'].parent().parent().parent().parent().parent().parent().prev().children('.headline').html();
        }
        //set Description Html
        if (options['descriptionActive']) {
            options['descriptionHtml'] = options['cta'].parent().parent().parent().parent().parent().parent().prev().find('.content .description').text();
        }
        //alert(options['descriptionHtml']);
        var targetType;

        switch (options['ctaType']) {
            case 'casestudy':
                var caseStudyId = jQuery.getUrlParametersFromUrlString_GetValue(options['cta'].attr('href'), 'id');
                options['targetContentHtml'] = '<iframe class="casestudy" src="http://www.microsoft.com/germany/kundenreferenzen/epaper/' + caseStudyId + '/' + caseStudyId + '.swf" style="width:100%;height:100%;display:block;overflow:hidden;"></iframe>';
                options['cta'].attr('tmpHref', options['cta'].attr('href'));
                options['cta'].attr('href', 'javascript:void(0);');
                break;
            case 'image':
                options['cta'].attr('tmpHref', options['cta'].attr('href'));
                var myImage = $('<img />')
                .attr({ 'src': options['cta'].attr('tmpHref') })
                .css({ 'margin': '10px 0' });
                var myimagewrapper = $('<div></div>');
                myImage.appendTo(myimagewrapper);
                options['targetContentHtml'] = myimagewrapper.html();
                options['cta'].attr('href', 'javascript:void(0);');
                break;
            case 'video':
                options['cta'].attr('tmpHref', options['cta'].attr('href'));
                options['targetContentHtml'] = '<div id="loadingVideo">Video wird geladen</div><iframe src="' + options['cta'].attr('tmpHref') + '" allowtransparency="true" width="430" height="326" scrolling="no" frameborder="0" id="videoEmbedded" onload="deleteLoadingVideo()"></iframe>';

                options['cta'].attr('href', 'javascript:void(0);');
                break;
        }
        if (options['windowWidth'] == null) {
            options['windowWidth'] = $.fn.setDefaultWindowWidth();
        }

        var icOverlay = $('<div class="containerPlus draggable {buttons:' + "'" + 'c' + "'" + ', icon:' + "'" + 'dynamics.png' + "'" + ', skin:' + "'" + 'dynamics' + "'" + ', width:' + "'" + options['windowWidth'] + "'" + ',height:' + "'" + options['windowHeight'] + "'" + '}" style="top: ' + options['topOffset'] + 'px; left: ' + options['leftOffset'] + 'px"><div class="no"><div class="ne"><div class="n"><div class="menu"></div></div></div><div class="o"><div class="e"><div class="c"><div class="mbcontainercontent"><div class="content" style="height:100%;"><div class="headline"></div><div class="description"></div><div class="targetContent"></div></div></div></div></div></div><div><div class="so"><div class="se"><div class="s"></div></div></div></div></div></div>")').appendTo(options['overlayContainer']);

        //        if (options['ctaType'] == "video") {
        //            alert("video");
        //            $('#videoEmbedded').load(function() {
        //                alert("loaded");
        //                $('#loadingVideo').remove();
        //            });
        //        }
        //var icOverlay = $('<div class="containerPlus draggable resizable {buttons:' + "'" + 'c' + "'" + ', icon:' + "'" + 'dynamics.png' + "'" + ', skin:' + "'" + 'default' + "'" + ', width:' + "'" + options['windowWidth'] + "'" + ',height:' + "'" + options['windowHeight'] + "'" + '}" style="top: ' + options['topOffset'] + 'px; left: ' + options['leftOffset'] + 'px"><div class="no"><div class="ne"><div class="n"><div class="menu"></div></div></div><div class="o"><div class="e"><div class="c"><div class="mbcontainercontent"><div class="content" style="height:100%;"><div class="headline"></div><div class="description"></div><div class="targetContent"></div></div></div></div></div></div><div><div class="so"><div class="se"><div class="s"></div></div></div></div></div></div>")').appendTo(options['overlayContainer']);
        //Menu not finished yet
        //icOverlay.createMenu(options['menuHtml']);
        icOverlay.setHeadline(options['headlineHtml']);
        icOverlay.setDescription(options['descriptionHtml']);
        icOverlay.setTargetContent(options['targetContentHtml']);

        $(".containerPlus").buildContainers({
            containment: "document",
            elementsPath: "/germany/dynamics/_global/components/infocenter/img/mbContainerPlus2/",
            onResize: function(o) {
                //                var totalContentWidth = o.outerWidth() - 50;
                //                var totalContentHeight = o.find('.e .c').outerHeight() - 20;
                //                var headlineHeight = o.find(options['headlineObj']).outerHeight();
                //                var descriptionHeight = o.find(options['descriptionObj']).outerHeight();
                //                var iframeHeight = totalContentHeight - (headlineHeight + descriptionHeight);
                //                o.find('.e .c .mbcontainercontent .content .targetContent:first').css({ 'height': iframeHeight + 'px', 'width': totalContentWidth + 'px' });

                //                if (options['ctaType'] == 'image') {
                //                    var headlineHeight = o.find('.e .c .mbcontainercontent .content .headline').outerHeight();
                //                    var descriptionHeight = o.find('.e .c .mbcontainercontent .content .description').outerHeight();
                //                    var imageSizeRation = options['imageWidth'] / options['imageHeight'];
                //                    var imageHeight = o.find('.e .c:first').height() - (headlineHeight + descriptionHeight) - 30;
                //                    var imageWidth = imageHeight * imageSizeRation;
                //                    o.find('.e .c .mbcontainercontent .content .targetContent img:first').attr({ 'height': imageHeight, 'width': imageWidth });
                //                }
            },
            onClose: function(o) {
                options['overlayContainer'].html('');
                options['cta'].attr('href', options['cta'].attr('tmpHref'));
                options['cta'].removeAttr('tmpHref');
            },
            onCollapse: function(o) {
            },
            onIconize: function(o) {
            },
            onDrag: function(o) {
            }
        });
        icOverlay.setWindowSize(options);
        return icOverlay;

    },

    $.fn.setWindowSize = function(options) {
        var o = $(this).find('.no .o:first');
        if (options['ctaType'] == 'casestudy') {

            var totalContentHeight = $(this).find('.no .e .c').outerHeight() - 20;
            var headlineHeight = o.find('.e .c .mbcontainercontent .content .headline').outerHeight();
            var descriptionHeight = o.find('.e .c .mbcontainercontent .content .description').outerHeight();
            var iframeHeight = totalContentHeight - (headlineHeight + descriptionHeight);
            o.find('.e .c .mbcontainercontent .content .targetContent').css({ 'height': iframeHeight + 'px' });
        }
        if (options['ctaType'] == 'image') {
            var headlineHeight = o.find('.e .c .mbcontainercontent .content .headline').outerHeight();
            var descriptionHeight = o.find('.e .c .mbcontainercontent .content .description').outerHeight();
            var imageHeight = 0;
            var imageWidth = 0;
            var windowFormat;
            var windowSizeRatio = options['windowWidth'] / options['windowHeight'];
            if (windowSizeRatio >= 1) {
                windowFormat = 'querformat';
            } else {
                windowFormat = 'hochformat';
            }
            var imageFormat;
            var imageSizeRatio = options['imageWidth'] / options['imageHeight'];
            if (imageSizeRatio >= 1) {
                imageFormat = 'querformat';
            } else {
                imageFormat = 'hochformat';
            }
            var maxImageWidth = 0;
            var maxImageHeight = 0;

            maxImageWidth = options['windowWidth'] - 75;
            maxImageHeight = o.find('.e .c:first').height() - (headlineHeight + descriptionHeight) - 50;

            imageWidth = maxImageWidth;
            imageHeight = imageWidth / imageSizeRatio;
            if (imageHeight > maxImageHeight) {
                imageHeight = maxImageHeight;
                imageWidth = imageSizeRatio * imageHeight;
            }
            //alert(options['imageHeight']);
            //alert(options['imageWidth']);
            tmpHeight = "";
            tmpWidth = "";
            //o.find('.e .c .mbcontainercontent .content .targetContent img:first').css({ 'height': options['imageHeight'] + "px", 'width': options['imageWidth'] + "px" });
            if (options['imageHeight'] != "") {
                o.find('.e .c .mbcontainercontent .content .targetContent img:first').css({ 'height': options['imageHeight'] + "px" });
            }
            if (options['imageWidth']!="") {
            o.find('.e .c .mbcontainercontent .content .targetContent img:first').css({ 'width': options['imageWidth'] + "px" });
            }
            //o.find('.e .c .mbcontainercontent .content .targetContent img:first').css({ 'height': imageHeight + "px", 'width': imageWidth + "px" });
            o.find('.e .c .mbcontainercontent .content .targetContent:first').css('text-align', 'center');

        }
        if (options['ctaType'] == 'video') {
            //            o.find('.e .c').css({ 'height': '600px' });
            //            o.find('.e .c .mbcontainercontent').css({ 'height': '600px' });
            o.find('.e .c .mbcontainercontent .content .targetContent:first').css('text-align', 'center');
        }
    },
    $.fn.setDefaultWindowWidth = function() {
        var abstand = 20;
        return $('#core_content').outerWidth() - (abstand * 2);
    },
    $.fn.createMenu = function(html) {
        $(html).appendTo($(this).find('.menu'))
    }
    ,
    $.fn.setHeadline = function(html) {
        $(html).appendTo($(this).find('.headline'))
    },
    $.fn.setDescription = function(html) {
        if (jQuery.trim(html) != "") {
            html = ("<div>" + html + "</div>");
            $(html).appendTo($(this).find('.description'));

        } else {
            $(this).find('.description').css({ 'display': 'none' });
        }
    },
    $.fn.setTargetContent = function(html) {
        $(html).appendTo($(this).find('.targetContent'))
    }

})(jQuery);

function deleteLoadingVideo() {
    jQuery('#loadingVideo').remove();
}
