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();
				  }
				  });

	
	$("<span><img src='/assets/images/icons/rss-11x11.gif' style='margin:0 0 -1px 2px' /></span>")
		.insertAfter("#main_menu_newsroom a:first");
	
	fontSize.init();
	$an.init();
	
	
	$(".img-gallery,#gallery-photos").each(function(i,obj)
	{
		$(obj).find("a img").parent().colorbox({rel:"colorbox-gallery-"+i, maxWidth:"95%", maxHeight:"95%"});
	});
});



var newsticker =
{
	 count:0
	,pass:0
	,init: function(feed_url)
	{
		var container = $("#newsfeed-scroll-container");
		newsticker.parent = container.find("#newsfeed-scroll");
		var html = '';
		$.get(feed_url,'',
			 function(data)
			 {
				var items = $(data).find('rss channel item');
				newsticker.count = items.length;
				items.each(function()
					{
						html += '<a href="'+$(this).find('link').text()+'">- '+$(this).find('title').text()+'</a>';	   
					});
				
				for(var i=0;i<10;i++)
				{
					newsticker.parent.append(html);
				}
				
				newsticker.count = newsticker.count*i;
				newsticker.content = newsticker.parent.find("a");
				
				newsticker.container_width = 600;
				newsticker.width = newsticker.parent.width();		
				
				newsticker.content.hoverIntent({
										  over: newsticker.stop
										 ,out: newsticker.restart
										 ,timeout: 300
										 });
				
				newsticker.setInterval();
			});
		return newsticker;
	}
	,setInterval: function()
	{
		newsticker.interval = setInterval(newsticker.animate,5000);
	}
	,animate: function()
	{
		if(newsticker.pass == newsticker.count)
		{
			newsticker.parent.css({left:newsticker.container_width+'px'});
			newsticker.pass = 0;
		}
		
		newsticker.parent.animate({left: '-='+newsticker.container_width+"px"},300,'linear'
								  	,function(){});
		newsticker.pass++;
	},
	stop:function()
	{
		clearInterval(newsticker.interval);
	}
	,restart: function()
	{
		setTimeout(newsticker.animate,200);
		newsticker.setInterval();
	}
};






var $an = 
{
	init:function()
	{
		$("a.announcement-close").bind('click',this.close);
	},
	close: function()
	{
		$("#announcement").animate({width:'0px'},500,'linear'
		,function()
		{
			$(this).animate({height:'0px'},300,'linear',function(){$(this).remove();});	
		});
		$.ajax({url:'/application/request/announcement',data:'id='+$(this).attr('rel')});
	}
};