/*
 * porownanie telefonow 
 * (c) 2007 Darek Szymaczek, projekty.www[at]gmail.com
 */
phonescompare = function() {
	this.telcont = {'tel-lst-first':0, 'tel-lst-second':0, 'tel-lst-third':0};
	this.telcomp_cont = null;
	this.telcomp_cont_id = 'telcomp_cont';
	this.telcomp_cnt = 0;
	this.telcomp_cnt_max = 0
	this.telcnt = 0;
	this.telsdata = []
	this.onload_telschecked = []
	
	for(i in this.telcont) {this.telcomp_cnt_max++}
	
	this.onloadteldata = function(tel1,tel2,tel3) {
		if (tel1!='') {this.onload_telschecked.push(tel1)}
		if (tel2!='') {this.onload_telschecked.push(tel2)}
		if (tel3!='') {this.onload_telschecked.push(tel3)}
	}
	
	this.loadteldata = function(conf) {
		if (typeof conf == 'undefined') {
			conf = '';
		}
		advAJAX.get({
			url: "/ajax.php?m=telscompare&a=loadteldata&conf="+this.telreqconf,
			myObj: this,
			onLoading : function(obj){ $('load_img').style.display='block' },
			onComplete : function(obj) { $('load_img').style.display='none' },
			onSuccess : function(obj) {
				obj.myObj.telsdata = eval('(' + obj.responseText + ')');
				obj.myObj.refreshData();
				
				for(i=0; i<obj.myObj.onload_telschecked.length; i++)
				{
					if (typeof obj.myObj.onload_telschecked[i] != 'function')
					{
						obj.myObj.addtel(obj.myObj.onload_telschecked[i])
					}
				}
			}
		})
	}
	
	this.refreshData = function() {
		this.telcomp_cont = $(this.telcomp_cont_id)
		this.telcomp_cont.innerHTML='';
		this.telcnt=0;
		
		var li = null, d=document;
		
		for(i in this.telcont) {
			$(i+'-cimg').style.display='none';
			$(i+'-cont').innerHTML = '';
			this.telcont[i] = 0
		}
		
		for(i in this.telsdata) {
			li = d.createElement('li');
			li.innerHTML='<img src="../i/cms/'+this.telsdata[i].imgm_src+'" height="100" alt="" class="link" onclick="phonescompare.addtel(\''+this.telsdata[i].tid+'\')" id="telm_'+this.telsdata[i].tid+'"/><p>'+this.telsdata[i].title+'</p>';
			this.telcomp_cont.appendChild(li)
			this.telcnt++;
		}
		
		this.telcomp_cont.style.width=(120*this.telcnt+10)+'px'
	}
	
	this.addtel = function(tid) {
		if (this.telcomp_cnt>=this.telcomp_cnt_max) {
			return false;
		}
		var free_cont = false;
		for(i in this.telcont)
		{
			if (this.telcont[i] == tid) {
				return false;
			} else if(!free_cont && $(i+'-cimg').style.display=='none') {
				free_cont = i
			}
		}
		if (free_cont)
		{
			this.telcont[free_cont] = tid
			$(free_cont+'-cimg').style.display='block';
			$('telm_'+tid).parentNode.className='link selected';
			$(free_cont+'-cont').innerHTML='<img src="../i/cms/'+this.telsdata['t'+tid].imgm_src+'" class="tel" height="200" alt="" /><p>'+this.telsdata['t'+tid].title+'</p>';
			var i=new Image()
			i.onload=function(){$(free_cont+'-cont').firstChild.src=this.src}
			i.src='../i/cms/'+this.telsdata['t'+tid].imgd_src;
			
		}
		this.telcomp_cnt++;
	}
	
	this.deltel = function(contid) {
		if ($(contid+'-cimg').style.display=='block') {
			$(contid+'-cimg').style.display='none';
			$(contid+'-cont').innerHTML = '';
			$('telm_'+this.telcont[contid]).parentNode.className='link';
			this.telcont[contid] = 0
			if (this.telcomp_cnt>0) {
				this.telcomp_cnt--;
			}
		}
	}
	
	this.cleartels = function() {
		for(i in this.telcont) {
			this.deltel(i);
		}
		this.clearcompare();
	}
	
	this.dictTN = function(x)
	{
		var tmap = {'T':'tak','N':'nie','-':'-','bd':'brak danych'};
		if (tmap[x]) {
			return tmap[x];
		} else {
			return x;
		}
	}
	
	this.clearcompare = function() {
		$('tab-porownanie').style.display='none';
		$('telefony-lst').style.display='block';
		$('showtelsbox').style.display='none';
		for(i=0; i<$('tab-porownanie').getElementsByTagName('tr').length; i++) {
			for(j=1; j<=3; j++) {
				$('tab-porownanie').getElementsByTagName('tr')[i].getElementsByTagName('td')[j].innerHTML='';
			}
		}
	}
	
	this.compare = function() {
		this.clearcompare();
		
		var tel_ids = []
		for(i in this.telcont) {
			if (this.telcont[i] != 0) {
				tel_ids.push(this.telcont[i]);
			}
		}
		if (tel_ids.length < 2) {
			alert('Wybierz conajmniej 2 telefony');
			return false;
		}
		//$('telcomp_cont').display='none';
		var compare_tels = []
		advAJAX.get({
			url: "/ajax.php?m=telscompare&a=compare_tels&tels="+tel_ids.toString(),
			myObj: this,
			onLoading : function(obj){ $('load_img').style.display='block' },
			onComplete : function(obj) { $('load_img').style.display='none' },
			onSuccess : function(obj) {
				compare_tels = eval('(' + obj.responseText + ')');
				var dictTN = obj.myObj.dictTN;
				var i=-1,j=0;
				for(j in obj.myObj.telcont) {
					i++;
					if (obj.myObj.telcont[j] == 0) {
						continue;
					}
					id=obj.myObj.telcont[j]
					$('por-tel-system').getElementsByTagName('td')[i+1].innerHTML = compare_tels[id].CA_GENERAL_SYSTEM;
					$('por-tel-wymiary').getElementsByTagName('td')[i+1].innerHTML = compare_tels[id].CA_GENERAL_WIDTH + 'x' + compare_tels[id].CA_GENERAL_HEIGHT + 'x' + compare_tels[id].CA_GENERAL_THICKNESS + ' mm';
					$('por-tel-waga').getElementsByTagName('td')[i+1].innerHTML = compare_tels[id].CA_GENERAL_WEIGHT + ' g';
					$('por-tel-aku').getElementsByTagName('td')[i+1].innerHTML = compare_tels[id].CA_GENERAL_ACU;
					$('por-tel-aku-par').getElementsByTagName('td')[i+1].innerHTML = compare_tels[id].CA_GENERAL_ACU_MAH;
					$('por-tel-czasczuw').getElementsByTagName('td')[i+1].innerHTML = compare_tels[id].CA_GENERAL_WAKINGTIME;
					$('por-tel-czasrozm').getElementsByTagName('td')[i+1].innerHTML = compare_tels[id].CA_GENERAL_TALKINGTIME;
					$('por-tel-display').getElementsByTagName('td')[i+1].innerHTML = compare_tels[id].CA_GENERAL_DISPLAY!='' ? compare_tels[id].CA_GENERAL_DISPLAY : '';
					$('por-tel-display-par').getElementsByTagName('td')[i+1].innerHTML = compare_tels[id].CA_GENERAL_DISPLAY_PAR ? compare_tels[id].CA_GENERAL_DISPLAY_PAR : '';
					$('por-tel-displayadd').getElementsByTagName('td')[i+1].innerHTML = compare_tels[id].CA_GENERAL_DISPLAY_ADD!='N' ? compare_tels[id].CA_GENERAL_DISPLAY_ADD : 'nie';
					$('por-tel-displayadd-par').getElementsByTagName('td')[i+1].innerHTML = compare_tels[id].CA_GENERAL_DISPLAY_ADD!='N' ? compare_tels[id].CA_GENERAL_DISPLAY_ADD_PAR : '';
					$('por-tel-vibra').getElementsByTagName('td')[i+1].innerHTML = dictTN(compare_tels[id].CA_GENERAL_VIBRA);
					$('por-tel-memory').getElementsByTagName('td')[i+1].innerHTML = compare_tels[id].CA_GENERAL_MEMORY;
					$('por-tel-cardslot').getElementsByTagName('td')[i+1].innerHTML = dictTN(compare_tels[id].CA_GENERAL_CARDSLOT);
					$('por-tel-memcard').getElementsByTagName('td')[i+1].innerHTML = compare_tels[id].CA_GENERAL_MEMORYCARD;
					$('por-tel-remcovers').getElementsByTagName('td')[i+1].innerHTML = dictTN(compare_tels[id].CA_GENERAL_REMOVABLECOVER);
					
					$('por-tel-irda').getElementsByTagName('td')[i+1].innerHTML = dictTN(compare_tels[id].CA_DANE_IRDA);
					$('por-tel-bt').getElementsByTagName('td')[i+1].innerHTML = dictTN(compare_tels[id].CA_DANE_BT);
					$('por-tel-csd').getElementsByTagName('td')[i+1].innerHTML = dictTN(compare_tels[id].CA_DANE_CSD);
					$('por-tel-hscsd').getElementsByTagName('td')[i+1].innerHTML = dictTN(compare_tels[id].CA_DANE_HSCSD);
					$('por-tel-gprs').getElementsByTagName('td')[i+1].innerHTML = dictTN(compare_tels[id].CA_DANE_GPRS);
					$('por-tel-edge').getElementsByTagName('td')[i+1].innerHTML = dictTN(compare_tels[id].CA_DANE_EDGE);
					$('por-tel-wifi').getElementsByTagName('td')[i+1].innerHTML = dictTN(compare_tels[id].CA_DANE_WIFI);
					$('por-tel-wap').getElementsByTagName('td')[i+1].innerHTML = dictTN(compare_tels[id].CA_DANE_WAP);
					$('por-tel-imode').getElementsByTagName('td')[i+1].innerHTML = dictTN(compare_tels[id].CA_DANE_IMODE);
					$('por-tel-aparat').getElementsByTagName('td')[i+1].innerHTML = dictTN(compare_tels[id].CA_DANE_APARAT);
					$('por-tel-aparatpar').getElementsByTagName('td')[i+1].innerHTML = compare_tels[id].CA_DANE_APARAT_PAR;
					$('por-tel-pda').getElementsByTagName('td')[i+1].innerHTML = dictTN(compare_tels[id].CA_DANE_PDA);
					$('por-tel-pdapar').getElementsByTagName('td')[i+1].innerHTML = compare_tels[id].CA_DANE_PDA_PAR;
					
					$('por-tel-slowniksms').getElementsByTagName('td')[i+1].innerHTML = dictTN(compare_tels[id].CA_MSGS_SLOWNIK);
					$('por-tel-longmsgs').getElementsByTagName('td')[i+1].innerHTML = dictTN(compare_tels[id].CA_MSGS_LONGMSGS);
					$('por-tel-smartmsgs').getElementsByTagName('td')[i+1].innerHTML = dictTN(compare_tels[id].CA_MSGS_SMARTMSGS);
					$('por-tel-ems').getElementsByTagName('td')[i+1].innerHTML = dictTN(compare_tels[id].CA_MSGS_EMS);
					$('por-tel-mms').getElementsByTagName('td')[i+1].innerHTML = dictTN(compare_tels[id].CA_MSGS_MMS);
					$('por-tel-emailclient').getElementsByTagName('td')[i+1].innerHTML = dictTN(compare_tels[id].CA_MSGS_EMAILCLIENT);
					
					
					$('por-tel-java').getElementsByTagName('td')[i+1].innerHTML = dictTN(compare_tels[id].CA_SOFT_JAVA);
					$('por-tel-telcontacts').getElementsByTagName('td')[i+1].innerHTML = dictTN(compare_tels[id].CA_SOFT_TELCONTACTS);
					$('por-tel-profiles').getElementsByTagName('td')[i+1].innerHTML = dictTN(compare_tels[id].CA_SOFT_PROFILES);
					$('por-tel-calendar').getElementsByTagName('td')[i+1].innerHTML = dictTN(compare_tels[id].CA_SOFT_CALENDAR);
					$('por-tel-tasklist').getElementsByTagName('td')[i+1].innerHTML = dictTN(compare_tels[id].CA_SOFT_TASKLIST);
					$('por-tel-notepad').getElementsByTagName('td')[i+1].innerHTML = dictTN(compare_tels[id].CA_SOFT_NOTEPAD);
					$('por-tel-clock').getElementsByTagName('td')[i+1].innerHTML = dictTN(compare_tels[id].CA_SOFT_CLOCK);
					$('por-tel-alarm').getElementsByTagName('td')[i+1].innerHTML = dictTN(compare_tels[id].CA_SOFT_ALARM);
					$('por-tel-stopwatch').getElementsByTagName('td')[i+1].innerHTML = dictTN(compare_tels[id].CA_SOFT_STOPWATCH);
					$('por-tel-countdown').getElementsByTagName('td')[i+1].innerHTML = dictTN(compare_tels[id].CA_SOFT_COUNTDOWN);
					$('por-tel-polyphony').getElementsByTagName('td')[i+1].innerHTML = dictTN(compare_tels[id].CA_SOFT_POLYPHONY);
					$('por-tel-polyphonypar').getElementsByTagName('td')[i+1].innerHTML = compare_tels[id].CA_SOFT_POLYPHONY_PAR;
					
					$('por-tel-voicedial').getElementsByTagName('td')[i+1].innerHTML = compare_tels[id].CA_SOFT_VOICEDIAL;
					$('por-tel-voicerec').getElementsByTagName('td')[i+1].innerHTML = dictTN(compare_tels[id].CA_SOFT_VOICEREC);
					$('por-tel-handsfree').getElementsByTagName('td')[i+1].innerHTML = dictTN(compare_tels[id].CA_SOFT_HANDSFREE);
					$('por-tel-radio').getElementsByTagName('td')[i+1].innerHTML = dictTN(compare_tels[id].CA_SOFT_RADIO);
					$('por-tel-mp3').getElementsByTagName('td')[i+1].innerHTML = dictTN(compare_tels[id].CA_SOFT_MP3);
					$('por-tel-videoconf').getElementsByTagName('td')[i+1].innerHTML = dictTN(compare_tels[id].CA_SOFT_VIDEOCONF);
					
				}
				$('tab-porownanie').style.display='block';
				$('telefony-lst').style.display='none';
				$('showtelsbox').style.display='block';
				
				for(i=0; i<$('tab-porownanie').getElementsByTagName('tr').length; i++) {
					for(j=1; j<=3; j++) {
						if($('tab-porownanie').getElementsByTagName('tr')[i].getElementsByTagName('td')[j].innerHTML=='') {
							$('tab-porownanie').getElementsByTagName('tr')[i].getElementsByTagName('td')[j].innerHTML='&nbsp;'
						}
					}
				}
			}
		})
	}
	
	this.showtelsbox = function() {
		$('telefony-lst').style.display='block';
		$('showtelsbox').style.display='none';
	}
}

var phonescompare = new phonescompare();