var thexml = '';
var cycleseconds = 4;
var allimages=new Array();

function imgcycle() {
	if(allimages.length > 1) {
		var bye = jQuery('#topimg img:visible');
		bye.addClass('bye');
		var next = (bye.next().size() > 0) ? bye.next() : bye.parent().children('img:first-child');
		next.show();
		bye.fadeOut('slow',function() {
			jQuery(this).removeClass('bye');
			setTimeout("imgcycle();",cycleseconds*1000);
		});
	}
}

function launchvideo(url) {
//	alert('you want to watch ' + url);
	jQuery('body').append('<a href="" id="closer" /><div id="video" />');
	jQuery('#closer').css({'width':jQuery('#rep').width(),'height':jQuery('#rep').height()}).fadeTo("fast",0.6,function() { jQuery(this).show(); });
	var wh = jQuery(window).height();
	var ww = jQuery(window).width();
	
	var vidpop = '<a href="" id="vclose">x</a><object width="480" height="274"><param name="allowfullscreen" value="true" />';
	vidpop += '<param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=7971353&amp;server=vimeo.com&amp;';
	vidpop += 'show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=B4CC27&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=';
	vidpop += '7971353&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=B4CC27&amp;fullscreen=1&amp;autoplay=1" ';
	vidpop += 'type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="480" height="274"></embed></object><a href="';
	vidpop += jQuery('#topnav .page-item-47 a').attr('href') + '" class="btn">Click Here to Contact the Cambridge School</a>';
	
	jQuery('#video').css({'position' : ((wh > 320 && ww > 480) ? 'fixed' : 'absolute'),'top':((wh > 320) ? (wh-320)/2 : 0),'left':((ww > 290) ? (ww-480)/2 : 0)})
	.append(vidpop);
	jQuery('#closer,#vclose').click(byevideo);
	return false;
}

function byevideo() {
	jQuery('#video,#closer').remove();
	return false;
}

jQuery(function() {
	jQuery("#sidebar .block.video a").click(function() {
		return launchvideo(jQuery(this).attr('href'));
	});
	jQuery("#sidebar .block.pages:not(.dl) .mid").children('ul').children('li')
		.bind('mouseenter',function() { jQuery(this).children('ul').show(); })
		.bind('mouseleave',function () {
			if(!(jQuery(this).hasClass('current_page_item') || jQuery(this).hasClass('current_page_parent'))) jQuery(this).children('ul').hide();
		}).trigger('mouseleave').children('ul').children('li').children('a')
		.bind('mouseenter',function() { jQuery(this).addClass('roll'); })
		.bind('mouseleave',function() { jQuery(this).removeClass('roll'); });
		
	if(thexml != '') {
		jQuery.ajax({
			type: "GET",
			url: thexml,
			dataType: "xml",
			success: function(xml) {
				jQuery('image',xml).each(function(i) {
					allimages[i] = jQuery(this).text();
				});
				jQuery('#topimg').empty();
				var randomstart = Math.floor(Math.random()*allimages.length);
				for(var j=0;j<allimages.length;j++) {
					jQuery('#topimg').append('<img src="'+imgbase+allimages[j]+'"'+(j!=randomstart ? ' style="display:none;"' : '')+' />');
				}
				setTimeout("imgcycle(); ",cycleseconds*1000); 
			}
		});
	}
	
	jQuery("#sidebar .block.join input:submit").hide().after('<a href="" class="btn" onclick="jQuery(this).parent().submit(); return false;">'+jQuery("#sidebar .block.join input:submit").val()+'</a>');
	
	jQuery('form.wpcf7-form input.txt').each(function() {
		jQuery(this).attr('title',jQuery(this).val()).bind('focus',function() {
			if(jQuery(this).val() == jQuery(this).attr('title')) jQuery(this).val('');
		}).bind('blur',function() {
			if(jQuery(this).val() == '') jQuery(this).val((jQuery(this).attr('name') == 'Church') ? jQuery(this).attr('title') : jQuery(this).attr('name'));
		});
	});
	jQuery('form.wpcf7-form textarea[name=inquiry]').attr('title','Inquiry : Please indicate the grade level of the children in your household for the coming Fall')
	.bind('focus',function() {
		if(jQuery(this).html() == 'Inquiry : Please indicate the grade level of the children in your household for the coming Fall') jQuery(this).html('');
	}).bind('blur',function() {
		if(jQuery(this).html() == '') jQuery(this).html(jQuery(this).attr('title'));
	});
	
	jQuery('#joinpop .closer, #joinhider').click(function() { jQuery('#joinpop, #joinhider').hide(); return false; });
	jQuery('#joinhider').css('height',jQuery('#rep').height());
	var whash = '' + window.location.hash;
	if(whash == '#newsletter') jQuery('#joinpop, #joinhider').show();
	
	jQuery('#sidebar .block.pages:not(.dl)').each(function() {
		jQuery(this).find('cufon').addClass('bye');
		jQuery(this).find('a cufon').removeClass('bye');
		jQuery(this).find('cufon.bye').remove();
	});
});
