/* 
* 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 vmfInputName='';
parentClass='';

window.addEvent('domready', function() {
document.addEvent('click', function(event){
	//vmcats vars
	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'){
		
			}		
	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();
		}
}
	
	});
//--------------prices-----------------//
if(displ_price==1){
	var fr_price=$('fr_price');
	var t_price=$('to_price');
	
	fr_price.addEvent('click',function(){
		
		if (!id){
			fr_price.setProperty("readonly","readonly");
			displayMsg($('searchMsgI'));
		}
	});
	
	t_price.addEvent('click',function(){
		
		if (!id){
			t_price.setProperty("readonly","readonly");
			displayMsg($('searchMsgI'));
	
		}
	});




}

});

//-----------------VM Categories---------//

function setInput(text,idd){//puts the text into the input box
var input=$('selectedVMcat');
text=text.replace(/^\s*/,'');
input.innerHTML=text;

errormsgDivs=$$('.srcmsg');
for(i=0; i<errormsgDivs.length; i++){
clearMsg(errormsgDivs[i]);
}

if(displ_price==1){//if there is the price field
$('fr_price').setProperty("readonly","");//do the fields editable	
$('to_price').setProperty("readonly","");
}
id=idd;
}

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'));
			}
			else		
			featInput.innerHTML+=(feat+' ,');
			}
	}//end if (disp_features)
}


function appearFeats(){///apears the feature box //trigered when the fake input is pressed
	if (disp_features){
		if((id) && (id!=idold)){
			var featInput=$('selectedFeature'); 
			
			featInput.innerHTML=featInputTxt;
			var featBox=$('features');//the box with the features
			featBox.setStyle('visibility','visible');
			url='index.php?option=com_vmfiltering&task=showSubCats&vm_cat='+id+'&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 (id && id==idold){
		var featBox=$('features');//the box with the features
			featBox.setStyle('visibility','visible');
		}
		else if(!id){
		displayMsg($('searchMsg'));//display a msg if no category has been selected
		}
	}//end if (disp_features){
}



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){
		if((id) && (id!=idold)){//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.innerHTML=msg;
}

function clearMsg(element){
element.innerHTML='';
}






