/**
 * Put jQuery in no conflict mode because Joomla comes with Mootools by default!
 */
jQuery.noConflict();

/**
 * Base class, extended by specialised JS objects that may or may not be included
 * 
 * Namespace "Base"
 * @class Base
 * @desc Base class of this project
 */
var Base = function($) {
	// VARIABLES DEFINED HERE ARE PRIVATE
	var arr_init = []; // Array containing all registered init-functions of subclasses
	
	// FUNCTIONS DEFINED HERE ARE PRIVATE
	
	/**
	 * Check and execute registered subclass functions
	 */
	function check_register() {
		for (var i=0; i<arr_init.length; i++) {
			arr_init[i]();
		}
	}
	
  
  /**
   * Checks IE version. This requires a line of JS in the <head> tags within
   * IE conditional statements similar to the one below. If not given,
   * Base.ieVersion stays null.
   * 
   * document.getElementsByTagName("html")[0].className += (" ie6");
   */
  function check_ie(){
    if( $ ){
      if(! window.XMLHttpRequest )
        Base.ieVersion = 6;
      
      if( $('html.ie7').length > 0 )
        Base.ieVersion = 7;
      
      if( $('html.ie8').length > 0 )
        Base.ieVersion = 8;
    }
  }
	
	return { // Public area
		/**
		* Register (initialization) calls from subclasses
		* @param (obj_function) Function to initialize subclass
		*/
		register: function(obj_function) {
			arr_init.push(obj_function);
		},
		
		
		/**
		* Initialize this class
		*/
		init: function() {
      check_ie();
			
			check_register();
		}
	}
}(jQuery);

jQuery(document).ready(function(){
	Base.init();
});

/**
 * Namespace "Subclass"
 * @class Subclass
 * @desc Subclass of Base
 */
Base.Poelenburg = function($) {
	// Variables & functions defined here are private

	var bln_loginbox_opened = false; // Boolean holding the state (open, closed) of the login box
	
	/**
	 * Add event handlers to DOM elements on page load
	 */
	function addEventHandlers() {
		// Add toggle event to loginbox links (Top menu link "login")
		$("#topmenu .moduletable .separator").click( function() {
			toggleLoginBox();
		});
		// Open loginbox from comment form
		$(".goto-loginbox a").click( function() {
			toggleLoginBox( true );
		});
		// Handle submit module news archive
		archiveSelector();
		// Toggle forum details visibility
		$(".forum .collapse").click( function() {
			$(this).next().toggle();
		});
		// Open search options box
		$("#open-searchoptions").click( function() {
			$("#searchoptions").toggle();
		});
	}
	
	/**
	 * Handle submit module news archive
	 */
	function archiveSelector() {
		// Catch submit event of form
		$form = $(".k2-module-archive form"); 
		$form.submit( function() {
			var sel = $(this).find("select").val();
			if (sel==0) return false; // Option "make selection"
			if (sel == -1) {
		  	location.href = "/nieuws"; // Go to news section, TODO: more generic please
		  	return false;
	  	}
			return true;
		})
		// Catch change event of SELECT to submit FORM
		.find("select").change ( function() {
			$form.submit();
		});
	}
	
	/**
	 * Slidetoggle login box
	 */
	function toggleLoginBox(bln_open) {
		if (bln_open) {
			// Always open
			bln_loginbox_opened = false;
		}
		// Toggle
		if ( bln_loginbox_opened ) {
			$("#header #topmenu form").stop().animate( {top: '-100px'} , 600 );
			$("#topmenu .moduletable").stop().animate( {top: '0px'} , 600 );
			$("#header h1, #topmenu .moduletable_loginbox_top h3").stop().animate( {top: '0px'} , 600);
			$(".project-home").stop().animate( {top: '5px'} , 600);
			$(".multisite #header").stop().animate( {height: '100px'} , 600, function() {
				$("#topmenu, #topmenu .moduletable .separator, .multisite #bgholder, .multisite #bgholder-left, .multisite #bgholder-right").removeClass("open");
			});
			bln_loginbox_opened = false;
		} else {
			$("#header #topmenu form").stop().animate( {top: '0px'} , 600 );
			$("#topmenu .moduletable").stop().animate( {top: '100px'} , 600 );
			$("#header h1, #topmenu .moduletable_loginbox_top h3").stop().animate( {top: '100px'} , 600);
			$(".project-home").stop().animate( {top: '105px'} , 600);
			$(".multisite #header").stop().animate( {height: '200px'} , 600);
			$("#topmenu, #topmenu .moduletable .separator, .multisite #bgholder, .multisite #bgholder-left, .multisite #bgholder-right").addClass("open");
			bln_loginbox_opened = true;
		}
		// Give focus to first input field
		$(".moduletable_loginbox_top form #modlgn_username").focus();
	}
	
	/**
	 * Because some parts of the website are loaded in iframes
	 * 	we have to check if the anchors inside the iframe
	 * 	redirect to a part inside the iframe or some other URL
	 */
	function checkAnchors() {
		if (top.location.href != location.href) {
	  	// We're in an iframe
			$("a").click( function() {
				var link = $(this).attr("href");
				if (link && link.indexOf("#") == 0) {
					if (link.indexOf("#bgholder") == 0) {
						top.location.href = top.location.href + "#bgholder";
						return false;
		  		}
					return true; // hash link
				}
				if (link && link.indexOf("ccboard") <= 0) {
				// This link links outside the forum, bust the iframe
					if (link.indexOf("http:") != 0) {
						// Relative link, add hostname
						link = location.protocol + "//" + location.hostname + "/" + link;
		  		}
					top.location.href = link;
					return false;
				}
			});
		}
	}		
	
	return {
  	// Variables & functions defined here are public
		/**
		 * Initialize this Class
		 */
		init: function() {
			addEventHandlers();
			checkAnchors();
		}		
	};
}(jQuery);

/* Initialize this class */
Base.register(Base.Poelenburg.init);

