var barray;
var widthOffset = "";
function InitMaster()
{
	$("#menu").supersubs({ 
        minWidth:    12,   // minimum width of sub-menus in em units 
        maxWidth:    27,   // maximum width of sub-menus in em units 
        extraWidth:  1     // extra width can ensure lines don't sometimes turn over 
                           // due to slight rounding differences and font-family 
    }).superfish();  // call supersubs first, then superfish, so that subs are 
                     // not display:none when measuring. Call before initialising 
                     // containing tabs for same reason. 
	//position bottom gradiant
	$("#bottom-gradient").css("top",790).show();	
	
	//load background array
	$.get(
			"Controllers/BackgroundController.php?cmd=get",
			function(data)
			{
				if (data.length == 0)
				{
					//show message
					return;
				}
				widthOffset = $(document).width();
				$("div#background").css({width: widthOffset});
				for(var i = 0;i < data.length; i++)
				{
					var bg = data[i];
					$("div#background").append("<div class=\"showcase-slide\"><div class=\"showcase-content\"><img class='bimage' width='100%' src=\""+bg.url+"\" /></div></div>");
				}
				$("div#background").awShowcase({
					content_width:			widthOffset,
					content_height:			800,
					fit_to_parent:			false,
					auto:					true,
					interval:				20000,
					continuous:				true,
					loading:				true,
					tooltip_width:			200,
					tooltip_icon_width:		32,
					tooltip_icon_height:	32,
					tooltip_offsetx:		18,
					tooltip_offsety:		0,
					arrows:					false,
					buttons:				false,
					btn_numbers:			false,
					keybord_keys:			false,
					mousetrace:				false, /* Trace x and y coordinates for the mouse */
					pauseonover:			false,
					stoponclick:			false,
					transition:				'fade', /* hslide/vslide/fade */
					transition_delay:		0,
					transition_speed:		500,
					show_caption:			'onload', /* onload/onhover/show */
					thumbnails:				false,
					thumbnails_position:	'outside-last', /* outside-last/outside-first/inside-last/inside-first */
					thumbnails_direction:	'vertical', /* vertical/horizontal */
					thumbnails_slidex:		0, /* 0 = auto / 1 = slide one thumbnail / 2 = slide two thumbnails / etc. */
					dynamic_height:			false, /* For dynamic height to work in webkit you need to set the width and height of images in the source. Usually works to only set the dimension of the first slide in the showcase. */
					speed_change:			true, /* Set to true to prevent users from swithing more then one slide at once. */
					viewline:				false, /* If set to true content_width, thumbnails, transition and dynamic_height will be disabled. As for dynamic height you need to set the width and height of images in the source. */
					custom_function:		null /* Define a custom function that runs on content change */

			    });

			},
			"json"
	
	);
	$(window).resize(function(){
		widthOffset = $(document).width();
		$("div#background").css({width: widthOffset});
		$("img.bimage").css({width: widthOffset});
		$("img.bimage").parent().css({width: widthOffset});	
		$("img.bimage").parent().parent().css({width: widthOffset});	
	});
	//Get Ministries
	GetMinistries();
}

function GetMinistries()
{
	//fetch events from controller
	$.get(
			"Controllers/MinistryController.php?cmd=getAll",
			function(data)
			{
				if (data.length == 0)
				{
					//show message
					return;
				}
				var ministryList = "";
				var ministryCol="<tr>";
				var currentCol = 0;
				for(var i =0; i < data.length; i++){
					var ministry = data[i];
					var minAnchor ="<a href='"+ministry.pageUrl+"'>"+ministry.Name+"</a>";
					ministryList += "<li>"+minAnchor+"&nbsp;</li>";
					if (currentCol < 2)
					{
						currentCol++;
						ministryCol += "<td>"+minAnchor+"</td>";
					}
					else
					{
						ministryCol +="<td>"+minAnchor+"</td></tr><tr>";
						currentCol = 0;
					}
				}
				$("#ministryList").append(ministryList);
				$("td.ftr-links ul").append(ministryList);
			},
			"json"
	
	);
}

// Simple JavaScript Templating
// John Resig - http://ejohn.org/ - MIT Licensed
(function(){
  var cache = {};
  
  this.tmpl = function tmpl(str, data){
    // Figure out if we're getting a template, or if we need to
    // load the template - and be sure to cache the result.
    var fn = !/\W/.test(str) ?
      cache[str] = cache[str] ||
        tmpl(document.getElementById(str).innerHTML) :
      
      // Generate a reusable function that will serve as a template
      // generator (and which will be cached).
      new Function("obj",
        "var p=[],print=function(){p.push.apply(p,arguments);};" +
        
        // Introduce the data as local variables using with(){}
        "with(obj){p.push('" +
        
        // Convert the template into pure JavaScript
        str
          .replace(/[\r\t\n]/g, " ")
          .split("<%").join("\t")
          .replace(/((^|%>)[^\t]*)'/g, "$1\r")
          .replace(/\t=(.*?)%>/g, "',$1,'")
          .split("\t").join("');")
          .split("%>").join("p.push('")
          .split("\r").join("\\'")
      + "');}return p.join('');");
    
    // Provide some basic currying to the user
    return data ? fn( data ) : fn;
  };
})();
