// JavaScript Document

// Mootools Library Needed

var ProductsManager = {
	
	init: function(){
		// --------------------------------------
		// Basket
		// --------------------------------------
		// Check for Gutscheine
		if($$('#tx-ttproducts-pi1-basket .products_basket_table_single_item_weight').length > 0){
			var total_weight = 0.0;
			$$('#tx-ttproducts-pi1-basket .products_basket_table_single_item_weight').each(function(element){
				total_weight += parseFloat($(element).get('value'));
			});
			if(total_weight == 0.0){
				$$('#shipping-select option').each(function(element){
					if($(element).get('value') == '30' || $(element).get('value') == '35'){
						$(element).dispose();
					}
				});
			}
		}
		// Delete Item - List of Links Variables
		var list_link_delete_item = $$('products_basket_delete_item_link');
		if(list_link_delete_item){
			for(var i=0;i<list_link_delete_item.length;i++){
				
			}
		}
		// Continue Shopping - List of Links Variables
		var list_link_continue_shopping = $$('products_basket_continue_shopping_link');
		if(list_link_continue_shopping){
			for(var i=0;i<list_link_continue_shopping.length;i++){
				
			}
		}
		// Navigation - Links Variables
		var link_first_step = $('products_basket_navigation_first_step');
		var link_second_step = $('products_basket_navigation_second_step');
		var link_third_step = $('products_basket_navigation_third_step');
		var link_fourth_step = $('products_basket_navigation_fourth_step');
		if(link_first_step){
			
		}
		if(link_second_step){
			
		}
		if(link_third_step){
			
		}
		if(link_fourth_step){
			
		}
		// Checkout - Link Variable
		var link_checkout = $('products_basket_checkout_link');
		if(link_checkout){
			link_checkout.addEvent('click', ProductsManager.chekoutBasketListener);
		}
		// --------------------------------------
		// Link to AGB (Ajax)
		// --------------------------------------
		var link_to_agb = $('products_link_to_agb');
		if(link_to_agb){
			link_to_agb.addEvent('click', ProductsManager.linkToAgbAjaxListener);
		}
		
	},
	
	
	// Basket Checkout Listener
	chekoutBasketListener: function(event){
		ProductsManager.chekoutBasket(this);
		event.preventDefault(); 
	},
	
	// Basket Checkout
	chekoutBasket: function(element){
		if($('country_preselect') && $('shipping-select')){
			if($('country_preselect').value.length == "3" && $('shipping-select').value != "5") {
				$('basketform').action='index.php?id=58&tx_ttproducts_pi1[backPID]=68&products_info=1'; 
				$('basketform').submit();
			}else{
				if($('country_preselect').value.length != "3" && $('shipping-select').value == "5") {
					alert("Bitte geben Sie Empfängerland und Versandart an!");
				}else if($('country_preselect').value.length != "3") {
					alert("Bitte geben Sie Empfängerland an!");	
				}else if($('shipping-select').value == "5") {
					alert("Bitte geben Sie Versandart an!");	
				}
			}
		}
	},
	
	// Link To Agb Content Generator Listener
	linkToAgbAjaxListener: function(event){
		ProductsManager.linkToAgbAjax(this);
		event.preventDefault();
	},
	
	// Link To Agb Content Generator (with Ajax Request)
	linkToAgbAjax: function(element){
		// Host To Ajax Request
		var host = window.location.host;
		// Paramter to Controller Dispatching (type, action)
		var type = 'tt_products';
		var action = 'get_agb_content';
		// Controller URL
		var url_controller = 'http://'+host+'/fileadmin/templates/php_contents/tt_products/controller_products.php';
		// Ajax Request 
		var req = new Request({  
				url: url_controller,    
				method: 'post',
				data: 'type='+type+'&action='+action,
				onComplete: function(response){
					if($('mb_agb_text')){
						if(response == 'parameter_error'){
							alert('Error: Invalid Parameters!');
							return false;
						}
						$('mb_agb_text').innerHTML = "<div style='height:100%; overflow:auto;'>"+response+"</div>";
						Mediabox.open('#mb_agb_text', 'Die Allgemeinen Geschäftsbedingungen', '640 480');
					}
					
				}  
			}  
		).send(); 
	}
};

// Mootools DOM ready
window.addEvent('domready', function(){
		ProductsManager.init();
	}
);
