<?php
/**
 * @package		HikaShop for Joomla!
 * @version		1.5.3
 * @author		hikashop.com
 * @copyright	(C) 2010-2011 HIKARI SOFTWARE. All rights reserved.
 * @license		http://www.hikashop.com/commercial_license.php
 */
defined('_JEXEC') or die('Restricted access');
?>
<table class="hikashop_product_options_table">
<?php
			$this->setLayout('listing_price');
			$old_show_discount = $this->params->get('show_discount');
			$this->params->set('show_discount',0);
			$i=0;
			$js="var hikashop_options=Array();";
			foreach($this->element->options as $optionElement){
?>
	<tr>
<?php			$this->values = array();
				$value = 0;
				$currency = hikashop_get('class.currency');
				$map = 'hikashop_product_option['.$i.']';
				$id = 'hikashop_product_option_'.$i;
				if(empty($optionElement->variants)){
					$this->values[] = JHTML::_('select.option', 0,JText::_('HIKA_NONE'));
					$text = $optionElement->product_name;
					$this->row =& $optionElement;
					if(!empty($optionElement->prices)){
						$ok = 0;
						foreach($optionElement->prices as $k => $price){
							if(empty($price->price_min_quantity)){
								$ok = $k;
								break;
							}
						}
						$this->row->prices = array($optionElement->prices[$k]);
						$text.=' ( + '.strip_tags($this->loadTemplate()).' )';
						if($this->params->get('price_with_tax')){
							$price = $optionElement->prices[$k]->price_value_with_tax;
						}else{
							$price = $optionElement->prices[$k]->price_value;
						}
						$js.="
						hikashop_options[".(int)$optionElement->product_id."]=".$price.";";
					}
					$this->values[] = JHTML::_('select.option', $optionElement->product_id,$text);
				}else{
					$defaultValue=array();
					if(isset($optionElement->characteristics) && is_array($optionElement->characteristics) && count($optionElement->characteristics)){
						foreach($optionElement->characteristics as $char){
								$defaultValue[]=$char->characteristic_id;
						}
					}
					foreach($optionElement->variants as $variant){
						if(empty($variant->variant_name)){
							if(empty($variant->characteristics_text)){
								$text = $variant->product_name;
							}else{
								$text = $variant->characteristics_text;
							}
						}else{
							$text = $variant->variant_name;
						}
						$this->row =& $variant;
						if(!empty($variant->prices)){
							$ok = 0;
							foreach($variant->prices as $k => $price){
								if(empty($price->price_min_quantity)){
									$ok = $k;
									break;
								}
							}
							$this->row->prices = array($variant->prices[$k]);
							$text.=' ( + '.strip_tags($this->loadTemplate()).' )';
							if($this->params->get('price_with_tax')){
								$price = $variant->prices[$k]->price_value_with_tax;
							}else{
								$price = $variant->prices[$k]->price_value;
							}
							$js.="
							hikashop_options[".(int)$variant->product_id."]=".$price.";";
						}
						if($defaultValue && isset($variant->characteristics) && is_array($variant->characteristics) && count($variant->characteristics)){
							$default = true;
							foreach($variant->characteristics as $char){
								if(!in_array($char->characteristic_id,$defaultValue)){
									$default = false;
								}
							}
							if($default){
								$value = $variant->product_id;
							}
						}
						$this->values[] = JHTML::_('select.option', $variant->product_id,$text);
					}
				}
				$html = JHTML::_('select.genericlist', $this->values, $map, 'class="inputbox" size="1"  onchange="hikashopChangeOption();"', 'value', 'text', (int)$value,$id );
				if(!empty($optionElement->variants)){
					$optionElements='';
					if(!empty($optionElement->main->product_description) || !empty($optionElement->main->product_url)){
						$optionElements='<img src="'.HIKASHOP_IMAGES.'icons/icon-16-info.png" alt="Information"/>';
						$description = '';
						if(!empty($optionElement->main->product_description)){
							$description = strip_tags(JHTML::_('content.prepare',$optionElement->main->product_description));
							$optionElements='<span class="hikashop_option_info" title="'.$description.'">'.$optionElements.'</span>';
						}
						if(!empty($optionElement->main->product_url)){
							JHTML::_('behavior.modal');
							if(empty($description)){
								$description = $optionElement->main->product_name;
							}
							$optionElements='<a class="hikashop_option_info_link modal" rel="{handler: \'iframe\', size: {x: 450, y: 480}}" title="'.$description.'" href="'.$optionElement->main->product_url.'">'.$optionElements.'</a>';
						}
					}
					$html='<span class="hikashop_option_name" >'.$optionElement->main->product_name.$optionElements.'</span></td><td>'.$html;
				}
?>
		<td>
			<?php echo $html; ?>
		</td>
	</tr>
				<?php $i++;
			}
			global $Itemid;
			$url_itemid='';
			if(!empty($Itemid)){
				$url_itemid='&Itemid='.$Itemid;
			}
			$baseUrl = hikashop_completeLink('product&task=price',true,true);
			if(strpos($baseUrl,'?')!==false){
				$baseUrl.='&';
			}else{
				$baseUrl.='?';
			}
			$js = $js. "
			function hikashopChangeOption(){
				var j = 0;
				total_option_price = 0;
				while(true){
					var option = document.getElementById('hikashop_product_option_'+j);
					if(!option){
						break;
					}
					j++;
					var option_price = hikashop_options[option.value];
					if(option_price){
						total_option_price+=option_price;
					}
				}
				var arr = new Array();
				arr = document.getElementsByName('hikashop_price_product');
				for(var i = 0; i < arr.length; i++){
					var obj = document.getElementsByName('hikashop_price_product').item(i);
					var id_price = 'hikashop_price_product_' + obj.value;
					var id_price_with_options = 'hikashop_price_product_with_options_' + obj.value;
					var price = document.getElementById(id_price);
					var price_with_options = document.getElementById(id_price_with_options);
					if(price && price_with_options){
						price_with_options.value = parseFloat(price.value) + total_option_price;
					}
				}
				hikashopRefreshOptionPrice();
			}
			function hikashopRefreshOptionPrice(){
				var price_div = document.getElementById('hikashop_product_id_main');
				var inputs = price_div.getElementsByTagName('input');
				if(inputs[0]){
					var id_price_with_options = 'hikashop_price_product_with_options_' + inputs[0].value;
					var price_with_options = document.getElementById(id_price_with_options);
					if(price_with_options){
						try{
							new Ajax('".$baseUrl."price='+price_with_options.value+'".$url_itemid."',  { method: 'get', onComplete: function(result) { var totalPrice = window.document.getElementById('hikashop_product_price_with_options_main'); if(totalPrice) totalPrice.innerHTML = result;}}).request();
						}catch(err){
							new Request({url:'".$baseUrl."price='+price_with_options.value+'".$url_itemid."', method: 'get', onComplete: function(result) { var totalPrice = window.document.getElementById('hikashop_product_price_with_options_main'); if(totalPrice) totalPrice.innerHTML = result;}}).send();
						}
					}
				}
			}
			window.addEvent('domready', function() { hikashopChangeOption(); });
			";
			$doc =& JFactory::getDocument();
			$doc->addScriptDeclaration("<!--\n".$js."\n//-->");
			$this->params->set('show_discount',$old_show_discount); ?>
</table>