<?php

	$data_lang = $this->getData("language");
	$data_contact = $this->getData("contact");

	/* ---- Language ---- */
	$email_address_invalid =		htmlspecialchars($data_lang->getNodeVal("data.contact_and_mailing_list.email_address_invalid"));
	$highlighted_fields_required =	htmlspecialchars($data_lang->getNodeVal("data.contact_and_mailing_list.highlighted_fields_required"));
	$sending_data_to_server = 		htmlspecialchars($data_lang->getNodeVal("data.contact_and_mailing_list.sending_data_to_server"));
	$contact_page_not_setup = 		htmlspecialchars($LANG->lookup('Contact Page Not Setup'));
	$contact_form_send_failure = 		htmlspecialchars($data_lang->getNodeVal("data.contact_and_mailing_list.contact_form_send_failure"));
	$contact_form_send_success = 		htmlspecialchars($data_lang->getNodeVal("data.contact_and_mailing_list.contact_form_send_success"));
	$mailing_list_name = 			htmlspecialchars($data_lang->getNodeVal("data.contact_and_mailing_list.mailing_list_name"));
	$mailing_list_email_address = 	htmlspecialchars($data_lang->getNodeVal("data.contact_and_mailing_list.mailing_list_email_address"));
	$mailing_list_failed = 			htmlspecialchars($data_lang->getNodeVal("data.contact_and_mailing_list.mailing_list_failed"));
	$mailing_list_removed = 		htmlspecialchars($data_lang->getNodeVal("data.contact_and_mailing_list.mailing_list_removed"));
	$mailing_list_success = 		htmlspecialchars($data_lang->getNodeVal("data.contact_and_mailing_list.mailing_list_success"));

	/* ---- Language --- */
	$language = 				$CONFIG->getNodeVal("setup.language");
	$language_jquery = 			strtolower($language);
	$language_jquery = 			$language_jquery=="en" ? "" : $language_jquery;
	$mailing_list_timer_num = 		$data_contact==false ? 0 : $this->getTypedValue("contact", "settings", "mailing_list_timer_num");

?>

	Stage = new function () {
		var _listeners = new Array();
		var _timer;
		var _interval = 500;
		function _broadcast (method, data) {
			if (!_listeners[method]) return;
			for (var i=0; i<_listeners[method].length; i++) _listeners[method][i](data);
		}
		function _resize () {
			clearTimeout(_timer);
			_timer = setTimeout(_fire, _interval);
		}
		function _fire () {
			_broadcast("onResize");
		}
		this.addEventListener = function (method, listener) {
			if (!_listeners[method]) _listeners[method] = new Array();
			for (var i=0; i<_listeners[method].length; i++) if (_listeners[method][i] == listener) return;
			_listeners[method].push(listener);
		};
		this.removeEventListener = function(method, listener) {
			if (!_listeners[method]) return;
			for (var i=0; i<_listeners[method].length; i++) {
				if (_listeners[method][i] == listener) {
					_listeners.splice(i,1);
					return;
				}
			}
		};
		$(window).resize(_resize);
	};

	Navigation = new function () {
		function _init () {
			$('ul.sf-menu').supersubs({
				minWidth:    12,
				maxWidth:    27,
				extraWidth:  3
			}).superfish({
				delay: 300,
				speed: "fast"
			});
		}
		this.start = function () {
			_init();
		};
	};

	GalleryList = new function () {
		var _cols = 3;
		var _gutter = 15;
		function _resize () {
			if ($(window).width()>=600) {
				var inner_width = $('#main').width();
				var child_width = Math.floor(((inner_width+_gutter)/_cols)-_gutter);
				$('.GridItems').width(((child_width+_gutter)*_cols)+"px");
				$('.GridItems .GridItem')
					.width(child_width+"px")
					.css("float", "left");
			} else {
				$('.GridItems').width("auto");
				$('.GridItems .GridItem')
					.width("100%")
					.css("float", "none");
			}
		}
		this.setColumns = function (num) {
			_cols = num;
		};
		this.start = function () {
			_resize();
			Stage.addEventListener("onResize", _resize);
		};
	};

	ListView = new function () {
		var _id;
		var _row;
		var _panel;
		var _indexgroup;
		var _accordian;
		function _resize () {
			_indexgroup.show();
			var max = Math.max.apply(null, $('.IndexRows .Row').map(function () {
				return $(this).height();
			}).get());
			$('.IndexRows').height(max+"px");
			if ($(window).width()>=600) {
				_indexgroup.show();
				_accordian.hide();
			} else {
				_indexgroup.hide();
				_accordian.show();
			}
		}
		function _activate (id) {
			// turn both views on
			_indexgroup.show();
			_accordian.show();
			// highlight the correct item in the index
			$('.InfoList .Index').removeClass("Active");
			$('.InfoList #index-'+id).addClass("Active");
			$('.Accordian .Index').removeClass("Active");
			$('.Accordian #index-'+id).addClass("Active");
			// cross-fade current item
			if (id!=_id) {
				if (_row) _row.fadeOut();
				_row = $('#item-'+id);
				_row.fadeIn();
			}
			// accordian current item
			if (id!=_id) {
				if (_panel) _panel.slideUp();
				_panel = $('#panel-'+id+" .IndexWrapper");
				_panel.slideDown();
			}
			_id = id;
			// this essentially turns on/off the correct view
			_resize();
		}
		function _build () {
			// create the accordian view
			$(".IndexRows")
				.clone()
				.removeClass("IndexRows")
				.addClass("Accordian")
				.insertBefore($(".IndexTable"));
			$(".Accordian .Row")
				.attr("id", function () {
					var id = $(this).attr("id");
					id = id.split("item").join("panel");
					return id;
				})
			$('.Accordian .IndexWrapper')
				.hide()
				.slideUp();
			// give the anchors in the index view a function
			$('.InfoList .Index')
				.each(function () {
					var id = $(this).attr("id");
						id = id.split("-");
						id = id.pop();
					$(this)
						.data("id", id)
						.attr("href", "")
						.click(function (e) {
							e.preventDefault();
							_activate($(this).data("id"));
						});
				});
			// set up the styling of the index view
			$('.IndexRows')
				.css({
					overflow: "hidden",
					height: "0px"
				})
				.find(".Row")
					.each(function () {
						$(this)
							.css({
								position: "absolute",
								top: "0",
								left: "0"
							})
							.hide();
					});
			_indexgroup = $('.IndexTable, .IndexRows');
			_accordian = $('.Accordian');
		}
		this.start = function () {
			_build();
			_activate("0");
			$(".IndexRows img").load(_resize);
			Stage.addEventListener("onResize", _resize);
		};
	};

	VideoObject = new function () {
		var _video;
		var _aspect = 1;
		var _type = "hosted";
		function _resize () {
			var width = $('.VideoObject').width();
			var height = Math.round(width*_aspect);
			if (_type=="hosted") {
				_video.size(width, height);
			} else {
				_video.attr({
					width: width,
					height: height
				});
			}
		}
		this.setType = function (str) {
			_type = str;
		};
		this.setAspect = function (num) {
			if (_type=="hosted") {
				_aspect = num;
			} else {
				var iframe = $('.VideoObject iframe');
				if (iframe.length>0) {
					var width = iframe.attr("width");
					var height = iframe.attr("height");
					_video = iframe;
					_aspect = height/width;
				} else {
					var object = $('.VideoObject object');
					if (object.length>0) {
						var width = object.attr("width");
						var height = object.attr("height");
						_video = $('.VideoObject object, .VideoObject object embed');
						_aspect = height/width;
					}
				}
			}
		};
		this.start = function () {
			if (_type=="hosted") {
				_video = _V_("the-video");
			}
			_resize();
			Stage.addEventListener("onResize", _resize);
		};
	};

	Slideshow = new function () {
		var _aspect = 1;
		var _timer = 5;
		var _index = -1;
		var _graphic = new Array();
		var _loaded = new Array();
		var _appended = new Array();
		var _multiplier = window.devicePixelRatio ? window.devicePixelRatio : 1;
		function _resize () {
			var width = $('.Slideshow').width();
			var height = Math.round(width*_aspect);
			$('.Slideshow').css("height", height+"px");
			$('.Slideshow .Graphic')
				.css("margin", "0px")
				.each(function(i){
					var ah = $(this).height();
					var mh = Math.ceil((height-ah) / 2);
					var aw = $(this).width();
					var mw = Math.ceil((width-aw) / 2);
					$(this).css({
						top: mh+"px",
						left: mw+"px"
					});
				})
				.find(".Caption")
					.click(function () {
						$(this).fadeOut();
					})
					.find("*")
					.css("cursor", "pointer");
		}
		function _showIndex () {
			for (var i=0; i<_graphic.length; ++i) {
				var graphic = $('.Slideshow #g_'+i);
				if (i==_index) graphic.fadeIn();
				else graphic.fadeOut();
			}
			_resize();
			if (_graphic.length<=1) return;
			var active = $('.Slideshow #g_'+_index);
			setTimeout(_next, _timer*1000);
		}
		function _next () {
			++_index;
			if (_index==_graphic.length) _index = 0;
			if (_loaded[_index]==true) {
				_showIndex(_index);
			} else {
				if (_appended[_index]==false) {
					_appended[_index] = true;
					$('.Slideshow').append(_graphic[_index]);
				}
				$('.Slideshow #g_'+_index)
					.css({
						position: "absolute",
						top: 0,
						left: 0
					})
					.hide()
					.find('img')
					.load(function () {
						_loaded[_index] = true;
						_showIndex(_index);
					});
			}
		};
		this.setAspect = function (num) {
			_aspect = num;
		};
		this.setTimer = function (num) {
			_timer = num;
		};
		this.setGraphic = function (str) {
			str = str.split("{MULTIPLIER}").join(_multiplier);
			_graphic.push(str);
			_loaded.push(false);
			_appended.push(false);
		};
		this.start = function () {
			_next();
			_resize();
			Stage.addEventListener("onResize", _resize);
		};
	};

	Album = new function () {
		var _aspect = 1;
		var _galleria;
		var _playlist = [];
		var _volume = .5;
		var _loop = true;
		var _captions = false;
		var _autoplay = true;
		var _images = [];
		var _timer = 3;
		var _multiplier = window.devicePixelRatio ? window.devicePixelRatio : 1;
		var _thumbnail = [0, 0];
		var _ua = navigator.userAgent;
		function _showFullscreen () {
			var isMobile = /(iPhone|iPod|Android|BlackBerry|IEMobile)/i.test(_ua);
			var isPad = /iPad/i.test(_ua);
			var isOS4 = /OS [1-4](.*)like Mac OS X/i.test(_ua);
			var smallScreen = Math.min(screen.width,screen.height)<640;
			if (isMobile || (isPad&&isOS4) || smallScreen) return false;
			return true;
		};
		function _init () {
			// init the audio player, if nececcary
			var unsupported = /(Android|BlackBerry)/i.test(_ua);
			if (_playlist.length>0 && unsupported==false) {
				Galleria.ready(function() {
					$("#jquery_jplayer,#jp_container").show();
					$("#jp_container").appendTo("#galleria .galleria-bar")
					new jPlayerPlaylist({
						jPlayer: "#jquery_jplayer",
						cssSelectorAncestor: "#jp_container"
					}, _playlist, {
						playlistOptions: {
							autoPlay: _autoplay
						},
						wmode: "window",
						swfPath: '<?php echo SA_DIR_COREPATH; ?>/swf/Jplayer.swf',
						solution: 'html, flash',
						supplied: 'mp3',
						volume: _volume,
						loop: _loop
					});
				});
			}
			// init gallery
			var width = $('#galleria').width();
			var height = Math.round(width*_aspect) + _thumbnail[1] + 30 + (_thumbnail[1]>0 ? 10 + 10 : 0);
			$('#galleria').galleria({
				autoplay: (_autoplay?_timer*1000:false),
				carousel: (_thumbnail[1]>0),
				debug: false,
				dataSource: _images,
				easing: "galleriaOut",
				fullscreenCrop: false,
				fullscreenDoubleTap: _showFullscreen(),
				height: height,
				idleMode: false,
				imageCrop: false,
				preload: 3,
				queue : false,
				thumbCrop: 'height',
				thumbnails: (_thumbnail[1]>0),
				touchTransition: "slide",
				transition: 'fade',
				width: width,
				_captions: _captions,
				_fullscreen: _showFullscreen()
			});
			_galleria = Galleria.get(0);
		};
		function _resize () {
			if (_galleria.isFullscreen()==false) {
				var width = $('#galleria').width();
				var height = Math.round(width*_aspect) + _thumbnail[1] + 30 + (_thumbnail[1]>0 ? 10 + 10 : 0);
				_galleria.resize({width:width, height:height});
				$('#galleria').height(height+"px");
			}
		};
		this.addTrack = function (obj) {
			_playlist.push(obj);
		};
		this.addImage = function (obj) {
			var maxwidth = !window.orientation ? screen.width : Math.max(screen.width, screen.height);
			var maxheight = Math.round(maxwidth*_aspect);
			obj.thumb = obj.thumb.split("{MULTIPLIER}").join(_multiplier);
			obj.image = obj.image.split("{MULTIPLIER}").join(_multiplier);
			obj.big = obj.big.split("{MULTIPLIER}").join(_multiplier);
			obj.big = obj.big.split("{WIDTH}").join(maxwidth);
			obj.big = obj.big.split("{HEIGHT}").join(maxheight);
			_images.push(obj);
		};
		this.setVolume = function (num) {
			_volume = num;
		};
		this.setLoop = function (bool) {
			_loop = bool;
		};
		this.setAspect = function (num) {
			_aspect = num;
		};
		this.setTimer = function (num) {
			_timer = num;
		};
		this.autoPlay = function (bool) {
			_autoplay = bool;
		};
		this.setThumbnailSize = function (num, num2) {
			if (num) _thumbnail[0] = num;
			if (num2) _thumbnail[1] = num2;
		};
		this.showCaptions = function (bool) {
			_captions = bool;
		};
		this.start = function () {
			_init();
			_resize();
			Stage.addEventListener("onResize", _resize);
		};
	};

	ContactPage = new function () {
		var _map;
		var _aspect = 1;
		var _form;
		var _l10n = {
			email_address_invalid: "<?php echo $email_address_invalid; ?>",
			highlighted_fields_required: "<?php echo $highlighted_fields_required; ?>",
			sending_data_to_server: "<?php echo $sending_data_to_server; ?>",
			contact_page_not_setup: "<?php echo $contact_page_not_setup; ?>",
			contact_form_send_failure: "<?php echo $contact_form_send_failure; ?>",
			contact_form_send_success: "<?php echo $contact_form_send_success; ?>"
		};
		function _fulfilled () {
			var result = true;
			_form.find(".Required").each(function () {
				var obj = $(this);
				var input = obj.find("input");
					if (obj.hasClass("Dropdown")) input = obj.find("select");
					else if (obj.hasClass("Area")) input = obj.find("textarea");
				var name = input.attr("name");
				var val = obj.hasClass("Checkbox")||obj.hasClass("Radio") ? obj.find("input:checked").val() : input.val();
				var placeholder = input.attr('placeholder');
				obj.removeClass("Focused");
				obj.removeClass("Invalid");
				obj.addClass("Default");
				if (val==""||val==null||val==placeholder) {
					obj.removeClass("Default");
					obj.addClass("Invalid");
					if (result==true) result = false
				}
			});
			return result;
		}
		function _init () {
			_form = $("#contact-form");
			_form.show();
			_form
				.submit(function(e) {
					// we'll take it from here
					e.preventDefault();
					var url = _form.attr("action");
					var result = _form.find(".Result");
					// check the required fields
					if (!_fulfilled()) {
						result.html(_l10n["highlighted_fields_required"]);
						return;
					}
					// ok, everything checks out let's submit
					if (!Modernizr.input.placeholder) {
						_form.find('[placeholder]').each(function() {
							if ($(this).val() == $(this).attr('placeholder')) {
								$(this).val('');
								$(this).removeClass('placeholder');
							}
						});
					}
					var postobj = new Object();
						postobj["action"] = "send_email";
					_form.find(".Input input, .Dropdown select, .Date input, .Area textarea, .Checkbox input:checked, .Radio input:checked").each(function () {
						var input = $(this);
						var name = input.attr("name");
						var placeholder = input.attr('placeholder');
						var val = input.val();
						if (val!=""&&val!=null&&val!=placeholder) {
							if (!postobj[name]) postobj[name] = val;
							else postobj[name] += "\n" + val;
						}
					});
					var inputs = _form.find("input, textarea, select");
					inputs.attr("disabled", true);
					result.html(_l10n["sending_data_to_server"]);
					$.post(url, postobj, function(data) {
						if (!Modernizr.input.placeholder) {
							_form.find('[placeholder]').each(function() {
								if ($(this).val() == '') {
									$(this).val( $(this).attr('placeholder') );
									$(this).addClass('placeholder');
								}
							});
						}
						inputs.removeAttr("disabled");
						result.html(_l10n[data]);
						if (data=="contact_page_not_setup") {
							inputs.attr("disabled", true);
						} else if (data=="contact_form_send_success") {
							inputs.attr("disabled", true);
							//inputs.val('');
							//inputs.blur();
						}
					});
				})
				.find("input, textarea")
					.focus(function () {
						$(this).parent().removeClass("Default");
						$(this).parent().addClass("Focused");
					})
					.blur(function () {
						$(this).parent().removeClass("Focused");
						$(this).parent().addClass("Default");
					})
					.blur();
			if (!Modernizr.input.placeholder) {
				_form.find('[placeholder]').each(function() {
						if ($(this).val() == '') {
							$(this).val( $(this).attr('placeholder') );
							$(this).addClass('placeholder');
						}
					});
				$('[placeholder]')
					.focus(function() {
						if ($(this).val() == $(this).attr('placeholder')) {
							$(this).val('');
							$(this).removeClass('placeholder');
						}
					})
					.blur(function() {
						if ($(this).val() == '' || $(this).val() == $(this).attr('placeholder')) {
							$(this).val($(this).attr('placeholder'));
							$(this).addClass('placeholder');
						}
					});
			}
			if (!Modernizr.inputtypes.number) {
				$('input[type=number]').each(function() {
					var $input = $(this);
					$input.spinner();
				});
			}
			if (!Modernizr.inputtypes.date) {
				$('input[type=date]').each(function() {
					var $input = $(this);
					$input.datepicker({
						dateFormat: 'yy-mm-dd'
					});
				});
			}
		}
		function _resize () {
			if (_map) {
				var width = $('.MapObject').width();
				var height = Math.round(width*_aspect);
				_map.attr({
					width: width,
					height: height
				});
			}
		}
		function _getaspect () {
			var iframe = $('.MapObject iframe');
			if (iframe.length>0) {
				var width = iframe.attr("width");
				var height = iframe.attr("height");
				_map = iframe;
				_aspect = height/width;
			} else {
				var object = $('.MapObject object');
				if (object.length>0) {
					var width = object.attr("width");
					var height = object.attr("height");
					_map = $('.MapObject object, .MapObject object embed');
					_aspect = height/width;
				}
			}
		};
		this.l10n = function (obj) {
			_l10n = obj;
		}
		this.start = function () {
			$.datepicker.setDefaults($.datepicker.regional["<?php echo $language_jquery; ?>"]);
			_init();
			_getaspect();
			_resize();
			Stage.addEventListener("onResize", _resize);
		};
	};



	MailingList = new function () {
		var _form;
		var _l10n = {
			email_address_invalid: "<?php echo $email_address_invalid; ?>",
			mailing_list_name: "<?php echo $mailing_list_name; ?>",
			mailing_list_email_address: "<?php echo $mailing_list_email_address; ?>",
			highlighted_fields_required: "<?php echo $highlighted_fields_required; ?>",
			sending_data_to_server: "<?php echo $sending_data_to_server; ?>",
			mailing_list_failed: "<?php echo $mailing_list_failed; ?>",
			mailing_list_removed: "<?php echo $mailing_list_removed; ?>",
			mailing_list_success: "<?php echo $mailing_list_success; ?>"
		};
		function _set_cookie (c_name,value,exdays) {
			var exdate=new Date();
			exdate.setDate(exdate.getDate() + exdays);
			var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
			document.cookie=c_name + "=" + c_value;
		};
		function _get_cookie (c_name) {
			var c_value = document.cookie;
			var c_start = c_value.indexOf(" " + c_name + "=");
			if (c_start == -1) {
				c_start = c_value.indexOf(c_name + "=");
			}
			if (c_start == -1) {
				c_value = null;
			}
			else {
				c_start = c_value.indexOf("=", c_start) + 1;
				var c_end = c_value.indexOf(";", c_start);
				if (c_end == -1) {
					c_end = c_value.length;
				}
				c_value = unescape(c_value.substring(c_start,c_end));
			}
			return c_value;
		};
		function _open () {
			var width = Math.min(500, $(window).width());
			var height = $('#sign-up').show().width(width).height();
				$('#sign-up').hide();
			$('#sign-up').modal({
				maxWidth: 500,
				minHeight: height,
				autoResize: true,
				containerId: "simplemodal-container",
				onOpen: function (dialog) {
					dialog.overlay.fadeIn('fast', function () {
						dialog.data.hide();
						dialog.container.fadeIn('fast', function () {
							dialog.data.fadeIn('fast');
						});
					});
				},
				overlayClose:true
			});
		};
		function _init () {
<?php if ($mailing_list_timer_num>0) { ?>
			setTimeout(function(){
				var modal_cookie = _get_cookie('modal_cookie');
				if ($.trim(modal_cookie) == "") {
					_set_cookie('modal_cookie', 'set', 30);
					_open();
				}
			}, <?php echo $mailing_list_timer_num*1000 ?>);
<?php } ?>
			$('#mailing-list-button').click(function (e) {
				_open();
				return false;
			});
			$("#mailing-list-form .Inputs")
				.find("input")
				.data("fullname", _l10n.mailing_list_name)
				.data("email", _l10n.mailing_list_email_address)
				.focus(function () {
					var val = $(this).val();
					var key = $(this).attr("name");
					var def = $(this).data(key);
					if (val==def) {
						$(this).val("");
					}
					$(this).removeClass("Default");
					$(this).addClass("Focused");
				})
				.blur(function () {
					var val = $(this).val();
					var key = $(this).attr("name");
					var def = $(this).data(key);
					if (val=="") {
						$(this).val(def);
					}
					$(this).removeClass("Focused");
					$(this).addClass("Default");
				})
				.blur();
			$("#mailing-list-form").submit(function(event) {
				event.preventDefault();
				var form = $(this);
				var url = form.attr("action");
				var fullname = form.find('input[name="fullname"]');
				var email = form.find('input[name="email"]');
				var submit = form.find('input[name="submit"]');
				var result = form.find(".Result");
				var inputs = form.find("input");
				var required = [fullname, email];
				var errors = 0;
				for (var i=0; i<required.length; ++i) {
					var input = required[i];
					input.removeClass("Focused");
					input.removeClass("Invalid");
					input.addClass("Default");
					if (input.val()==input.data(input.attr("name"))) {
						input.removeClass("Focused");
						input.addClass("Invalid");
						++errors;
					}
				}
				if (errors>0) {
					result.html(_l10n.highlighted_fields_required);
					return;
				}
				inputs.attr("disabled", true);
				result.html(_l10n.sending_data_to_server);
				$.post(url, {
					action: "add_to_mailing_list",
					fullname: fullname.val(),
					email: email.val()
				}, function(data) {
					inputs.removeAttr("disabled");
					result.html(_l10n[data]);
					if (data=="highlighted_fields_required") {
						fullname.addClass("Invalid");
						email.addClass("Invalid");
					} else if (data=="email_address_invalid") {
						email.addClass("Invalid");
					} else if (data=="mailing_list_success") {
						inputs.attr("disabled", true);
						inputs.val('');
						inputs.blur();
					}
				});
			});
		}
		this.l10n = function (obj) {
			_l10n = obj;
		}
		this.start = function () {
			_init();
		};
	};