var panorama = function( dataNode ){
    
    this.cNode = dataNode;
    this.panview = null;
    this.panNumber = 0;
    this.menu = (this.cNode.length>1)?(new pager( this.cNode.length, 0, 1, $('#panselect').get(0) )):null;
    this.imageSize = 'nameSizeHigh';

    this.show = function(){
		this.panview = new panView( $('#winpan'), 'http://images.traveltainment.de'+ this.cNode[ this.panNumber ].image[ this.imageSize ] );
		if (this.menu){
			this.menu.onclick = this.changePicture;
			this.menu.nextLink='<img src="'+hib.pfadImages+'arrowsright.png" alt="" title="następna panorama">';
			this.menu.prevLink='<img src="'+hib.pfadImages+'arrowsleft.png" alt="" title="poprzednia panorama">';		
			this.menu.draw();
		}
    }
    
    this.changePicture = function(a){
	hib.panorama.panNumber = a;
	hib.panorama.panview.destroy();
	hib.panorama.show();
    }
    
    this.show(); 
}

var areainfo = function( dataNode, poiType, cfg ){
    
    this.config=cfg;
    this.cNode = dataNode;
    this.currentPoiType = poiType;
    this.currentSubpage = 0;
    this.totalPOIs = null;
    this.menu = null;
    this.currentData = null;
    this.slide = null;
    
    this.drawPOIsList = function(){

		var out=['<tr><th class="c1">Odległość</th><th class="c2"></th><th class="c3">Nazwa</th><th class="c4">Miasto</th><th class="c5"></th></tr>'];
		var cnt=0;
		for( var n=0; n<this.cNode.pois.length; n++ ){
			with( this.cNode.pois[n] ){			
			if (poiType == this.currentPoiType ){
				var subpageCnt = parseInt(cnt/this.config.areasPerPage);
				if (subpageCnt == this.currentSubpage) {
				out.push( '<tr class="row'+(cnt%2)+'"><td>' + distanceInterval + '</td>');
				out.push( '<td><img src="' + thumbnail + '" alt="" onclick="hib.areashow.showDetails(\''+poiRefID+'\')"/></td>');
				out.push( '<td>' + headline + '</td><td>' + village + '</td>');
				out.push( '<td><span title="pokaż więcej" onclick="hib.areashow.showDetails(\''+poiRefID+'\')">' + this.config.areaMoreLinkTxt + '</span></td></tr>' );
				}
				cnt++;
			}
			}
		}
		this.totalPOIs = cnt;
	
		if (!this.menu && cnt>this.config.areasPerPage){
			this.menu = new pager( this.totalPOIs, 0, this.config.areasPerPage, $( this.config.areaListPagerSelector).get(0) );
			this.displayPager();
		}
	
		$(this.config.areaListContentSelector).html(out.join(''));
		this.switchToList();
    }
    
    this.changePage = function(x){
		hib.areashow.currentSubpage = x/hib.config.areasPerPage;
		hib.areashow.drawPOIsList();
    }
    
    this.displayPager = function(){
		this.menu.onclick = this.changePage;
		this.menu.nextLink='<img src="'+hib.pfadImages+'arrowsright.png" alt="" title="następna strona">';
		this.menu.prevLink='<img src="'+hib.pfadImages+'arrowsleft.png" alt="" title="poprzednia strona">';		
		this.menu.draw();
    }
    
    this.switchToImages = function(){
		$('div.poilist').hide();
		$('div.poipanorama').hide();
		$('div.poiimages').show();
		$('div.poiimages h1').html('<strong>'+this.currentData.headline+'</strong> ('+this.currentData.village+')');
		$('div.poiimages p.desc').html( this.currentData.description );	
		
		var e = $('div.poiimages ul.menu li:eq(1)');
		if (this.currentData.panoramas.length>0) e.show(); else e.hide();
		
		if (hib.panorama) hib.panorama.panview.destroy();
		this.slide = new slideshow( this.currentData.images , hib.pfadImages );
		this.slide.cfgAllowResize = true;
		this.slide.init();
		this.slide.showFirst();
		hib.fitHeight();
    }

    this.switchToPanorama = function(){
		$('div.poilist').hide();
		$('div.poipanorama').show();
		$('div.poiimages').hide();
		$('div.poipanorama h1').html('<strong>'+this.currentData.headline+'</strong> ('+this.currentData.village+')');
		
		hib.panorama = new panorama( this.currentData.panoramas );
		hib.fitHeight();
    }
	
    this.switchToList = function(){
		$('div.poilist').show();
		$('div.poipanorama').hide();
		$('div.poiimages').hide();
		
		if (hib.panorama) hib.panorama.panview.destroy();
	}
	
    this.showDetails = function( poiRefID ){
		hib.getData('TTWSPOI/getPOIDetail', {poiRefID : poiRefID } , this.parseData );
    }
    
    this.parseData = function( data ){

		if (typeof data == 'object'){ // przyszły "świerze" dane z WS
			hib_cache.WSresults[data.id] = data;
			hib.areashow.currentData = data;
			hib.areashow.switchToImages();
		} else if (typeof data == 'string'){ // dane już były w cache
			hib.areashow.currentData = hib_cache.WSresults[data];
			hib.areashow.switchToImages();
		} else {
			alert('unknown type data');
		}
    }
}

var timeManager = function( delay ){
// obiekt do zarządzania intervalami	
	this.run = function(){
		this.timer = setInterval( this.doEvents, delay );
	}
	
	this.stop = function(){
		clearInterval( this.timer );
		this.timer = null;
	}
	
	this.addAction = function( name, actFn, obj ){
		this.actions[ name ] = actFn;
		this.contexts[ name ] = obj;
		if ( !this.timer ) this.run();
	}
	
	this.remAction = function( name ){
		delete this.actions[ name ];
		if ( this.actions.length==0 ) this.stop();
	}

	this.doEvents = function(){
		for (var n in timer.actions){
			timer.actions[n].apply(timer.contexts[n]);
		}
		timer.value++;
	}
	
	this.clear = function(){
		this.stop();
		this.actions=[];
		this.contexts=[];		
	}

	// inicjalizacja
	this.delay = delay;
	this.value = 0;
	this.actions=[];
	this.contexts=[];
}


