// JavaScript Document

////////////////////////////////////////////
// Standard initialization for each webpage

function initialize()
{
	// Establish a jQuery tagName function to return the name of the tag for a jQuery item
	jQuery.fn.tagName = function() { return this.get(0).tagName.toLowerCase();}

 	initializeFooter();
};

function initializeFooter()
{
	// Produce a timely copyright
	
	var year = new Date().getFullYear();
	var copyright = 'Copyright &copy; '+year+' by Missouri Karate Association';
	
	jQuery('#copyright').html(copyright);
	
};


// OPTIONAL FUNCTIONS 

function populateScrollerFromXML(xmlFile,tagTarget,tagItem,tagTitle,tagContent)
{
	// Replace the contents of the 'tagTarget' from the XML file 'xmlFile' which contains a <group>
	// with includes <item> members.
	// This populates the tagTarget with content suitable for jScroller.
	// The 'tagItem' and 'tagTitle' are key tags for this format.
	//
	// The xmlFile is expected to have content of the following form:
	// <items>
    //   <item title="Weather">
	//	   <text>Check <link href="http://www.ksdk.com" target="_blank" style="color:#0000FF">here</link> for closings.</text>
	//   </item>
    //   <item title="Shiah | Seminar | Grading">
	//	   <text>Shiah starts this Friday.  Click <link href="events.htm">here</link> for details</text>
    //   </item>
	// </items>
	//
	// The <items> outer tag can be named anything.  The <item>,<text>,<link> tags are however meeaningful.
	// The xml may have embedded <link> tags ...
	// <link  href="xxx/yyy">text</link>				- defines an external link
	// <link> href="xxx/yyy" type="local">text</link>	- defines a link to local HTML page/section
	
	var itemTag		= 'item';
	var linkTag		= 'link';
	var textTag		= 'text';
	var boldTag	 	= 'b';
	var italicTag 	= 'i';
	var breakTag 	= 'br';
	
	var targetHtml = '';
	
	$.get(xmlFile, function(xmlData)
	{
		var $tagTarget = $(tagTarget);
		
		if ($tagTarget)
		{	
			$(xmlData).find(itemTag).each(function()
			{
				var $item   = $(this);
				var title   = $item.attr("title");
				var content = $item.text();	
				
				var html 	= '<div class="bulletinItem"> <h3>' + title + '</h3> ';

				$item.find(textTag).each(function()
				{
					html     += '<p>';
					var $p    = $(this);	
					var pelem = $p.get(0);
					for (var c=pelem.firstChild; c != null; c=c.nextSibling)
					{
						var tag     = c.tagName;
						var content = $(c).text(); //textContent(c);
		
						if (tag == linkTag)
						{
							html += '<a';
							var noTarget = true;
							var noLocal  = true;
							var attributes = c.attributes;
							for (var a=0; a<attributes.length; a++)
							{
								var attrName  = attributes[a].nodeName;
								var attrValue = attributes[a].nodeValue;
								if (attrName == 'target') noTarget = false;
								if (attrName == 'local' && attrValue == 'true') noLocal = false;
								else html += ' ' + attrName + '="' + attrValue + '"';
							}
							
							if (noTarget && noLocal)
								html += ' target="_blank"';
							html += '>' + content + '</a>';
						}
						else if (tag == boldTag)   html += '<b>' + content + '</b>'
						else if (tag == italicTag) html += '<em>' + content + '</em>'
						else if (tag == breakTag)  html += '<br/>' + content;
						else html += content; 
					};
					
					html += '</p> ';

				});
								
				html += '</div> ';
//				alert('html=' + html);
				targetHtml += html;
			});
			
			$tagTarget.html(targetHtml);
		}
    });
	
};

function traverse(element)
{
	for (var c=element.firstChild; c != null; c=c.nextSibling)
	{
		var tag	 	= c.tagName;
		var text	= textContent(c);
		
		alert('tag=' + tag + ' text=' + text);
		
		traverse(c);
	};
	
};


///////////////////////////////////////////////////////
// ButtonLink Functionality

function preloadImages(selector, attribute, count)
{
	// Preload all the images referenced by the argument attribute of elements returned by the argument selector
	//
	// The selector could be something like '#gallery img' with the attribute of 'src'
	// Another example would be '#gallery href' with an attribute of 'href'
	
	var elements = (isNaN(count)) ? $(selector) : $(selector).slice(0,count);
	
	elements.each(function()
	{
		var preLoad = new Image();
		preLoad.src = $(this).attr(attribute);					// assign src from element and force the preload
		//alert(preLoad.src);
	});
}

function initializeButtonLinks()
{
	// Establish a hover for each element with the buttonLink class
	//
	// Change the class for each item holding class .buttonLink to  class .buttonLinkHover.
		
	// Provide the CSS based hovering effects
	var target     = '.buttonLink';
	var stdClass   = 'buttonLink';
	var hoverClass = 'buttonLinkHover';
	
	$(target).each(function() 
	{
		$(this).hover(
		function()
		{
			$(this).removeClass(stdClass).addClass(hoverClass);
			status=$(this).find('a').attr('href');
		},
		function()
		{
			$(this).removeClass(hoverClass).addClass(stdClass);
			status='';
		});
		
		$(this).click(
		function()
		{
			var targetHref = $(this).find('a').attr('href');
			
			// If the target is _blank then use window.open to create a new window
			if ($(this).find('a').attr('target')=='_blank')
			{
//				alert('_blank for ' + targetHref);
				window.open(targetHref);       
				return false;
			}
			
			// otherwise just use default processing
			else location = targetHref;								// just assign location  and process normally
		});
		
		$(this).css('cursor','pointer');
	});	
};


function initializeCustomAccordian(trigger, content)
{	
	// Set default open/close settings
	$(content).hide(); 												// hide/close all containers
	
	// Add "active" class to first trigger, then show/open the immediate next container
	triggerFirst = trigger + ':first';
	$(triggerFirst).addClass('active').next().show(); 		

	// On Click
	$(trigger).click(function()
	{
		//If immediate next container is closed...
		if ( $(this).next().is(':hidden') ) 
		{ 
			// Remove all "active" state and slide up the immediate next container
			$(trigger).removeClass('active').next().slideUp(); 
			// Add "active" state to clicked trigger and slide down the immediate next container
			$(this).toggleClass('active').next().slideDown(); 
		}
		return false; 												// prevent the browser jump to the link anchor
	});
};

// VERTICALLY ALIGN FUNCTION

(function ($) {
$.fn.vAlign = function() {
	return this.each(function(i){
	var ah = $(this).height();
	var ph = $(this).parent().height();
	var mh = (ph - ah) / 2;
	$(this).css('margin-top', mh);
	});
};
})(jQuery);

