
jQuery.fn.pSlide = function() {
    var id = $(this).attr("id");
    if( $('#pSlide'+id).hasClass('active') ){
        $('.pSlide.default').slideToggle("slow");
        $('.pSlide.default').toggleClass("active");
    }else{
        $('.pSlide.active').slideToggle("slow");
        $('.pSlide.active').toggleClass('active');
    }
    $('#pSlide'+id).slideToggle("slow");
    $('#pSlide'+id).toggleClass('active');
};
jQuery.fn.pDialog = function() {
    var modal_height    = $(this).outerHeight();
    var modal_width     = $(this).outerWidth();
    var posTop     = ($(window).height()-modal_height)/2;
    var posLeft    = ($(window).width()-modal_width)/2;
    if($(this).css('position') == 'absolute'){
        posTop = posTop + $(window).scrollTop();
    }

    if( !$("#pDialogMask").length ){
        $('body').append('<div id="pDialogMask"></div>');
        $("#pDialogMask").click(function(){
            $('.modal').each(function(){
                $(this).pCloseDialog();
            });
        });
        $(".pDialogClose").click(function(){
            $('.modal').each(function(){
                $(this).pCloseDialog();
            });
        });
    }
    $('#pDialogMask').css({'width':$(window).width(),'height':$(document).height()});
    $('#pDialogMask').css({'display' : 'block', opacity : 0});
    $('#pDialogMask').fadeTo(500,0.8);

    $(this).css({'top' : posTop , 'left' : posLeft});
    $(this).fadeIn(500);
};
jQuery.fn.pCloseDialog = function() {
    $('#pDialogMask').fadeOut(500);
    $('.modal').fadeOut(500);
};
jQuery.fn.pBubbleMessage = function() {
        var id = $(this).attr("id");
        if( id == "" ){
            return;
        }
        // Erzeuge BubbleMessage, falls noch nicht erzeugt
        if( !$("#bm"+id).length ){
            $(this).append('<div class="pbm" id="bm'+id+'"></div>');
            if( $("#"+id+" .pBubbleMessage").hasClass('trans') ){
                $('#bm'+id).css({opacity : 0.8});
                //$("#bm"+id).addClass('trans');
            }
            if($("#"+id+" .pBubbleMessage").hasClass('bottom')){
                $("#bm"+id).css({
                            'top'       : $(this).outerHeight() - 2,
                            'right'     : -10});
                $("#bm"+id).append('<div class="arrowtop"></div>');
                $("#bm"+id).append('<div class="contentbottom"></div>');
                $("#bm"+id+" .contentbottom").html($("#"+id+" .pBubbleMessage").html());
            }else{
                $("#bm"+id).css({
                            'top'       : 5,
                            'right'     : $(this).outerWidth() - 3});
                $("#bm"+id).append('<div class="content"></div>');
                $("#bm"+id).append('<div class="arrow"></div>');
                $("#bm"+id+" .content").html($("#"+id+" .pBubbleMessage").html());
            }
        }
        $("#bm"+id).fadeIn(100);
};
jQuery.fn.pCloseBubbleMessage = function() {
    var id = $(this).attr("id");
    if( id == "" ){
        return;
    }
    $("#bm"+id).fadeOut(100);
};

/*  */
$(function(){
    $(".pSlider").click(function(){
        $(this).pSlide();
    });

    $(".jqLogin").click(function(){
	$("#LoginDialog").pDialog();
    });

    $('<img />').attr('src', '/mall/1/css/screen/images/loading.gif').css({
        'display' : 'none'
    }).appendTo('body');
    
    $(".cartForm").submit(function(){
	$("#LoadingDialog").pDialog();
    });

    $(".jqRecommend").click(function(){
	$("#RecommendDialog").pDialog();
    });
    $(".jqCreditConditions").click(function(){
	$("#CreditConditionsDialog").pDialog();
    });
    var bmActive = false;
    $(".hasBubbleMessage").hover(function(){
        var obj = this;
        bmActive = setTimeout(function(){$(obj).pBubbleMessage();}, 500);
    },function(){
        try{clearTimeout(bmActive);}catch(e){};
        $(this).pCloseBubbleMessage();
    });
    $("a.image_group").fancybox({
	'zoomSpeedIn': 300,
	'zoomSpeedOut': 300,
	'overlayShow': true
    });

    $(".jqdd").click(function(){
        var id = $(this).attr("id");
        $('#'+id+' .ddItem').toggleClass("visible");
	$('#'+id+' .adItem').toggleClass("active");
        if ($('#'+id+' .ddItem').hasClass('visible')) {
            if ($('#'+id).outerHeight() > 250) {
                $('#'+id).addClass('scrollable');
            }
        } else {
            $('#'+id).removeClass('scrollable');
        }
    });
});
