$(document).ready(function() {

	var url = window.location.href.split("/");
	url3 = url[3];
	url30 = (url3.split("?"))[0];
	$('#menu-main-nav li a, #menu-top-nav li a').each(function(){
		nav_href = $(this).attr('href');
		if (nav_href.indexOf(url30) != -1 && url30 != '') {
			$(this).parent('li').addClass('current-menu-item');
		}
	});

	// blog post nav
	if ($('.single-post').length > 0) {

		parent_landing = $('ul.breadcrumb li:eq(1)').text();
		$('ul#menu-main-nav li a:contains("'+parent_landing+'")').parent('li').addClass('current-menu-item');

		parent_page = $('ul.breadcrumb li.last').prev('li').text();
		if ($('ul.breadcrumb li').length = 4) {
			$('#sidebar-left .menu li a:contains("'+parent_page+'")').parent('li').addClass('current-menu-item');
		}
	}


	// scrollable
	$(".carousel .scrollable").scrollable({circular: true}).navigator();
	$(".caring-in-numbers .scrollable").scrollable({circular: true});
	$(".testimonials-box .scrollable").scrollable({circular: true}).navigator();

	if ($('#programs-scrollable').length > 0) {
		$(".programs-landing-top .scrollable").scrollable({circular: true, itemsPerFrame: '3'}).autoscroll({interval: 7000, autopause: false}).navigator();

		$(".programs-landing-top .scrollable .item").hover(
			function(){
				$(".feature-program-titling").stop(true, true);
				$(".feature-program-titling").css({'top': '332px'});
				$(this).find('.feature-program-titling').animate({'top': '230px'}, 300);
			},
			function(){
				$(".feature-program-titling").animate({'top': '332px'}, 300);
			}
		);

		var programs_scrollable = jQuery("#programs-scrollable").data("scrollable");
		programs_scrollable.onSeek(function(event, index) {
			$(".feature-program-titling").animate({'top': '332px'}, 300);
		});

	}


	// main gallery
	$("#main-gallery .scrollable").scrollable();
	$('#main-gallery .items img').css('opacity', 0.5);
	$('#main-gallery .items img.active').css('opacity', 1);

	$("#main-gallery .items img").live('click', function() {

		// see if same thumb is being clicked
		if ($(this).hasClass("active")) { return; }

		// thumbnail 'active' state
		$('#main-gallery .items img').css('opacity', 0.5);
		$('.thumb-mask').hide();
		$(this).css('opacity', 1);
		$(this).next('.thumb-mask').show();

		// calclulate large image's URL based on the thumbnail URL (flickr specific)
		var url = $(this).attr("src").replace("_s", "_b");

		// get handle to element that wraps the image and make it semi-transparent
		var wrap = $("#image_wrap").fadeTo("medium", 0.5);

		// the large image from www.flickr.com
		var img = new Image();


		// call this function after it's loaded
		img.onload = function() {

			// make wrapper fully visible
			wrap.fadeTo("fast", 1);

			// change the image
			wrap.find("img").attr("src", url);

		};

		// begin loading the image from www.flickr.com
		img.src = url;

		// activate item
		$(".items img").removeClass("active");
		$(this).addClass("active");

	// when page loads simulate a "click" on the first image
	}).filter(":first").click();


	// load new gallery
	$('.all-galleries li a').click(function(){
		photos_page = $(this).attr('href');
		$('#main-gallery').html('<img src="/wp-includes/js/thickbox/loadingAnimation.gif" style="margin: 200px 0 0 366px" />');
		$('#main-gallery').load(photos_page, function(){
			$("#main-gallery .items .thumb-wrap:first img").click();
		});
		$(document).scrollTop(270);
		return false;
	});


	// programs tabs
	$('.programs-nav ul li a').click(function(){
		tab_index = $('.programs-nav ul li').index($(this).parent('li'));
		$('.programs-nav ul li a').removeClass('active');
		$(this).addClass('active');
		$('.programs-main .programs-tab-content').hide();
		$('.programs-main .programs-tab-content:eq('+tab_index+')').show();
		return false;
	});


	// forms initial value

	$('#searchform input#s').data('initial', $('#searchform input#s').val());
	$('#searchform input#s').focusin(function(){
		if ($(this).val() == $(this).data('initial')) {
			$(this).val('');
		}
	});
	$('#searchform input#s').focusout(function(){
		if ($(this).val() == '') {
			$(this).val($(this).data('initial'));
		}
	});
	$('#searchform .reset').click(function(){
		$('#searchform input#s').val('');
	});

	$('.newsletter-signup input#email').data('initial', $('.newsletter-signup input#email').val());
	$('.newsletter-signup input#email').focusin(function(){
		if ($(this).val() == $(this).data('initial')) {
			$(this).val('');
		}
	});
	$('.newsletter-signup input#email').focusout(function(){
		if ($(this).val() == '') {
			$(this).val($(this).data('initial'));
		}
	});

	$('.footer-wrapper .newsletter-signup input#email').data('initial', $('.footer-wrapper .newsletter-signup input#email').val());
	$('.footer-wrapper .newsletter-signup input#email').focusin(function(){
		if ($(this).val() == $(this).data('initial')) {
			$(this).val('');
		}
	});
	$('.footer-wrapper .newsletter-signup input#email').focusout(function(){
		if ($(this).val() == '') {
			$(this).val($(this).data('initial'));
		}
	});

	//fb wall module
	$('#fb-wall').fbWall({id:'15894647497',accessToken:'206158599425293|341b453e941ca71fa77ecdb9.1-1028862273|7Mp_ITfnx2Gy7dkw_6vpW5PzyD8',showGuestEntries:false,showComments:false,max:5,timeConversion:24});

	//happening now title
	$('#fb-wall').ajaxComplete(function(){
		fb_message = $('#fb-wall .fb-wall-box-first .fb-wall-message').html();
		if (fb_message != null) {
			fb_message = fb_message.replace(/<a(.*)<\/a>/g, '');
			fb_message = fb_message.replace(/<A(.*)<\/A>/g, '');//ie
			fb_message = fb_message.substring(0, 50);
			fb_message = fb_message.replace(/\s+\S+$/, '');//trim last word
			$('.happening-now h3 a').html(fb_message + '...<span>&nbsp;&raquo;</span>');
		}
	});


	//animation
	function cloud_1_animation(){
		$('.sprite-cloud-1')
			.css({ 'left': -150 })
			.animate({ 'left': bw+50 }, 120000, function(){
				setTimeout(cloud_1_animation, 1000);
			});
	}

	function cloud_2_animation(){
		$('.sprite-cloud-2')
			.css({ 'left': -150 })
			.animate({ 'left': bw+50 }, 130000, function(){
				setTimeout(cloud_2_animation, 2000);
			});
	}

	function cloud_3_animation(){
		$('.sprite-cloud-3')
			.css({ 'left': -150 })
			.animate({ 'left': bw+50 }, 125000, function(){
				setTimeout(cloud_3_animation, 4000);
			});
	}

	var bw = $('body').width();
	$('body').prepend('<div class="animation-frame">');
	$('.animation-frame')
		.append('<div class="sprite-cloud-1"><img src="/wp-content/themes/cambodia/images/cloud1.png" alt="" /></div>')
		.append('<div class="sprite-cloud-2"><img src="/wp-content/themes/cambodia/images/cloud2.png" alt="" /></div>')
		.append('<div class="sprite-cloud-3"><img src="/wp-content/themes/cambodia/images/cloud2.png" alt="" /></div>')
		.css('width', bw);

	$('.sprite-cloud-1 img').load(function(){
		cloud_1_animation();
	});
	$('.sprite-cloud-2 img').load(function(){
		cloud_2_animation();
		setTimeout(cloud_3_animation, 40000);
	});

});
