var Modal, Photoalbum, InfoBrowser, FAQ, Tab;

/* !Site */
var Site = ({
	initialize: function() {
		var Modals, Photoalbums, InfoBrowsers, FAQs, Tabs;

		Modals = $('.modal');
		Modal.initialize(Modals);

		Photoalbums = $('.photoalbums li');
		Photoalbum.initialize(Photoalbums);

		InfoBrowsers = $('.infobrowser');
		InfoBrowser.initialize(InfoBrowsers);

		FAQs = $('.faq');
		FAQ.initialize(FAQs);

		Tabs = $('.tabs');
		Tab.initialize(Tabs);

		if($('#map')) {
			var mapObj = [];
			initCenter = new google.maps.LatLng(53.112664, 6.093626);
			initZoomLevel = 13;

			mapOptions = {
				zoom: initZoomLevel,
				center: initCenter,
				mapTypeId: google.maps.MapTypeId.ROADMAP,
				mapTypeControl: false,
				mapTypeControlOptions: {
					position: google.maps.ControlPosition.TOP_RIGHT,
					style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
				},
				navigationControl: true,
				navigationControlOptions: {
					position: google.maps.ControlPosition.TOP_LEFT,
					style: google.maps.NavigationControlStyle.SMALL
				}
			};

			map = new google.maps.Map($('#map')[0], mapOptions);
			map.scrollwheel = false;

			var marker = new google.maps.Marker({
				position: new google.maps.LatLng(53.112664, 6.093626),
				map: map,
				icon: '/Themes/Shared/Images/MapsPin/pin-active.png',
				title: 'Evangelische Open Thuis Gemeente'
			});
			var infowindow = new google.maps.InfoWindow({
				content: '<div style="font-size:11px; line-height:15px;height:auto;overflow-y:hidden;"><strong>Evangelische Open Thuis Gemeente</strong><br />Brouwerssingel 3<br />9201 VB Drachten</div>'
			});

			google.maps.event.addListener(marker, 'mouseover', function() {
				infowindow.open(map, marker);
			});
		}
	}
});

/* !Modals */
Modal = {
	initialize: function(Modals) {
		Modals.each(function() {
			var modalElement = $(this);

			$(this).parent().bind('click', function() {
				modalElement.modal({
					overlayClose: true
				});
			});
		});
	}
};

/* !Photoalbum */
Photoalbum = {
	initialize: function(Photoalbums) {
		Photoalbum.preloadImages = [];
		Photoalbums.each(function() {
			$(this).one('mouseover', function() {
				Photoalbum.album = $(this);
				Photoalbum.loadImages();
			});

			$(this).click(function() {
				$('#simplemodal-data div.main').mouseover(function() {
					$('#simplemodal-data div.controls').css({opacity:1});
				});

				$('#simplemodal-data div.main').mouseout(function() {
					$('#simplemodal-data div.controls').css({opacity:0});
				});

				// Play/pause
				$('#simplemodal-data div.controls div.play-pause').click(function() {
					Photoalbum.play();
					$('#simplemodal-data div.controls').css({opacity:0});
				});

				$('#simplemodal-data div.controls div.play-pause').mousedown(function() {
					$(this).css({ backgroundPosition: '0px -126px'});
				});

				$('#simplemodal-data div.controls div.play-pause').mouseup(function() {
					$(this).css({ backgroundPosition: '0px -66px'});
				});

				// Previous
				$('#simplemodal-data div.controls div.previous').click(function() {
					Photoalbum.prev();
				});

				$('#simplemodal-data div.controls div.previous').mousedown(function() {
					$(this).css({ backgroundPosition: '-64px -126px'});
				});

				$('#simplemodal-data div.controls div.previous').mouseup(function() {
					$(this).css({ backgroundPosition: '-64px -66px'});
				});

				// Next
				$('#simplemodal-data div.controls div.next').click(function() {
					Photoalbum.next();
				});

				$('#simplemodal-data div.controls div.next').mousedown(function() {
					$(this).css({ backgroundPosition: '-98px -126px'});
				});

				$('#simplemodal-data div.controls div.next').mouseup(function() {
					$(this).css({ backgroundPosition: '-98px -66px'});
				});
			});
		});

		Photoalbums.find('div.main img[source!=""]').each(function() {
			$(this).attr('src', $(this).attr('source'));
			$(this).load(function() {
				$('#simplemodal-data div.browser img[name="' + $(this).attr('src') + '"]').css({opacity:1});
			});
		});
	},

	loadImages: function() {
		Photoalbum.album.find('.browser img').each(function() {
			$(this).attr('src', $(this).attr('source'));

			i = (Photoalbum.preloadImages.length + 1);
			Photoalbum.preloadImages[i] = new Image(1,1);
			Photoalbum.preloadImages[i].src = $(this).attr('name');
			$(Photoalbum.preloadImages[i]).load(function() {
				$('#simplemodal-data div.browser img[name="' + $(this).attr('src') + '"]').css({opacity:1});
			});

			var mainImage = $(this).parent().parent().find('img.main');
			$(this).bind('mouseover', function() {
				$(this).parent().find('.active').removeClass('active');
				$(this).addClass('active');

				mainImage.attr('src', $(this).attr('name'));
			});
		});

		Photoalbum.preloadImagesArrayCount = (Photoalbum.preloadImages.length / 2);
	},

	play: function() {
		Photoalbum.next();

		if($('#simplemodal-data div.browser img.active').next().attr('class') == 'thumbnail') {
			Photoalbum.timer = setTimeout(Photoalbum.play, 2000);
		}
	},

	prev: function() {
		if(Photoalbum.timer) {
			clearTimeout(Photoalbum.timer);
		}

		prevItem = $('#simplemodal-data div.browser img.active').prev();
		if(prevItem.attr('class') == 'thumbnail') {
			$('#simplemodal-data div.main img').attr('src', prevItem.attr('name'));
			$('#simplemodal-data div.browser img.active').removeClass('active');
			prevItem.addClass('active');
		}

		Photoalbum.updateControls();
	},

	next: function() {
		if(Photoalbum.timer) {
			clearTimeout(Photoalbum.timer);
		}

		nextItem = $('#simplemodal-data div.browser img.active').next();
		if(nextItem.attr('class') == 'thumbnail') {
			$('#simplemodal-data div.main img').attr('src', nextItem.attr('name'));
			$('#simplemodal-data div.browser img.active').removeClass('active');
			nextItem.addClass('active');
		}
	
		Photoalbum.updateControls();
	},

	updateControls: function() {
		if($('#simplemodal-data div.browser img.active').prev().attr('class') === undefined) {
			$('#simplemodal-data div.controls div.previous').css({ backgroundPosition: '-64px -95px'});
		} else {
			$('#simplemodal-data div.controls div.previous').css({ backgroundPosition: '-64px -66px'});
		}
	
		if($('#simplemodal-data div.browser img.active').next().attr('class') === undefined) {
			$('#simplemodal-data div.controls div.next').css({ backgroundPosition: '-98px -95px'});
		} else {
			$('#simplemodal-data div.controls div.next').css({ backgroundPosition: '-98px -66px'});
		}
	}
};

/* !InfoBrowser */
InfoBrowser = {
	initialize: function(InfoBrowsers) {
		var secondsRemaining;

		$(InfoBrowsers).each(function() {
			$(this).find('ul>li:first>ul>li:first').addClass('active');
		});

		$(InfoBrowsers).find('ul>li>ul>li').each(function() {
			$(this).click(InfoBrowser.itemClick);
			$(this).mouseover(InfoBrowser.itemClick);
		});
	},

	itemClick: function() {
		$(this).parent().parent().parent().find('.active').removeClass('active');
		$(this).addClass('active');
	}
};

/* !FAQ */
FAQ = {
	initialize: function(FAQs) {
		var TOC = $(document.createElement('ul')).addClass('TOC');
		var TOCitems = 0;

		$(FAQs).find('h3').each(function() {
			var headingText, target, toggleOpen, TOCitem;
			var contentType = 'antwoorden ';

			var header = $(this);
			headingText = header.text();
			target = headingText.toLowerCase().replace(/ /g, '-');

			if(header.attr('title')) {
				contentType = header.attr('title') + ' ';
			}

			toggleOpen = $('<a class="toggleOpen">Alle ' + contentType + 'weergeven</a>');
			TOCitem = $(document.createElement('li')).html('<a href="' + window.location.pathname + '#' + target + '">' + headingText + '</a>');
			TOCitems++;

			header.prepend($(document.createElement('a')).attr('name', target));
			header.after(toggleOpen);
			TOC.append(TOCitem);

			TOCitem.click(function() {
				if(header.next().next().hasClass('open')) {
					toggleOpen.text('Alle ' + contentType + 'verbergen');
				} else {
					toggleOpen.text('Alle ' + contentType + 'weergeven');
					header.next().next().find('.active').removeClass('active');
				}
			});

			toggleOpen.click(function() {
				header.next().next().toggleClass('open');

				if(header.next().next().hasClass('open')) {
					toggleOpen.text('Alle ' + contentType + 'verbergen');
				} else {
					toggleOpen.text('Alle ' + contentType + 'weergeven');
					header.next().next().find('.active').removeClass('active');
				}
			});

			$(this).click(function() {
				header.next().next().toggleClass('open');

				if(header.next().next().hasClass('open')) {
					toggleOpen.text('Alle ' + contentType + 'verbergen');
				} else {
					toggleOpen.text('Alle ' + contentType + 'weergeven');
					header.next().next().find('.active').removeClass('active');
				}
			});
		});

		if(TOCitems > 1) {
			TOC.append($('<div class="clear"></div>'));
			FAQs.prepend(TOC);
		}

		$(FAQs).find('ul>li').each(function() {
			$(this).click(function() {
				$(this).toggleClass('active', '');
			});
		});
	}
};

/* !Tab */
Tab = {
	initialize: function(Tabs) {
		Tabs.each(function() {
			$(this).find('ul').tabs('enable', { fxFade: true, fxSpeed: 150, event: 'click' });
		});
	}
};

/* !Initialize when document is ready */
$(document).ready(function() {
	$.ajaxSetup({ scriptCharset: "utf-8", contentType: "application/x-www-form-urlencoded; charset=UTF-8" });
	Site.initialize();
});
