<?php
defined('_JEXEC') or die;
jimport('joomla.html.html');
jimport('joomla.form.formfield');//import the necessary class definition for formfield
class JFormFieldSlideshow extends JFormField
{
		
	protected function getInput()
	{
		
		JHTML::_('behavior.mootools');
		JHTML::_('behavior.modal');
		
		jimport('joomla.filesystem.file');
		$document=&JFactory::getDocument();
		$document->addScriptDeclaration("
		var images;
		window.addEvent('load',function(){
			
			images=$$('#img-choser img');
			
			$('Remove').addEvent('click',function(){
			
				$('jformparamsslides').getChildren().each(function(el){
					
					if(el.selected == true){
						el.destroy();
					}
					
					if(el.selected == false){
						el.selected=true;
					}
				
				})
				
			
			});
			
			$('SelectAll').addEvent('click',function(){
			
				$('jformparamsslides').getChildren().each(function(el){
					
					if(el.selected == false){
						el.selected =true;
					}
					
					
				
				})
				
			
			});
			
			$('OrderUp').addEvent('click',function(){
			
				var select=$('jformparamsslides'); 
				var cildren=select.getChildren();
				cildren.each(function(el,index){
				
					if(el.selected == true){
						
						if((index-1) >= 0){
							
							var tmp=cildren[index-1].value;
							cildren[index-1].value=el.value;
							cildren[index-1].innerHTML=el.value;
							el.value=tmp;
							el.innerHTML=tmp;
							
						}
					
					}
				
				});
			
			});
			$('OrderDown').addEvent('click',function(){
			
				var select=$('jformparamsslides'); 
				var cildren=select.getChildren();
				cildren.each(function(el,index){
				
					if(el.selected == true){
						
						if((index+1) < cildren.length){
							
							var tmp=cildren[index+1].value;
							cildren[index+1].value=el.value;
							cildren[index+1].innerHTML=el.value;
							el.value=tmp;
							el.innerHTML=tmp;
							
						}
					
					}
				
				});
			
			});
			
			
		});
		
		function addThis(el){
		
			var name=images[el].name;
			var select=$('jformparamsslides');
			var newoption = new Option(\"\"+name+\"\", \"\"+name+\"\");
			try
			{
				newoption.setProperty('selected', 'selected');
				select.add(newoption, null);
			}
			catch (err)
			{
				newoption.selected=true;
				select.add(newoption);
				
			}

		}

		");
		$document->addStyleDeclaration("#img-choser{display:none;}");
		$files=JFolder::files(JPATH_SITE.DS.'templates'.DS.'braincode-grunge'.DS.'slides');
		$return='<div id="img-choser">';
		if(!empty($files)){
		$list=array();
			
			$i=0;
			foreach($files as $file){
				$ext=JFile::getExt($file);
				if(($ext=='jpg'||$ext=='jpeg'||$ext=='png'||$ext=='gif')){
					$return.='<div style="padding:5px;margin:10px;float:left;border:1px solid #000"><a href="javascript:void(0);"  onclick="parent.addThis(\''.$i.'\');parent.SqueezeBox.close();"><img title="Click to add" style="width:110px;height:80px;display:block" src="'.JURI::root().'templates/braincode-grunge/slides/'.JFile::getName($file).'" name="'.JFile::getName($file).'" /></a><br /> <span>'.JFile::getName($file).'</span></div>';
					$i++;
				}
			}
		
		}
		$return.='</div>';
		$mitems=array();
		$i=0;
		if(!empty($this->value)){
			
			if(is_array($this->value)){
		
				foreach($this->value as $val){
					$mitems[$i]->value=$val;
					$mitems[$i]->name=$val;
					$i++;
				}
		
			}else{
				$mitems[$i]->value=$this->value;
				$mitems[$i]->name=$this->value;
			}
		}
		
		$output= JHTML::_('select.genericlist',  $mitems, ''.$this->name.'', 'class="inputbox" style="width:100%"   multiple="multiple" size="10"', 'value', 'value', $this->value );

		return $output.'<div>
			<a href="#img-choser" class="modal"><img src="'.JURI::root().'templates/braincode-grunge/images/icon/add.png"  alt="'.JText::_('TPL_BRAINCODE-GRUNGE_LABEL_ADD SLIDE').'" title="'.JText::_('ADD SLIDE').'"></a>&nbsp;
		
		<a href="javascript:void(0)" id="SelectAll"><img src="'.JURI::root().'templates/braincode-grunge/images/icon/all.png"  alt="'.JText::_('SELECT ALL').'" title="'.JText::_('SELECT ALL').'"></a>&nbsp;
		
		<a href="javascript:void(0)" id="Remove"><img src="'.JURI::root().'templates/braincode-grunge/images/icon/delete.png" alt="'.JText::_('DELETE SELECTED').'" title="'.JText::_('DELETE SELECTED').'"></a>&nbsp;
		
		<a href="javascript:void(0)" id="OrderUp"><img src="'.JURI::root().'templates/braincode-grunge/images/icon/up.png"  alt="'.JText::_('MOVE UP').'" title="'.JText::_('MOVE UP').'"></a>&nbsp;
		
		<a href="javascript:void(0)" id="OrderDown"><img src="'.JURI::root().'templates/braincode-grunge/images/icon/down.png"  alt="'.JText::_('MOVE UP').'" title="'.JText::_('MOVE UP').' slide"/></a>
		
		<p style="clear:both">'.JText::_('STORED').'</p></div>'.$return;
		
	}
	
}	?>
