﻿/* Copyright 2009 by editio.at */

// jQuery-Funktion zum Vorladen von Bildern
jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img />").attr("src", arguments[i]);
  }
}

// default-Einstellungen für das editio-Script
var editio_defaults = {
    kwicksmenu: false,
    slidingnavigation: false,
    
    lavalamp: false,
    
    bookmarklink: true,
    favouriteslink: true,
    
    sitestretch: false,
    objectToStretch: '#main .page',
    stretchOverwriteFormCallback: true,

    colEqualize: [],
    
    acmenu: false,
    editioContentToggle: false,
    editioContentToggleSettings: {
        initCollapsed: true,
        initDisplayClass: 'e_toggleitem_initdisplay',
        showevent: 'click',
        hideevent: 'click',
        slideduration: 600
    },
    
    fancybox: true,
    slideshowplay: false,
    slideshowdelay: 5000,
    
    xfade: true,

    facebookSDK: true
};

// Stretch divs in .subcolumns to full height for dottet lines etc.
var xfading = false; // set to true by xfade setup
function resizeDivs(){
    if(xfading) {
        var subcr = $("#main .subcr");
        if(subcr.length > 0)
            subcr.each(function(){ $(this).parent().height($(this).height()); });
    }
    $(".subcolumns").each(function() { // HACK: subcolumns
		var mainsubheight = $(this).height();
		$(this).children("div").each(function(){
			$(this).height(mainsubheight);
		});
	});
}

// Helper function to perorm height adjustment
var diff = 0;
var initOHeight= 0;
function recalcHeight(o) {
	diff = $(window).height();
	diff -= $("#wrapper").height();
	oheight = $(o).height();
	diff += $(o).height();
	if (diff > oheight) {
		$(o).height(diff);
		$(".ecol", $(o)).each(function() { $(this).height(diff); }); // HACK: subcolumns
	} else if (diff < oheight && oheight > initOHeight){
		$(o).height(initOHeight);
		$(".ecol", $(o)).each(function() { $(this).height(initOHeight); }); // HACK: subcolumns
	}
	resizeDivs();
}

// Adjust .subcolumns container to height of highest child
function equalizeColHeights()
{
    $(".subcolumns").each(function(){
        var maxheight = $(this).height();
        $(this).children().each(function(){
            var thisheight = $(this).height();
            if(thisheight > maxheight)
                maxheight = thisheight;
        });
        $(this).height(maxheight);
        $(this).children().height(maxheight);
    });
}

//DoPrint - called from Service Link "Diese Seite drucken" from SideBar_Service.xslt
function doPrint(){
    print($(".eprint", $("#main")).get()); // HACK: c50l
}

$.editioSite = function (settings) {
    var opts = $.extend({}, editio_defaults, settings);

    // hier wird das Kwicks-Menue erstellt, benoetigt jquery.kwicks.mod.js
    if (opts.kwicksmenu) {
        $('.kwicks').kwicks({
            max: 220,
            spacing: 5,
            easing: 'easeInQuad',
            sticky: false,
            normWoH: 151,
            defaultKwick: 0
        });
        $('.kwicks').trigger('mouseout');
    }
    // end kwicks

    // XFade-Aktivierung - z.B. fuer wechselnde Zitate oder Bilder
    if (opts.xfade) {
        var xfadeobj = $('.xfade_example');
        if (xfadeobj.length != 0) {
            xfadeobj.xfade({ duration: 7000, speed: 1000 });
            var subcr = $("#main .subcr");
            if (subcr.length > 0)
                subcr.each(function () { $(this).parent().height($(this).height()); });

            // ensure resize of .subcolumns on window.resize()
            xfading = true;
            // update col heights
            equalizeColHeights();
        }
    }
    // end XFade

    // lavalamp
    if (opts.lavalamp) {
        $(".lavaLamp").lavaLamp({ fx: "easeOutBack", speed: 600 });
    }
    // end lavalamp

    // Height-Adjustment.. full page size
    if (opts.sitestretch) {
        initOHeight = $(opts.objectToStretch).height();
        recalcHeight(opts.objectToStretch);
        $(window).resize(function () { recalcHeight(opts.objectToStretch); });

        if (opts.stretchOverwriteFormCallback && typeof WebForm_ExecuteCallback == "function" && typeof recalcHeight == "function") {
            jQuery.editio_oldFormCallback = WebForm_ExecuteCallback;
            WebForm_DoPostBackWithOptions = function (options) {
                $.editio_oldFormCallback(options);
                $(window).trigger("resize");
            };
        }
    }
    // end Height-Adjustment

    // custom subcolumn equalization
    if (opts.colEqualize.length > 0) {
        for (var i = 0; i < opts.colEqualize.length; i++) {
            var children = $(opts.colEqualize[i]).children();
            var maxheight = 0;
            children.each(function () {
                var childheight = $(this).height();
                if (childheight > maxheight)
                    maxheight = childheight;
            });
            children.height(maxheight);
        }
    }
    // end custom subcolumn equalization

    // bookmark-Link
    if (opts.bookmarklink) {
var bookmarklink = $(".bookmarklink");
if (bookmarklink.length != 0) {
bookmarklink.each(function () {
var bookmark = $($(this).attr("href"));
bookmark.bookmark({
compact: false,
icons: '/images/bookmarks/bookmarks.png'
});
bookmark.wrapInner('<div class="bookmarkContainer"></div>');
$("ul", bookmark).scrollTop(0);
$(this).fancybox({
'overlayShow': true,
'overlayOpacity': 0.8,
'speedIn': 500,
'speedOut': 400,
'transitionIn': 'elastic',
'transitionOut': 'elastic',
'easingIn': 'easeOutBack',
'easingOut': 'easeInBack',
'hideOnContentClick': false,
'width': 434,
'height': 290,
'autoDimensions': false
});
});
}
} 
    //end bookmark

    // favourites
    if (opts.favouriteslink) {
        var favlink = $(".add2Favorites");
        if (favlink.length != 0) {
            favlink.jFav({ msg: "Drücken Sie \"Strg + D\" bzw. \"CMD + D\" für Mac, um diese Seite zu Ihren Favoriten hinzuzufügen." });
        }
    }
    //end favourites

    // editio.at - Set up fancy box
    if (opts.fancybox) {
        var fbox = $("#fbox a");
        if (fbox.length != 0) {
            fbox.fancybox({
                'titlePosition': 'outside',
                'cycle': true,
                'overlayShow': true,
                'overlayOpacity': 0.8,
                'speedIn': 600,
                'speedOut': 500,
                'changeSpeed': 500,
                'transitionIn': 'elastic',
                'transitionOut': 'elastic',
                'easingIn': 'easeOutBack',
                'easingOut': 'easeInBack',
                'easingChange': 'easeOutBack',
                'hideOnContentClick': false,
                'slideshow': opts.slideshowplay,
                'slideshowInterval': opts.slideshowdelay
            });
        }
        var fpic = $(".fpic a");
        if (fpic.length != 0) {
            fpic.fancybox({
                'titlePosition': 'outside',
                'cycle': true,
                'overlayShow': true,
                'overlayOpacity': 0.8,
                'speedIn': 600,
                'speedOut': 500,
                'changeSpeed': 500,
                'transitionIn': 'elastic',
                'transitionOut': 'elastic',
                'easingIn': 'easeOutBack',
                'easingOut': 'easeInBack',
                'easingChange': 'easeOutBack',
                'hideOnContentClick': false
            });
        }
        var fiframe = $("a.iframe");
        if (fiframe.length != 0) {
            fiframe.fancybox({
                'modal': true,
                'autoDimensions': true,
                'overlayShow': true,
                'overlayOpacity': 0.8,
                'speedIn': 600,
                'speedOut': 500,
                'changeSpeed': 500,
                'transitionIn': 'elastic',
                'transitionOut': 'elastic',
                'easingIn': 'easeOutBack',
                'easingOut': 'easeInBack',
                'easingChange': 'easeOutBack',
                'imageScale': true,
                'showNavArrows': false
            });
        }
        var servicelinks = $("a.service");
        servicelinks.each(function () {
            var link = $(this);
            link.attr("href", link.attr("rel"));
            link.attr("rel", "");
        });
        servicelinks.fancybox({
            'type': 'iframe',
            'overlayShow': true,
            'overlayOpacity': 0.8,
            'speedIn': 600,
            'speedOut': 500,
            'transitionIn': 'elastic',
            'transitionOut': 'elastic',
            'easingIn': 'easeOutBack',
            'easingOut': 'easeInBack',
            'scrolling': 'yes',
            'width': 560,
            'height': 700,
            'autoDimensions': false,
            'hideOnContentClick': true,
            'centerOnScroll': true
        });
    } 
    // end fancybox

    // sliding-navigation (submenu)
    if (opts.slidingnavigation) {
        // creates the target paths
        var list_elements = "ul.sliding-navigation li.sliding-element";
        var link_elements = list_elements + " a";

        // initiates the timer used for the sliding animation
        var timer = 0;

        // creates the slide animation for all list elements 
        $(list_elements).each(function (i) {
            // margin left = - ([width of element] + [total vertical padding of element])
            $(this).css("margin-left", "-180px");
            // updates timer
            timer = (timer * 0.5 + 150);
            $(this).animate({ marginLeft: "0" }, timer);
            $(this).animate({ marginLeft: "15px" }, timer);
            $(this).animate({ marginLeft: "0" }, timer);
        });

        // creates the hover-slide effect for all link elements 		
        $(link_elements).each(function (i) {
            $(this).hover(
		        function () {
		            $(this).animate({ paddingLeft: 35 }, 100);
		        },
		        function () {
		            $(this).animate({ paddingLeft: 30 }, 100);
		        }
		    );
        });
    }
    //end sliding navigation

    // acmenu
    if (opts.acmenu) {
        $('#acmenu ul').hide();
        /*$('#acmenu ul:first').show();*/
        $('#acmenu li.active').parent().show();
        $('#acmenu li.active ul').show();
        $('#acmenu li a.nosite').click(
            function () {
                var checkElement = $(this).next();
                if ((checkElement.is('ul')) && (checkElement.is(':visible'))) {
                    return false;
                }
                if ((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
                    $('#acmenu ul:visible').slideUp('normal');
                    checkElement.slideDown('normal');
                    return false;
                }
            }
        );
    }
    // end acmenu

    // editioContentToggle
    if (opts.editioContentToggle) {
        var list = $('.e_togglelist');
        if (list.length > 0) {
            list.editioContentToggle(opts.editioContentToggleSettings);
        }
    }
    // end editioContentToggle

    // Facebook JS API

    if (opts.facebookSDK) {
        window.fbAsyncInit = function () {
            FB.init({ status: true, cookie: true, xfbml: true });
        };
        (function () {
            var e = document.createElement('script'); e.async = true;
            e.src = document.location.protocol + '//connect.facebook.net/de_DE/all.js';
            var fbroot = document.getElementById('fbroot');
            fbroot.appendChild(e);
            var f = document.createElement('script'); f.async = true;
            f.src = '/scripts/editio_fbhelper.js';
            fbroot.appendChild(f);
        } ());
    }

    // end Facebook JS API

};                    // end jQuery.editioSite

