/*
 * @author: Klaus Baake , Oliver Kreft
 * 
 * Eventhandling Wolford  
 */

var PRODUCT_ID = null ;
var POPUP = null ;
var RELOAD = null ;
var COLOR_IS_OUT = false;


$(document).ready(function(){
	
    productSuggestion('search_string');
    productSuggestion('search');
    initSlider();
    initSkuOptionJSEvent();
    initGreetingCardJS();
    
	// Customer öffnen im Header
	$("#header-cust").mouseover(function(){
	    $('#header-cust-box-login').removeClass('hide');
		$('#header-cust-box-welcome').removeClass('hide');		 
	});
	 
	//  Customer schliessen im Header
	$("#header-cust").mouseout(function(){
	     $('#header-cust-box-login').addClass('hide');
		 $('#header-cust-box-welcome').addClass('hide');		 
	});

    // Warenkorb öffnen im Header
    $("#header-cart").mouseover(function(){
        $('#header-cart-box').removeClass('hide');
    });

    // Warenkorb schliessen im Header
    $("#header-cart").mouseout(function(){
        $('#header-cart-box').addClass('hide');
    });

    // Popup-Menüs im Footer
	$('.footer-popup-menu-link').mouseover(function(){
        var id = this.id;  
        var pos = id.replace(/footer-popup-menu-link-/,'');
		$('#footer-popup-menu-' + pos ).removeClass('hide') ;
	});
	 
	$('.footer-popup-menu').mouseout(function(){
		$('.footer-popup-menu').addClass('hide') ;		 
	});
    
 	 // Filter-Handluing in der Listenansicht
 	 
 	 $("#listing-filter-articelview").mouseover(function(){
 	     $('#listing-filter-articelview-values').removeClass('hide');
 	 });  
 	 $("#listing-filter-articelview").mouseout(function(){
 	     $('#listing-filter-articelview-values').addClass('hide');
 	 }); 	 
 	 $("#listing-filter-colors").mouseover(function(){
 	     $('#listing-filter-colors-values').removeClass('hide');
 	 });  
 	 $("#listing-filter-colors").mouseout(function(){
 	     $('#listing-filter-colors-values').addClass('hide');
 	 });
 	 $("#listing-filter-order").mouseover(function(){
 	     $('#listing-filter-order-values').removeClass('hide');
 	 });  
 	 $("#listing-filter-order").mouseout(function(){
 	     $('#listing-filter-order-values').addClass('hide');
 	 }); 	 
    
    
	// Varianthandling Farbe wählen
	
    $("#color-variants ol li").click(function(i){
        
        
        var colorId = this.id ;
               
        
        cId = colorId.replace(/colorId-/, '');
		 
        COLOR_IS_OUT = $('#'+colorId).hasClass('out');                            
        
        if($('#'+colorId).hasClass('selected')){
        } else {
        
            //andere Farben wieder freigeben
			$('#color-variants ol li').each(function () {
				$('#'+this.id).removeClass('selected') ;
			});
			
            $('#'+colorId).addClass('selected');
		        //größen varianten aktualisieren
			pId = $(".add-to-cart").attr('id');
			pId = pId.replace(/product-/, '');
				 
			$('#size-variants ol li').each(function () {
				var sId = this.id;
				sId = sId.replace(/sizeId-/, '');
					 
				var aid = pId+'.'+cId+'.'+sId;
				$('#'+this.id).attr('class', '');
                //wenn die Größe nicht verfügbar ist, sperren
				if(availableVariants[aid] == undefined || !availableVariants[aid].available){
				    $('#'+this.id).addClass('out') ;
				}
			});				 
            	 
			setPrice();
			setPics(cId);
		}

        if($('#'+colorId).hasClass('out')){
            $('#product-price-cart .add-to-cart' ).html( sku_tell_me_when_in_stock );
            $('#product-price-cart .add-to-cart' ).addClass( "not-in-stock" );
            $('#product-in-stock').addClass("hide");
            $('#product-not-in-stock').removeClass("hide");                                
        }
        else {
            $('#product-price-cart .add-to-cart' ).html( sku_add_to_cart );	
            $('#product-price-cart .add-to-cart' ).removeClass( "not-in-stock" );
            $('#product-in-stock').removeClass("hide");
            $('#product-not-in-stock').addClass("hide");            	
        }
		 			 
	 });
	 
	 // Varianthandling Size wählen
	 $("#size-variants ol li").click(function(i){
		 var sizeId = i.target.parentNode.id;
                 
		 if(sizeId == ""){
			 sizeId = i.target.id;
			 sizeId = sizeId.replace(/sId-/, 'sizeId-');
		 }
		 
		 sId = sizeId.replace(/sizeId-/, '');
		 
		 if($('#'+sizeId).hasClass('selected')){
                     
                    $('#'+sizeId).removeClass('selected');                                                                                     
                 }
                 
                 else if ($('#'+sizeId).hasClass('out-selected')) {
                    $('#'+sizeId).removeClass('out-selected');   
                    if ( ! COLOR_IS_OUT ) {
                        $('#product-price-cart .add-to-cart' ).html( sku_add_to_cart );	
                        $('#product-price-cart .add-to-cart' ).removeClass( "not-in-stock" );	
                        $('#product-in-stock').removeClass("hide");
                        $('#product-not-in-stock').addClass("hide");                    		
                    }
                 }
                 
                 else{
                    //andere Größen wieder freigeben
				 $('#size-variants ol li').each(function (sv) {
					 // if(!$('#'+this.id).hasClass('out')){
					 //	 $('#'+this.id).attr('class', '');
					 // }
					 $('#'+this.id).removeClass('selected');
					 $('#'+this.id).removeClass('out-selected');
					 
				 });				 
				 
				 if(!$('#'+sizeId).hasClass('out')){
				    $('#'+sizeId).addClass('selected');
                    $('#product-price-cart .add-to-cart' ).html( sku_add_to_cart );	
                    $('#product-price-cart .add-to-cart' ).removeClass( "not-in-stock" );	
                    $('#product-in-stock').removeClass("hide");
                    $('#product-not-in-stock').addClass("hide");                    				    
				 }
				 else {
                    $('#'+sizeId).addClass('out-selected');
                    $('#product-price-cart .add-to-cart' ).html( sku_tell_me_when_in_stock );
                    $('#product-price-cart .add-to-cart' ).addClass( "not-in-stock" );
                    $('#product-in-stock').addClass("hide");
                    $('#product-not-in-stock').removeClass("hide");                                        
                 }

				 setPrice();
			 }
		 			 
		 
	 });	
	 
	 
	 // Zum Wahrenkorb hinzufügen bzw. Benachrichtigung bei Verfügbarkeit
	 $(".add-to-cart").click(function(i){

        var colorId = $('#color-variants .selected').attr('id');
        if(colorId == null){
            messageModal(sku_choose_color);
            return;
		}
		cId = colorId.replace(/colorId-/, '');
	 
        if ( $('#product-price-cart .add-to-cart' ).hasClass( "not-in-stock" ) ) {
         
            var sizeId = $('#size-variants .out-selected').attr('id');
            if(sizeId == null){
                messageModal(sku_choose_size);
                return;
            }

            sId = sizeId.replace(/sizeId-/, '');
            pId = this.id;
            pId = pId.replace(/product-/, '') + '.' + cId + '.' + sId;
         
            PRODUCT_ID = pId;
         
            if ( is_logged_in ) {
                setModal( "#show-product" ) ;
                $('#show-product').html( tmwis_start ) ;

                tellMeWhenInStock(pId,"logged_in");
                window.setTimeout("showTellMeWhenInStock()", 1000);
                $('#show-product').removeClass('hide');                
            }
            else {
                setModal( "#dialog-tmwis" )
                $('#dialog-tmwis').removeClass('hide');
            }
        }     
	 
	    else {
	    
            var sizeId = $('#size-variants .selected').attr('id');
            if(sizeId == null){
                messageModal(sku_choose_size);
                return;
            }
            
            setModal( "#show-product" ) ;
            
            $('#show-product').html( sku_product_add_to_cart ) ; 
            
            sId = sizeId.replace(/sizeId-/, '');
            pId = this.id;
            pId = pId.replace(/product-/, '') + '.' + cId + '.' + sId;
		 
            addToCart(pId, $('#productQuantity').val());
        }
            
	 });	 
	 

    $(".js-add-to-cart-wishlist").click(function() {
        try {
            pId = $(this).siblings('.js-add-to-cart-wishlist-reference').val();
            addToCart(pId, 1);
        } catch(e) {
            messageModal(wishlist_sku_out);
        }
    });
		 
	 /*
	  * Zur Wunschliste hinzufügen
	  */
	  
	 $(".add-to-wishlist").click(function(i){
	 
        var colorId = $('#color-variants .selected').attr('id');
        if(colorId == null){
            messageModal(sku_choose_color);
            return;
		}
		cId = colorId.replace(/colorId-/, '');
		 
        var sizeId = $('#size-variants .selected').attr('id');
        
        if(sizeId == null){
            sizeId = $('#size-variants .out-selected').attr('id');
        }
        
        if(sizeId == null){
            messageModal(sku_choose_size);
            return;
        }
        
        setModal('#show-product') ;
        
        $('#show-product').html( sku_product_add_to_wishlist ) ; 
        
        sId = sizeId.replace(/sizeId-/, '');
        pId = this.id;
        pId = pId.replace(/wishlist-/, '') + '.' + cId + '.' + sId;
                        
        addToWishlist(pId);
        window.setTimeout("showWishlistProductLayer(pId)", 1000);
        $('#show-product').removeClass('hide');        
            
	 });	 

 
	 
	 /*
	  * Tell a Friend Dialog öffnen
	  */
	 $("#button-tellafriend").click(function(i){
        setModal( "#dialog-tellafriend" ) ;
        $('#dialog-tellafriend').removeClass('hide');
	 });	 
	 
	 $(".popup-wrapper").click(function(i){
        closeModal();
        $(this).addClass('hide');
	 });	 
     
     
	 /*
	  * SizeGuide Dialog öffnen
	  */
	 $("#sizeguide-link").click(function(i){
        setModal( "#dialog-sizeguide" ) ;
        $('#dialog-sizeguide').removeClass('hide'); 
	 });	 	 
	 
	 /*
	  * Zoom Dialog öffnen
	  */
	 $("#sku-zoom").click(function(i){                     
            setModal( "#dialog-zoom-wrapper" ) ;
            $('#dialog-zoom-wrapper').removeClass('hide'); 
	 });		  

        // Zoom-Dialog mit Click außerhalb schließen 
        $('#dialog-zoom-wrapper').click(function(i){
           close_dialog_zoom(); 
        }) ;

	 // TMWIS Dialog abschicken
	 $("#tmwis-submit").click(function(i){
        var email = $('#email').val() ;
        $('#show-product').html( tmwis_start ) ;
        tellMeWhenInStock(PRODUCT_ID,email) ;
	});	 

    $("#email").click(function(i){
        $('#tmwisEmail-error').html( "&nbsp;" ) ;        
    });
	 
    /*
     * Tell a Friend Dialog abschicken
     */
    $("#taf-submit").click(function(i){
        
        var ret = validateTellafriendForm() ; 
         
        if ( ret ) {
                        
            var friend_name   = $('#friendName').val() ;
            var friend_email  = $('#friendEmail').val() ; 
            var my_email      = $('#myEmail').val() ;
            var my_first_name = $('#myFirstName').val() ;
            var my_last_name  = $('#myLastName').val() ;
            var my_message    = $('#myMessage').val() ;
            var product_id    = $('#tellafriend-content .product-id').attr('id');
            
            product_id = product_id.replace(/taf-product-/, '') ;
            
            close_dialog_tellafriend() ;
                        
            $('#show-product').html( sku_product_tell_a_friend ) ; 
        
            tellaFriend(friend_name,friend_email,my_email,my_first_name,my_last_name,my_message,product_id);

            setModal('#show-product') ;
            $('#show-product').removeClass('hide');        

        }
        
        else {
            
            $('#tellafriend-errormessage').html( sku_product_tell_a_friend_error ) ;
                        
        }
        
    });	 
	 
    $("#dialog-tellafriend input").click(function(i){       
            
            $('#tellafriend-errormessage').html("") ;                              
        
    });	          
         
         
    
	//checkout onestep
	
	 $('#addCouponBtn').click(function(){
    	var code = $('#couponcode').val();
    	if(code == ''){
    		$('#coupon-error-message').removeClass('hide');
    		return false;
    	}else{
    		addCoupon(code);
    		//removePayment();
    	}
	 });
	 
    $(".delete-coupon-btn").click(function(){
    	var code = this.id;
    	deleteCoupon(code);
		window.setTimeout("window.location.reload(true)", 1000);
    });  	
    
    $("#onestep-shipping input:radio").click(function(){
    	$('#shipping-price').html($('#shipping-cost-'+$(this).val()).html());
    	updatePrices();
    });
    
    $("#onestep-payment input:radio").click(function(){
    	$('#payment-price').html($('#payment-cost-'+$(this).val()).html());
    	$('#orderBtn').removeClass('hide');
    	$('#acknowledgeBtn').addClass('hide');
    	updatePrices();
    	$('.payment-desc').each(function(){
    		$(this).addClass('hide');
    	});
    	$('#payment-desc-'+$(this).val()).removeClass('hide');
        $('#error-paymentMethod').addClass('hide');
    });   
    
    $('#PAYMENT_METHOD_COMMPAY_CREDITCARD').click(function(){
    	$('#orderBtn').addClass('hide');
    	$('#acknowledgeBtn').removeClass('hide');
        $('#error-paymentMethod').addClass('hide');        
    });
    
    $('#PAYMENT_METHOD_COMMPAY_PAYPAL').click(function(){
    	$('#orderBtn').addClass('hide');
    	$('#acknowledgeBtn').removeClass('hide');
        $('#error-paymentMethod').addClass('hide');        
    }); 
    
	//Vorbelegte Inputfelder beim Anklicken leeren
	$("#newsletterEmail").click(function(){
       $("#newsletterEmail").val("");
    }); 
 
	 
	//checkout login/register
	$('.checkout-select').click(function(){
		var active = this;
        
		$('#checkout-forms div').each(function(){
			if(!$(this).hasClass('input-grp')){
				$(this).addClass('hide');
			}
		});
		$('#'+active.value).removeClass('hide');
	});
	
	$('#checkout-forms input').click(function(){
		if($(this).parent()[0].tagName == 'FORM'){
			var formId = $(this).parent().attr('id');
		}else{
			var formId = $(this).parent().parent().attr('id');
		}
		// $('#' + formId + ' #error-'+this.id).addClass('hide');
		$(this).removeClass('form-error');
		if(!$('#checkout-forms #error-'+this.id).hasClass('hide')){
			$('#checkout-forms #error-'+this.id).addClass('hide');
		}
	});
	 
	 // Fehlermeldung ausblenden, wenn Gutscheincode wieder angeklickt wird
	 //
	 $("#couponcode").click(function(){
        $("#coupon-error-message").addClass("hide");
     });   	 	 
	 
	 // Fenster für Auswahl der Lieferadresse
	 //
	 $("#change-shipping-address-btn").click(function(){   
	    changeAddress();
        setModal( "#change-shipping-address" ) ;                                              
	    $("#change-shipping-address").removeClass("hide");
	 });
	 
	// MyAccount Events
	$(".expand-fold-trigger").unbind('click').click( function(){
		$(this).children("span").toggleClass("fold-icon");
		$(this).next(".expand-fold-content").toggleClass("show hide");
	});

	$(".offline-orders-expand-fold-trigger").unbind('click').click( function(){
		$(this).children("span").toggleClass("fold-icon expand-icon");
		$(this).next(".offline-orders-expand-fold-content").toggleClass("show hide");
	});

    //showContentModal
	 $(".showContentModal").click(function(){
		 page = $(this).attr('title');
		 showContentModal(page);
	 });
    
	//Trusted Shop
	initTrustedShop();
    
});  // Ende document.ready







