//# jQuery - Horizontal Accordion
//# Version 2.00.00 Alpha 1
//#
//# portalZINE(R) - New Media Network
//# http://www.portalzine.de
//#
//# Alexander Graef
//# portalzine@gmail.com
//#
//# Copyright 2007-2009

(function($) {
	$.hrzAccordion = {
       
	   
	   setOnEvent: function(i, container, finalWidth, settings){
			$("#"+container+"Handle"+i).bind(settings.eventTrigger,function() {			 
			   			
						var status = $('[rel='+container+'ContainerSelected]').data('status');
						
						if(status ==1 && settings.eventWaitForAnim === true){
						 return false;	
						}
						
						if( $("#"+container+"Handle"+i).attr("rel") != container+"HandleSelected"){
			    		
						  settings.eventAction;
							
							$('[id*='+container+'Handle]').attr("rel","");			   				
			   				
							$('[id*='+container+'Handle]').attr("class",settings.handleClass);
		
			   				$("#"+container+"Handle"+i).addClass(settings.handleClassSelected);
										   
			   		
							$("."+settings.contentWrapper).css({width: finalWidth+"px" });
							
							switch(settings.closeOpenAnimation)
							{
							case 1:
      
				   
						if($('[rel='+container+'ContainerSelected]').get(0)  ){
						$('[rel='+container+'ContainerSelected]').data('status',1);
							
							//current_width = $('[rel='+container+'ContainerSelected]').width();
							
							$('[rel='+container+'ContainerSelected]').animate({width: "0px",opacity:"0"}, { queue:true, duration:settings.closeSpeed ,easing:settings.closeEaseAction,complete: function(){	
				 																																	
							$('[rel='+container+'ContainerSelected]').data('status',0);																																											} ,step: function(now){
																																																		 							width = $(this).width();
						
							//new_width = finalWidth- (finalWidth  * (width/current_width));
							new_width = finalWidth - width;
							$('#'+container+'Content'+i).width(Math.ceil(new_width)).css("opacity","1");
							
							}});
		
						}else{
							$('[rel='+container+'ContainerSelected]').data('status',1);
								
							$('#'+container+'Content'+i).animate({width: finalWidth,opacity:"1"}, { queue:false, duration:settings.closeSpeed ,easing:settings.closeEaseAction,complete: function(){
																																																	  $('[rel='+container+'ContainerSelected]').data('status',0);	
																																																	  }});
							
								
							}
							
							break;
							case 2:
								$('[id*='+container+'Content]').css({width: "0px"});
								$('#'+container+'Content'+i).animate({width: finalWidth+"px",opacity:"1"}, { queue:false, duration:settings.openSpeed ,easing:settings.openEaseAction, complete: 
 settings.completeAction																																																									});						
							
							break;
							}

							$('[id*='+container+'Content]').attr("rel","");			
							$("#"+container+"Handle"+i).attr("rel",container+"HandleSelected");
							$("#"+container+"Content"+i).attr("rel",container+"ContainerSelected");					
							
						
						}
						
					});	
}
	    };
	
	$.fn.extend({
	   
		hrzAccordionLoop: function(options) {
			return this.each(function(a){  
				
				var container = $(this).attr("id") || $(this).attr("class");
				var elementCount = $('#'+container+' > li, .'+container+' > li').size();
				var settings = $(this).data('settings');
				
				variable_holder="interval"+container ;
				var i =0;
				var loopStatus  = "start";
				
				variable_holder = window.setInterval(function(){							
				
				$("#"+container+"Handle"+i).trigger(settings.eventTrigger);
				
				if(loopStatus =="start"){
						i = i + 1;
					}else{
						i = i-1;	
					}
					
					if(i==elementCount && loopStatus  == "start"){
						loopStatus  = "end";
						i=elementCount-1;

					}
					
					if(i==0 && loopStatus  == "end"){
						loopStatus  = "start";
						i=0;

					}
												},settings.cycleInterval);
				
				
				});
			},
		hrzAccordion: function(options) {
			this.settings = {
			eventTrigger	   		: "click",
			containerClass     		: "container",
			listItemClass      		: "listItem",					
			contentContainerClass  	: "contentContainer",
			contentWrapper     		: "contentWrapper",
			contentInnerWrapper		: "contentInnerWrapper",
			handleClass        		: "handle",
			handleClassOver    		: "handleOver",
			handleClassSelected		: "handleSelected",
			handlePosition     		: "right",
			handlePositionArray		: "", // left,left,right,right,right
			closeEaseAction    		: "swing",
			closeSpeed     			: 500,
			openEaseAction     		: "swing",
			openSpeed      			: 500,
			openOnLoad		   		: 2,
			hashPrefix		   		: "tab",
			eventAction		   		: function(){
								 	//add your own extra clickAction function here
								 	},
			completeAction	   		: function(){
								 	//add your own onComplete function here
								 	},
			closeOpenAnimation 		: 1,// 1 - open and close at the same time / 2- close all and than open next
			cycle			   		: false, // not integrated yet, will allow to cycle through tabs by interval
			cycleInterval	   		: 10000,
			fixedWidth				: "",
			eventWaitForAnim		: true
				
		};
	
		if(options){
			$.extend(this.settings, options);
		}
			var settings = this.settings;
			
			
			
			return this.each(function(a){    		
				
				var container = $(this).attr("id") || $(this).attr("class");			
				
				$(this).data('settings', settings);
				
				$(this).wrap("<div class='"+settings.containerClass+"'></div>");
			
				var elementCount = $('#'+container+' > li, .'+container+' > li').size();
												
				var containerWidth =  $("."+settings.containerClass).width();
				
				var handleWidth = $("."+settings.handleClass).css("width");
		
				handleWidth =  handleWidth.replace(/px/,"");
			    var finalWidth;
				var handle;
				
				if(settings.fixedWidth){
					finalWidth = settings.fixedWidth;
				}else{
					finalWidth = containerWidth-(elementCount*handleWidth)-handleWidth;
				}
				
				$('#'+container+' > li, .'+container+' > li').each(function(i) {
			
					$(this).attr('id', container+"ListItem"+i);
			   		$(this).attr('class',settings.listItemClass);
		       		$(this).html("<div class='"+settings.contentContainerClass+"' id='"+container+"Content"+i+"'>"
								 +"<div class=\""+settings.contentWrapper+"\">"
								 +"<div class=\""+settings.contentInnerWrapper+"\">"
								 +$(this).html()
								 +"</div></div></div>");
			   		
					if($("div",this).hasClass(settings.handleClass)){
					
					var html = $("div."+settings.handleClass,this).attr("id",""+container+"Handle"+i+"").html();
					$("div."+settings.handleClass,this).remove();
					
					 handle = "<div class=\""+settings.handleClass+"\" id='"+container+"Handle"+i+"'>"+html+"</div>";
					}else{
					 handle = "<div class=\""+settings.handleClass+"\" id='"+container+"Handle"+i+"'></div>";
					}
					
				
					
					if(settings.handlePositionArray){
						splitthis 				= settings.handlePositionArray.split(",");
						settings.handlePosition = splitthis[i];
					}
					
					switch(settings.handlePosition ){
						case "left":
						$(this).prepend( handle );
						break;
						case "right":	
						$(this).append( handle );	
						break;
						case "top":	
						$("."+container+"Top").append( handle );	
						break;
						case "bottom":	
						$("."+container+"Bottom").append( handle );	
						break;
					}					
				
					$("#"+container+"Handle"+i).bind("mouseover", function(){
						$("#"+container+"Handle"+i).addClass(settings.handleClassOver);
					});
			    
					$("#"+container+"Handle"+i).bind("mouseout", function(){
						if( $("#"+container+"Handle"+i).attr("rel") != "selected"){
							$("#"+container+"Handle"+i).removeClass(settings.handleClassOver);
						}
					});
					
				
					$.hrzAccordion.setOnEvent(i, container, finalWidth, settings);				
					
					if(i == elementCount-1){
						$('#'+container+",."+container).show();					
					}
					
					
								
					if(settings.openOnLoad !== false && i == elementCount-1){
							var location_hash = location.hash;
							location_hash  = location_hash.replace("#", "");	
							if(location_hash.search(settings.hashPrefix) != '-1' ){
							var tab = 1;
							location_hash  = location_hash.replace(settings.hashPrefix, "");
							}
							
							if(location_hash && tab ==1){
						 		$("#"+container+"Handle"+(location_hash)).attr("rel",container+"HandleSelected");
								$("#"+container+"Content"+(location_hash)).attr("rel",container+"ContainerSelected");		
								$("#"+container+"Handle"+(location_hash-1)).trigger(settings.eventTrigger);
												
							}else{
								$("#"+container+"Handle"+(settings.openOnLoad)).attr("rel",container+"HandleSelected");
							    $("#"+container+"Content"+(settings.openOnLoad)).attr("rel",container+"ContainerSelected");	
								$("#"+container+"Handle"+(settings.openOnLoad-1)).trigger(settings.eventTrigger);
							}					
					}	
				});	
				
				if(settings.cycle === true){
					$(this).hrzAccordionLoop();
				}
			});				
		}		
	});
})(jQuery);	

$(document).ready(function() {

	var category = $("body").attr("id");
	var index = 0;
	$(".nav_list .nav_stuff").each(function(i) {
		if ($(this).attr("id") == "nav_" + category) {
			index = i;
		}
	});

	$(".nav_list").hrzAccordion({
		containerClass     : "navigation1",
		contentWrapper     : "contentWrapper3",
		contentInnerWrapper: "contentInnerWrapper3",
		handleClass        : "nav_handle",
		handleClassOver    : "nav_handle_over",
		handleClassSelected: "nav_handle_selected",
		openOnLoad         : (index + 1)
	});

});
function navigation() {
  if (document.getElementById) {
    root = document.getElementById("navigation");
    if (root) {
      for (i = 0; i < root.childNodes.length; i++) {
        node = root.childNodes[i];
        if (node.nodeName == "LI") {
          node.onmouseover = function() { navMouseOver(this) };
          node.onmouseout = function() { navMouseOut(this) };
        }
      }
    }
  }
}

function navMouseOver(object) {
  current = document.getElementById("selected");
  object.className = "selected";
  if (current != object) {
    current.childNodes[1].style.display = 'none';
  }
}

function navMouseOut(object) {
  current = document.getElementById("selected");
  if (current != object) {
    current.childNodes[1].style.display = 'block';
  }
  object.className = "";
}


function slideshowInit() {
  if (document.getElementById) {
    var root = document.getElementById('slideshow');
    if (root) {
      var tags = root.getElementsByTagName('div');
      tags[0].className = "current";

      var switcher = document.createElement('span');
      switcher.id = 'switcher';
      root.insertBefore(switcher, root.firstChild);

      for (i = 0; i < tags.length; i++) {
        var node = tags[i];
        node.onclick = function() {
          location.href = this.childNodes[1].href;
        }
        var test = document.createElement('span');
        test.id = 'switcher' + i;
        test.onmouseover = function() { slidePreview(this); }
        var test2 = document.createElement('span');
        test2.appendChild(document.createTextNode(i));
        test.appendChild(test2);
        switcher.appendChild(test);
      }
      var test = document.getElementById('switcher' + 0);
      test.className = 'current';
      setTimeout('slideshow();', 6000);
    }
  }
}

function slidePreview(index) {
  if (document.getElementById) {
    var root = document.getElementById('slideshow');
    var tags = root.getElementsByTagName('div');
    for (i = 0; i < tags.length; i++) {
      var node = tags[i];
      if (node.className == 'current') {
        node.className = '';
        var switcher = document.getElementById('switcher' + i);
        switcher.className = '';
        break;
      }
    }
    tags[index.id.substring(8)].className = 'current';
    index.className = 'current';
  }
}

function slideshow() {
  if (document.getElementById) {
    var root = document.getElementById('slideshow');
    var tags = root.getElementsByTagName('div');
    for (i = 0; i < tags.length; i++) {
      var node = tags[i];
      if (node.className == 'current') {
        node.className = '';
        tags[(i + 1) % tags.length].className = 'current';
        var switcher = document.getElementById('switcher' + i);
        switcher.className = '';
        var switcher = document.getElementById('switcher' + ((i + 1) % tags.length));
        switcher.className = 'current';
        break;
      }
    }
    setTimeout('slideshow();', 6000);
  }
}

function photoWidget() {
  if (document.getElementById) {
    var root = document.getElementById('article');
    if (root) {
      var tags = root.getElementsByTagName('img');
      for (i = 0; i < tags.length; i++) {
        var node = tags[i];
        if (node.className == 'photoWidgetLeft' || node.className == 'photoWidgetRight') {
          var container = document.createElement('div');
          container.className = node.className + 'Container';
          container.style.width = node.width + 'px';
          var image = node.cloneNode(false);
          description = document.createElement('p');
          description.appendChild(document.createTextNode(node.title));
          container.appendChild(image);
          container.appendChild(description);
          node.parentNode.replaceChild(container, node);
        }
      }
    }
  }
}

function widescreen() {
  if (document.getElementById) {
    var body = document.getElementsByTagName("body").item(0);
    body.className = body.id + '-widescreen';
  }
}

function domEdit() {
  if (document.getElementById) {
    var root = document.getElementById('options');
    if (root) {
      var tags = root.getElementsByTagName('div');
      for (i = 0; i < tags.length; i++) {
        var node = tags[i];
        node.onclick = function() {
          location.href = this.childNodes[1].href;
        }
      }
    }
    var root = document.getElementById('minicalendar');
    if (root) {
      root.onclick = function() {
        location.href = "http://www.carmelcentre.org/events/";
      }
    }
    var root = document.getElementById('search');
    if (root) {
      input = root.childNodes[0].childNodes[1];
      input.onfocus = function() {
        if (this.value == 'search...') {
          this.value = '';
        }
        this.style.color = '#000000';
      }
      input.onblur = function() {
        if (this.value == '') {
          this.value = 'search...';
        }
        this.style.color = '#999999';
      }
    }
  }
}

var preloads = new Array(
  "church/banner.jpg",
  "support/banner.jpg",
  "creative/banner.jpg",
  "lifeskills/banner.jpg",
  "education/banner.jpg",
  "community/banner.jpg",
  "outreach/banner.jpg",
  "funding/banner.jpg",
  "contact/banner.jpg",
  "church/calendar.png",
  "support/calendar.png",
  "creative/calendar.png",
  "lifeskills/calendar.png",
  "education/calendar.png",
  "community/calendar.png",
  "outreach/calendar.png",
  "funding/calendar.png",
  "contact/calendar.png",
  "all/postit_bottom.png",
  "all/postit_middle.png",
  "all/postit_top.png",
  "all/navigation.png"
);
function preload() {
  if (document.images) {
    var loaded = new Array();
    for (var i = 0; i<preloads.length; i++) {
      loaded[i] = new Image();
      loaded[i].src = preloads[i];
    }
  }
}

function zebratables() {
  var tables = document.getElementsByTagName("table");  
  for(var x=0;x!=tables.length;x++){
    var table = tables[x];
    if (! table) { return; }
    var tbodies = table.getElementsByTagName("tbody");
    for (var h = 0; h < tbodies.length; h++) {
      var even = true;
      var trs = tbodies[h].getElementsByTagName("tr");
      for (var i = 0; i < trs.length; i++) {
        trs[i].onmouseover=function(){
          this.className += " ruled"; return false
        }
        trs[i].onmouseout=function(){
          this.className = this.className.replace("ruled", ""); return false
        }
        if(even)
          trs[i].className += " even";
        even = !even;
      }
    }
  }
}

var _POPUP_FEATURES = 'location=0, statusbar=0, menubar=0, width=400, height=300';
function raw_popup(url, target, features) {
  if (features == '') {
    features = _POPUP_FEATURES;
  }
  if (target == '') {
    target = '_blank';
  }
  var theWindow = window.open(url, target, features);
  theWindow.focus();
  return theWindow;
}
function link_popup(src, features) {
  return
    raw_popup(src.getAttribute('href'),
    src.getAttribute('target') || '_blank',
    features);
}

window.onload = function() { navigation(); slideshowInit(); photoWidget(); domEdit(); zebratables(); /* preload(); */ widescreen(); }

/***
*  jMP3 v0.2.1 - 10.10.2006 (w/Eolas fix & jQuery object replacement)
* an MP3 Player jQuery Plugin (http://www.sean-o.com/jquery/jmp3)
* by Sean O
*
* An easy way make any MP3 playable directly on most any web site (to those using Flash & JS),
* using the sleek Flash Single MP3 Player & the fantabulous jQuery.
*
* SIMPLE USAGE Example:
* $(youridorclass).jMP3();
*
* ADVANCED USAGE Example:
* $("#sounddl").jmp3({
*   showfilename: "false",
*   backcolor: "000000",
*   forecolor: "00ff00",
*   width: 200,
*   showdownload: "false"
* });
*
* HTML:
* <span class="mp3">sound.mp3</span>
*
* NOTE: filename must be enclosed in tag.  Various file paths can be set using the filepath option.
*
* Copyright (c) 2006 Sean O (http://www.sean-o.com)
* Licensed under the MIT License:
* http://www.opensource.org/licenses/mit-license.php
*
***/
jQuery.fn.jmp3 = function(passedOptions){
	// hard-wired options
	var playerpath = "/media/flash/smp/";					// SET THIS FIRST: path to singlemp3player.swf

	// passable options
	var options = {
		"filepath": "",										// path to MP3 file (default: current directory)
		"backcolor": "",									// background color
		"forecolor": "ffffff",								// foreground color (buttons)
		"width": "25",										// width of player
		"repeat": "no",										// repeat mp3?
		"volume": "50",										// mp3 volume (0-100)
		"autoplay": "false",								// play immediately on page load?
		"showdownload": "true",								// show download button in player
		"showfilename": "true"								// show .mp3 filename after player
	};
	
	// use passed options, if they exist
	if (passedOptions) {
		jQuery.extend(options, passedOptions);
	}
	
	// iterate through each object
	return this.each(function(){
		// filename needs to be enclosed in tag (e.g. <span class='mp3'>mysong.mp3</span>)
		var filename = options.filepath + jQuery(this).html();
		// do nothing if not an .mp3 file
		var validfilename = filename.indexOf(".mp3");
		if (validfilename == -1) { return false; }
		// build the player HTML
		var mp3html = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ';
		mp3html += 'width="' + options.width + '" height="20" ';
		mp3html += 'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab">';
		mp3html += '<param name="movie" value="' + playerpath + '/downloads/singlemp3player.swf?';
		mp3html += 'showDownload=' + options.showdownload + '&file=' + filename + '&autoStart=' + options.autoplay;
		mp3html += '&backColor=' + options.backcolor + '&frontColor=' + options.forecolor;
		mp3html += '&repeatPlay=' + options.repeat + '&songVolume=' + options.volume + '" />';
		mp3html += '<param name="wmode" value="transparent" />';
		mp3html += '<embed wmode="transparent" width="' + options.width + '" height="20" ';
		mp3html += 'src="' + playerpath + '/downloads/singlemp3player.swf?'
		mp3html += 'showDownload=' + options.showdownload + '&file=' + filename + '&autoStart=' + options.autoplay;
		mp3html += '&backColor=' + options.backcolor + '&frontColor=' + options.forecolor;
		mp3html += '&repeatPlay=' + options.repeat + '&songVolume=' + options.volume + '" ';
		mp3html += 'type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
		mp3html += '</object>';
		// don't display filename if option is set
		if (options.showfilename == "false") { jQuery(this).html(""); }
		jQuery(this).prepend(mp3html+"&nbsp;");
		
		// Eolas workaround for IE (Thanks Kurt!)
		if(jQuery.browser.msie){ this.outerHTML = this.outerHTML; }
	});
};
