$(document).ready( function() {

	var zerogrey = {
		init: function () {
			var lang = $("meta[http-equiv='Content-Language']").attr('content');
			
			
			$(".whitebox.main").html($(".whitebox.slide-1[lang=" + lang + "]").html());
			
			// Bind handler to expander
			$("a.expander").bind('click',this.expander_click);

			// Set the caption for the first image
			//$(".caption").html($('#slider > img:first').attr('title'));
			$(".caption").html( "<h1>" + $(".slidecontent.slide-1[lang=" + lang + "]:first").find('h1').html() + "</h1>");
			$(".tagline").html( $(".slidecontent.slide-1[lang=" + lang + "]:first").find('p').html());
			
			// Initialize slider
			try {
				$('#slider').data('index', 1).anythingSlider({
					buildArrows: false,
					buildNavigation: false,
					autoPlay: true,
					delay: 20000,
					pauseOnHover: false,
					animationTime: 1000,
					autoPlay: true,
					onSlideComplete: function(slider) {
						$(".controls a").removeClass("active");
						$(".controls a:eq("+(slider.currentPage - 1)+")").addClass("active");
						$(".caption").html(slider.$currentPage.attr('title'));						
						$(".whitebox.main").html($(".slidecontent.slide-" + slider.currentPage + "[lang=" + lang + "]").html());
						
						$(".caption").html( "<h1>" + $(".slidecontent.slide-" + slider.currentPage + "[lang=" + lang + "]").find('h1').html() + "</h1>");
						$(".tagline").html( $(".slidecontent.slide-" + slider.currentPage + "[lang=" + lang + "]").find('p').html());
					}
				});
				$(".controls a").click( function(e) {
					e.preventDefault();
					var control = $(this);
					$(".controls a").removeClass("active");
					control.addClass("active");
					$('#slider').anythingSlider(control.attr('href').replace("#slide-",""));
					return false;
				});
			} catch (e) {
				if (console) {
					console.log("AnythingSlider not included", e);
				}
			}

			// Initialize logo scroller
			$("#logoscroller").logoScroller();

			// Set up animated hide/reveal functionality for Clients page
			$(".client_imagebox > .window").hover( function() {
				$(this).find("img").stop().animate({
					left:'256px'
				}, {
					queue:false,
					duration:350
				});
			}, function() {
				$(this).find("img").stop().animate({
					left:'0px'
				}, {
					queue:false,
					duration:350
				});
			});
			
			// Transform elements intended to behave like a button to jQuery UI buttons
			//$("#newsletter button").button();

			// Set up newsletter form submit handling
			$("#newsletter").bind('submit', this.newsletter_form_submit);
		},
		expander_click: function(e) {
			e.preventDefault();
			var parent = $(this).parent();
			if ($("#main_imagebox").length) {
				var imagebox = $("#main_imagebox");
			} else {
				var imagebox = $(".client_imagebox");
			}
			if (parent.hasClass("short_version")) {
				parent.hide();
				imagebox.hide("drop", {
					direction: "up"
				}, "fast", function() {
					parent.next(".long_version").fadeIn( function() {
						$.scrollTo($(this), 1000);
					});
				});
			}
			if (parent.hasClass("long_version")) {
				parent.hide();
				imagebox.show("drop", {
					direction: "up"
				}, "fast", function() {
					parent.prev(".short_version").fadeIn( function() {
						$.scrollTo($(this), 1000);
					});
				});
			}
			return false;
		},
		newsletter_form_submit: function() {
			$('#frmEmailsubError, #frmEmailsubError2').hide();

			var frmEmailSub = jQuery('#frmEmailSub').val();
			var check = 0;

			if(frmEmailSub == '' || frmEmailSub == '{/literal}{$label}{literal}!') {
				$('#frmEmailsubError').show();
				check = 1;
			} else {
				var mailformatted = 0;
				Filtro = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
				if (Filtro.test(frmEmailSub))
					mailformatted = 1;
				if(mailformatted == 0) {
					$('#frmEmailsubError2').show();
					check = 1;
				}
			}
			if(check == 0) {
				return true;
			}
			return false;
		}
	};
		
	zerogrey.init();
});
