// JavaScript Document

var active_option	= 0;
var dropdown_addon	= '';
var multiple_values	= new Array;

String.prototype.stripSpacesFull = function( ){ return this.replace( /\s/g, "" );};

function loadDropDownActions(element_name, to_select){
	var element_clicker = $(element_name);
	//alert(to_select);
	multiple_values[to_select]		= new Array;
	
	//big ones
	var current_value	= $('drop_'+to_select).value; //pre defined by url bijvoorbeeld
	$('dropdown_footer'+to_select).setStyle('opacity',0);
			
	element_clicker.addEvent('click', function(){
		
		if(element_clicker.getProperty('id').substring(13)!=active_option && allow_click == true){
			if(active_option!=0){
				var valueTarget = $('search_option_values'+active_option);
				if(valueTarget){
					var clickTarget = $('search_option'+active_option);
					ChangeAnim('height', valueTarget, 0, 500, Fx.Transitions.Cubic.easeOut)
					clickTarget.setStyle('backgroundImage', 'url('+root_domain+'img/'+root_short+'/assets/nav/dropdown/dropdown_main'+dropdown_addon+'.png)');
					ChangeAnim('top', $('dropdown_footer'+active_option), 13 , 300, Fx.Transitions.Cubic.easeOut)
					ChangeAnim('opacity', $('dropdown_footer'+active_option), 0 , 300, Fx.Transitions.Cubic.easeOut)
				}
			}
			
			dropdown_addon	= '';
			if(to_select=='address' || to_select=='address_payment'){
				dropdown_addon	= '_big';
			}else if(to_select=='method_payment'){
				dropdown_addon	= '_payment';
			}
			
			active_option 	= element_clicker.getProperty('id').substring(13);
			element_clicker.setStyle('backgroundImage', 'url('+root_domain+'img/'+root_short+'/assets/nav/dropdown/dropdown_main_open'+dropdown_addon+'.png)');
			var nr_ofValues	= $('option_value_counter'+active_option).value;
			valueTarget 	= $('search_option_values'+active_option);
			var toMove;
			if(to_select!='city'){
				toMove		= nr_ofValues * 21;
			}else{
				toMove		= nr_ofValues * 21;
			}
			var need_scroll	= false;
			if(toMove>440){
				need_scroll	= true;
				toMove 		= 440;
			}
			
			ChangeAnim('height', valueTarget, toMove, 300, Fx.Transitions.Cubic.easeOut)
			
			$('dropdown_footer'+active_option).setStyle('opacity',1);
			ChangeAnim('top', $('dropdown_footer'+active_option), toMove + 17 , 300, Fx.Transitions.Cubic.easeOut)
		
			//if(need_scroll == true){			
			//scroller = new Scroller(valueTarget, {area: 70, velocity: 0.1, fps: 40});
			//valueTarget.addEvent('mouseover', scroller.start.bind(scroller));
			//}
			//valueTarget.addEvent('mouseout', scroller.stop.bind(scroller));			 	
			//valueTarget.addEvent('mouseover', function(){ Log.log('in') });
			//valueTarget.addEvent('mouseout', function(){ Log.log('out') });	
			
		}else{
			if(allow_click == true){
				closeCloset(active_option);
			}
		}
	});
	
	var optionsValuelist = $$('#search_option_values'+element_clicker.getProperty('id').substring(13)+' .search_option_value, #search_option_values'+element_clicker.getProperty('id').substring(13)+' .search_option_value_multiple');
	optionsValuelist.each(function(element) {
	 
		var fxvalues = MorpheItems(element, 400, Fx.Transitions.Cubic.easeOut);;
		element.addEvent('mouseenter', function(){
			if(element.getProperty('class').stripSpacesFull() =='search_option_value' || element.getProperty('class').stripSpacesFull() =='search_option_value_multiple'){									
			fxvalues.cancel();
			fxvalues.start({
				'background-color': '#000000',
				'color': '#ffffff'
			});
			}
		});
	 
		element.addEvent('mouseleave', function(){
			if(element.getProperty('class').stripSpacesFull() =='search_option_value' || element.getProperty('class').stripSpacesFull() =='search_option_value_multiple'){									
			fxvalues.cancel();								
			fxvalues.start({
				'background-color': '#000000',
				'color': '#8c8c8c'
			});
			}
		});

		element.addEvent('click', function(){
			if(element.getProperty('class').stripSpacesFull() =='search_option_value'){									
				//alert('click');
				var childof 		= element.getProperty('childof');
				var current_value	= $('drop_'+childof).value;
				value_element		= $('value_'+current_value);
				
				if(value_element){
					if(value_element.getProperty('childof') == childof){
						value_element.removeClass('selected');
					}
				}
	
				var my_tag_1		= 'assets/nav/close.png';
				var my_tag_2		= 'assets/nav/reset.png';
				
				if((element.innerHTML).indexOf(my_tag_1) > 0 ){
					//do nothing
				}else{
					var disablelist = $$('#search_option_values'+childof+' .search_option_value');
					disablelist.each(function(element) {
						element.setStyles({'background-color':'#000000', 'color': '#8c8c8c'});
						element.removeClass('selected');
					});
					if((element.innerHTML).indexOf(my_tag_2) > 0 ){
						//do nothing
					}else{
						element.addClass('selected');
						fxvalues.start({
							'background-color': '#000000',
							'color': '#ffffff'
						});
					}
				}
				
				var tempval;
				if(to_select!='method_payment' && to_select!='filter_price'){
					tempval	= parseInt(element.getProperty('id').substring(6));
				}else{
					tempval	= element.getProperty('id').substring(6);
				}
				
				$('drop_'+childof).value 		= tempval;
				
				if($('active_'+childof)){
					$('active_'+childof).value 	= tempval;
				}
				allow_click		= false;
				afterDropDownClick(childof, tempval, element.innerHTML);
			}else{
				if(element.getProperty('class').stripSpacesFull() =='search_option_value_multiple'){	
					var childof 		= element.getProperty('childof');
					var my_tag_1		= 'assets/nav/close.png';
					var my_tag_2		= 'assets/nav/reset.png';
					
					if((element.innerHTML).indexOf(my_tag_1) > 0 || (element.innerHTML).indexOf(my_tag_2) > 0 ){
						//do nothing
					}else{
						element.addClass('selected');
						fxvalues.cancel();
						fxvalues.start({
							'background-color': '#000000',
							'color': '#ffffff'
						});
					}
					
					var tempval;
					tempval						= element.getProperty('id').substring(6);
					
					var temp_current_values 	= new Array;
					temp_current_values			= multiple_values[childof];
					if(!in_array(tempval, temp_current_values)){
						temp_current_values[temp_current_values.length]	= tempval;
					}
					
					//alert(temp_current_values.join(','));
														 
					multiple_values[childof] 	= temp_current_values;
					
					
					$('drop_'+childof).value 		= temp_current_values.join(',');
					if($('active_'+childof)){
						$('active_'+childof).value 	= temp_current_values.join(',');
					}
					allow_click		= false;
					afterDropDownClickMultiple(childof, tempval, element.innerHTML);
				}else if(element.getProperty('class').stripSpacesFull() =='search_option_value_multipleselected'){	
					var childof 		= element.getProperty('childof');
					var current_value	= $('drop_'+childof).value;
					
					if(value_element){
						if(value_element.getProperty('childof') == childof){
							fxvalues.cancel();
							value_element.removeClass('selected');
						}
					}
		
					element.removeClass('selected');
					
					var tempval;
					tempval	= element.getProperty('id').substring(6);
					
					var temp_current_values 	= new Array;
					temp_current_values			= multiple_values[childof];
					
					if(in_array(tempval, temp_current_values)){
						var key					= position_in_array(tempval, temp_current_values);
						temp_current_values.splice(key,1);
					}
					
					multiple_values[childof] 	= temp_current_values;
					
					$('drop_'+childof).value 		= temp_current_values.join(',');
					if($('active_'+childof)){
						$('active_'+childof).value 	= temp_current_values.join(',');
					}
					allow_click		= false;
					afterDropDownClickMultiple(childof, tempval, element.innerHTML);
				}	
			}
		});
		
		
	});
	
	if(current_value!=0){
		var value_element;
		if(to_select == 'search_main' || to_select == 'search_sub' || to_select == 'search_attr1' || to_select == 'search_attr2'){
			value_element	= 'search'+current_value;
		}else{
			value_element	= 'value_'+current_value;
		}
		var completelist 			= $$('#search_option_values'+to_select+' .search_option_value');
		completelist.each(function(element) {
								   
			if(element.getProperty('id') == value_element){
				
				element.addClass('selected')
				element.setStyles({
					'background-color':'#000000','color': '#ffffff'
				});
				  
				var str				= element.innerHTML;
				$(element_name).innerHTML 	= str;
				
				if(to_select == 'color'){
					ReInitSizes(current_value);	
				}
				
			}
			
		});
	}
}

function closeCloset(childof){
	var valueTarget = $('search_option_values'+childof);
	ChangeAnim('height', valueTarget, 0, 300, Fx.Transitions.Cubic.easeOut);
	ChangeAnim('top', $('dropdown_footer'+childof), 13 , 300, Fx.Transitions.Cubic.easeOut)
	ChangeAnim('opacity', $('dropdown_footer'+childof), 0 , 300, Fx.Transitions.Cubic.easeOut)
			
/*	if(gallery_type[active_current]==1){
		var clickTarget = $('search_option'+active_option);
	}else if(gallery_type[active_current]==2){
		var clickTarget = $('search_optionGallery');
	}else{*/
		var clickTarget = $('search_option'+active_option);
	//}
	
	ChangeAnim('height', valueTarget, 0, 300, Fx.Transitions.Cubic.easeOut)
	clickTarget.setStyle('backgroundImage', 'url('+root_domain+'img/'+root_short+'/assets/nav/dropdown/dropdown_main'+dropdown_addon+'.png)');
	active_option = 0;
}

function afterDropDownClick(childof, tempval, element_txt){
	switch(childof){
		case 'categories':
			$('search_option'+childof).innerHTML 	= 'category:'+element_txt;
			closeCloset(childof);
			dropDownHandler(tempval, childof, element_txt);
		break;
		case 'address':
		case 'address_payment':
		case 'method_payment':
		case 'search_main':
		case 'search_sub':
		case 'search_attr1':
		case 'search_attr2':
		case 'country':
		case 'city':
		case 'language':
			$('search_option'+childof).innerHTML 	= element_txt;
			closeCloset(childof);
			dropDownHandler(tempval, childof, element_txt);
		break;
		default:
			var my_tag;
			//filter??
			//if(q_str_page != 'home'){
				
				//if(element_txt == '<div class="option-icon"><img src="http://www.maxmodels.nl//img/max/assets/nav/close.png" alt="close"></div><div class="option-text">close</div>'){
				
				my_tag		= 'assets/nav/close.png';
				if(element_txt.indexOf(my_tag) > 0){
					closeCloset(childof);
					allow_click  = true;
				}else{
				//	if(element_txt == '<div class="option-icon"><img src="http://www.maxmodels.nl//img/max/assets/nav/reset.png" alt="reset"></div><div class="option-text">reset</div>'){
					my_tag		= 'assets/nav/reset.png';
					if(element_txt.indexOf(my_tag) > 0){
						element_txt	= 'search';
					}
				
					$('search_option'+childof).innerHTML 	= element_txt;
					closeCloset(childof);
					dropDownHandler(tempval, 'filter', element_txt);
				
				}
			//}else{
				//$('search_option'+childof).innerHTML 	= element_txt;
				//closeCloset(childof);
				//allow_click  = true;
			//}
		break;
	}
}

function afterDropDownClickMultiple(childof, tempval, element_txt){
	var my_tag;
	my_tag		= 'assets/nav/close.png';
	if(element_txt.indexOf(my_tag) > 0){
		closeCloset(childof);
		allow_click  = true;
	}else{
		my_tag		= 'assets/nav/reset.png';
		if(element_txt.indexOf(my_tag) > 0){
			element_txt	= 'search';
		}
	
		$('search_option'+childof).innerHTML 	= element_txt;
		dropDownHandler(tempval, 'filter', element_txt);
		closeCloset(childof);
	}
}

function dropDownHandler(value, click_target,element_txt){
	var selected_option	= value;
	switch(click_target){
		case 'address':
			HandleAddress(value,'shipping');
		break;
		case 'address_payment':
			HandleAddress(value,'payment');
		break;
		case 'method_payment':
			HandlePaymentMethod(value);
		break;
		case 'country':
			HandleCountry(value);
		break;
		case 'city':
			HandleCity(value);
		break;
		case 'language':
			HandleLanguage(value);
		break;
		case 'filter':
			HandleFilter(value);
		break;
	}
}

function HandleFilter(value){
	//alert(value);	
	runFilter();
}

function HandleAddress(selected_value, soort){
	var this_target				= 'current_'+ soort +'_address';
	
	if($(this_target)){
		mySpinner 				= new Spinner(this_target);
		mySpinner.show(true);
				
		var url				= root_domain + 'shop/checkout/address/flow-get-address.php?selected_value='+selected_value+'&soort='+soort;
		loadNshow(url, this_target, 'get', false);
	}
}
function HandlePaymentMethod(selected_value){
	var this_target				= 'method_payment_container';
	if($(this_target)){
		mySpinner 				= new Spinner(this_target);
		mySpinner.show(true);
		
		var url					= root_domain + 'shop/checkout/payment/flow-handle-method.php?selected_value='+selected_value;
		loadNshow(url, this_target, 'get', false);
	}
}

function HandleCountry(value){
	mySpinner 				= new Spinner('city_selecter');
	mySpinner.show(true);
	
	var url	= root_domain + 'storelocator/flow-city-selecter.php?country='+value;
	loadNshow(url, 'city_selecter', 'get', false);
}

function HandleCity(value){
	mySpinner 				= new Spinner('storelocator_result');
	mySpinner.show(true);
	
	var url	= root_domain + 'storelocator/listing.php?city='+value;
	loadNshow(url, 'storelocator_result', 'get', false);
}

function HandleLanguage(value){
	var url	= root_domain + 'set_language.php?lan='+value;
	window.location	= url;
}
