var site = {

	start: function () {
		site.installSmoothScroll();
		if ($('sidebar')) site.installSideBarSliders();
		if ($('map_canvas')) map.render();
		form = $('form_archives');
		if (form) {
			$('year').addEvent ('change', function (e) {archive.updateControls(form.m.value, form.y.value);});
			$('month').addEvent ('change', function (e) {archive.updateControls(form.m.value, form.y.value);});
			$('page').addEvent ('change', function (e) {archive.updatePage(form.m.value, form.y.value, form.p.value);});
			$('nav_back').addEvent('click', function (e) {archive.previousPage(); new Event(e).stop();});
			$('nav_next').addEvent('click', function (e) {archive.nextPage(); new Event(e).stop();});
			archive.updateControls(new Date().getMonth() + 1, new Date().getFullYear());
			toc.initialize();
			$('toc_show').addEvent('click', function (e) {toc.show()});
		}
	},

	installSmoothScroll: function () {
		new SmoothScroll();
	},

	installSideBarSliders: function () {
		$$('.toggler').each (function (el, i) {
			var slider = new Fx.Slide($$('.stretch')[i], {
				onComplete: function () {
					el.toggleClass('expand');
					el.toggleClass('collapse');
				}}
			);
	
			slider.hide();
			el.addEvent ('click', function () {slider.toggle()})
	
		})
	}

}

var archive = {

	updatePage: function (month, year, page) {
		querystring = Object.toQueryString({m: month, y: year, p: page});
		var ajaxPage = new Ajax('pageExists.php', {

			data: querystring,

			onRequest: function () {
				$('archive_preview').empty();
				if (!$('archive_preview').hasClass('placeholder')) $('archive_preview').addClass('placeholder');
				$('archive_preview').adopt(new Element('img', {src: 'images/ajax-loader.gif'}).addClass('ajax'));
			},
			
			onComplete: function (response) {
				if (response) {
					var page_image = new Element('img', {src: 'viewArchivePage.php?'+querystring+'&mode=preview'});
					page_image.addClass('page');
					page_image.setStyle('opacity', 0);
					page_image.addEvent ('load', function () {
						$('archive_preview').empty();
						if ($('archive_preview').hasClass('placeholder')) $('archive_preview').removeClass('placeholder');
						var page_url = 'viewArchivePage.php?'+querystring;
						var page_anchor = new Element('a', {href: page_url, target: '_blank', title: 'Click to view full page'});
						$('archive_preview').adopt(page_anchor);
						page_image.injectInside(page_anchor);
						new Fx.Style(page_image, 'opacity',{duration:250}).start(0, 1);
					});
				} else {
					$('archive_preview').empty();
					if (!$('archive_preview').hasClass('placeholder')) $('archive_preview').addClass('placeholder');
					$('archive_preview').adopt(new Element('p')).setHTML('Page not found');
				}
			}
		});
		ajaxPage.request();
	},

	updateControls: function (month, year) {
		querystring = Object.toQueryString({m: month, y: year});
		var ajaxControls = new Ajax('getArchivePages.php', {

			data: querystring,

			onComplete: function (response) {
				$('page').empty();
				if (response == 0) $('page').adopt(new Element('option', {value: i, selected: 'selected'}).setHTML('---'));
				for (i = 1; i <= response; i++) {
					$('page').adopt(new Element('option', {value: i}).setHTML(i));
				}
				archive.updatePage(month, year, 1);
			}
		});
		ajaxControls.request();
	},

	nextPage: function () {
		if($('page').value != $('page').getChildren().length) {
			$('page').value++;
			$('page').fireEvent('change');
		}
	},

	previousPage: function () {
		if ($('page').value != 1) {
			$('page').value--;
			$('page').fireEvent('change');
		}
	}

}

var map = {
	render: function () {
		if (GBrowserIsCompatible()) {
			var map = new GMap2($('map_canvas'));
			map.addControl(new GSmallMapControl());
			map.addControl(new GMapTypeControl());
			var point = new GLatLng(15.165548, 120.572333);
			map.setCenter(point, 16);
			map.setMapType(G_SATELLITE_MAP);
			var marker = new GMarker(point);
			map.addOverlay(marker);
		} else {
			$('contact_map').remove();
			$('map_canvas').remove();
		}
	}

}

var toc = {
	initialize: function() {
		month = new Date().getMonth() + 1;
		year = new Date().getFullYear();
		tocFile = (month < 10 ? '0' + month : month) + '' + year + '.txt';
		querystring = Object.toQueryString({toc: 'toc/'+tocFile});
		var ajaxTOC = new Ajax ('getTOC.php', {
			data: querystring,
			onComplete: function (response) {

				var toc_window = new Element('div', {id: 'tocWindow'});
				pos = $('archives_toc').getCoordinates();
				toc_window.setStyles ({top: pos['bottom'], opacity: 0});
				toc_window.addEvent ('blur', function (e) {toc.hide()});
		
				var toc_close = new Element('a', {id: 'tocClose'});
				toc_close.addEvent('click', function (e) {toc.hide()});

				var toc_titlebar = new Element('h3');
				toc_titlebar.setHTML('Table of Contents');

				if (response) {
					result = eval(response);
					toc_window.setStyles({
						'height': '400px'
					});
					var toc_pages = new Element('ul', {id: 'list_toc'});
					result.each(function(advertisers, pageindex){
						var page = new Element('li');
						page.adopt(new Element('h4').setHTML('Page '+(pageindex+1)));
						var list_advertisers = new Element('ul');
						advertisers.each(function(name, index){
							list_advertisers.adopt(new Element('li').setHTML(name));
						});
						page.adopt(list_advertisers);
						toc_pages.adopt(page);
					});
				} else {
					toc_window.setStyles({'height': '80px'});
					var toc_pages = new Element('p').setHTML('Not Available');
					toc_pages.setStyles({
						'text-align': 'center',
						'padding-top': '20px'
					});
				}
				toc_window.adopt(toc_close);
				toc_window.adopt(toc_titlebar);
				toc_window.adopt(toc_pages);
				$('wrapper').adopt(toc_window);
			}
		});
		ajaxTOC.request();
	},

	show: function() {
		new Fx.Style($('tocWindow'), 'opacity').start(1);
	},
	
	hide: function() {
		new Fx.Style($('tocWindow'), 'opacity').start(0);
	}
}

window.addEvent('domready', site.start);