$(function() {
    
      var popupStatus = 0;
    
//    accordion menu
//	  var drawer = jQuery.cookie('acc-drawer'); // save opened state to a cookie
//	  if (drawer === undefined) {
//		drawer = '0';
//	  }

	  $("#content").accordion({ navigation: true, header: '.toggler', autoHeight: false  //,
//		active: parseInt(drawer),
//		change: function(event,ui) {
//			var index = jQuery(this).find("h3").index(ui.newHeader[0]);
//			if (index > -1) {
//				jQuery.cookie("acc-drawer", index, {path: "/"});
//			}
//		}
	  });
	  
	  // feature accordion
	  $("#feature").accordion({ active: false, header: '.toggler', autoHeight: false  });
	  	
	$(".toggler").click(function(event){
		$('body').animate({scrollTop : 0},'slow'); 
		window.location.hash=this.hash;
	});
	
	$("#accmenu a").click(function(event){
		var myID = $(this).attr('id');
		var i = parseInt(myID.charAt(myID.length-1));
		$('#content').accordion('activate', i);
		$('body').animate({scrollTop : 0},'slow'); 
	});

	$('#allison_box').colorbox({width:"60%", inline: true, href:"#allison"});
	$('#lisamarie_box').colorbox({width:"50%", inline: true, href:"#lisamarie"});
	$('#michael_box').colorbox({width:"50%", inline: true, href:"#michael"});
	$('#sarah_box').colorbox({width:"50%", inline: true, href:"#sarah"});
	$('#william_box').colorbox({width:"50%", inline: true, href:"#william"});
	$('#garry_box').colorbox({width:"50%", inline: true, href:"#garry"});
	
	$('#figaro_box a').colorbox({inline: true, href:"#figaro"});
	$('#promo_box a').colorbox({inline: true, href:"#promo"});
	
	$('#youtube1_box').colorbox({
		inline: true,
		width: 640, 
		href: "#youtube1",
		onOpen: function(){ colorboxOpen(); },
		onClosed: function(){ colorboxClosed(); }
	});
	$('#youtube2_box').colorbox({
		inline: true,
		width: 640, 
		href: "#youtube2",
		onOpen: function(){ colorboxOpen(); },
		onClosed: function(){ colorboxClosed(); }
	});
	
	$('.enews_box').colorbox({
		width:"60%", 
		maxHeight: "75%", 
		minWidth: 450, 
		inline: true,
		href: "#enews",
		onOpen: function(){ colorboxOpen(); },
		onClosed: function(){ colorboxClosed(); }
	});	
	
	$('.contact_box').colorbox({
		width:"60%", 
		minWidth: 450, 
		inline: true, 
		href: "#contact",
		onOpen: function(){ colorboxOpen(); },
		onClosed: function(){ colorboxClosed(); }
	});	
 
  $('.sponsorship_box').colorbox({
  	width:"60%", 
  	maxHeight: "75%", 
  	inline: true, 
  	href:"#sponsorship",
		onOpen: function(){ colorboxOpen(); },
		onClosed: function(){ colorboxClosed(); }
  });
  
	$('.privacy_box').colorbox({
		width:"60%", 
		maxHeight: "75%", 
		inline: true, 
		href:"#privacy",
		onOpen: function(){ colorboxOpen(); },
		onClosed: function(){ colorboxClosed(); }
	});

	$('#feature_box').colorbox({
		width:"873px", 
		maxHeight: "90%", 
		onOpen: function(){ colorboxOpen(); },
		onClosed: function(){ colorboxClosed(); },
		inline: true, 
		href:"#feature"
	});
	
	$('.corner_link a').click(function(){
		var linkText = $(this).text();
		if(linkText.match(/show/gi) != null){
			myHTML = linkText.replace(/show/gi,"hide")
			$(this).css({
				'padding-bottom' : '21px',
				'border-bottom-style' : 'none'
			});
			$(this).parent().nextAll(".infobox").slideDown("slow");
		} else {
			myHTML = linkText.replace(/hide/gi,"show")
			$(this).parent().nextAll(".infobox").slideUp("slow")		
			$(this).css({
				'padding-bottom' : '4px',
				'border-bottom-style' : 'solid'
			});
		}
		$(this).html(myHTML);
		return false;
	});
  
  // email obfuscation
  $('a.email').nospam({
  	replaceText: true,
  	filterLevel: 'high'
	});

	$("#contact_submit").click(function(){
		var rBox = "#contact_response";
		var valid = '';
		var isr = ' is required.';
		
		var Subject = "Website Contact Form Submission";

		var ContactID = $("#contact_select").val();
		var Name = $("#contact_name").val();
		var Email = $("#contact_email").val();
		var Telephone = $("#contact_telephone").val();
		var Comments = $("#contact_comments").val();
		
		if (Name.length<1) {
			valid += '<br />Name'+isr;
		}
		if (!Email.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i)) {
			valid += '<br />A valid Email'+isr;
		}
		if (valid!='') {
			$(rBox).removeClass('notice success').addClass('error');
			$(rBox).fadeIn("slow");
			$(rBox).html("Error:"+valid);
		}
		else {
			var datastr = 'Subject=' + Subject + '&Name=' + Name + '&Contact=' + ContactID + '&Telephone=' 
			    + Telephone + '&Email=' + Email + '&Comments=' + Comments;
			$(rBox).removeClass('error success').addClass('notice').css("display", "block");
			$(rBox).html("Sending message .... ");
			$(rBox).fadeIn("slow");
			setTimeout("send('"+datastr+"','"+rBox+"')",2000);
		}
		return false;
	});
	
	$("#enews_submit").click(function(){
	
		var rBox = "#enews_response";
		var valid = '';
		var isr = ' is required.';
		
		var Subject = "Email Newsletter Signup";

		var First = $("#enews_first").val();
		var Last = $("#enews_last").val();
		var Email = $("#enews_email").val();
		var Address = $("#enews_address").val();
		var City = $("#enews_city").val();
		var Province = $("#enews_province").val();
		var Postal = $("#enews_postal").val();
		var Telephone = $("#enews_telephone").val();
		
		var AttPerf = $("#enews_attend_perf_yes").val() || $("#enews_attend_perf_no").val() || "";
		var AttPerfYear = $("#enews_attend_perf_year").val();
		
		var AttFund = $("#enews_attend_fund_yes").val() || $("#enews_attend_fund_no").val() || "";
		var AttFundYear = $("#enews_attend_perf_year").val();
		
		var Volunteer = $("#enews_volunteer_yes").val() || $("#enews_volunteer_no").val() || "";
		
		if (First.length<1) {
			valid += '<br />First Name'+isr;
		}
		
		if (Last.length<1) {
			valid += '<br />Last Name'+isr;
		}

		if (Address.length<1) {
			valid += '<br />Address'+isr;
		}

		if (City.length<1) {
			valid += '<br />City'+isr;
		}
		
		if (Postal.length<1) {
			valid += '<br />Postal Code'+isr;
		}
		
		if (!Email.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i)) {
			valid += '<br />A valid Email'+isr;
		}
		
		if (valid!='') {
			$(rBox).removeClass('notice success').addClass('error');
			$(rBox).fadeIn("slow");
			$(rBox).html("Error:"+valid);
		}
		else {
			var datastr = 'Subject=' + Subject + '&First=' + First + '&Last=' + Last + '&Email=' + Email
			    + '&Address=' + Address + '&City=' + City + '&Province=' + '&Postal=' + Postal + '&Telephone=' + Telephone  
			    + '&AttPerf=' + AttPerf + '&AttPerfYear=' + AttPerfYear 
			    + '&AttFund=' + AttFund + '&AttFundYear=' + AttFundYear + '&Volunteer=' + Volunteer;
			$(rBox).removeClass('error success').addClass('notice').css("display", "block");
			$(rBox).html("Sending message .... ");
			$(rBox).fadeIn("slow");
			setTimeout("send('"+datastr+"','"+rBox+"')",2000);
		}
		return false;
	});	
});

function send(datastr,response){
	$.ajax({	
		type: "POST",
		url: "mail.php",
		data: datastr,
		cache: false,
		success: function(html){
			$(response).removeClass('notice error').addClass('success');
			$(response).fadeIn("slow");
			$(response).html(html);
			setTimeout("$('"+response+"').fadeOut('slow')",2000);
			setTimeout("$.fn.colorbox.close()",3000);
		}
	});
}

function colorboxOpen() {
	var a = [
        self.pageXOffset ||
        document.documentElement.scrollLeft ||
        document.body.scrollLeft
        ,
        self.pageYOffset ||
        document.documentElement.scrollTop ||
        document.body.scrollTop
        ];
  $(window).bind('scroll',{pos: a},scrollLock);
}

function colorboxClosed() {
	$(window).unbind('scroll',scrollLock); // unbinds lock
}

function scrollLock(e) {
    window.scrollTo(e.data.pos[0],e.data.pos[1]);
    e.stopPropagation()
    return false;
}
