$(document).ready(function() {

	var ie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
	var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);

	if ($.browser.msie && (ie55 || ie6)) {

        $("#logo, #slogan").each(function() {
			$(this).css("filter", "progid:DXImageTransform.Microsoft.AlphaImageLoader" + "(src='" + $(this).attr("src") + "', sizingMethod=\'scale\');");
			$(this).attr("src", "/wp-content/themes/sue/_ext/blank.gif");
        });
    }

    //nav hover
    $("#nav li").hover(function() {
        $(this).css("backgroundImage", "url(/wp-content/themes/sue/_img/nav/over.png)");
    }, function() {
        $(this).css("backgroundImage", "none");
    });
    
    $(".teaser").hover(function() {
        $(this).addClass("h");
    }, function() {
        $(this).removeClass("h");
    });

    //sponsoren
    $("#sp").each(function() {
        //$(this).children(".sponsor").hide();
        $(this).children(".sponsor:first").show();
        setTimeout("Sponsors.loop()", 8000);
    });

    //termine
    $(".tl-event").click(function() {
        $(".tl-event .t-content").hide();
        $(this).children(".t-content").show(500);
    });
    if (document.location.hash) {
        $(document.location.hash + " .t-content").show();
    }
    
    //SAB
    $("#sab-a").click(SAB.store);
    $("input[name='sab-proto']").live("click", SAB.prefillText);
    $("#sab-image").change(SAB.upload.start);
    
    $("#SAB-tab-1, #SAB-tab-2").click(function() {
        SAB.openTab = $(this).attr("id").split("-")[2];
        $("#SAB-tab-content-2").toggle();
        $("#SAB-tab-content-1").toggle();
        $("#SAB-tab-1, #SAB-tab-2").toggleClass("active");
    });
});

var Sponsors = {
    loop: function() {
        $("#sp").children(".sponsor:first").hide().remove().appendTo("#sp").fadeIn(2500, function() { setTimeout("Sponsors.loop()", 8000); });
    }
}

var SAB = {
    openTab: 1,
    store: function() {
        if (SAB.upload.isInProgress == true) {
            alert("Dein Bild wird gerade hochgeladen. Bitte nach dem Hochladen noch einmal auf Absenden klicken.");
            return;
        }
        if (!$("#sab-email").val().match(/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i)) {
            alert("Bitte eine gültige Email-Adresse eingeben!");
            return;
        }
        if (!$("#sab-description").val()) {
            alert("Bitte eine Beschreibung eingeben!");
            return;
        }
        var obj = {};
        obj.action = "sab_store";
        obj.title = $("#sab-title").val();
        obj.email = $("#sab-email").val();
        obj.description = $("#sab-description").val();
        obj.image = $("#sab-H-image").val();
        obj.proto = $("input[name='sab-proto']:checked").val();
        obj.submitted = true;
        
        SAB.openTab = (obj.proto == "biete") ? 1 : 2;
        $.post("/wp-admin/admin-ajax.php", obj, SAB.postStore);
    },
    
    postStore: function(r) {
        $("#sab-email").val("");
        $("#sab-title").val("");
        $("#sab-description").val("");
        $("#sab-image").val("");
        $("input[name='sab-proto']").attr("checked", false);
        if (r != "Error") {
            $("#SAB").html(r);
        }

        $("#SAB-tab-content-1, #SAB-tab-content-2").hide();
        $("#SAB-tab-content-" + SAB.openTab).show();
        $("#SAB-tab-1, #SAB-tab-2").removeClass("active");
        $("#SAB-tab-" + SAB.openTab).addClass("active");
        window.location.hash = "sabresults";
    },
    
    prefillText: function() {
        if ($("#sab-description").val() == "" ) {
            $("#sab-description").val("Ich " + $(this).val());
        }
    },

    upload: {
        isInProgress: false,
        done: function(pic) {
            $("#upload-progress").hide();
            $("#upload-result").html("<img src=\"/thumb.php?src=/wp-content/uploads/sab/" + pic + "&w=100&zc=1\" width=\"100\"/>").show();
            $("#sab-image").val("");
            $("#sab-H-image").val(pic);
            SAB.upload.isInProgress = false;
        },
        start: function() {
            $("#upload-form").submit();
            $("#upload-progress").show();
            $("#upload-form").hide();
            $("#upload-result").html("").hide();
            $("#sab-H-image").val("");
            SAB.upload.isInProgress = true;
        },
        error: function() {
            $("#upload-progress").hide();
            $("#upload-form").show();
            $("#sab-image").val("");
            SAB.upload.isInProgress = false;
            alert("Es ist ein Fehler aufgetreten. Bitte versuchen Sie es noch einmal.");
        }
    }
    
};
