var home_html_holder="";
var equip_html_holder="";
var worship_html_holder="";
var serve_html_holder="";
var liveService = false;
var popUp = false
var current_page = location.href

function loadWorship(mylink, windowname, targetlink)
{
	//popUp = true
	if($.browser.msie){
	
		if (home_html_holder==""){
			home_html_holder = $("#home_vid_wrapper").html();
		}
	}
  var mywin, href;
  if (typeof(mylink) == 'string')
    href = mylink;
  else 
    href = mylink.href;

  mywin = window.open(href, windowname, "width=1024,height=768,titlebar=no,status=no,resizable=yes,location=no,scrollbars=yes,menubar=yes");
	//alert(home_html_holder);
  saveLink(targetlink);
	if (home_html_holder!=""){
		$("#home_vid_wrapper").html(home_html_holder);
		$("#home_vid_wrapper").bind("click",function(){
			playHome();
		});
	}

  return false;
}

function setCookie(name, value, expires)
{
  document.cookie = name + "=" + escape(value) + "; path=/; expires=" + expires.toGMTString() + "; domain=.northlandchurch.net;";
}

// save targetlink in cookie, then the flex client will know which tab it should go(e.g. My Profile or Resource)
function saveLink(targetlink)
{
  var expdate = new Date();
  expdate.setTime(expdate.getTime() + 1000 * 1000);

  setCookie("insite.northlandchurch.nextlink", targetlink, expdate);
}

$(document).ready(function(){

$('a#click_happeningnow').click(function(){
	$('#happeningcontent1').toggle();
	document["happ-now"].src="/_img/happening-now-on.jpg";
	if ($('#happeningcontent1').css("display")=="none"){
		document["happ-now"].src="/_img/happening-now-off.jpg";
	}
});




if (document.location.href=="http://www.northlandchurch.net/"){
	document["happ-now"].src="/_img/happening-now-on.jpg";
}
	$("a.ministry_partners").click(function(){
		var openFolder = "."+this.id;
		$(".ministry_partners p").hide("fast");
		$(openFolder).show("fast");
	});
	which_Section = current_page.substring(current_page.indexOf("#"), current_page.length);
	switch (which_Section)
	{
		case "#cal":
			expandMain("#page_events");
			break;
		case "#happening":
			//expandMain("#page_home");
			break;
		case "#calendar":
			expandMain("#page_events");
			break;
		case "#serve":
			expandMain("#page_serve");
			break;
		case "#worship":
			expandMain("#page_worship");
			break;
		case "#equip":
			expandMain("#page_equip");
			break;
		case "#about":
			expandMain("#page_about");
			break;

		default:
			break;
	}

	//$("#home_vid_wrapper").html(home_html_holder);
	function filterStaff(department){
		$(".staff").hide();
		var toShow = "." + department;
		//alert(toShow);
		$(toShow).show();
		var toHighlight = "a#" + department;
		$("#filter-list a").css({"text-transform":"none","font-weight":"normal"});
		$(toHighlight).css({"text-transform":"uppercase","font-weight":"bold"});
		//var whichStaff = department + " Staff";
		//var test = 
		$("#which_staff").html($(toHighlight).html());
		
	}
	function filterMedia(category, offset){
		$(".media").hide();
		var toShow = "." + category;
		//alert(toShow);
		$(toShow).show();
		var loadString = '/includes/playlist/';
		$("#playlist").load(loadString);
		//var toHighlight = "a#" + category;
		//$("#filter-list a").css({"text-transform":"none","font-weight":"normal"});
		//$(toHighlight).css({"text-transform":"uppercase","font-weight":"bold"});
		//var whichStaff = department + " Staff";
		//var test = 
		//$("#which_staff").html($(toHighlight).html());
		
	}
	$("a#Sermons").click(function(){
		filterMedia("Sermons");
	});
	$("a#all").click(function(){
		$(".staff").show();
		$("#which_staff").html("All Staff")
	});
	$("a#worship").click(function(){
		filterStaff("worship");
	});
	$("a#service").click(function(){
		filterStaff("service");
	});
	$("a#equipping").click(function(){
		filterStaff("equipping");
	});
	$("a#media-design").click(function(){
		filterStaff("media-design");
	});
	$("a#operations").click(function(){
		filterStaff("operations");
	});
	$("a#senior-pastor").click(function(){
		filterStaff("senior-pastor");
	});
	$("a#congregational-services").click(function(){
		filterStaff("congregational-services");
	});
	$("a#youth").click(function(){
		filterStaff("youth");
	});
	$("a#children").click(function(){
		filterStaff("children");
	});
	$("a#pastoral-care").click(function(){
		filterStaff("pastoral-care");
	});
	var weeks=$("#studies").html();
	//alert(weeks);
	$("#week1listbutton").click(function(){
		$("#week1list").show();
		$('#week2list').hide();
		$('#week3list').hide();
		$('#week4list').hide();
		$('#week1listbutton').html("Week 1");
		$('#week2listbutton').html("Week 2 (click to expand list)");
		$('#week3listbutton').html("Week 3 (click to expand list)");
		$('#week4listbutton').html("Week 4 (click to expand list)");
	});
	$("#week2listbutton").click(function(){
		$("#week1list").hide();
		$('#week2list').show();
		$('#week3list').hide();
		$('#week4list').hide();
		$('#week1listbutton').html("Week 1 (click to expand list)");
		$('#week2listbutton').html("Week 2");
		$('#week3listbutton').html("Week 3 (click to expand list)");
		$('#week4listbutton').html("Week 4 (click to expand list)");
	});
	$("#week3listbutton").click(function(){
		$("#week1list").hide();
		$('#week2list').hide();
		$('#week3list').show();
		$('#week4list').hide();
		$('#week1listbutton').html("Week 1 (click to expand list)");
		$('#week2listbutton').html("Week 2 (click to expand list)");
		$('#week3listbutton').html("Week 3");
		$('#week4listbutton').html("Week 4 (click to expand list)");
	});	
	$("#week4listbutton").click(function(){
		$("#week1list").hide();
		$('#week2list').hide();
		$('#week3list').hide();
		$('#week4list').show();
		$('#week1listbutton').html("Week 1 (click to expand list)");
		$('#week2listbutton').html("Week 2 (click to expand list)");
		$('#week3listbutton').html("Week 3 (click to expand list)");
		$('#week4listbutton').html("Week 4");
		
	});
	$("#week1button").click(function(){
		$("#week1").show();
		$('#week2').hide();
		$('#week3').hide();
		$('#week4').hide();		
	});
	$("#week2button").click(function(){
		$("#week2").show();
		$('#week1').hide();
		$('#week3').hide();
		$('#week4').hide();
	});
	$("#week3button").click(function(){
		$("#week3").show();
		$('#week1').hide();
		$('#week2').hide();
		$('#week4').hide();
	});
	$("#week4button").click(function(){
		$("#week4").show();
		$('#week1').hide();
		$('#week2').hide();
		$('#week3').hide();
	});
	$("a #closeButton").click(function(){
		$(".secondary").show();
		$('#page_slide').hide();
		$("#closeButton").hide();
		
	});
	$("#searchbox").mouseout(function(){
		$("#searchbox .mid").hide();
	});
	$("#searchbox").mouseover(function(){
		$("#searchbox .mid").show();
		$("#searchbox .mid").css({"zIndex":"400"});

	});
	function resetTab(whichTab)
	{
		document["happ-now"].src="/_img/happening-now-off.jpg";
		//losing a:hover style in here somehow
		var currBG = $(whichTab).css('background-position');
		if (currBG == 'undefined' || currBG == null) {
		        currBG = $(whichTab).css("background-position-x") + " " + $(whichTab).css("background-position-y");
		    }
		currBG = currBG.substr(0,currBG.indexOf(" ")) + " 0px";
		$(whichTab).css({'background-position': currBG});

	}
	function playHome(){
		if (home_html_holder==""){
			home_html_holder = $("#home_vid_wrapper").html();
		}
        
		swfobject.embedSWF(player, "home_vid", "643","380", "9.0.45.0", "expressInstall.swf", flashvars_home, params_home, attributes_home);		
		
	}
	$("#home_vid_watcher").click(function(){
		if (!popUp){
			playHome();
		}

	});
	function playServe(){
		if (serve_html_holder==""){
			serve_html_holder = $("#serve_vid_wrapper").html();
		}        
		swfobject.embedSWF(player, "serve_vid", "620", "380", "9.0.45.0", "expressInstall.swf", flashvars_serve, params_serve, attributes_serve);
				
	}
	$("#serve_vid_wrapper").click(function(){
		playServe();
	});
	function playWorship(){
		if (worship_html_holder==""){
			worship_html_holder = $("#worship_vid_wrapper").html();
		}        
//		alert(worship_html_holder);
//		swfobject.embedSWF(player, "worship_vid", "690", "369", "9.0.45.0", "expressInstall.swf", flashvars_worship, params_worship, attributes_worship);
		swfobject.embedSWF(player, "worship_vid", "620", "380", "9.0.45.0", "expressInstall.swf", flashvars_worship, params_worship, attributes_worship);
				
	}
	$("#worship_vid_wrapper").click(function(){
		playWorship();
	});
	function playEquip(){	
		if (equip_html_holder==""){
			equip_html_holder = $("#equip_vid_wrapper").html();
		}     
		swfobject.embedSWF(player, "equip_vid", "620", "380", "9.0.45.0", "expressInstall.swf", flashvars_equip, params_equip, attributes_equip);
	}
	$("#equip_vid_wrapper").click(function(){
		playEquip()
	});

	function loadService(mylink, windowname, targetlink){
		return loadWorship(this, 'profile', 'Profile'); 
		resetTabs();
	}

	
	var MAX_DUMP_DEPTH = 10;
    function dumpObj(obj, name, indent, depth) {
		if (depth > MAX_DUMP_DEPTH) {
			return indent + name + ": <Maximum Depth Reached>\n";
		}
		if (typeof obj == "object") {
			var child = null;
			var output = indent + name;
			indent += "&amp;";
			for (var item in obj){
				try {
					child = obj[item];
				} catch (e) {
					child = "<Unable to Evaluate>";
				}
				if (typeof child == "object") {
					output += dumpObj(child, item, indent, depth + 1);
				} else {
					output += indent + item + ": " + child;
				}
			}
			output=output.replace(/: /g, "=");
			output=output.replace(/undefined/g, "");
			output=output.replace(/NaN/g, "");
			output=output.replace(/#/g, "");
			output=output.replace(/"/g, "=");
			output=output.replace(/ /g, "+");

			output=output.substr(5)
			return output;
		} else {
			return obj;
 		}
	}
	function unloadFlash(){
		$("#flashObject_home").empty(); 
		$("#flashObject_home").hide();
	}


	$("#tab_worship a").click(function () {
		//grabbing the position, it automatically left-aligns the new div
		//expandMain("#page_worship");
		var flashString = dumpObj(flashvars_worship);
		
		$("#flashObject_worship").html("<param name='allowscriptaccess' value='always'><param name='allowfullscreen' value='true'><param name='flashvars' value="+flashString+"></object>");
		$("#flashObject_worship").show();
		
	});
	$("#tab_equip a").click(function () {
		//grabbing the position, it automatically left-aligns the new div
		//expandMain("#page_equip");
		if($.browser.mozilla){
			if (equip_html_holder==""){
				equip_html_holder = $("#equip_vid_wrapper").html();
			}
				
			swfobject.embedSWF(player, "equip_vid", "620", "380", "9.0.45.0", "expressInstall.swf", flashvars_equip, params_equip, attributes_equip);
		}
		var flashString = dumpObj(flashvars_equip);
		$("#flashObject_equip").html("<param name='allowscriptaccess' value='always'><param name='allowfullscreen' value='true'><param name='flashvars' value="+flashString+"></object>");
		$("#flashObject_equip").show();

		
	});

	$("#happening_now_slide a").click(function () {
		//grabbing the position, it automatically left-aligns the new div
		//expandMain("#page_home");
		//var flashString = dumpObj(flashvars_home)
		//$("#flashObject_home").html("<param name='allowscriptaccess' value='always'><param name='allowfullscreen' value='true'><param name='flashvars' value="+flashString+"></object>");
		//$("#flashObject_home").show();		
	});


});