/*
* 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_feat=1; //indicates that no feature has been selected yet
var start_mnf=1; //indicates that no manuf has been selected yet
var start_cat=1;
var start_dim=1;
var vmfInputName='';
parentClass='';
var checkbx_cat=null;
var id_array=new Array();
var id_array_old_toString='';
var difference=0;
var difference_feat=0;
var difference_mnf=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 checkBxF=featBox.getElements('input');
    //alert(checkBxF);
	var featInput=$('selectedFeature');
	}

    if (disp_manufs){
	//manuf vars
	var mnfBox=$('Manufs');
	var mnfInput=$('selectedMnf');
	}
	
	var event = new Event(event);
	targ=event.target;	

	//dissapear categories
	if(targ==vmcatsBox || targ==input || targ.className=='vmcatsL' || targ.className=='vmCatsUL'||targ.className=='vmHeader'||targ.className=='headerIn' || targ.className=='headerLi'||targ.className=='vmcatsLi'||targ.name=='vm_cat[]'){
		
			}		
	else if (vmcatsBox.getStyle('visibility')=='visible'){
		//alert(targ.className);
		dissapearVM();
		}
		
		
//----------------dissapear features------------------//
if (disp_features==1){
 	if(targ==featBox || (targ.name && targ.name.match(/^feat_/)) || targ==featInput || targ.className=='vmfWrapperInner' || targ.className=='headerIn' || targ.className=='headerLi'||  targ.className=='vmfsub'||targ.name=='vmf_subcat[]' || targ.className=='vmHeader' || targ.className=='featureList'){
		}
	else if(featBox.getStyle('visibility')=='visible'){
		dissapearFeat();
		}
}

//----------------dissapear Manuf------------------//
if (disp_manufs==1){

	if(targ==mnfBox || targ==mnfInput || targ.className=='vmfWrapperInner' || targ.className=='headerIn' || targ.className=='headerLi'||  targ.className=='vmfsub'||targ.name=='vm_mnuf[]'|| targ.className=='vmHeader' || targ.className=='featureList'){
		}
	else if (mnfBox.getStyle('visibility')=='visible'){
		dissapearMnf();
		}
}

	});

});
//-----------------VM Categories---------//

function setInput(text,iddz){//puts the text into the input box
        text=text.replace(/&/g,'&amp;');
        text=text.replace(/</g,'&lt;');
	    text=text.replace(/>/g,'&gt;');
               //&nbsp;

        var cat_id=iddz.substr(1); //the category id
		var input=$('selectedVMcat');
		//text=txt.replace(/^\s*/g,'');
		//text=text.replace(/&nbsp;/g,'');
		
		//clear the select categories notifications
		errormsgDivs=$$('.srcmsg');
		for(i=0; i<errormsgDivs.length; i++){
			clearMsg(errormsgDivs[i]);
		}
		id=cat_id;
    	input.innerHTML=text;
    	id_array[0]=cat_id;
      	$('vmc').setProperty('value',cat_id);
        dissapearVM();
}

function dissapearVM(){
  var vmcatsBox=$('vmcats');

  if(vmcatsBox.getStyle('visibility')=='visible'){
      vmcatsBox.setStyle('visibility','hidden');
  }
  setDifference();
if (disp_features==1)  clearVmf_feat();
if (disp_manufs==1)  clearVmf_mnf();
}


function appearVM(){//trigered when the fake input is pressed
	//alert ('hahah');
    var vmcatsBox=$('vmcats');
    vmcatsBox.setStyle('visibility','visible');
}

function setDifference(){
  	difference=0;

   //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;
  		}
  	}
  }
  //alert(difference);
  id_array_old_toString=id_array_toString;
  difference_feat=difference;
  difference_mnf=difference;
}
//------------------Features------------------------//
function appearFeats(){///apears the feature box //trigered when the fake input is pressed
    //setDifference();
	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){
			var featInput=$('selectedFeature');
			
			featInput.innerHTML=featInputTxt;

			featBox.setStyle('visibility','visible');
			url=siteURL+'index.php?option=com_vmfiltering&task=getFeatures'+cat_query+'&featApp='+featApp;
			ajax=new Ajax(url,{method: 'get',onRequest:function(){$('features').innerHTML='<img src=\''+siteURL+'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_feat=1;	}
			
	
		else if(!cat_query){
		displayMsg($('searchMsgFeat'));//display a msg if no category has been selected
		}
		else{
		featBox.setStyle('visibility','visible');		
		}
        difference_feat=0;
	}//end if (disp_features){

}



function dissapearFeat(){//dissapears the feature box
	var featBox=$('features');
	featBox.setStyle('visibility','hidden');
}




function  setFeatInput(feat,idd,nam){
	if (disp_features){
	    hidenID='feat_'+nam;
		var featInput=$('selectedFeature');//the input text
		var txt=featInput.innerHTML;
		//special html entities----------//

		feat=feat.replace(/&/g,'&amp;');
		feat=feat.replace(/</g,'&lt;');
		feat=feat.replace(/>/g,'&gt;');
        featInput.innerHTML=feat;
        $(hidenID).value=idd;
	}//end if (disp_features)
      dissapearFeat();
}

function clearVmf_feat(){//clears the features input

	if (disp_features){
		if(difference_feat==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){

}

//----------------------Manufacturers-----------------//

function appearManufs(){///apears the manufacturers box //trigered when the fake input is pressed
    //setDifference();
	cat_query='';

	if (disp_manufs){
		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 mnfBox=$('Manufs');//the box with the Manufs

		if(cat_query){
			var mnfInput=$('selectedMnf');

			mnfInput.innerHTML=mnfInputTxt;

			mnfBox.setStyle('visibility','visible');
			url=siteURL+'index.php?option=com_vmfiltering&task=getmanufs'+cat_query;

			ajax=new Ajax(url,{method: 'get',onRequest:function(){$('Manufs').innerHTML='<img src=\''+siteURL+'modules/mod_vmfiltering/images/loading.gif\' style=\'display:block;padding: 10px 80px;\' alt=\'Loading data...\'/>';},onComplete:function(){$('Manufs').innerHTML=this.response.text}}).request();
			idold=id;
			start_mnf=1;	}


		else if(!cat_query){
		displayMsg($('searchMsgManuf'));//display a msg if no category has been selected
		}
		else{
		mnfBox.setStyle('visibility','visible');
		}
        difference_mnf=0;
	}//end if (disp_features){
}

function  setManufInput(feat,id){
    if(disp_manufs){
      $('mf_hid').value=id;
		var mnfInput=$('selectedMnf');//the input text

		var txt=mnfInput.innerHTML;

		//special html entities----------//

		feat=feat.replace(/&/g,'&amp;');
		feat=feat.replace(/</g,'&lt;');
		feat=feat.replace(/>/g,'&gt;');

		mnfInput.innerHTML=feat;

  }//end if (disp_manufs)
  dissapearMnf();
}

function dissapearMnf(){//dissapears the feature box
	var mnfBox=$('Manufs');
	mnfBox.setStyle('visibility','hidden');
}

function clearVmf_mnf(){//clears the features input

	if (disp_manufs){
		if(difference_mnf==1){//if new category
			var mnfInput=$('selectedMnf');//the input text
			mnfInput.innerHTML=mnfInputTxt;
			mnfInputs=$$('input[class=vmfsub]')
			//mnfInputs=$ES('input','vmfsub')
			mnfInputs.setProperty('checked','');//uncheck all the input checkboxes
			}
	}//end if (disp_manufs){

}
//----------------------------
function checkAll(cls,drop_down_type){
	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;
    }
       //alert(property);

	for (x=0; x<classes.length; x++){//give property checked to all child elements
     //alert(classes[i].getProperty('id'));
    	ch=classes[x].getProperty('checked');
        //alert(ch);

    	if(ch!=property){
        	classes[x].setProperty('checked',property) ;
        	var checkbx_id=classes[x].getProperty('id');
        	var feat=classes[x].getProperty('title');

            //decide where to input
            if(drop_down_type=='feat')	setFeatInput(feat,checkbx_id);
            else if(drop_down_type=='manuf') setManufInput(feat,checkbx_id);
            else if(drop_down_type=='categ') setInput(feat,checkbx_id);
        }
    }
}

function displayMsg(element){
    element.setStyle('display','block');
    element.innerHTML=msg;
   // var Tips1 = new Tips($('selectedMnf'));
   //$('selectedMnf').setProperty('title','sakiss')
   //alert($('selectedMnf').client.x );

}

function clearMsg(element){
    element.setStyle('display','none');
}






