/*
	========================
	User Interface Functions
	=================================================================
	
	Description:	Userinterface Scripts für Lucia Cosmetic
	
	-----------------------------------------------------------------
	
	Dependencies:	Benötigt jQuery
	-----------------------------------------------------------------
	
	Author:			Sascha Merkofer, end if Design & Code

	Datum:			26. 11. 2009

	Version:		0.1
	
	-----------------------------------------------------------------
	
*/


/*	-----------------------------------------------------------------
	Window ready
	----------------------------------------------------------------- */

$(window).load(function () {

/*	-----------------------------------------------------------------
	Print document if URL contains #Print
	----------------------------------------------------------------- */
	var dsprint_match = document.URL.search( /\&Print/ );
    if ( dsprint_match >= 0 ) {
        window.print();
    }

/*	-----------------------------------------------------------------
	Philosophie, Lichtwässer Popups
	----------------------------------------------------------------- */
    
    if ( $("a.Licht-Trigger").length > 0 ) {
    
        $("div#Inhaltsstoffe-Popup").css({'visibility': 'visible', 'opacity': 0});
        
        $("a.Licht-Trigger").mouseenter( function() {
            
            $("div#Inhaltsstoffe-Popup").css({
                'left': ( $(this).offset().left + ( $(this).width() / 2 ) - 113 ) + 'px',
                'top': ( $(this).offset().top - $("div#Inhaltsstoffe-Popup").height() - 18 ) + 'px',
                'opacity': 0
            })
            .stop()
            .animate({
                'opacity': 1
            }, 800);
        })
        .mouseleave( function() {
            $("div#Inhaltsstoffe-Popup").stop().animate({
                'opacity': 0
            }, 200, function() {
                $("div#Inhaltsstoffe-Popup").css({'left': '-900px'});
            });
        })
        .click( function() {
            return false;
        });
    }

});


/*	-----------------------------------------------------------------
	Document Ready
	----------------------------------------------------------------- */

$(document).ready(function () {


/*	-----------------------------------------------------------------
	View Status ermitteln
	----------------------------------------------------------------- */
	
	var dsfe_view = 'show';
    var dsfe_view_match = document.location.search.substr(1).match( /dsfe_view=[^#&\s]*/ );
    if ( dsfe_view_match ) {
        dsfe_view = dsfe_view_match.toString().replace(/dsfe_view=/, '');
        
        // Check, if we're in drag-list mode
        var dsfe_drag_match = document.location.search.substr(1).match( /dsfe_edit_id=drag-list-[^#&\s]*/ );
        if ( dsfe_drag_match ) {
            dsfe_view = 'show';
        }
    }
    
	
	
/*	-----------------------------------------------------------------
	Weiteren Inhalt ein-/ausblenden
	----------------------------------------------------------------- */
	
	if ( dsfe_view == 'show' ) {
    	$("div.inhalt-ein-ausblenden").css({
    	   'display': 'none'
        })
        .before('<p><a href="#" class="inhalt-einblenden">mehr lesen</a></p>');
        
        $("a.inhalt-einblenden").click( function() {
            $("div.inhalt-ein-ausblenden").slideDown("fast");
            $(this).parent().slideUp("fast");
            return false;
        });
    }
    
    
/*	-----------------------------------------------------------------
	Produkte - Inhaltsstoffe, Anwendung und Details ein-/ausblenden
	----------------------------------------------------------------- */
	
	if ( dsfe_view == 'show' && $("ul.Produkte-Liste").length > 0 ) {
	
    	$("h4.Produkt-Inhaltsstoffe-Titel").map( function() {
            var ihs_p = $("p#" + $(this).attr("id").substr(1));
            ihs_p.css({
                'display': 'none'
            });
            $(this).click( function() {
                if ( ihs_p.css('display') == 'none' ) {
                    ihs_p.stop().css({'opacity': 0, 'margin-bottom': '0px'}).slideDown("fast").animate({
                        opacity: 1,
                        marginBottom: '36px'
                    }, 200 );
                    $(this).addClass('Popup-Link-Active');
                }
                else {
                    ihs_p.stop().animate({
                        opacity: 0,
                        marginBottom: '0px'
                    }, 200 ).slideUp("fast");
                    $(this).removeClass('Popup-Link-Active');
                }
                return false;
            })
            .addClass('Popup-Link');
        });
        
        $("h4.Produkt-Anwendung-Titel").map( function() {
            var ihs_p = $("p#" + $(this).attr("id").substr(1));
            ihs_p.css({
                'display': 'none'
            });
            $(this).click( function() {
                if ( ihs_p.css('display') == 'none' ) {
                    ihs_p.stop().css({'opacity': 0, 'margin-bottom': '0px'}).slideDown("fast").animate({
                        opacity: 1,
                        marginBottom: '36px'
                    }, 200 );
                    $(this).addClass('Popup-Link-Active');
                }
                else {
                    ihs_p.stop().animate({
                        opacity: 0,
                        marginBottom: '0px'
                    }, 200 ).slideUp("fast");
                    $(this).removeClass('Popup-Link-Active');
                }
                return false;
            })
            .addClass('Popup-Link');
        });
        
        $("h4.Produkt-Details-Titel").map( function() {
            var ihs_div = $("div#" + $(this).attr("id").substr(1));
            ihs_div.css({
                'display': 'none'
            });
            $(this).click( function() {
                if ( ihs_div.css('display') == 'none' ) {
                    ihs_div.stop().css({'opacity': 0, 'margin-bottom': '0px'}).slideDown("fast").animate({
                        opacity: 1,
                        marginBottom: '36px'
                    }, 200, "linear", function() {$(this).removeAttr("filter");});
                    $(this).addClass('Popup-Link-Active');
                }
                else {
                    ihs_div.stop().animate({
                        opacity: 0,
                        marginBottom: '0px'
                    }, 200 ).slideUp("fast");
                    $(this).removeClass('Popup-Link-Active');
                }
                return false;
            })
            .addClass('Popup-Link');
        });
        
    
/*	-----------------------------------------------------------------
	Inhaltsstoffe Popups
	----------------------------------------------------------------- */
        
        $("div#Inhaltsstoffe-Popup").css({'opacity': 0});
        
        $("a.Inhaltsstoffe-Trigger").mouseenter( function() {
            
            $("span#Inhaltsstoffe-Bubble").html($("li#" + $(this).attr('id').substr(1)).html());
            $("div#Inhaltsstoffe-Popup").css({
                'left': ( $(this).offset().left + ( $(this).width() / 2 ) - 113 ) + 'px',
                'top': ( $(this).offset().top - $("div#Inhaltsstoffe-Popup").height() - 18 ) + 'px',
                'opacity': 0
            })
            .stop()
            .animate({
                'opacity': 1
            }, 800);
        })
        .mouseleave( function() {
            $("div#Inhaltsstoffe-Popup").stop().animate({
                'opacity': 0
            }, 200, function() {
                $("div#Inhaltsstoffe-Popup").css({'left': '-900px'});
            });
        })
        .click( function() {
            return false;
        });


/*	-----------------------------------------------------------------
	Warenkorb Popups
	----------------------------------------------------------------- */
	
        // Render the bubble
        $("body").append('<div id="Warenkorb-Popup"><span id="Warenkorb-Bubble"><span id="Warenkorb-Loading"><span id="Warenkorb-Inhalt"></span></span></span></div>');
        $("body").append('<div id="Warenkorb-Moving"></div>');
        $("div#Warenkorb-Moving").css({'display': 'none'});
        $("div#Warenkorb-Popup").css({'opacity': 0});
        $("span#Warenkorb-Loading").css({'display': 'block'});
        $("span#Warenkorb-Inhalt").css({'display': 'none'}).html('&nbsp;');
        
        $("a.Warenkorb-Button").mouseenter( function() {
            var p_id = $(this).attr('id');
            $("span#Warenkorb-Inhalt").css({'display': 'none'}).html('&nbsp;');
            $("div#Warenkorb-Popup").css({
                'left': ( $(this).offset().left + ( $(this).width() / 2 ) - 113 ) + 'px',
                'bottom': ( $('body').height() - $(this).offset().top ) + 'px'
            })
            .stop()
            .animate({
                'opacity': 1
                }, 800, function() {
                    GetCart( p_id );
                }
            );
        })
        .mouseleave( function() {
            $("div#Warenkorb-Popup").stop().animate({
                'opacity': 0
            }, 400, function() {
                $("div#Warenkorb-Popup").css({'left': '-900px'});
                $("span#Warenkorb-Inhalt").css({'display': 'none'}).html('&nbsp;');
            });
        })
        .click( function() {
            AddProduct( $(this).attr('id') );
            return false;
        });


/*	-----------------------------------------------------------------
	Warenkorb Button - position
	----------------------------------------------------------------- */
        
        if ( $("div#Navi-Warenkorb").length > 0 ) {
            
            WarenkorbBtnPos();
        
        }        

    } // END if ( dsfe_view == 'show' )
    
    
}); // END $(document).ready()



// Warenkorb Produkt hinzufügen
// ----------------------------

function AddProduct( p_id ) {
    
    var post_url = '/AjaxDsCart.php';
    
    $.post( post_url, { dscart_mode: 'cart_bubble', dscart_action: 'cart_add', dscart_product: p_id, dscart_p_count: 1 },
        function(data){
            
            // Display the button/link to the cart
            if ( $("div#Navi-Warenkorb").length < 1 ) {
                $("div#Navigation").append('<div id="Navi-Warenkorb"><a href="/warenkorb" id="Button-Warenkorb">zum Warenkorb</a></div>');
                 WarenkorbBtnPos();
                 $(window).scrollTop($(window).scrollTop() + 1);
                 $(window).scrollTop($(window).scrollTop() - 1);
                 WarenkorbBtnFlash();
            }
            
            // Animate the cart-icon to the cart
            WarenkorbAnimate( p_id );
            
            $("span#Warenkorb-Inhalt").html( data )
            .slideDown('fast');
            
        }, "html");
}

// Warenkorb Produkt holen
// -----------------------

function GetCart( p_id ) {
    
    var post_url = '/AjaxDsCart.php';
    
    $.post(post_url, { dscart_mode: 'cart_bubble', dscart_action: 'cart_get', dscart_product: p_id },
        function(data){
            $("span#Warenkorb-Inhalt").html( data )
            .slideDown('fast', function() {
                // IE7 bugfix
                $(this).css({'opacity': 1});
            });
        }, "html");
    
}


// Warenkorb Icon animieren
// ------------------------

function WarenkorbAnimate( p_id ) {
    
    var icon_top = $("a#" + p_id).offset().top + 'px';
    var icon_left = $("a#" + p_id).offset().left + 'px';
    var target_top = $("a#Button-Warenkorb" ).offset().top + 'px';
    var target_left = $("a#Button-Warenkorb" ).offset().left + 100 + 'px';
    
    $("div#Warenkorb-Moving").css({'display': 'block', 'opacity': .8, 'top': icon_top, 'left': icon_left})
    .animate({
        'top': target_top,
        'left': target_left,
        'opacity': .5
    }, 400, "swing", function() {
        // Flash the cart button/link
        WarenkorbBtnFlash();
        $("div#Warenkorb-Moving").css({'display': 'none', 'top': '200px', 'left': '-900px'})
    });
    
}


// Warenkorb Button immer sichtbar positionieren
// ---------------------------------------------

function WarenkorbBtnPos() {
    
    $(window).scroll(function() {
        $('a#Button-Warenkorb').css('top', $(this).scrollTop() + "px");
    });
    
}


// Warenkorb Button animieren
// --------------------------

function WarenkorbBtnFlash() {
    
    $("a#Button-Warenkorb").animate({
        'opacity': 0.2
        },50)
    .animate({
        'opacity': 1
    },100)
    .animate({
        'opacity': 0.4
    },50)
    .animate({
        'opacity': 1
    },100);
    
}


/*	-----------------------------------------------------------------
	Warenkorb Ajax-Update
	----------------------------------------------------------------- */

$(document).ready( function() {
    
    if ( $("form#Warenkorb-Formular").length > 0 ) {
        UpdateListeners();
    }
    
});


function UpdateListeners() {
    
    var post_url = '/AjaxDsCart.php';
        
    $("input.dsCart-P-Count-Input").change( function() {
        var update_obj = $(this).attr("id");
        $.post(post_url, { dscart_mode: 'cart_update', dscart_action: 'cart_set', dscart_product: update_obj, dscart_p_count: $(this).val() },
            function(data){
                $("form#Warenkorb-Formular").html( data );
                UpdateListeners();
                $("input#" + update_obj).focus();
            }, "html");
    });
    
}

/*	-----------------------------------------------------------------
	Kontaktabgaben Lieferadresse ein-/ausblenden
	----------------------------------------------------------------- */

$(document).ready(function () {
	
	if ( $("form#Benutzerangaben-Formular").length > 0 ) {
        var check_delivery_val = $("input#Lieferung-Vorname").val() + $("input#Lieferung-Nachname").val() + $("input#Lieferung-Strasse").val() + $("input#Lieferung-Hausnummer").val() + $("input#Lieferung-PLZ").val() + $("input#Lieferung-Ort").val();
        if ( check_delivery_val == '' ) {
            $("div#Liefer-Adresse").css({'display': 'none'});
            $("span#Lieferadresse-Einblenden").append(' &nbsp; <a id="Lieferadresse-Trigger" href="#" class="button-pyramid">ändern</a>');
            $("a#Lieferadresse-Trigger").click( function() {
                LieferadresseHide();
            });
        }
        else {
            $("span#Lieferadresse-Einblenden").html(' &nbsp; <a id="Lieferadresse-Trigger" href="#" class="button-pyramid">Rechnungsadresse verwenden</a>');
            $("a#Lieferadresse-Trigger").click( function() {
                LieferadresseShow();
            });
        }
            
    } // END form.length > 0
    
}); // END document.ready()

function LieferadresseHide() {
    
    $("div#Liefer-Adresse").slideDown('fast');
    $("span#Lieferadresse-Einblenden").html(' &nbsp; <a id="Lieferadresse-Trigger" href="#" class="button-pyramid">Rechnungsadresse verwenden</a>');
    $("a#Lieferadresse-Trigger").click( function() {
        LieferadresseShow();
    });
    return false;
}

function LieferadresseShow() {
    // Set button
    $("span#Lieferadresse-Einblenden").html('(gleich wie Rechnungsadresse) &nbsp; <a id="Lieferadresse-Trigger" href="#" class="button-pyramid">ändern</a>');
    // Clear fields
    $("input#Lieferung-Anrede-Herr").val([]);
    $("input#Lieferung-Anrede-Frau").val([]);
    $("input#Lieferung-Vorname").val('');
    $("input#Lieferung-Nachname").val('');
    $("input#Lieferung-Strasse").val('');
    $("input#Lieferung-Hausnummer").val('');
    $("input#Lieferung-PLZ").val('');
    $("input#Lieferung-Ort").val('');
    $("input#Lieferung-Land").val(['Schweiz']);
    // slide up
    $("div#Liefer-Adresse").slideUp('fast');
    // add listener
    $("a#Lieferadresse-Trigger").click( function() {
        LieferadresseHide();
    });
}


