/* 
* vmfiltering module
* @version $Id: mod_vmFiltering.php 1 2009-05-19 9:18:06Z sakisTerzis $
* @package vmfiltering module
* @author Sakis Terzis (sakis@breakDesigns.net)
* @copyright	Copyright (C) 2009 breakDesigns.net. All rights reserved
* @license	GNU/GPL v3
* see administrator/components/com_vmfiltering/COPYING.txt
* the idea and the development owns to Sakis Terzis
*/

var id=null;
var property='';
var idold='';
var start=1; //indicates that no feature has been selected yet
var start_cat=1;
var start_dim=1;
var vmfInputName='';
parentClass='';
var checkbx_cat=null;
var id_array=[];
//id_array_old_toString='';
var difference=0;

window.addEvent('domready', function() {
document.addEvent('click', function(event){
	//vmcats vars
	
	var catcheckbx=$$('.vmcatsL');
	
	
	var vmcatsBox=$('vmcats');	
	var input=$('selectedVMcat');
	
	if (disp_features){
	//features vars
	var featBox=$('features');
	var featInput=$('selectedFeature');
	}	
	
	var event = new Event(event);
	targ=event.target;	
	
	//creates the appear and dissapear of the boxes
	if(targ==vmcatsBox || targ==input || targ.className=='vmcatsL' || targ.className=='vmCatsUL'||targ.className=='vmHeader'||targ.className=='vmcatsLi'||targ.name=='vm_cat[]'){
		
			}		
	else{
		//alert(targ.className);
		dissapearVM();
		}
		
		
//----------------features------------------//
if (disp_features){
	if(targ==featBox || targ==featInput || targ.className=='vmfWrapperInner' || targ.className=='headerIn' || targ.className=='headerLi'||  targ.className=='vmfsub'||targ.name=='vmf_subcat[]'||targ.name=='vm_mnuf[]'|| targ.className=='vmHeader' || targ.className=='featureList'){		
		}		
	else{
		dissapearFeat();
		}
}
	
	});

});
//-----------------VM Categories---------//

function setInput(text,iddz,parent){//puts the text into the input box
		var input=$('selectedVMcat');
		//text=txt.replace(/^\s*/g,'');
		//text=text.replace(/&nbsp;/g,'');
		
		
		errormsgDivs=$$('.srcmsg');
		for(i=0; i<errormsgDivs.length; i++){
			clearMsg(errormsgDivs[i]);
		}
		id=iddz;
		
		if(cat_sel_type==1){//in case of radio btns
			input.innerHTML=text;
			id_array[0]=iddz;
		}
		
		else{//in case of checkboxes
		
			checkbx_cat=$('a'+iddz);
			var chckbCheck=checkbx_cat.getProperty('checked');//the property checked	
					
			if (start_cat==1){
				
				input.innerHTML=text+' ,';
				start_cat=0;
				if(id_array.indexOf(iddz)<0){
					id_array.push(iddz);
				}			
			}
			
			else{//no start
				if(chckbCheck==0){
					var txt=input.innerHTML;
					
					//special html entities----------//
					
					text=text.replace(/&/g,'&amp;');
					text=text.replace(/</g,'&lt;');
					text=text.replace(/>/g,'&gt;');	
					
					
					txt=txt.replace(text+' ,','');
					input.innerHTML=txt;
					id_array.splice(id_array.indexOf(iddz),1);
					
					if(input.innerHTML.replace(/&nbsp;/g,'')==''){				
						input.innerHTML=vmCatInputTxt;
						start_cat=1;
					}
				
				}
				else	{	
					input.innerHTML+=(text+' ,');
					if(id_array.indexOf(iddz)<0){
						id_array.push(iddz);
					}				
				}
			}
		}//end in case of checkboxes
				
}

function dissapearVM(){
var vmcatsBox=$('vmcats');

if(vmcatsBox.getStyle('visibility')=='visible'){
vmcatsBox.setStyle('visibility','hidden');}

clearVmf();
}


function appearVM(){//trigered when the fake input is pressed
	//alert ('hahah');
var vmcatsBox=$('vmcats');
vmcatsBox.setStyle('visibility','visible');
}

//------------------Features------------------------//


function  setFeatInput(feat,idd){
	if (disp_features){
		var featInput=$('selectedFeature');//the input text
		var checkbx=$(idd);
		var clasName=checkbx.getProperty('class');//return the className which will be used to retrieve the header li
		var clname=clasName+'Header';
		var header=$(clname);
		var chckbCheck=checkbx.getProperty('checked');//the property checked	
		
		if (start==1){
			featInput.innerHTML=feat+' ,';
			start=0;
		}
		
		else{
			if(chckbCheck==0){
				var txt=featInput.innerHTML;
				
				//special html entities----------//
				
				feat=feat.replace(/&/g,'&amp;');
				feat=feat.replace(/</g,'&lt;');
				feat=feat.replace(/>/g,'&gt;');	
				
				
				txt=txt.replace(feat+' ,','');
				featInput.innerHTML=txt;
				//unchecks the header li Input
				headr=$E('input',clasName+'Header');
				headr.setProperty('checked','');
				header.setProperty('checked','');
				//alert(headr.getProperty('checked'));
				if(featInput.innerHTML==''){
					featInput.innerHTML=featInputTxt;
					start=1;
				}
				
			}
			else		
			featInput.innerHTML+=(feat+' ,');
			
			}
	}//end if (disp_features)
}


function appearFeats(){///apears the feature box //trigered when the fake input is pressed
	cat_query='';
		
	if (disp_features){
		var new_cat_ar=new Array();
		var j=0;
		for(i=0; i<id_array.length; i++){
			if(id_array[i]){
				cat_query+='&vm_cat[]='+id_array[i];
			}			
		}		
		
			
		var featBox=$('features');//the box with the features
		
		if(cat_query && difference){
			var featInput=$('selectedFeature'); 
			
			featInput.innerHTML=featInputTxt;
			
			featBox.setStyle('visibility','visible');
			url='index.php?option=com_vmfiltering&task=showSubCats'+cat_query+'&featApp='+featApp+'&inclManuf='+inclManuf;
						
			ajax=new Ajax(url,{method: 'get',onRequest:function(){$('features').innerHTML='<img src=\'modules/mod_vmFiltering/images/loading.gif\' style=\'display:block;padding: 10px 80px;\' alt=\'Loading data...\'/>';},onComplete:function(){$('features').innerHTML=this.response.text}}).request();
			idold=id;
			start=1;	}
			
	
		else if(!cat_query){
		displayMsg($('searchMsg'));//display a msg if no category has been selected
		}
		else{
		featBox.setStyle('visibility','visible');		
		}
	}//end if (disp_features){
difference=0;
}



function dissapearFeat(){//dissapears the feature box
	var featBox=$('features');
	featBox.setStyle('visibility','hidden');
}


function checkAll(cls){
	var clssName='.'+cls;
	
	classes=$$(clssName);
	lihead=$(cls+'Header');	
	prop=lihead.getProperty('checked');
	

	if(prop==''||prop==undefined||prop=='false'){ //if not checked
		lihead.setProperty('checked','true');
		property=true;}
	
	else {//if checked
		lihead.setProperty('checked','false');
		property=false;}
		
		
	for (i=0; i<classes.length; i++){//give property checked to all child elements	
	ch=classes[i].getProperty('checked')
	
	if(ch!=property){ 
	classes[i].setProperty('checked',property) ;	
	var checkbx_id=classes[i].getProperty('id');
	var feat=classes[i].getProperty('title');
	setFeatInput(feat,checkbx_id);}	
	}
}

function clearVmf(){//clears the features input
	if (disp_features){
		//check if new categories has been selected compared to last selected
		id_array_toString=id_array.join(',');
		
		if(id_array_toString.length!=id_array_old_toString.length){
			difference=1;			
		}
		
		else {
			for(i=0; i<id_array.length; i++){
				if(id_array_old_toString.indexOf(id_array[i])<0){
					difference=1;					
				}
			}
		}
		
		id_array_old_toString=id_array_toString;	
		if(difference==1){//if new category
			var featInput=$('selectedFeature');//the input text
			featInput.innerHTML=featInputTxt;
			featInputs=$$('input[class=vmfsub]')
			//featInputs=$ES('input','vmfsub')
			featInputs.setProperty('checked','');//uncheck all the input checkboxes									  
			}
	}//end if (disp_features){
		
}




function displayMsg(element){
element.setStyle('visibility','visible');
element.innerHTML=msg;
}

function clearMsg(element){
element.innerHTML='';
}






