//---------------------------------------------------------
//
//  General javascript file for Wonderwijs
//
//  Authors:	Davy De Pauw (davy@marlon.be)
//				Gerrit Bertier (gerrit@marlon.be)
//				Dieter Provoost (dieter@marlon.be)
//
//---------------------------------------------------------

/**
 * On DOM ready
 */
$(document).ready(function()
{
	// General javascript interaction
	general.init();
	
	// Init modules
	modules.init();

    // Gmaps
    $('a.gmap').click(function(e)
    {
        // prevent default behaviour
        e.preventDefault();

        // get coordinates
        var coordinates = $.evalJSON($(this).siblings('span').html());
        var module = $(this).closest('div.locations').siblings('h2').children('a').text();

        // open map
        lightbox.openMap(coordinates, module);
    });
});

/**
 * General JS functions
 */
var general = 
{
	/**
	 * Initialize
	 */
	init: function()
	{
		// Set hoverables
		this.hoverable();
		
		// Set location links
		this.initLocationLinks();
	},
	
	/**
	 * Set hoverables.
	 *
	 * @author Davy De Pauw
	 */
	hoverable: function()
	{
		var hoverIn = function()
		{
			$(this).addClass('hover');
		};
		var hoverOut = function()
		{
			$(this).removeClass('hover');
		}
		
		// Generic hoverable function
		if ($('ul.hoverable').length > 0) 
			$('.hoverable > li').hover(hoverIn, hoverOut);
		
		// Generic hoverable function
		if ($('tr.hoverable').length > 0) 
		{
			// Add hover behaviour
			$('tr.hoverable').hover(hoverIn, hoverOut);
			
			// Listen for TR click
			$('tr.hoverable').bind("click", function()
			{
				// Get URL
				var url = $(this).find('a').attr('href');
				
				// Redirect
				window.location = url;
			});
		}
	},
	
	/**
	 * Init location links and add their actions.
	 * 
	 * @author Gerrit Bertier
	 */
	initLocationLinks: function()
	{
		if ($('.locationlink').length > 0)
		{
			// Listen for location link click
			$('.locationlink').bind("click", function()
			{
				if (this.id == "select-centre-link")
				{
					$("#select-centre").toggle();
					$("#select-city").css("display", "none");
				}
				else if (this.id == "select-city-link")
				{
					$("#select-centre").css("display", "none");
					$("#select-city").toggle();
				}
			});
		} 
	}
}

/**
 * Sidebar related JS functions
 */
var sidebar = 
{
	/**
	 * Show info
	 */
	showInfo: function(infoToShow)
	{
		if (infoToShow == 'opleiding')
		{
			// Info box
			if ($('#info-opleiding').hasClass('invisible'))
			{
				$('#info-opleiding').removeClass('invisible');
			}
			if (!$('#info-module').hasClass('invisible'))
			{
				$('#info-module').addClass('invisible');
			}
			// Info link
			if (!$('#infolink-opleiding').hasClass('active'))
			{
				$('#infolink-opleiding').addClass('active');
			}
			if ($('#infolink-module').hasClass('active'))
			{
				$('#infolink-module').removeClass('active');
			}
		}
		else if (infoToShow == 'module')
		{
			// Info box
			if ($('#info-module').hasClass('invisible'))
			{
				$('#info-module').removeClass('invisible');
			}
			if (!$('#info-opleiding').hasClass('invisible'))
			{
				$('#info-opleiding').addClass('invisible');
			}
			// Info link
			if (!$('#infolink-module').hasClass('active'))
			{
				$('#infolink-module').addClass('active');
			}
			if ($('#infolink-opleiding').hasClass('active'))
			{
				$('#infolink-opleiding').removeClass('active');
			}
		}
	}
}

/**
 * Lightbox related JS functions
 */
var lightbox = 
{
	/**
	 * Open a lightbox.
	 * 
	 * @param id The id of the lightbox to open (with preceeding # character in it).
	 */
	open: function(id)
	{
		// Reposition white block in IE6
		if ($.browser.msie && $.browser.version.substr(0, 1) < 7) 
		{
			// Set position absolute
			$(id).css('position', 'absolute');
			
			// Calculate top
			var top = $(document).scrollTop() + ($(window).height() * .5);
			$(id).css('top', top + 'px');
		}
		
		// Resize the background fade so it matches current page height
		$('#lb-fade').height($('body').outerHeight());
		// Show lightbox
		$(id).css('display', 'block');
		$('#lb-fade').css('display', 'block');
	},
	
	/**
	 * Open a lightbox containing a Google Map.
	 * 
	 * @param id The id of the lightbox to open (with preceeding # character in it).
	 */
	openMap: function(coordinates, module)
	{
		// Reposition white block in IE6
		if ($.browser.msie && $.browser.version.substr(0, 1) < 7) 
		{
			// Set position absolute
			$('#lb-map').css('position', 'absolute');
			
			// Calculate top
			var top = $(document).scrollTop() + ($(window).height() * .5);
			$('#lb-map').css('top', top + 'px');
		}
		
		// Resize the background fade so it matches current page height
		$('#lb-fade').height($('body').outerHeight());
		// Show lightbox
		$('#lb-map').css('display', 'block');
		$('#lb-fade').css('display', 'block');
		
		// Add the map
		if (GBrowserIsCompatible())
		{
            // set module name
            $('#gmap').siblings('h2').html(module);

        	var gmap = new google.maps.Map2(document.getElementById("gmap"));
        	gmap.addControl(new GSmallMapControl());
        	gmap.addControl(new GMapTypeControl());

            // bounds object
            var bounds = new GLatLngBounds();

            // loop coordinates to add to map
            $(coordinates).each(function(i){

                // lat/lng instance
                var gLatLng = new GLatLng($(this).attr('lat'), $(this).attr('lng'));

                // extends bounds
                bounds.extend(gLatLng);

				var marker = new GMarker(gLatLng);
/*
				GEvent.addListener(marker, 'click', function()
					{
						marker.openInfoWindow();
					});
 */              
                // add marker
                gmap.addOverlay(marker);
            });

            // set center
            gmap.setCenter(bounds.getCenter(), gmap.getBoundsZoomLevel(bounds));
      	}
	},
	
	/**
	 * Close a lightbox.
	 * 
	 * @param id The id of the lightbox to close (with preceeding # character in it).
	 */
	close: function(id)
	{
		$(id).css('display', 'none');
		$('#lb-fade').css('display', 'none');
	}
}

/**
 * Module blocks related functions (show hide info and location blocks)
 */
var modules =
{
	/**
	 * Init modules
	 */
	init: function()
	{
	    var $tabs = $('.tabs');
	    
	    if($tabs.length > 0)
	    {
	    
	        var $detailLinks = $tabs.find('.detaillinks');
	        
	        $detailLinks.find('a').bind('click', modules.showTab);
    	    $detailLinks.find('a:first').trigger('click');
	    }
	    
	
		// If there are location links
		if ($('a.location-info').length > 0)
		{
			$('a.location-info').bind('click', modules.showLocationInfo)
		}
				
		// If there are info links
		if ($('a.general-info').length > 0)
		{
			$('a.general-info')
			    .bind('click', modules.showGeneralInfo)
			    .trigger('click');
		}
		
		// If there module titles
		if ($('a.module-title').length > 0)
		{
			$('a.module-title').bind('click', modules.showGeneralInfo)
		}
		
		// If there are minimize links
		if ($('a.minimize').length > 0)
		{
			$('a.minimize').bind('click', modules.minimizeInfo)
		}		
	},
	
	showTab: function(e)
	{
        // Find li with class 'module'
        var $this = $(this),
            $ul = $this.closest('ul'),
            $li = $this.parent(),
            listItem = $this.closest('li.module');
        
        $ul.find('li').removeClass('active');
        $li.addClass('active');
        
                    
        // Add open class to list
        listItem.addClass('open');
        
        // Show location info, hide general info
        listItem.find('.tab').hide();
        listItem.find('.' + $this.attr('rel')).siblings('div').hide();
        listItem.find('.' + $this.attr('rel')).toggle();
        
        e.preventDefault();
        /*
        // Check if all closed, if so, remove 'open' class from li
        var isOpen = false;
        if (listItem.find('div.locations').css('display') == 'block')
        {
        	isOpen = true;
        }
        else if (listItem.find('div.info').css('display') == 'block')
        {
        	isOpen = true;
        }
        if (!isOpen)
        {
        	listItem.find('a.minimize').css('display', 'none');
        	listItem.removeClass('open');
        }
        else
        {
        	listItem.find('a.minimize').css('display', 'inline');
        }
        */
	    
	},
	
	/**
	 * Show location info on a module.
	 * 
	 * @param event The triggered mouse event
	 */
	showLocationInfo: function(event)
	{
		// Find li with class 'module'
		var listItem = $(this);
		while (!listItem.hasClass('module'))
		{
			listItem = listItem.parent();
		}
		
		// Add open class to list
		listItem.addClass('open');
		
		// Show location info, hide general info
		listItem.find('div.info').css('display', 'none');
		listItem.find('div.locations').toggle();
		
		// Check if all closed, if so, remove 'open' class from li
		var isOpen = false;
		if (listItem.find('div.locations').css('display') == 'block')
		{
			isOpen = true;
		}
		else if (listItem.find('div.info').css('display') == 'block')
		{
			isOpen = true;
		}
		if (!isOpen)
		{
			listItem.find('a.minimize').css('display', 'none');
			listItem.removeClass('open');
		}
		else
		{
			listItem.find('a.minimize').css('display', 'inline');
		}
	},
	
	/**
	 * Show general info on a module.
	 * 
	 * @param event The triggered mouse event
	 */
	showGeneralInfo: function(event)
	{
		// Find li with class 'module'
		var listItem = $(this);
		while (!listItem.hasClass('module'))
		{
			listItem = listItem.parent();
		}
		
		// Add open class to list
		listItem.addClass('open');
		
		// Hide location info, show general info
		listItem.find('div.locations').css('display', 'none');
		listItem.find('div.info').toggle();
		
		// Check if all closed, if so, remove 'open' class from li
		var isOpen = false;
		if (listItem.find('div.locations').css('display') == 'block')
		{
			isOpen = true;
		}
		else if (listItem.find('div.info').css('display') == 'block')
		{
			isOpen = true;
		}
		if (!isOpen)
		{
			listItem.find('a.minimize').css('display', 'none');
			listItem.removeClass('open');
		}
		else
		{
			listItem.find('a.minimize').css('display', 'inline');
		}
	},
	
	/**
	 * Minimize info
	 * 
	 * @param event The triggered mouse event
	 */
	minimizeInfo: function(event)
	{
		// Find li with class 'module'
		var listItem = $(this);
		while (!listItem.hasClass('module')) 
		{
			listItem = listItem.parent();
		}
		
		// Hide location info & general info
		listItem.find('div.locations').css('display', 'none');
		listItem.find('div.info').css('display', 'none');
		
		// Check if all closed, if so, remove 'open' class from li
		listItem.find('a.minimize').css('display', 'none');
		listItem.removeClass('open');
	}
}

