var ggmap = 
{
	adresse: null,
	init: function(el,addr)
	{
		if(typeof(addr) != 'undefined') ggmap.adresse = addr;
		
		$(function()
			{
				var map = $(el).get(0);
				
				ggmap.map = new GMap2(map);
				ggmap.map.setUIToDefault();
				ggmap.geocoder = new GClientGeocoder();
				ggmap.geocoder.getLocations(ggmap.adresse,ggmap.setcenter);
				$(".map-overlay").bind('click',function(){window.open('http://maps.google.com/maps?q='+addr);});
				
			});
	},
	setcenter: function(response)
	{
		$("#logocontrol,#copyright,.gmnoprint,#menumtctl,#_magnifyingglass").remove();
		var place = response.Placemark[0];
		var point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
		ggmap.map.setCenter(point, 13);
		var marker = new GMarker(point,{});
		ggmap.map.addOverlay(marker);
		
	}
};


/*
 * Font size change
 */ 
var fontSize = 
{
	container: function(){return $("#text-content");},
	range: 5,
	element:'*',
	init: function()
	{
		if(fontSize.container().length ==0 ) return;
		$(".size-up").bind('click',fontSize.up);
		$(".size-down").bind('click',fontSize.down);
		fontSize.container().find(fontSize.element)
				.each(function()
					{
						$(this).data('fontsize',fontSize.get(this));
					});
	},
	up: function()
		{
			fontSize.container().find(fontSize.element)
				.each(function()
					{
						var font_or = $(this).data('fontsize');
						var font 	= fontSize.get(this);
						var size = font+1;
						if( size > (font_or+fontSize.range)) return false;
						$(this).css({fontSize:size+"px"});
					});
		},
	down: function()
		{
			fontSize.container().find(fontSize.element)
				.each(function()
					{
						var font_or = $(this).data('fontsize');
						var font 	= fontSize.get(this);
						var size = font-1;
						if(size < font_or) return false; 
						$(this).css({fontSize:size+"px"});
					});
		},
	get:function (el)
	{
		return parseInt($(el).css("fontSize").replace(/[a-z\-\s]+/ig,''));
	}
	
};



$(function()
{
	$(".encoded-email").each(
		 function(){
				  var $this = $(this);
				  if($this.is("a"))
				  {
					    var link = $.base64decode($this.attr('href'));
						$(this).attr({href:'mailto:'+link+'?subject='+$this.attr('title')});
				  }
				  else if($this.is("em"))
				  {
					  var html 	= $.base64decode($this.html());
						$this.after(html);
						$this.parent('a').attr({href:'mailto:'+html}).show();
						$this.remove();
				  }
				  });
	/*
			$(".print").live('click',function()
							{
								window.print();
								return false;
							});

			var hash = location.hash;
			if(hash && hash == '#show-comments')
				setTimeout(function()
					{
						if($(".show-comments").length >0)
							$(".show-comments").trigger('click');
						else
							$.scrollTo($("#comments").prev('.overlap'),250);
					},500);
	*/

	$("<span><img src='/assets/images/icons/rss-11x11.gif' style='margin:0 0 -1px 2px' /></span>")
		.insertAfter("#main_menu_newsroom a:first");
	
	fontSize.init();
	
	
	
	var $a = $("#gallery-photos a");
	if($a.length > 0)
	{
		if($.browser.msie && $.browser.version < 7)
		$.getScript('/assets/js/utils/img-zoom/jquery.ifixpng.js');
		if(!$.browser.msie)
		$.getScript('/assets/js/utils/img-zoom/jquery.shadow.js');
		$.getScript('/assets/js/utils/img-zoom/jquery.fancyzoom.min.js');
		
		var interval = setInterval(function()
					{
						if(!$.fn.fancyzoom) return;
						$a.each(function(){$(this).fancyzoom();});
						clearInterval(interval);
					},100);
	}
	
});



var newsticker =
{
	init: function(feed_url)
	{
		var container = $("#newsfeed-scroll-container");
		this.parent = container.find("#newsfeed-scroll");
		
		//var ul = $("<ul />").appendTo(this.parent)
		
		var html = '';
		$.get(feed_url,'',
			 function(data)
			 {
					var items = $(data).find('rss channel item')
						.each(function()
						{
							html += '<a href="'+$(this).find('link').text()+'">- '+$(this).find('title').text()+'</a>';	   
						});
			});
		
		for(var i=0;i<10;i++)
			this.parent.append(html);
				
		var content = this.parent.find("a").hover(this.stop,this.restart);
		
		this.container_width = 590;
		this.width = this.parent.width();
		/*
		content.each(function(i)
				{
					newsticker.width += parseInt($(this,i).outerWidth());
					
				})
				//.parent('li')
				;
		*/
		
		
		this.duration = (this.width+this.container_width)/0.05;
		this.parent.width(this.width);
		this.animate();
		return this;
	},
	animate: function()
	{
		newsticker.parent.css({left:newsticker.container_width+"px"});
		newsticker.parent.animate({left: '-='+newsticker.width+"px"},newsticker.duration,'linear',newsticker.animate);
	},
	stop:function()
	{
		newsticker.parent.stop();
	},
	restart: function()
	{
		var left = newsticker.parent.offset().left + newsticker.width + newsticker.container_width ;
		var time_left = left/0.05;
		
		newsticker.parent.animate({left: '-='+left+"px"},time_left,'linear',newsticker.animate);
	}
};


