/**
 * Styling of page and other minor tweaks.
 * 
 * Placed here to keep templates as clean as possible.
 **/
var Main = new Class({
	mainMenu: null,
	
	initialize: function() {
		// IE-warning
		this.warnIfIe7OrLess();
		
		// add extra dom elements for styling
		this.styleTopbar();
		
		// add main navigation (drop down)
		this.addDropDownMainMenu();
		
	},
	
	warnIfIe7OrLess: function() {
		if (Browser.ie && Browser.version < 8) {
			$('topNavigation').grab(new Element('div', {
				'style': 'background-color: #F7DAE1; color: #8A0B2B; padding: 10px;',
				'html': '<strong>Bemærk!</strong> Du kører en forældet udgave af Internet Explorer. Opgradér til minimum Internet Explorer 8 for at denne side fungerer korrekt. <a href="http://microsoft.com/internetexplorer" style="color: #8A0B2B">Læs mere her...</a>'
			}), 'before');
		}
	},
	
	styleTopbar: function() {
		$('topNavigation').grab(new Element('div', {'class': 'background'}));
	},
	
	addDropDownMainMenu: function() {
		// find out how many items in the first level navigation and make the widths
		// of the li's accordingly
		var width = 100 / $$('#nav > li').length;
		$$('#nav > li').each(function(elem) {
			elem.setStyle('width', width+"%");
		});
		
		// drop down
		var menu = this.mainMenu = new MenuMatic();
	}
	
	
});

/**
 * Handle newsletter signups/unsubscriptions using AJAX.
 **/
var NewsletterSignup = new Class({
	outer: null,
	
	initialize: function() {
		this.outer = $('newsletterSignup');
		if (!this.outer) return;
		
		this.styleRadio();
	},
	
	styleRadio: function() {
		var li = this.outer.getElement('label[for="newsletter_action"]').getParent();
		li.addClass('actions');
		
		Array.each(li.getElements('ul.radio_list li'), function(li) {
			var radio = new Element('div', {'class': 'radio'});
			li.grab(radio);
			var input = li.getElement('input');
			
			li.addEvent('click', function(ev) {
				input.set('checked', 1);
				li.addClass('checked');
				
				this.outer.getElement('form').submit();
			}.bind(this));
		}.bind(this));
	}
});

/**
 * News rotator
 */
var NewsRotator = new Class({
	itemCount: 0,
	curItem: 0,
	items: null,
	linkEl: null,
	
	initialize: function() {
		this.items = $$('#newsSummaryContainer p.newsItem');
		this.linkEl = $$('#newsSlot p.readMoreLink a')[0];
		this.styleSummary();
		this.startRotate();
	},
	
	styleSummary: function() {
		var height = $('newsSummaryContainer').getHeight();
		var width = $('newsSummaryContainer').getWidth();
		this.items.each(function(elem,i) {
			new ShortenLongText({container: elem, height: height, width: width});
			if (i>0) elem.setStyle('display', 'none').fade('hide');
		});
		this.itemCount = this.items.length;
	},
	
	startRotate: function() {
		this.rotate.periodical(10000, this);
		this.linkEl.set('href', this.items[0].get('data-link'));
	},
	
	rotate: function() {
		var nextItem = this.curItem + 1;
		if (nextItem >= this.itemCount) nextItem = 0;
		
		var nextEl = this.items[nextItem];
		var curEl = this.items[this.curItem];
		
		curEl.fade('out');
		this.linkEl.getParent().fade('out');
		
		(function() {
			curEl.setStyle('display', 'none');
			this.linkEl.set('href', nextEl.get('data-link'));
			this.linkEl.getParent().fade('in');
			nextEl.fade('in');
			nextEl.setStyle('display', 'block');
		}.delay(500, this));
		
		this.curItem = nextItem;
	}
});

/**
 * Gallery rotator
 */
var GalleryRotator = new Class({
	itemCount: 0,
	curItem: 0,
	items: null,
	
	initialize: function() {
		this.items = $$('#gallerySlot div.image');
		this.itemCount = this.items.length;
		this.startRotate();
	},
	
	startRotate: function() {
		this.rotate.periodical(10000, this);
	},
	
	rotate: function() {
		var nextItem = this.curItem + 1;
		if (nextItem >= this.itemCount) nextItem = 0;
		
		var nextEl = this.items[nextItem];
		var curEl = this.items[this.curItem];
		
		curEl.fade('out');
		
		(function() {
			curEl.setStyle('display', 'none');
			nextEl.fade('in');
			nextEl.setStyle('display', 'block');
		}.delay(500, this));
		
		this.curItem = nextItem;
	}
});

/**
 * Banner rotator
 */
var BannerRotator = new Class({
	/**
	 * Finds all banners and starts rotation on the ones which has more than one
	 * image.
	 */
	initialize: function() {
		var sameBanners = {};
		
		$$('#banners div.banner').each(function(elem) {
			var bannerClass = elem.get('class').replace(/banner (banner.+)/g,'$1');
			if (!sameBanners[bannerClass]) sameBanners[bannerClass] = 0;
			sameBanners[bannerClass]++;
		});
		
		Object.each(sameBanners, function(i,banner) {
			if (i>1) {
				this.startRotate(banner, i);
			}
		}.bind(this));
	},
	
	startRotate: function(banner, count) {
		var items = $$('#banners div.banner.'+banner);
		var curIdx = 0;
		
		// banner switching
		(function() {
			var curItem = items[curIdx++%items.length];
			curItem.fade('hide');
			curItem.setStyle('z-index', curIdx);
			curItem.fade('in');
		}.periodical(10000, this));
	}
});



var GlobalSearch = new Class({
	initialize: function() {
		$('globalSearch').getParent().addEvent('submit', function(ev) {ev.stop();});
		
		$('globalSearch').getElement('input').addEvent('keyup', function(ev) {
			if (ev.key == 'enter') {
				var val = $('globalSearch').getElement('input').get('value').trim();
				if (val.length >= 3) {
					$('globalSearch').getElement('form').submit();
				} else {
					window.alert("Du skal skrive minimum 3 tegn for at starte en søgning.");
				}
			}
		});
		//GLOBALSEARCH_URL
	}
});

var main;
var newsletterSignup;
var newsRotator;
var galleryRotator;
var galleryTitleShorten;
var bannerRotator;
var leftCalendar;
var globalSearch;

window.addEvent('domready', function() {
	main = new Main();
	newsletterSignup = new NewsletterSignup();
	newsRotator = new NewsRotator();
	galleryRotator = new GalleryRotator();
	bannerRotator = new BannerRotator();
	//leftCalendar = new LeftCalendar();
	globalSearch = new GlobalSearch();
});
