var flashProxy = false;

$(document).ready(function(){
    if ($("#hotelteaser").length > 0) {
        $("#hotelteaser > .hotel > div").fadeTo("fast", 0.9);
        $("#hotelteaser > .hotel > div").hover(
            function () { 
            	$(this).children(".hdesc").addClass("hover");
            	$(this).fadeTo("normal", 1);
            },
            function () { 
            	$(this).children(".hdesc").removeClass("hover");
            	$(this).fadeTo("normal", 0.9); 
            }
        );
        //Hotelteaser verschieben
        if ($("#emotionHome,#presseNavi,#beLogin").length > 0) {
            $("#hotelteaser").css("top", "252px");
        } else if ($("#emotionHotel").length > 0 && $("#videoButtons").length > 0) {
            $("#hotelteaser").css("top", "646px");
        } else if ($("#emotionHotel").length > 0) {
        	$("#hotelteaser").css("top", "617px");
        } else if ($("#hotelteaser").length > 0) {
            $("#hotelteaser").css("top", "588px");
        }

        $("#hotelteaser").show();
    }

    if ($("#laenderNavi > ul > li.active").length > 0) {
        $("#hotelNavi").css("top", $("#laenderNavi > ul > li.active").position().top+28+"px");
        jQuery.each($("#hotelNavi > ul > li > a > strong"), function() {
            //Höhenprüfung
            if ($(this).height() < 36)
                $(this).parent("a").height($(this).parent("a").height() - 28);
            if ($(this).height() > 36 || $(this).width() > 148)
                $(this).parent("a").height($(this).parent("a").height() + 28);
        });
        $("#hotelNavi").css("visibility", "visible");
    }

    if ($("#presseNavi > ul > li.current").length > 0) {
        if ($("#presseClippings").length > 0 ||
        	$("#newsletterArchive").length > 0) {
        	
        	var layerID = '';
        	if ($("#presseClippings").length > 0)
        		layerID = '#presseClippings';
        	else
        		layerID = '#newsletterArchive';
        	
            $(layerID).css("top", $("#presseNavi > ul > li.current").position().top+28+"px");
            jQuery.each($(layerID + " > ul > li > a > strong"), function() {
                //Höhenprüfung
                if ($(this).height() < 36)
                    $(this).parent("a").height($(this).parent("a").height() - 28);
                if ($(this).height() > 36 || $(this).width() > 317)
                    $(this).parent("a").height($(this).parent("a").height() + 28);
            });
            $(layerID).css("visibility", "visible");
        } else if ($("#nlAnmeldung").length > 0) {
            $("#nlAnmeldung").css("top", $("#presseNavi > ul > li.current").position().top+28+"px");
            $("#nlAnmeldung").css("visibility", "visible");
        }
    }

    if ($("#kontinentNavi,#reisespecialNavi").length > 0) {
        jQuery.each($("#kontinentNavi > div > h3,#reisespecialNavi > div > h3"), function() {
            $(this).click(
                function () {
                    if (activeKontinent == $(this).attr("id")) {
                        $(this).removeClass("active");
                        $("#" + $(this).attr("id") + " + div").hide();
                        if (flashProxy)
                            flashProxy.call('zoomOut');
                        activeKontinent = null;
                    } else {
                        $("#" + $(this).attr("id") + " + div").css("top", $(this).position().top+28+"px");
                        if (activeKontinent != null) {
                            $("#" + activeKontinent).removeClass("active");
                            $("#" + activeKontinent + " + div").hide();
                        }
                        $(this).addClass("active");
                        $("#" + $(this).attr("id") + " + div").show();
                        if (flashProxy)
                            flashProxy.call('zoomRegion', $(this).text());
                        activeKontinent = $(this).attr("id");
                    }
                }
            );
        });
        if (typeof $("#" + activeKontinent).position() != "undefined") {
        	if ($("#" + activeKontinent + " > h3").length > 0)
        		$("#" + activeKontinent + " > h3").addClass("active");
        	else
        		$("#" + activeKontinent).addClass("active");

        	$("#" + activeKontinent + " + div").css("top", $("#" + activeKontinent).position().top+28+"px");
            $("#" + activeKontinent + " + div").show();
        }
    }

    if ($("#frmSearch_freeText").length > 0) {
        $("#frmSearch_freeText").autocomplete(makeUrl({module: "object", action: "gettitle"}), {
            minChars: 3,
            scroll: false,
            width: 337
        });

    }

    if ($("a[rel^='prettyPhoto']").length > 0) {
        $("a[rel^='prettyPhoto']").prettyPhoto({
            animationSpeed: 'slow',
            padding: 40,
            opacity: 0.5,
            showTitle: true,
            allowresize: false,
            counter_separator_label: ' / '
        });
    }

    if ($("#angebotAnfordernBox").length > 0) {
        $("#angebotAnfordernBox").dialog({
            autoOpen: false,
            draggable: false,
            resizable: false,
            modal: true,
            buttons: {
                "Reiseziel merken": function() {
                    if (frmObjectId != false)
                        jQuery.fn.objectMgrNotice(frmObjectId);
                },
                "Angebot anfordern": function() {
                    if (frmObjectId != false)
                        window.location.href = makeUrl({module: "contactus"}) + 'frmObjectId/' + frmObjectId + '/';

                }
            },
            overlay: {
                opacity: 0.5,
                background: "black"
            },
            position: ['center', 150]
        });
    }

    if ($("#deleteHotelBox").length > 0) {
        $("#deleteHotelBox").dialog({
            autoOpen: false,
            draggable: false,
            resizable: false,
            modal: true,
            buttons: {
                "Hotel löschen": function() {
                    if (frmClickedHotel != false &&
                        jQuery.fn.objectMgrDelete($(frmClickedHotel).attr('objectid'))) {

                        $(frmClickedHotel).parent("div").parent("div").remove();
                    }

                    if ($('.hotel').length == 1) {
                        $('.hotel').children('div').children('.hdel').html('&nbsp;');
                    }

                    $(this).dialog("close");
                },
                "Schliessen": function() {
                    $(this).dialog("close");

                }
            },
            overlay: {
                opacity: 0.5,
                background: "black"
            },
            position: ['center', 150]
        });
    }

    jQuery.each(['angebotSuccessBox', 'angebotErrorBox'], function(index, value) {
        if ($("#"+value).length > 0) {
            $("#"+value).dialog({
                autoOpen: false,
                draggable: false,
                resizable: false,
                modal: true,
                overlay: {
                    opacity: 0.5,
                    background: "black"
                },
                position: ['center', 150]
            });
        }
    });

    if ($("#messageBox").length > 0) {
        $("#messageBox").dialog({
            autoOpen: true,
            draggable: false,
            resizable: false,
            height: 'auto',
            width: 400,
            modal: true,
            buttons: {
                "Schliessen": function() {
                    $(this).dialog("close");
                }
            },
            overlay: {
                opacity: 0.5,
                background: "black"
            },
            position: ['center', 150]
        });
    }

    //Messen der Seitenhöhe und Contenthöhe anpassen
    if ($(".messTop").length > 0) {
        var posBottomMax = 0;
        var sDivName = '';
        $(".messTop").each(function(){
            if (posBottomMax < $(this).offset().top + $(this).height()) {
                posBottomMax = $(this).offset().top + $(this).height();
                sDivName = $(this).attr("id");
            }
        });

        if (posBottomMax > 0) {
            if ($("#contentText").length > 0) {
                var iHeightDiv = posBottomMax - $("#contentText").offset().top;
                var iPadding = parseInt($("#contentText").css("paddingTop"));
                $("#contentText").css("height", (iHeightDiv - iPadding - 5)+"px");
            }
        } else {
            $("#" + sDivName).css("paddingBottom", "27px");
        }
    }
    
    //Check if Centerimage is set
    if ($(".teaserPhoto").length > 0 && $("#flashPhoto0").length > 0) {
    	$(".teaserPhoto").css("background", "url(" + $("#flashPhoto0").attr("href") + ") no-repeat center");
    }
    
    // Styles all checkboxes
    $(document).cssCheckbox();
});

function slideUpDown(blockName) {
    $("#"+blockName).slideToggle('slow');
    $(document).bind(($.browser.opera ? "keypress" : "keydown"), function(e) {
        if (e.keyCode == 27) $("#"+blockName).hide();
    });
}

function updateHiddenInput(elLi, blockName) {
    $("#"+$(elLi).attr('fieldname')).val($(elLi).attr('svalue'));
    $("#"+blockName).prev("div").html($(elLi).html());
    $("#"+blockName).slideToggle("slow");
}

function GroupDelegate(id) {
    $('#'+id).click();
}

function showDialog(id) {
    if ($("#"+id).length > 0) {
        $("#"+id).dialog("open");
    }
}

// dummyclass
function switchRowColorOnHover() {}

//  calling -> makeUrl({'module':'mymodule', 'action':'generateReport', 'param2': 'foo bar'});
function makeUrl(params)
{
    var rslt = SGL_JS_WEBROOT + '/' + SGL_JS_FRONT_CONTROLLER;
    var moduleName  =  (params.module) ? params.module : '';
    var managerName =  (params.manager) ? params.manager : moduleName;

    switch (SGL_JS_URL_STRATEGY) {
    case 'SGL_UrlParser_ClassicStrategy':
        if (rslt.charAt(rslt.length - 1) != '?') {
          rslt += '?';
        }
        rslt += 'moduleName=' + escape(moduleName) + '&managerName=' + escape(managerName) + '&';
        for (x in params) {
            if ((x == 'module') || (x =='manager')) {
                continue;
            }
            rslt += escape(x) + '=' + escape(params[x]) + '&';
        }
        break;

    default:
        if (SGL_JS_FRONT_CONTROLLER != "")
            rslt += '/';

        rslt += escape(moduleName) + '/' + escape(managerName) + '/';
        for (x in params) {
            if ((x == 'module') || (x =='manager')) {
                continue;
            }
            rslt += escape(x) + '/' + escape(params[x]) + '/';
        }
        break;
    }

    return rslt;
}