/**
 * @author Dipl.-Ing. (FH) André Fiedler <a.fiedler@oe-konzept.de>
 */

var Global = Global || {};
Global.el = Global.el || {};

Locale.use('de-DE');

window.addEvents({

	'domready': function(){

		// caching elements
		Global.el.body = $(document.body);

        // layout function calls
        Global.checkScreenResolution();

		// Menu effects
		new BadegaertenMenu();

		// fade all links a little bit
		$$(	'#header ul.headermenu li a, ' +
			'#header #user_menu a, ' +
			'#footer ul.footermenu li a, ' +
			'#footer a.go-top').hoverColor('#3378cc'); 							// weis 		-> blau
		$$(	'body.wohlfuehlen #header ul.headermenu li a, ' +
			'body.wohlfuehlen #footer ul.footermenu li a, ' +
			'body.wohlfuehlen #footer a.go-top').hoverColor('#999900'); 		// weis 		-> grün
		$$(	'body.baden       #header ul.headermenu li a, ' +
			'body.baden       #footer ul.footermenu li a, ' +
			'body.baden       #footer a.go-top').hoverColor('#DB8400'); 		// weis 		-> orange
		$$(	'body.saunieren   #header ul.headermenu li a, ' +
			'body.saunieren   #footer ul.footermenu li a, ' +
			'body.saunieren   #footer a.go-top').hoverColor('#DA3D16'); 		// weis 		-> rot
		$$(	'div.menu .tab-content .info ul li a, ' +
			'body.info        .text a, ' +
			'body.info        .backbutton, ' +
            'body.info        .list-more-offer a, ' +
            'body.info        .box-more a, ' +
			'body.info        div.pagination a, ' +
			'body.info        .fliesstext a').hoverColor('#004080'); 			// dunkelblau 	-> mittelblau
		$$('div.menu .tab-content .wohlfuehlen ul li a, ' +
			'body.wohlfuehlen .text a, ' +
            'body.wohlfuehlen .list-more-offer a, ' +
            'body.wohlfuehlen .box-more a, ' +
			'body.wohlfuehlen div.pagination a, ' +
			'body.wohlfuehlen .fliesstext a').hoverColor('#666600'); 			// dunkelgruen 	-> mittelgruen
		$$('div.menu .tab-content .baden ul li a, ' +
			'body.baden       .text a, ' +
            'body.baden       .list-more-offer a, ' +
            'body.baden       .box-more a, ' +
			'body.baden       div.pagination a, ' +
			'body.baden       .fliesstext a').hoverColor('#B35900'); 			// dunkelorange -> mittelorange
		$$('div.menu .tab-content .saunieren ul li a, ' +
			'body.saunieren   .text a, ' +
            'body.saunieren   .list-more-offer a, ' +
            'body.saunieren   .box-more a, ' +
			'body.saunieren   div.pagination a, ' +
			'body.saunieren	  .fliesstext a').hoverColor('#990000'); 			// dunkelrot 	-> mittelrot
		$$(	'.termine_news .item .link a, ' +
			'#banner a, ' +
			'#right-col .box-angebote .item .price-links ul li a, ' +
			'#right-col .box-angebote .more a, ' +
			'#right-col .box-aktuelles .item .link, ' +
			'#right-col .box-aktuelles .more a, ' +
			'#right-col .box-wetter .more a').hoverColor('#fff'); 				// *** 			-> weis


		// right column - gallery
		new BadegaertenLayoutGallery();
		$$('#right-col .box-angebote .label, #right-col .box-aktuelles .label, #right-col .box-wetter .label').rotate90ccw();

		Global.switchLayoutImage();
		Global.switchLayoutGalleryImage();

		// scroll to top
		$$('.go-top').addEvent('click', function(e){
			e.stop();
			new Fx.Scroll(window).toTop();
			this.blur();
		});

		BadegaertenMerkzettel.printToElementTop($$('#header .bookmark-counter'));
		BadegaertenMerkzettel.printToElementMenu($$('.menu .bookmark-counter'));
		BadegaertenMerkzettel.refreshCounter();

		// Bookmarks
		$$('.bookmark-link').each(function(link){
		  matches = link.get('href').match(/#([a-z]+):(\d+)/);
		  link.store('badegaerten::bookmark::type', matches[1]);
		  link.store('badegaerten::bookmark::id', matches[2]);
          /*
		  if(BadegaertenMerkzettel.isBookmarked(matches[1], matches[2])){
		      if(link.getParent('.box-more') && link.getParent('.box-shopping-cart').getElements('a').length == 1) link.getParent('.box-shopping-cart').fade('hide');
		      else link.setStyle('opacity', 0.4);
		  }
          */
		  link.addEvent('click', function(e){
                e.stop();
                /*
                this.get('tween').cancel()
                this.fade.delay(200, this, [0.4]);
                */
				var prev;
				if(this.getParent('.box-shopping-cart'))
					prev = this.getParent('.box-shopping-cart').getElement('.bookmark-value');
                BadegaertenMerkzettel.bookmark(this.retrieve('badegaerten::bookmark::type'), this.retrieve('badegaerten::bookmark::id'), prev ? prev.get('value') : 1);
                this.blur();
                BadegaertenMerkzettel.refreshCounter();
            });
		});

		// Terminanfrage
		$$('.appointment-request-link').each(function(link){
          matches = link.get('href').match(/#([a-z\-]+):(\d+)/);
          if(document.body.hasClass('wohlfuehlen'))
            link.set('href', 'wohlfuehlen_terminanfrage.php?id=' + matches[2]);
          else if(document.body.hasClass('saunieren'))
            link.set('href', 'saunieren_terminanfrage.php?id=' + matches[2]);
          else
            link.set('href', 'terminanfrage.php?id=' + matches[2]);
        });

		/*
		 * 360° Panorama PopUps
		 */
		$$('a.panoramen').addEvent('click', function(e){
		    e.stop();
		    var vrHeight = 650;
            var vrWidth = 950;

            var vrLeft = ((screen.availWidth - vrWidth)/2);
            var vrTop = ((screen.availHeight - vrHeight)/2);

            window.open('360-grad-panoramen/html/de/eibenstock_8904.html', '360_degree_vrtour', 'width=' + vrWidth + ',height=' + vrHeight + ',top=' + vrTop + ',left=' + vrLeft + ',menubar=no,resizable=no, scrollbars=no, toolbar=no');
		});

		/*
		 * Avoid flickering
		 */
		$('header').removeClass('hidden');
		$('content-outer').removeClass('hidden');
	},

	'resize': function(){
		Global.checkScreenResolution();

		Global.switchLayoutImage();
		Global.switchLayoutGalleryImage();
	},

	'load': function(){

        // caching elements
        Global.el.body = $(document.body);

        // Image rollover/enlargement
        new BadegaertenImages();

        // layout function calls
        Global.checkScreenResolution();

	}
});

// Layout an Bildschirm anpassen
//-------------------------------------------------------
Global.checkScreenResolution = function(add){
    add = add || 0;
	var s = window.getSize();
	if(Global.el.body){
		if(s.x > 1300 && !Config.forceSmallLayout) Global.el.body.addClass('large');
		else Global.el.body.removeClass('large');
	}

	// maximize left column if necessary
	(function(){
		var cl = $('left-col');
		var menu = $$('div.tab-content')[0];
		if(!cl) return;
		cl.setStyle('height', 'auto');
		var cr = $('right-col');
		var sl = cl.getSize();
        var sr = cr.getSize();
		if(menu){
		  var currentTab = menu.getElement('div.current');
		  var sm = currentTab.getSize();
		  sr.y = Math.max(sr.y, sm.y + 50);
		}
		if(sl.y + add < sr.y) cl.setStyle('height', sr.y - 6);
		else if(sl.y + add < 456) cl.setStyle('height', 456);
	}).delay(50);
}

Global.switchLayoutImage = function(){
	$$('#right-col .box-image img').each(function(img){
		if(Global.el.body && Global.el.body.hasClass('large')){
			if(img.get('width') == 216){
				img.set('src', img.get('src').replace('216px', '540px'));
				img.set('width', 540);
				img.set('height', 540);
			}
		} else {
			if(img.get('width') == 540){
				img.set('src', img.get('src').replace('540px', '216px'));
				img.set('width', 216);
				img.set('height', 216);
			}
		}
	});
}
Global.switchLayoutGalleryImage = function(){
	$$('#right-col .box-gallery img').each(function(img){
		if(Global.el.body.hasClass('large')){
			if(img.get('width') == 216){
                img.set('src', img.get('src').replace('216px', '540px'));
                img.set('width', 540);
                img.set('height', 540);
			}
		} else {
			if(img.get('width') == 540){
                img.set('src', img.get('src').replace('540px', '216px'));
                img.set('width', 216);
                img.set('height', 216);
			}
		}
	});
    $$('#right-col .box-gallery span').each(function(img){
		if(Global.el.body.hasClass('large')){
			if(img.get('data-width') == 216){
                img.set('data-src', img.get('data-src').replace('216px', '540px'));
                img.set('data-width', 540);
                img.set('data-height', 540);
			}
		} else {
			if(img.get('data-width') == 540){
                img.set('data-src', img.get('data-src').replace('540px', '216px'));
                img.set('data-width', 216);
                img.set('data-height', 216);
			}
		}
	});
}
//-------------------------------------------------------

// Bildervergrößerung laden
//-------------------------------------------------------
Slimbox.scanPage = function() {

	Global.popupImages = $$('div.images div.image');

	Global.popupImages.each(function(img){
		var captionEl = img.getChildren('div.template-low')[0];
		if(captionEl && captionEl.get('text').trim() == '')
			captionEl.destroy();
	});

	Global.popupImages.slimbox({overlayOpacity: 0.8, counterText: '{x}/{y}'}, function(img) {

		var captionEl = img.getChildren('div.caption');
		var caption = captionEl ? captionEl.get('text') : '';
		var imgEl = img.getElements('div.template-img')[0];
		var imgSrc = imgEl && imgEl.getElements('img')[0] ? imgEl.getElements('img[rel^=popup]')[0].get('rel').replace(/^popup\(/, '').replace(/\)$/, '') : '';
		return [
			imgSrc,
			caption
		];
	});
};
window.addEvent('domready', Slimbox.scanPage);
//-------------------------------------------------------
