var errormsg="";

 
 

function validateNewsletterForm(f) {
	var e = f.email.value;
	var e2 = f.email2.value;
	
	if (!e){ 
		alert('Du måste ange en e-mailadress');
	}else if(!e.match(/^.+@.+\.[a-z]{2,10}$/gi)){
		alert('E-postadressen är inte korrekt ifylld');
	}else if (e != e2){ 
		alert('E-postadresserna måste vara samma');
	}else{
		void(f.submit());
	}
}



if(errormsg){
	alert(errormsg);
}


function set_cookie(name,value) {
	document.cookie = name + '=' + escape(value)+'; expires=Fri, 31-Jan-2010 00:00:01 GMT';
}

function get_cookie(name) {
	var index = document.cookie.indexOf(name + "=");
	if (index == -1) return null;
	index = document.cookie.indexOf("=", index) + 1;
	var endstr = document.cookie.indexOf(";", index);
	if (endstr == -1) endstr = document.cookie.length;
	return unescape(document.cookie.substring(index, endstr));
}

function tip_a_friend() {
	window.open('http://www.wwf.se/tip_a_friend.php','wwf_tip','width=300,height=300,toolbars=0,scrollbars=0,resizable=0');
}

function showlayer(i) {
	document.getElementById(i).style.display = 'block';
}

function hidelayer(i) {
	document.getElementById(i).style.display = 'none';
}

function showflash(id,width,height,bgcolor) {
	if (!bgcolor) bgcolor = 'white';
	var url = 'showflash.php?id='+id+'&width='+width+'&height='+height+'&bgcolor='+bgcolor;
	window.open(url,'wwf_flash_'+id,'width='+(width)+',height='+(height)+',toolbars=0,scrollbars=0,status=0,resizable=0');
}

function showSpel(){
	var spelWin = window.open('http://www.wwf.se/avtryck.html','spelwin','toolbar=no,directories=no,status=no,menubar=no,scrollbars=auto,left=0,top=0,resizable=yes,width=800,height=600');
	if (spelWin) {
		spelWin.focus();
	}
}


//Fontstorlekar
window.addEvent("domready", function() {
  new Fx.Font($$('div#left, div#main, div#footer'), $('big-text'), $('medium-text'), $('normal-text'));
  applesearch.init();
});

// Expandable
window.addEvent("domready", function() {
	$$('.extendable').each(function(extendable) {
		extendable.triggerElement = extendable.getElementsBySelector('.ext-top')[0];
		
		if (extendable.triggerElement != null) {
			extendable.triggerElement.style.cursor = "pointer";
			extendable.triggerElement.fxAnimator = new Fx.Slide(extendable.getElementsBySelector('.ext-content', {duration: 500})[0]);
			extendable.triggerElement.addEvent('click', function() {
			  if (window.webbrowser.ie6) {
			    // IE6 doesn't like Fx.Slider.
			    this.fxAnimator.show();
			  } else {
			    this.fxAnimator.toggle();
		    }
			});
			if (extendable.getElementsBySelector('.error').length == 0) {
			  extendable.triggerElement.fxAnimator.hide();
		  }
		}
	});
});

// Footer
window.addEvent("domready", fix_footer_at_bottom);
window.addEvent("resize", fix_footer_at_bottom);

function fix_footer_at_bottom() {
  var footer = $('footer');
  var footerBottom = footer.getCoordinates().bottom - (footer.marginFixedAt != null ? footer.marginFixedAt : 0);
  var windowHeight = window.getHeight();
  if (windowHeight - footerBottom > 20) {
    footer.style.marginTop = (windowHeight - footerBottom) + 'px';
    footer.marginFixedAt = (windowHeight - footerBottom);
  } else {
    footer.style.marginTop = '20px';
    footer.marginFixedAt = 20;
  }
}

// Clickable top in IE6
if (window.ie6) {
  window.addEvent("domready", function() {
    $E('h1').style.cursor = 'pointer';
    $$('h1').addEvent("click", function() {
      $E('h1 a').click();
    });
    
  });
}

window.addEvent("domready", function() {
  if (!window.ie) {
    $$('#top-image img').each(function(element) {
      if (element.alt != '' && element.title == '') {
        element.title = element.alt;
      }
    });
  }
});

// Dropobjekt
window.addEvent("domready", function() {
	$$('.drop').each(function(extendable) {
		extendable.triggerElement = extendable.getElementsBySelector('.drop-rub')[0];
		
		if (extendable.triggerElement != null) {
			extendable.triggerElement.style.cursor = "pointer";
			extendable.triggerElement.fxAnimator = new Fx.Slide(extendable.getElementsBySelector('.drop-content', {duration: 500})[0]);
			extendable.triggerElement.addEvent('click', function() {
			  if (window.ie6) {
			    // IE6 doesn't like Fx.Slider.
			    this.fxAnimator.show();
			  } else {
			    this.fxAnimator.toggle();
		    }
			});
			if (extendable.getElementsBySelector('.error').length == 0) {
			  extendable.triggerElement.fxAnimator.hide();
		  }
		}
	});
});

//Press gallery

var ImageGallery = new Class({
	_image_elements: null,
	_options: null,
	_overlay: null,
	_background: null,
	_image: null,
	_initialized: false,
	_hidden: false,
	_spinner: null,
	
	initialize: function(class_name, options) {
		this._options = Object.extend({
			pre_source_url: '',
			post_source_url: '',
			overlay_class: 'gallery-overlay',
			image_bg_class: 'gallery-background',
			image_class: 'gallery-image',
			top_offset: 30,
			spinner_url: null,
			spinner_width: 0,
			spinner_height: 0,
			preload: false
		}, options || {});
		
		this._all_images = $A($$('img.' + class_name));
		this._image_elements = new Array();
		this._all_images.each(function(img) {
		  if (!img.hasClass('g_inline')) this._image_elements.push(img);
		  else img.onclick = this.inline_click.bindWithEvent(this);
		}, this);
		
		if (this._options.preload)
			this.preload();
		this.setup();
	},
	
	inline_click: function(e) {
	  var e = new Event(e);
	  var src = this._options.pre_source_url + e.target.id + this._options.post_source_url;
	  var img = $('content-left').getElementsByTagName('img')[0];
	  
	  img.src = src;
	},
	
	preload: function() {
		this._image_elements.each(function(image) {
			var url = this._options.pre_source_url + image.id + this._options.post_source_url;
			
			var preloaded_image = new Image(20, 20);
			preloaded_image.src = url;
		}, this);
		
		if (this._options.spinner_url) {
			this._spinner = new Image(this._options.spinner_width, this._options.spinner_height);
			this._spinner.url = this._options.spinner_url;
		}
	},
	
	setup: function() {
		this._image_elements.each(function(image) {
			image.onclick = this.show.bindWithEvent(this);
		}, this);
	},
	
	setup_elements: function() {
		this._overlay = new Element('div');
		this._overlay.addClass(this._options.overlay_class);
		this._overlay.onclick = this.hide.bindWithEvent(this);
		this._overlay.setStyles({
			position: 'absolute',
			width: '100%',
			display: 'none',
			top: '0',
			left: '0',
			'z-index': '9000'
		});
		
		this._overlay.injectBefore($E('body').getFirst());
		
		this._background = new Element('div');
		this._background.addClass(this._options.image_bg_class);
		this._background.onclick = this.hide.bindWithEvent(this);
		this._background.injectInside(this._overlay);
		
		this._spinner = new Element('img');
		this._spinner.src = this._options.spinner_url;
		this._spinner.setStyles({
			display: 'none'
		});
		this._spinner.injectInside(this._background);
		
		this._image = new Element('img');
		this._image.injectInside(this._background);
						
		this._initialized = true;
	},
	
	show: function(e) {
		var e = new Event(e);
		var image = e.target;
		
		
		var url = this._options.pre_source_url + image.id + this._options.post_source_url;
							
		if (!this._initialized) {
			// Create the gallery elements
			this.setup_elements();
		}
		
		this._overlay.setStyles({
			display: 'block',
			height: window.getScrollHeight() + 'px'
		});
		
		this._hidden = false;
		
		this._spinner.setStyles({
			display: 'block'
		});
		this.resize_and_center_background(this._options.spinner_width, this._options.spinner_height);
							
		this._image.src = url;
		/*this._image.setStyles({
			//visibility: 'hidden'
		  'position': 'relative',
		  'left':'-3000px'
		});*/
		this._image.onload = this.image_loaded.bindWithEvent(this);
	},
	
	resize_and_center_background: function(width, height) {
		var center_win = window.getHeight() / 2;
		var center_div = height / 2;
		var offset = center_win - center_div + window.getScrollTop();
		
		this._background.setStyles({
			'margin-top': offset + 'px',
			width: width + 'px',
			height: height + 'px',
			display: 'block'
		});
	},
	
	image_loaded: function(e) {
		

		if (!this._hidden) {						
      window.status = e + '; '+Math.random();
			this.resize_and_center_background(this._image.width, this._image.height);
			
			this._spinner.setStyles({
				display: 'none'
			});
			/*this._image.setStyles({
				//visibility: 'visible',
				'position':'static',
				'left':'0px'
			});*/
		}
	},
	
	hide: function() {
		this._overlay.setStyles({
			display: 'none',
			height: '0'
		});
		this._background.setStyles({
			display: 'none'
		});
		this._image.src = "/img/gallery/spinner.gif";
		//this._image.setStyles({'height':'0px', 'width':'0px'})
		//this.resize_and_center_background(0,0);
		this._hidden = false;
	}
});

window.addEvent('domready', function() {
	new ImageGallery('gallery', {
		pre_source_url: '/source.php?id=',
		post_source_url: '',
		spinner_url: '/img/gallery/spinner.gif',
		spinner_width: 55,
		spinner_height: 55
	});
});
