STOREFRONT_AJAX_URL = '/common/ajax/front/components/StoreFront/ajax.php';

Event.observe(window, 'load', function() {
	// call gallery component
	// this only works on first load
	fncNewsComponent_Gallery_Rollover();
	
	
});


/* Gallery Hack (Using it from News) */
function fncNewsComponent_Gallery_Rollover(loadingbar) {
	var images_trigger = $$('.GalleryImage');
	
	if(images_trigger != null){		
		images_trigger.each(function(image, index){
			image.setAttribute('index', index);
				
			Event.observe(image, 'mouseover', function(event){
			
				//make dom compatible for ie vs fox
				if(event.target == undefined){
					var eTargetIndex = event.srcElement.index
				} else {
					var eTargetIndex = event.target.getAttribute('index');
				}
				
				images = $('images_source').immediateDescendants();
						
				myImage = new Image()
				myImage.src = images[eTargetIndex].src;
				
				if(myImage.width > 400) {
					$('event_image').width = 400;
				} else {
					$('event_image').width = myImage.width;
				}
				
				if(myImage.height > 300) {
					$('event_image').height = 300;
				} else {
					$('event_image').height = myImage.height;
				}
				
				$('GalleryImageName').innerHTML = images[eTargetIndex].getAttribute('title') || '&nbsp;';
				$('GalleryImageDescription').innerHTML = images[eTargetIndex].getAttribute('caption') ||  '&nbsp;';
				
			});
		});
	}

}


function fncStoreAjaxAddToCart(){
	//Get the one checked radio box
	//$intUnitID 		= Form.getInputs('frmUnitAdd','radio','unit_id').find(function(radio) { return radio.checked; }).value;
	
	
	$intUnitID		= $('unit_id').value;
	$intQuantity 	= $('txtNumUnits').value;
	
	//alert($intUnitID + '   _________  '    +   $intQuantity  );
	
	if( $intUnitID != ""  && $('txtNumUnits').present() ){
		
	  	new Ajax.Updater({success : 'ViewCartBlock', failure : null}, STOREFRONT_AJAX_URL, {
			  parameters: { action   : 'add',
			                unit_id  : $intUnitID,
			                qty      : $intQuantity
			  },
			  evalScripts : true,
	
			  
			  onComplete: function(transport){
			  	//$('ViewCartBlock').innerHTML = transport.responseText;
			  	
			  	$('divUnitAddMessage').show();
			  	
			  	new Effect.ScrollTo('ViewCartBlock', {offset: -10});
			  	//new Effect.BlindDown('ViewCartBlock');
			  	//new Effect.Pulse('ViewCartBlock', {duration: 3, pulses: 2});
			  },
			  
			  
			  onFailure: function(transport){
		 			//Error.alert(transport.responseText);
		 			//alert('onFailure');
			  }
			}
		);
		
		
		
			
	}
	
	return false;
}





function fncOnChange($intIndexOfChanged, $intAttribID){
	
	/*
	
	alert(		
			  "\r\n" + 'You Changed Index: ' + $intIndexOfChanged
			+ "\r\n" + 'intAttribID: '   	 + $intAttribID
			+ "\r\n" + 'Value: '   			 + $('attrib_' + $intAttribID).value
			+ "\r\n" + 'intProductID: '   	 + $intProductID
			+ "\r\n" + 'arr_index_to_attrib_id.length: '   	 + $arr_index_to_attrib_id.length
							
			+ "\r\n\r\n" + $('frmUnitFilter').serialize()
	);
	
	*/	
	
	//For loop to clear
	var $blnDidLoadSecond = false;
	for (var $idx=$intIndexOfChanged+1 ; $idx < $arr_index_to_attrib_id.length ; $idx++){
		
		//alert( 'Clear: ' + $idx );
		
		$(  'attrib_' + $arr_index_to_attrib_id[ $idx ]   ).options.length = 0;
	
		if( $blnDidLoadSecond == false ){
			$blnDidLoadSecond = true;
			$(  'attrib_' + $arr_index_to_attrib_id[ $idx ]   ).options[0] = new Option('..Loading...', 0);
		}
		else{
			$(  'attrib_' + $arr_index_to_attrib_id[ $idx ]   ).options[0] = new Option('Select Above Option(s)', 0);
		}
		
	}

	
	
	fncStoreAjaxRefreshDDL(
			$intIndexOfChanged
		,	$intAttribID
		,	$('attrib_' + $intAttribID).value
		,	$intProductID
		,	$arr_index_to_attrib_id.length
		,	$('frmUnitFilter').serialize()
	);
	
	
	
	return false;
}



function fncStoreAjaxRefreshDDL(  
		$intIndexOfChanged		// zero based of # of uniq attribs
	,	$intAttribID			// db id of 'size'
	,	$intAttribID_Value		//sub-id value of 'size 10'
	,	$intProductID
	,	$intNumDropDowns		// ie. 3 distinct attributes
	,	$strFormSerialzed		// attrib_#&....
){

	new Ajax.Request(STOREFRONT_AJAX_URL, {   
	
		method: 'post',   
		
		evalScripts : true,
		
		evalJSON : true, 
		
		parameters: { action             			: 'RefreshDDL',
		              intIndexOfChanged				: $intIndexOfChanged,
		              intAttribID					: $intAttribID,
		              intAttribID_Value				: $intAttribID_Value,
		              intProductID					: $intProductID,
		              intNumDropDowns				: $intNumDropDowns,
		              strFormSerialzed				: $strFormSerialzed
		},

						  		
		onSuccess: function(transport){
			var $arrOptions = transport.responseJSON;
			
			//alert( $arrOptions.strNextHTML_DDL_ID );
			//alert('onSuccess: ' + transport.responseText);
			//console.log( $arrOptions );
			
			if( $arrOptions.blnIsProductUnit == 0 ){
				
				$ddl = $( $arrOptions.strNextHTML_DDL_ID );
				$ddl.options.length = 0;
		  		$ddl.options[$ddl.options.length] = new Option('Please Select', '');
				
				$arrOptions.options.each(function(arrOptValue){
					$ddl.options[$ddl.options.length] = new Option(arrOptValue.attribute_sub_name, arrOptValue.attribute_sub_id);
				});
				
			}
			else{
				
				//$('divUnitInfo').innerHTML = '<h1> UNIT GO TIME ... SON: ___' + $arrOptions.intUnitID + '__' + $arrOptions.arrUnitData.unit_name  + '__ </h1>';
				//$('divUnitInfo').innerHTML  +
				
				$('divFirstLoadOnly_PsycheAddToCart').innerHTML = '';
				$('divUnitInfo').innerHTML =  $arrOptions.tplUnitInfo;
								
			}
		
		},
		
		onFailure: function(transport){
			alert('Request Failed');
			//alert('onFailure: ' + transport.responseText);
		}
		  
			
	});

}
