// Load jQuery
google.load("jquery", "1.4.2");
google.load("swfobject", "2.2");

// set onload
google.setOnLoadCallback(function() {
	
	LazyLoad=function(){var f=document,g,b={},e={css:[],js:[]},a;function j(l,k){var m=f.createElement(l),d;for(d in k){if(k.hasOwnProperty(d)){m.setAttribute(d,k[d])}}return m}function h(d){var l=b[d];if(!l){return}var m=l.callback,k=l.urls;k.shift();if(!k.length){if(m){m.call(l.scope||window,l.obj)}b[d]=null;if(e[d].length){i(d)}}}function c(){if(a){return}var k=navigator.userAgent,l=parseFloat,d;a={gecko:0,ie:0,opera:0,webkit:0};d=k.match(/AppleWebKit\/(\S*)/);if(d&&d[1]){a.webkit=l(d[1])}else{d=k.match(/MSIE\s([^;]*)/);if(d&&d[1]){a.ie=l(d[1])}else{if((/Gecko\/(\S*)/).test(k)){a.gecko=1;d=k.match(/rv:([^\s\)]*)/);if(d&&d[1]){a.gecko=l(d[1])}}else{if(d=k.match(/Opera\/(\S*)/)){a.opera=l(d[1])}}}}}function i(r,q,s,m,t){var n,o,l,k,d;c();if(q){q=q.constructor===Array?q:[q];if(r==="css"||a.gecko||a.opera){e[r].push({urls:[].concat(q),callback:s,obj:m,scope:t})}else{for(n=0,o=q.length;n<o;++n){e[r].push({urls:[q[n]],callback:n===o-1?s:null,obj:m,scope:t})}}}if(b[r]||!(k=b[r]=e[r].shift())){return}g=g||f.getElementsByTagName("head")[0];q=k.urls;for(n=0,o=q.length;n<o;++n){d=q[n];if(r==="css"){l=j("link",{href:d,rel:"stylesheet",type:"text/css"})}else{l=j("script",{src:d})}if(a.ie){l.onreadystatechange=function(){var p=this.readyState;if(p==="loaded"||p==="complete"){this.onreadystatechange=null;h(r)}}}else{if(r==="css"&&(a.gecko||a.webkit)){setTimeout(function(){h(r)},50*o)}else{l.onload=l.onerror=function(){h(r)}}}g.appendChild(l)}}return{css:function(l,m,k,d){i("css",l,m,k,d)},js:function(l,m,k,d){i("js",l,m,k,d)}}}();
	
	// remove no_JS class from body tag
	$('body').removeClass('no_JS');
	
	$("div#sidebar a").filter(function() {
		//Take the current URL and split it into chunks at each slash
		var currentURL = window.location.toString().split("/");
		//return true if the bit after the last slash is the current page name
		return $(this).attr("href") == currentURL[currentURL.length-1];
		//when the filter function is done, you're left with the links that match.
	}).addClass("active");
	
	$("p.graphic").hover(
		function () {
			$(this).next("h4").find("a").addClass("over");
		},
		function () {
			$(this).next("h4").find("a").removeClass("over");
		}
	);
	
	$("div#info h4").hover(
		function () {
			$(this).prev("p.graphic").find("img").addClass("over");
		},
		function () {
			$(this).prev("p.graphic").find("img").removeClass("over");
		}
	);
	
	$("a.mt").attr('href', function(){
		return "mailto:" + $(this).attr('href').replace(/.*emailgen\/([a-z0-9._%-]+)\+([a-z0-9._%-]+)\+([a-z.]+)/i, '$1' + '@' + '$2' + '.' + '$3').replace(/[a-zA-Z]/g, function(c){return String.fromCharCode((c<="Z"?90:122)>=(c=c.charCodeAt(0)+13)?c:c-26);});
	});
	
	if($('a.enlarge').length != 0){
		LazyLoad.js(['/includes/js/fancyzoom.min.js'], function () {
			$('a.enlarge').fancyZoom({scaleImg: true, closeOnClick: false});
		});
	}
	
	if($('#fblb').length != 0){
		LazyLoad.js(['http://static.ak.connect.facebook.com/connect.php/en_US'], function () {
			FB.init("7a5b3a2b603c101f0e532d8635ca2237", "http://opcc.codav.com/includes/js/xd_receiver.htm", { permsToRequestOnConnect : "publish_stream", });
		});
	}
	
	var cFfname = $('input[name=fname]'),
		cFlname = $('input[name=lname]'),
		cFemail = $('input[name=email]'),
		cFphone = $('input[name=phone]'),
		cFsubj1 = $('select[name=subject]'),
		cFmessg = $('textarea[name=message]'),
		cFpc = $('input[name=pc]'),
		cFpt = $('input[name=pt]'),
		cFsubmit = $('input[name=cfsubmit]'),
		cFfields = $([]).add(cFfname).add(cFlname).add(cFemail).add(cFphone).add(cFsubj1).add(cFmessg).add(cFpc).add(cFpt),
		formname = $('h1').text(),
		tips = $('#cf-validate'),
		domcphr = window.location.hostname;
	
	cFsubmit.val('');
	
	function eval_cb(obj){
		if($(obj).is(':checked')){
			return true;
		}
		else{
			return false;
		}
	}
	
	function updateTips(t) {
		tips.text(t);
	}

	function checkLength(o,n,min,max) {
		if ( o.val().length > max || o.val().length < min ) {
			o.addClass('ui-state-error');
			updateTips("Length of " + n + " must be between "+min+" and "+max+".");
			return false;
		} else {
			updateTips(" ");
			return true;
		}
	}

	function checkRegexp(o,regexp,n) {
		if ( !( regexp.test( o.val() ) ) ) {
			o.addClass('ui-state-error');
			updateTips(n);
			return false;
		} else {
			updateTips(" ");
			return true;
		}
	}
	
	$('input[type=submit]').click(function(){
		var isvalid = true;
		cFfields.removeClass('ui-state-error');
		isvalid = isvalid && checkLength(cFfname,"First Name",2,100);
		isvalid = isvalid && checkLength(cFlname,"Last Name",2,100);
		isvalid = isvalid && checkLength(cFemail,"Email",6,320);
		isvalid = isvalid && checkRegexp(cFemail,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"Email must be in format: email@domain.com");
		isvalid = isvalid && checkLength(cFmessg,"Message",2,5000);
		if (isvalid) {
			$(this).attr('disabled','disabled');
			$('div.contactform form').fadeTo('normal', 0.2);
			$('div.contactform').append('<div class="cf-loading"><strong>Sending...</strong><br /><img src="/images/ajax-loader.gif" /></div>');
			$.post("/includes/php/send.php",{
				sendDom: domcphr,
				senderName: cFfname.val()+' '+cFlname.val(),
				senderEmail: cFemail.val(),
				senderMesg: 'First Name: '+cFfname.val()+'\nLast Name: '+cFlname.val()+'\nEmail: '+cFemail.val()+'\nPhone: '+cFphone.val()+'\nSubject: '+cFsubj1.val()+'\nMessage: '+cFmessg.val()+'\nAdd to Prayer Chain: '+eval_cb(cFpc)+'\nHave Pastor Contact: '+eval_cb(cFpt),
				senderReply: 'Hello '+cFfname.val()+' '+cFlname.val()+',\n\nThanks for contacting Oak Park Christian Center. We have received your email and will get back to you as soon as possible.\n\nOak Park Christian Center\ninfo@opccag.org',
				senderSubj: formname+' form submit from opccag.org'
				},
				function(data){
					setTimeout(function() {
						$('div.contactform').html('<p><strong>Success!</strong><br />Your email was sent. Thank you for contacting Oak Park. We&rsquo;ll get back to you as soon as possible.</p>').fadeTo('fast',1);
					}, 2000);
				}
			);
		}
		return false;
	});

});

function strm_pub() {
	FB.ensureInit(function () {
		FB.Facebook.apiClient.pages_isFan(110320478981253, FB.Connect.get_loggedInUser(), function(result){
			if (result) {
				FB.Facebook.apiClient.users_hasAppPermission("publish_stream",function(has){
					if (has == 0) {
						FB.Connect.showPermissionDialog("publish_stream", function(granted){
							FB.Connect.streamPublish('', null, null, 110320478981253, 'Let us know what&rsquo;s up!');
						});
					}
					else {
						FB.Connect.streamPublish('', null, null, 110320478981253, 'Let us know what&rsquo;s up!');
					}
				});
			}
			else{
				$('#fblb').html('<fb:fan profile_id="110320478981253" stream="0" connections="0" width="200" height="66"></fb:fan>');
				FB.XFBML.Host.parseDomTree();
			}
		});
	});
}

Function.prototype.defaults = function(){
	var _f = this;
	var _a = Array(_f.length-arguments.length).concat(Array.prototype.slice.apply(arguments));
	return function(){
		return _f.apply(_f, Array.prototype.slice.apply(arguments).concat(_a.slice(arguments.length, _a.length)));
	}
}

var write_ytvid = function(c,y,w,h){
	var flash_version = "8.0.0",
		params = {
			allowScriptAccess: "always",
			allowFullScreen: true,
			wmode: "transparent"
		},
		attributes = {
			'id': c,
			'class': $('#'+c).attr('class')
		},
		yvidurl = "http://www.youtube.com/v/" + y + "?enablejsapi=1&playerapiid=ytplayer&fs=1";

	if (swfobject.hasFlashPlayerVersion(flash_version)){
		// has Flash
		swfobject.embedSWF(yvidurl, c, w, h, flash_version, null, null, params, attributes);
	}
}.defaults('', '', 425, 344);

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-16419786-1']);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_trackPageview']);
_gaq.push(['_trackPageLoadTime']);
(function() {
	var thisurl = document.location.href;
	if(thisurl.indexOf('opccag.org') > -1){
		var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
		ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
		var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
	}
})();
