var SiteClass = new Class({

	initialize: function(){
		
		hs.lang.cssDirection = lang.cssDirection;
		hs.lang.loadingText = lang.loadingText;
		hs.lang.loadingTitle = lang.loadingTitle;
		hs.lang.focusTitle = lang.focusTitle;
		hs.lang.fullExpandTitle = lang.fullExpandTitle;
		hs.lang.previousText = lang.previousText;
		hs.lang.nextText = lang.nextText; 
		hs.lang.moveText = lang.moveText;
		hs.lang.closeText = lang.closeText; 
		hs.lang.closeTitle = lang.closeTitle; 
		hs.lang.resizeTitle = lang.resizeTitle;
		hs.lang.playText = lang.playText;
		hs.lang.playTitle = lang.playTitle;
		hs.lang.pauseText = lang.pauseText;
		hs.lang.pauseTitle = lang.pauseTitle;
		hs.lang.previousTitle = lang.previousTitle;
		hs.lang.nextTitle = lang.nextTitle;
		hs.lang.moveTitle = lang.moveTitle;
		hs.lang.fullExpandText = lang.fullExpandText;
		hs.lang.number = lang.number;
		hs.lang.restoreTitle = lang.restoreTitle;			
		hs.graphicsDir = 'public/images/highslide/';
		hs.align = 'center';
		hs.transitions = ['expand', 'crossfade'];
		hs.outlineType = 'rounded-white';
		hs.fadeInOut = true;
		hs.dimmingOpacity = 0.50;
		
		hs.addSlideshow({
			//slideshowGroup: 'group1',
			interval: 5000,
			repeat: false,
			useControls: true,
			fixedControls: 'fit',
			overlayOptions: {
				opacity: .75,
				position: 'bottom center',
				hideOnMouseOut: true
			}
		});

	}, // initialize
	
	domReady: function() {
		
		this.setActive('div#menu');
		this.setActive('ul#submenu-tree');
		$$('div#menu a.active').each(function(a){
			var li = a.getParent('li').getParent('li');
			if(li) li.getFirst().getFirst().addClass('active');	
		});
		
		$$('div#menu a').each(function(a){ a.addClass('cufon').addClass('cufon900'); });
		Cufon.replace('.cufon100', { fontFamily: '100', hover: true });
		Cufon.replace('.cufon300', { fontFamily: '300', hover: true });
		Cufon.replace('.cufon900', { fontFamily: '900', hover: true });
		Cufon.replace('.cufonCompany', { fontFamily: '900', hover: true }); /* , textShadow: 'rgba(51,51,51,0.6) 1px 1px' rgba(255,255,255,0.6) -1px -1px,  */
		Cufon.replace('.cufonCaption', { fontFamily: '900', hover: true, color: '-linear-gradient(#002e20, #016648)' }); /* textShadow: 'rgba(51,51,51,0.2) 2px 2px', */
		Cufon.replace('.cufonGG', { fontFamily: '900', hover: true, color: '-linear-gradient(#002e20, #016648)' });
		Cufon.now();
		
		new OverText($('q'));
		if($('news-layers')) this.activateNews();
		if($('awards-layers')) this.activateAwards();
        if($('slideshow')) this.activateSlideshow();
		this.activateFontSize();
       
		if($('gotop')) {
			$('gotop').addEvent('click', function() {
				var scrollFx = new Fx.Scroll(window, {
					transition: Fx.Transitions.Quad.easeInOut
				});
				scrollFx.toTop();
			});
		}
		if($('goback')) {
			$('goback').addEvent('click', function() { history.back(); });
		}
		
		if($('print')) {
			$('print').addEvent('click', function(){
				window.open(document.location+'?print=1', '', 'menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=no,fullscreen=no,channelmode=no,width=800,height=600,left=50,top=50').focus();						 
				return false;
			});
		}

		if($('facebook')) {
			$('facebook').addEvent('mouseenter',function(e){
				e.stop();	
				new Fx.Tween($('facebook'), {
					duration: 500,
					transition: Fx.Transitions.Quad.easeInOut
				}).start('right', 0);				
			}).addEvent('mouseleave',function(e){
				e.stop();	
				new Fx.Tween($('facebook'), {
					duration: 500,
					transition: Fx.Transitions.Quad.easeInOut
				}).start('right', '-252px');				
			});
		}
		
		this.activatePoll();
		this.loadMap();

		(function(){
			var lang_id = $('langs-desc').className.substring(5,6);
			var count = $$('div#langs-desc span').length;
			$('langs-desc').className = 'lang-'+((lang_id % count)+1);
		}).periodical(5000);
		/*
		this.loadMap();
		*/
	}, // domReady

	map: null,
	markerBounds: null,
	infoWindows: new Array(),
	loadMap: function() {
		
		if($('locationsMap')) {
			var myLatlng = new google.maps.LatLng(51.89551905518783, 19.2587890625);
			var myOptions = {
				zoom: 6,
				center: myLatlng,
				mapTypeControl: true,
				navigationControl: true,
				navigationControlOptions: {
					style: google.maps.NavigationControlStyle.ZOOM_PAN
				},
				mapTypeId: google.maps.MapTypeId.ROADMAP
			}
			
			this.map = new google.maps.Map($('locationsMap'), myOptions);
			
			
			Site.markerBounds = new google.maps.LatLngBounds();
			
			locations.each(function(elem){
				var pos = elem.location.split(',');
				var point = new google.maps.LatLng(pos[0],pos[1]);
			  	Site.markerBounds.extend(point);				
				if(elem.id == 1)
					var marker = new google.maps.Marker({position: point, map: Site.map, icon: new google.maps.MarkerImage('http://'+document.location.host+'/public/images/gmaps-big-egg.png', new google.maps.Size(30,31), new google.maps.Point(0,0), new google.maps.Point(15,15)), title: elem.name, zIndex: 1000})
				else
					var marker = new google.maps.Marker({position: point, map: Site.map, icon: new google.maps.MarkerImage('http://'+document.location.host+'/public/images/gmaps-small-egg.png', new google.maps.Size(20,20), new google.maps.Point(0,0), new google.maps.Point(10,10)), title: elem.name})
				
				var location = document.location.href.split('#');
				var infowindow = new google.maps.InfoWindow(
			  	      { content: '<div class="gmapInfoWindow">'+
					  			 '<h2>'+elem.name+'</h2>'+
			  	    	  		 elem.short_desc+
								 '<a href="'+location[0]+'/'+elem.nameurl+'.html" class="more">'+lang.more+' &raquo;</a>'+
			  	    	  		 '</div>',
			  	        size: new google.maps.Size(50,50)
			  	      });
				Site.infoWindows.push(infowindow);
			  	google.maps.event.addListener(marker, 'click', function() {
					Site.infoWindows.each(function(iw){ iw.close(); });
			  	    infowindow.open(Site.map, marker);
			    });
				
			});
			this.map.fitBounds(this.markerBounds);
			
		}
		
	}, // loadMap
	
	activatePoll: function() {
		if(!$('poll')) return null;
		
		$$('ul.answers input').each(function(input){
			input.getParent().addEvent('click', function(){
				this.getFirst().checked = 'checked';
				$('poll-loader').show();
				$('poll-results').hide();
				$('poll-form').action = '/'+lang.lang+'/sondy/glosuj?ajax';
				$('poll-form').set('send', {
					onComplete: function(response) {
						$('poll-loader').hide();
						$('poll-content').innerHTML = response;
					},
					onFailure: function() {
						$('poll-loader').hide();
						$('poll-content').innerHTML = lang.error;
					},
					evalScripts: true
				}).send();
			});
		});
			
		if($('poll-results')) { 
			$('poll-results').addEvent('click', function(){
				$('poll-results').hide();
				$('poll-loader').show();
				$('poll-form').action = '/'+lang.lang+'/sondy/wyniki?ajax';
				$('poll-form').set('send', {
					onComplete: function(response) {
						$('poll-loader').hide();
						$('poll-content').innerHTML = response;
					},
					onFailure: function() {
						$('poll-loader').hide();
						$('poll-content').innerHTML = lang.error;
					},
					evalScripts: true
				}).send();
			});
		}
		
	}, //activatePoll

	activateFontSize: function() {
		$$('a.font-size').each(function(a){
			a.addEvent('click', function(){
				var size = a.rel;
				$('body').setStyle('fontSize', size+'px');
				Cookie.write('font-size', size, {duration: 999999});
			});
			if(Cookie.read('font-size')) $('body').setStyle('fontSize', Cookie.read('font-size')+'px');
		});	
	}, //activateFontSize

	slideshow: null,
	
	activateSlideshow: function() {
		this.slideshow = new SlideItMoo({
			overallContainer: 'slideshow',
			elementScrolled: 'slideshow-inner',
			thumbsContainer: 'slideshow-items',		
			itemsVisible:1,
			elemsSlide:1,
			itemsSelector: '.slideshow-element',
			itemWidth: 1040,
			showControls:1,
			startIndex:1,
			mouseWheelNav:false,
			navs: {
				fwd: '.slideshow-next',
				bk: '.slideshow-prev'
			},
			transition: Fx.Transitions.Quad.easeInOut,
			duration:600,
			autoSlide: 8000,
			onChange: function(index){
				var switches = $$('#slideshow-navigation a')
				for(var i=0; i<switches.length; i++) {
					if(i==index) switches[i].addClass('active');
						else switches[i].removeClass('active');
				}
			}
		});
		$('slideshow-navigation').getFirst().getFirst().getFirst().addClass('active');
		$('play-pause').addEvent('click',function(){
			if(this.hasClass('pause')) {
				this.removeClass('pause');
				Site.slideshow.startAutoSlide();
				Cookie.write('slideshow', '', {duration: 999999});
			} else {
				this.addClass('pause');
				Site.slideshow.stopAutoSlide();
				Cookie.write('slideshow', 'pause', {duration: 999999});
			}
		});
		$$('div#slideshow-navigation a').each(function(a){
			a.addEvent('click', function(){ Site.slideshow.goTo(this.rel); });
		});
		if(Cookie.read('slideshow') == 'pause') $('play-pause').fireEvent('click');
		
	}, //activateSlideshow
	
	delayNewsFunction: null,
	newsInterval: null,
	newsDuration: 8000,
	newsNumber: 1,

	activateNews: function() {
		$$('ul#news-switches a').each(function(a){ a.addEvent('click', function(){
			Site.newsInterval = $clear(Site.newsInterval);
			Site.switchNews(this.get('rel'));
			Site.newsInterval = Site.delayNewsFunction.periodical(Site.newsDuration);
			return false;											
		}); });
		this.delayNewsFunction = function() { Site.nextNews(); }
		this.newsInterval = this.delayNewsFunction.periodical(this.newsDuration);
		$('news-layers').getFirst().addClass('active');
		$('news-switches').getFirst().getFirst().addClass('active');
	}, //activateNews

	switchNews: function(class_number) {
		this.newsNumber = class_number;
		$$('ul#news-switches a').each(function(a){ if(a.get('rel') == class_number) { a.addClass('active'); } else { a.removeClass('active'); } });
		$$('ul#news-layers li').each(function(li){ li.removeClass('active') });
		$('news-layer-'+class_number).addClass('active');
	},

	nextNews: function() {
		this.switchNews((this.newsNumber % $$('ul#news-layers li').length) + 1);
	}, // nextNews


	delayAwardsFunction: null,
	awardsInterval: null,
	awardsDuration: 8000,
	awardsNumber: 1,
	
	activateAwards: function() {
		
		this.delayAwardsFunction = function() { Site.nextAward(); }
		this.awardsInterval = this.delayAwardsFunction.periodical(this.awardsDuration);
		$('awards-layers').getFirst().addClass('active');
		
		$('awards-next').addEvent('click', function(){
			Site.awardsInterval = $clear(Site.awardsInterval);
			Site.nextAward();
			Site.awardsInterval = Site.delayAwardsFunction.periodical(Site.awardsDuration);
			return false;											
		});
		
		$('awards-prev').addEvent('click', function(){
			Site.awardsInterval = $clear(Site.awardsInterval);
			Site.prevAward();
			Site.awardsInterval = Site.delayAwardsFunction.periodical(Site.awardsDuration);
			return false;											
		});
	}, //activateAwards

	switchAwards: function(class_number) {
		this.awardsNumber = class_number;
		$$('ul#awards-switches a').each(function(a){ if(a.get('rel') == class_number) { a.addClass('active'); } else { a.removeClass('active'); } });
		$$('ul#awards-layers li').each(function(li){ li.removeClass('active') });
		$('awards-layer-'+class_number).addClass('active');
	}, //switchAwards

	nextAward: function() {
		this.switchAwards((this.awardsNumber % $$('ul#awards-layers li').length) + 1);
	}, // nextAward

	prevAward: function() {
		var pos = this.awardsNumber - 1;
		this.switchAwards(pos <= 0 ? $$('ul#awards-layers li').length : pos);
	}, // prevAward

	setActive: function(pattern) {
		var path = document.location.pathname.substr(1);
		if(path.charAt(path.length-1) == '/') path = path.substr(0, path.length-1);
			else if(path == '') path = '/'; // main page
		path = path.replace(/\/\d+\/?$/, '').replace(/\/[0-9a-z-]+\.html\/?$/, '');//.replace(/(pl|en|de|ru)$/, '/');
		
		var links = $$(pattern+' a[href='+path+']');
		while(links.length == 0 && path != '') {
			path = path.split('/');
			path.pop();
			path = path.join('/');
			links = $$(pattern+' a[href='+path+']');
		}
		if(links.length == 1) {
			var a = links[0];
			a.addClass('active');
		}
		
	}, //setActiveMenu

	showMail: function(user, domain, country, attr) {
		var start = "<a href=\"mailto:" + user + "@" + domain + "." + country + "\"" + attr + ">"+user+'@'+domain+'.'+country;
		var end = "</a>";
		document.write(start + end);
		return false;
	} // showMail

});

var Site = new SiteClass();
window.addEvent('domready', function(){ Site.domReady(); });

