/**** REL = EXTERNAL ***/
addLoadEvent(function() {
	var anchors = document.getElementsByTagName("a"); 
	for (var i=0; i<anchors.length; i++) { 
		var anchor = anchors[i]; 
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
			anchor.target = "_blank"; 
		}
	} 
})

function initPage()
{
	if($.fn.cycle)
	{
		$(".slideshow ul").cycle({
			fx:'fade',
			speed:2500,
			timeout:6000
		});
	}

	if($.fn.edenCalendar) $(".calendar").edenCalendar();

	if($.fn.tabs) $('#tabvanilla > ul').tabs({ fx: { height: 'toggle', opacity: 'toggle' } });  

	// Find any forms with class of ajax and adjust their onsubmit actions
	$("form.ajax").each(function(){
		target = $(this).parent();
		$(this).ajaxForm({
			target:target,
			type:'post',
			beforeSubmit: 	formSubmitting,
			success:		formSubmitResponse
		});
	});
	
	$("form.ajax").each(function(){
		target = $(this).parent();
		$(this).ajaxForm({
			target:target,
			type:'post',
			beforeSubmit: 	formSubmitting,
			success:		formSubmitResponse
		});
	});	
	
	$('marquee').marquee("custom element editable")
		.mouseover(function(){
			$(this).trigger("pause");
		})
		.mouseout(function(){
			$(this).trigger("start");
		});
	if(window["initializeContextMenu"] && initializeContextMenu != "undefined") initializeContextMenu();
	
//	$('.marquee').each(function(){
	
//		$('.members').height($('.members').siblings('.testimonials').height()).css('overflow','hidden');
//		var containerHeight = $(this).height();
//		var childNodes = $(this).children();
//		var arraylength = childNodes.length;
//		var sheight = parseInt(0);
//		var swidth = $(this)[0].clientWidth;
//		var sbcolor='';
		var sspeed=1;
//		var resumesspeed=sspeed
//		for(var i = 0; i < arraylength; i++){
//			sheight += childNodes[i].clientHeight;
//		}

		// Sort of works
//		if($(this).height() > $(this).attr("rel")){
//			scrollerStart($(this), $(this).attr("height"), sspeed);
//			$(this).mouseover(
//				function(){
//					clearInterval(timer);
//				}).mouseout(
//				function(){
//					if (!document.all)
//						ns6slide();
//					else
//						ieslide();
//				});
//		}	
//	});
}

$(document).ready(function(){
	$("#emailFormContainer #email_cancelBtn").click(function(){
		$("#emailFormContainer").slideUp();
	});
	
	$("#emailFormContainer FORM").submit(function(){
		$(this).slideUp();
		$(this).parent().prepend("<h2>Sending your message now!</h2>");
		$.ajax({
			type: "POST",
			data: $(this).serialize(),
			url: SETTINGS.webRoot + "/pages/email/email/format/json/",
			success: function(data) {
				eval("var data = " + data);
				$("#emailFormContainer").children("h2").html(data.message);
				if(data.result){
					// Email sent successfully
					$("#emailFormContainer").fadeOut("slow");
				}else{
					// Error sending email
					$("#emailFormContainer FORM").slideDown();
					$("#emailFormContainer").children("h2").fadeOut("fast").fadeIn("fast").fadeOut("fast").fadeIn("fast");
				}
			}
		});
		
		return false;
	});
});

function formSubmitting(formData, jqForm, options)
{
	jqForm.fadeOut("fast");
	return true;
}

function formSubmitResponse(responseText, statusText)
{
	if(responseText.indexOf("<form") != -1)
	{
		// There was an error with the form so re-initialize the form handler JS
		$("form.ajax", this).each(function(){
			target = $(this).parent();
			$(this).ajaxForm({
				target:target,
				type:'post',
				beforeSubmit: 	formSubmitting,
				success:		formSubmitResponse
			});
		});		
	}else{
		// Response was successful
		if($.fn.progressBar)
		{
			$("dd", this).progressBar({
				barImage: SETTINGS.adminSkinRoot + '/img/progressbg_green.gif',
				boxImage: SETTINGS.adminSkinRoot + '/img/progressbar.gif'
			});
		}
	}
}


$(document).ready(function(){
	initPage();
});
