jQuery(function () {
/********** EffectImgOverlay **********/
    jQuery(".EffectImgOverlay").each(function () {
        var EffectImgOverlay = jQuery(this);
        var Description = EffectImgOverlay.attr("alt");

        var WrapDiv = jQuery("<div class='EffectImgOverlay'>");
        var DescriptionSpan = jQuery("<span>" + Description + "</span>");
        DescriptionSpan.width(EffectImgOverlay.width() - 10);
        EffectImgOverlay.removeClass("EffectImgOverlay").wrap(WrapDiv).after(DescriptionSpan);
        EffectImgOverlay.hoverIntent(function (e) {
            jQuery(this).next('span').fadeOut(650);
        }, function (e) {
            if (e.relatedTarget.tagName != "SPAN")
                jQuery(this).next('span').fadeIn();
            else {
                jQuery(e.relatedTarget).mouseleave(function (e) {
                    if (e.relatedTarget.tagName != "IMG")
                        jQuery(this).fadeOut();
                });
            }
        });
    });

/********** EffectPopup **********/	
		jQuery(".EffectPopup").each(function(i){
			jQuery(this).wrap("<div id='Popup" + i + "'>");
			var Container = jQuery(this).closest("div");
			var PopupWidth = jQuery(this).width();
			var PopupContent1 = jQuery("td:eq(0)", this).html();
			var PopupContent2 = jQuery("td:eq(1)", this).html();
			jQuery(this).remove();
			var Content = jQuery("<div style='display:none;'><div id='Popup_" + i +"' style='width:" + PopupWidth + "px;'>" + PopupContent2 + "</div></div>");
			Container.append(PopupContent1);
			jQuery(Container).append(Content);
			var PopupButton = jQuery(".EffectPopupButton", Container).html();
			var Link = jQuery("<a href='#Popup_" + i +"'>" + PopupButton + "</a>").addClass("EffectPopupButton");
			jQuery(".EffectPopupButton", Container).replaceWith(Link);
		});
		
		jQuery("a.EffectPopupButton").fancybox({
        'transitionIn': 'elastic',
        'transitionOut': 'elastic',
        'speedIn': 600,
        'speedOut': 200,
        'overlayShow': true,
				'overlayOpacity': 0,
				'showCloseButton': true,
				'titleShow': false,
				'padding': 20
    });

/********** EffectLightbox **********/
		jQuery("a.EffectLightbox").fancybox({
        'transitionIn': 'elastic',
        'transitionOut': 'elastic',
        'speedIn': 400,
        'speedOut': 200,
        'overlayShow': true,
				'overlayOpacity': 0.6,
				'overlayColor': '#000',
				'showCloseButton': true,
				'padding': 0	
    });

    jQuery(".EffectLightbox").attr("rel", "Slideshow");


/********** EffectZoom **********/
    jQuery('.EffectZoom').each(function () {
        var options = {
            zoomWidth: jQuery('img', this).width() * 1.8,
            zoomHeight: jQuery('img', this).height() * 1.8,
            xOffset: 10,
            yOffset: 0,
            title: false,
            showEffect: "fadein",
            fadeinSpeed: "medium",
            hideEffect: "fadeout",
            fadeoutSpeed: "medium",
            position: "right"
        };
        jQuery(this).jqzoom(options);
    });

/********** EffectExpand **********/
    jQuery('.EffectExpandButton').click(function () {
        jQuery(this).closest('div').next('.EffectExpandContent').slideToggle();
    });

});<!-- close event -->

/********** GoogleMap **********/
// Extend javascript array with search function
Array.prototype.search = function (s, d) {
    var len = this.length;
    for (var i = 0; i < len; i++) {
        if (this[i][0] == s)
            return this[i][1];
    }
    return d;
}

/********** Youtube **********/
var ToFlexYoutube = {
    SwfObjectFilePath: "swfobject/swfobject.js",
    GetSwfUrl: function (url) {
        if (url.substr(0, 31) == "http://www.youtube.com/watch?v=") url = "http://www.youtube.com/v/" + url.substr(31);
        return url;
    },
    InitializePlayer: function () {
        if (typeof (swfobject) == "undefined") {
            jQuery.getScript(this.SwfObjectFilePath, function () {
                // If swfobject is not defined, load required script first
                this.EmbedSwfPlayer();
            });
        } else
            this.EmbedSwfPlayer();
    },
    EmbedSwfPlayer: function () {
        jQuery(".xEffectYoutube").each(function (i) {
            var EffYoutube = jQuery(this);
            var swfWidth = EffYoutube.width();
            var swfHeight = EffYoutube.height();
            var url = ToFlexYoutube.GetSwfUrl(jQuery.trim(jQuery(".xEffectYoutubeAddress", this).text()));
            var ID = "EffectYoutube_" + i;
            EffYoutube.after(jQuery("<div id='" + ID + "'></div>"));
            if (url.match(/^(ht|f)tps?:\/\/[a-z0-9-\.]+\.[a-z]{2,4}\/?([^\s<>\#%"\,\{\}\\|\\\^\[\]`]+)?$/))
                swfobject.embedSWF(url, ID, swfWidth, swfHeight, "8", "", {}, { menu: "false", loop: "false", wmode: "opaque" });
            else
                jQuery("#" + ID).text("Video konnte nicht geladen werden. Möglicherweise haben Sie eine falsche Adresse angegeben.");
            jQuery("#" + ID).wrap("<div>");
            EffYoutube.remove();
        });
    }
};

var ToFlexGMap = {
    GMapScriptURL: "http://maps.google.com/maps?file=api&v=2&async=2&key=",
    ScriptIsLoaded: false,
    InitializeMap: function () {
        if (typeof (EffGMapKeys) == "undefined")
            return false;
        var CurrentKey = EffGMapKeys.search(window.location.hostname, "");
        if (typeof (CurrentKey) == "undefined" || CurrentKey == "")
            return false;
        jQuery.getScript(ToFlexGMap.GMapScriptURL + CurrentKey + "&callback=ToFlexGMap.ApplyMap");
    },

    ApplyMap: function () {
        if (GBrowserIsCompatible()) {
            jQuery(".xEffectGMap").each(function (o) {
                var Table = jQuery(this);

                var Wrapper = jQuery("<div class='EffectGMapWrapper'></div>");
                Table.after(Wrapper);

                var ID = "EffectGMap_" + o;
                var Map = jQuery("<div class='EffectGMapContent' id='" + ID + "'></div>").width(Table.width()).height(Table.height()).appendTo(Wrapper);

                var Map = new GMap2(document.getElementById(ID));
                Map.addControl(new GLargeMapControl3D());
                if (Map.getSize().width > 300)
                    Map.addControl(new GMapTypeControl());
                var Geocoder = new GClientGeocoder();

                ToFlexGMap.RunAdditionalCode(Map, Table);

                var ReceivedMapData = [];
                jQuery("tr", Table).each(function (i) {
                    var MapData = {
                        Address: jQuery.trim(jQuery(".xEffectGMapAddress", this).text()),
                        BubbleText: jQuery(".xEffectGMapCallout", this).html()
                    };
                    if (Geocoder && (MapData.Address != "" || MapData.Address != " ")) {
                        Geocoder.getLatLng(MapData.Address, function (point) {
                            if (point) {
                                ReceivedMapData.push(point);
                                var marker = new GMarker(point);
                                Map.addOverlay(marker);
                                marker.bindInfoWindowHtml(MapData.BubbleText);
                            }
                        });
                    }
                });
                setTimeout(function () {
                    // Center the map after all data is collected
                    var bounds = new GLatLngBounds();
                    for (var i = 0; i < ReceivedMapData.length; i++)
                        bounds.extend(ReceivedMapData[i]);

                    var ZoomLevel = Map.getBoundsZoomLevel(bounds);
                    if (ReceivedMapData.length == 1)
                        ZoomLevel = 10;
                    Map.setCenter(bounds.getCenter(), ZoomLevel);
                }, 400);
                Table.remove();
            });
        }
    },
    RunAdditionalCode: function (Map, Table) {
        var code = jQuery(".xEffectGMapAdditionalCode", Table).text();
        if (jQuery.trim(code) != "")
            eval(code);
    }

};

//Initialize Player and Maps
jQuery(function () {
    ToFlexGMap.InitializeMap();
    ToFlexYoutube.InitializePlayer();
});
