$().ready(function()
{	
	function share()
	{
		window.open($(this).attr('href'), 'sharer', 'toolbar=0,status=0,width=626,height=436');
		return false;
	};
	
	$('#facebook').live("click", function(){
		share;
	});
	
	$('#twitter').live("click", function(){
		share;
	});

    if($('.successMsg').length > 0)
    {
		$('.successMsg p').slideDown();
    }
	
	if($('ul.errors').length > 0)
	{
		$('ul.errors').next().addClass('error');
		$('ul.errors').hide();
	}
	
	$("a[rel]").overlay({
		onBeforeLoad: function() {

			// grab wrapper element inside content
			var wrap = this.getOverlay().find(".contentWrap");

			// load the page specified in the trigger
			wrap.load(this.getTrigger().attr("href"));
		}

	});
	
	/*if($('#overlay-more').length > 0)
	{
		$('#overlay-more .close').click(function(){
			$('#overlay-more .contentMore .publication').remove();
		})
	}*/
	
	if($('.containerLink').length > 0)
	{
		$('body').bind("click",function(){
			$('.containerLink div div.show').toggleClass('hide show');
		})
		
		$('.containerLink div a.file').live("click", function(e){
			e.preventDefault();
			e.stopPropagation();
			$(this).next('div').toggleClass('hide show');
		})
	}
	
	if($('#overlay-more').length > 0)
    {
		$("div.more a[rel]").overlay({
		    mask:
		    {
		    	color: '#000',
		    	loadSpeed: 200,
		    	opacity: 0.5
		    },	    
		    onBeforeLoad: function()
		    {
				var wrap = this.getOverlay().find('.contentMore');
				wrap.load(this.getTrigger().attr('href'));
		    },
		    closeOnClick: false
		});
    }
	
	if($(".per-page").length > 0)
	{
		$('.per-page').change(function() {
			
			$link = $(this).attr("value");
			
			window.location=($link);
		});
	}
	
	if($('#map').length > 0)
	{
		var name = 'Brussels Studies';
		var color = '#3A4A58';
		var street = $('p.address').text();
		var city = $('p.city').text();
		var tel = '02/211.79.70';
		var latlng = new google.maps.LatLng(50.8540333,4.3619845);
	
		var myOptions = {
			zoom: 14,
			center: latlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
	   
		var map = new google.maps.Map(document.getElementById("googleMap"), myOptions);
		var contentString = '<div style="width:250px; font-family:Verdana, Arial, Helvetica, sans-serif; border:1px solid #dddddd; background:#f5f5f5; padding:0 5px;"><p style="font-size:14px; font-weight:bold; color:' + color + ';">' + name + '</p><p>' + street + '</p><p>' + city +'</p><p>' + tel +'</p><\/div>';
	
		var infowindow = new google.maps.InfoWindow({
			content: contentString
		});
	 
		var marker = new google.maps.Marker({
			position: latlng,
			map: map,
			title: "Brussels Studies"
		});
	
		google.maps.event.addListener(marker, 'click', function() {
			infowindow.open(map,marker);
		});
	}
	
	if($('ul.footer').length > 0)
	{
		var lengthLi = $('ul.footer li').length;
		
		$('ul.footer li').each(function(){
			$(this).replaceWith("<p>"+$(this).html()+"</p>");
		})
		
		var content = $('ul.footer').html();
		
		$('ul.footer').replaceWith("<div class='footer'>"+content+"</div>");
		
		$('div.footer p:eq(0), div.footer p:eq(1)').wrapAll("<div class='wrap1'></div>")
		$('div.footer p:eq(2), div.footer p:eq(3)').wrapAll("<div class='wrap2'></div>")
		$('div.footer p:eq(4), div.footer p:eq(5)').wrapAll("<div class='wrap3'></div>")
		$('div.footer p:eq(6), div.footer p:eq(7)').wrapAll("<div class='wrap4'></div>")
	}
	
	/* COUNTER */
    if($('.containerLink').length > 0)
    {
		// ajax request
		/*$('.containerLink div div p a').live('click', function(e){
			var id = $(this).attr('id');
			
			var file = id.substring(0, id.indexOf('_', 0));
			var idPub = id.substring(id.indexOf('_')+1, id.lastIndexOf('_'));
			var lang = id.substring(id.lastIndexOf('_')+1, id.length);
	
		    $.ajax({
				url: '/publications/index/count/id/'+idPub+'/file/'+file+'/format/json/lang/'+lang,
				type: 'post',
				error: function(msg){
					
		    	},
		    	success: function(data){
		    	    
		    	}
		    });
		});*/
    }
	
	if($("#search").length > 0)
	{
		$('select.list').parent('p').hide();
		
		$("#search h4").css("cursor", "pointer");
		
		$("#search #searchForm").hide();
		
		if($('span#count').length > 0)
		{
			//$("#search #searchForm").show();
			$('select.list option:selected').not(':empty').parent('select').parent('p').show().addClass('current');
		}
		
		$("#search h4").click(function(){
			$(this).next("#searchForm").slideToggle();
		});		

		$('select.cat').live('change', function(e){
			var id = $(this).attr('id');
		    var num = id.substr(id.length-1, 1);
			var cat = $(this).attr('value');
			
			$('#word_'+num+' .current select option:selected').removeAttr('selected');
			
			if ((cat == 'content') || (cat == ''))
			{
				$('#word_'+num+' .current').animate({
					width: 'toggle'
				}, 600)
				$('#word_'+num+' .current').removeClass('current');
			}
			else
			{
				$('#word_'+num+' .current').hide();
			
				if ($('#word_'+num+' .current').length == 0) {
					$('#word_'+num+' .current').removeClass('current');
					$('#list_' + cat + '_' + num).parent('p').animate({
						width: 'toggle'
					}, 750).addClass('current');
				}
				else
				{
					$('#word_'+num+' .current').removeClass('current');
					$('#list_' + cat + '_' + num).parent('p').fadeIn(1000).addClass('current');
				}
			}
		});
	}
});
