$( document ).ready( function(){
	
	setSubNavPosition();
	
	$('#home-cols .divider:last').remove();
	$('img.tip').each(function(){
		var oldTitle = $(this).attr('title');
		var newTitle = oldTitle.replace(/\[blue\]/g,'<span>').replace(/\[\/blue\]/g,'</span>');
		$(this).attr('title',newTitle);
	});
	//tooltip();
	$("a[rel='lightbox']").colorbox();
	$(".thumb-list.badges a.lightbox").colorbox();
	check();
    
    $('.slider').cycle({ 
		fx:    'scrollDown', 
	    delay: 100,
		timeout: 6000,
	    cleartype: true,
		cleartypeNoBg: true
	});
	$('.blink')
		.focus(function(){
			if ( $(this).attr('value') == $(this).attr('title') ) {
				$(this).attr({ 'value': '' })
			}
		})
		.blur(function(){
			if ( $(this).attr('value') == '' ) {
				$(this).attr({ 'value': $(this).attr('title') })
			}
		})
	$('#navigation ul li').hover(function () {
		$(this).find('.dropdown:eq(0)').show();
	}, function () {
		$(this).find('.dropdown:eq(0)').hide();
	})
	$("img.tip").hover(function(){											  
		var caption = $(this).parents('body').find('.img-caption');
		caption.show();
		$(this).mousemove(function (e) {
			var caption_height = caption.height();
			var x = e.pageX - 90;
			var y = e.pageY - caption_height - 10;
			caption.css({'left': x, 'top': y});
		})
	}, function () {
		$(this).parents('body').find('.img-caption').hide();
	});
	$('.checks input, .radio-holder input, .custom-row input').customInput();
	choiceform_btn();
	$('.choice-form .radio-btn').live('click', function () {
		choiceform_btn();	
	});
	// Contact Form
	$('#give-form').submit(function(){
		var form = $(this);
		form.find('.required').parents('.field-bg:eq(0)').removeClass('field-error');
		var field, v, id, msg, t, field_holder;
		var alert_msg = $('.msg-alert');
		var error = false;
		form.find('.required').each(function(){
			field = $(this);
			field_holder = field.parents('.field-bg:eq(0)');
			v = $(this).val();
			t = $(this).attr('title');
			id = $(this).attr('id');
			if( $(this).hasClass('valid-email') ){
				if( /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(v) == false ) {
					error = true;
				}
			}else {
				if( v == '' || v == t ){
					error = true;
				}
			}
			if( error ) {
				field.addClass('field-error');
				field.parent().addClass('error-bg');
				var field_value = field.parent().find('span').text();
				/*field.attr('title', '* Please enter your ' + field_value + '!');
				field.attr('value', '* Please enter your ' + field_value + '!');*/
				field.parent().next().addClass('label-error');
				//error_fields.push( field );
			}
		});
		if( !error ) {
			form.find('.required').removeClass('field-error');
			var data = {}
			/*
			form.find('.text-field').each(function(){
				data[ $(this).attr('name') ] = $(this).val();
			});
			form.find('.text-field').each(function(){
				$(this).val( $(this).attr('title') );
			});
			*/			
			$.post('send.php', data, function(){
				alert_msg.fadeOut();
				form.find('.msg-thanks').fadeIn(function(){
					/*$('#contact-message').val( $('#contact-message').attr('title') );*/
					window.setTimeout(function(){
						form.find('.msg-thanks').fadeOut();
					}, 5000);
				});
			});
		}
		return false;
	}); 
	$('.share-holder a.share-btn').live('click', function () {
		$(this).toggleClass('active');
		$(this).parent('.share-holder').find('.share-lightbox').show();
		return false
	})
	$('.share-holder .share-lightbox').mouseleave(function () {
		$(this).hide();
	})
	$('.tabbed-box .tab-nav li a').live('click', function () {
		var href = $(this).attr('href');
		var tabs = $(this).parents('.tabbed-box:eq(0)').find('.tabs-content:eq(0)');
		$(this).parents('ul:eq(0)').find('a').removeClass('active');
		$(this).addClass('active');
		tabs.find('.tab-cnt').hide();
		$(this).parents('.tabbed-box').find(href).show();
		return false;
	})
	$('.dot-info').each(function(){
		var boxHeight = $(this).height();
		var boxAdjust = 12;
		var boxPosTop = boxHeight/2 - boxAdjust;
		var arrowHeight = 33;
		var arrowPosTop = boxHeight/2 - arrowHeight;
		$(this).css({'top': -boxPosTop}).find('.arrow').css({'top': arrowPosTop});
	});
	$('.dot').hover(function () {
		$(this).find('.dot-info').show();
	}, function () {
		$(this).find('.dot-info').hide();
	})
	$('a[rel*=facebox]').facebox();
	if($.browser.msie && $.browser.version==6) {
		DD_belatedPNG.fix('h1#logo a, #navigation ul li a.active small, #navigation ul li a.active span.left, #navigation ul li a.active span.right, #navigation ul li .dropdown .top, #navigation ul li .dropdown .bottom, #navigation ul li .dropdown .shadow, #navigation ul li .dropdown ul li .second-drop .top, #hand, #tooltip-row, #tooltip a, .img-caption .top, .img-caption .bottom, .img-caption .cnt, .section-image .frame, .thumb-list .thumb .img, .splash-form .field-row .field-bg, .choice-form .box-holder .box, .village-form .box-holder .box .adopted, .give-form-holder .step .top, .give-form-holder .step .bottom, .give-form-holder .step .cnt, .progress-bar .bar-frame, .globe .globe-info p, .globe .dot-link, .calculations .box, .calculations .plus, .calculations .equal, .dot-info .top, .dot-info .bottom, .dot-info .cnt, .dot-info .right-ar, .dot-info .left-ar, .dot-info .info p.more-btn a, #facebox .popup-holder , .popup-holder .close-btn, .popup-holder .village-info .image span.adopted');
	};
	
	
	var i=1;
	$('#home-cols .col').each(function(){
		$(this).addClass('col' + i);
		i++;
	});
	$('.choice-form .next-btn').live('click',function(){
		var choice = $('input[name=choose]:checked');
		var aboutChoice = choice.val();
		var skipAboutChoice = choice.attr('rel');
		if(choice.hasClass('show')){
			window.location = aboutChoice;
		} else if(choice.hasClass('skip')){
			window.location = skipAboutChoice;
		};
		return false;
	});
	$('div.side-links').each(function(){
		if($(this).children().length < 1){
			$(this).hide();
		};
	});
	$('.thumb-list .thumb .img').hover(function () {
		var embed = $(this).parents('.thumb:eq(0)').find('.copy').html();
		$('input.embedCode').attr('value', embed);
		$(this).parent().find('.frame-link').show();
	}, function () {
		$(this).parent().find('.frame-link').hide();
	})
	
	$('.thumb-list .thumb-info .copy').hover(function () {
		var text = $(this).html()
		$('input.embedCode').attr('value', text);
	});
	$('a[href$="#top"]').click(function(){
		back2top();
		return false;
	});
});
function choiceform_btn() {
	if (!$("input.radio-btn:checked").val()) {
       return false;
    } else {
    	$('.choice-form').find('input.next-btn').removeAttr('disabled').addClass('next-active');
    }
}
function check() {
	var href = $('.tabbed-box .tab-nav li a.active').attr('href');
	$(href).show();
};
function check_tweets() {
	$('.tweets-list .bottom ul.tweet_list').innerfade({ 
		speed: 750,
		timeout: 5000,
		containerheight: '126px'
	}); 
}
function mailchimpSuccess(resp){
	$('form').replaceWith('<h2>Thank you!</h2><p>Confirm your subscription in the email that was sent to you.</p>');
};
$(document).bind('reveal.facebox', function() {
	$( document ).ready( function(){	
		var href_link = $('.popup-holder .tabbed-box .tab-nav li a.active').attr('href');
		var href = href_link.split('#');
		$('.popup-holder').find('#' + href[1]).show();
		$('.tabbed-box .tab-nav li a').click(function () {
			var href_link = $(this).attr('href');
			var href = href_link.split('#');
			var tabs = $(this).parents('.tabbed-box:eq(0)').find('.tabs-content:eq(0)');
			$(this).parents('ul:eq(0)').find('a').removeClass('active');
			$(this).addClass('active');
			tabs.find('.tab-cnt').hide();
			$(this).parents('.tabbed-box').find('#' + href[1]).show();
			return false;
		})
		imagePreview();
	});
});

var clip = null;
function init() {
	// setup single ZeroClipboard object for all our elements
	clip = new ZeroClipboard.Client();
	clip.setHandCursor( true );

	// assign a common mouseover function for all elements using jQuery
	$('div.copy').mouseover( function() {
		// set the clip text to our innerHTML
		clip.setText( this.innerHTML );
	
		// reposition the movie over our element
		// or create it if this is the first time
		if (clip.div) {
			clip.receiveEvent('mouseout', null);
			clip.reposition(this);
		}
		else clip.glue(this);
	
		// gotta force these events due to the Flash movie
		// moving all around. This insures the CSS effects
		// are properly updated.
		clip.receiveEvent('mouseover', null);
	} );
	clip.destroy();
} 


function setSubNavPosition() {
	var active = $('.side-links ul li.active');
	var child = active.parents('ul:eq(0)').siblings();
	if (child.length) {
		child.appendTo(active);
	};
}

(function($){
	function recalculate_lives() {
		var baseDate = new Date("July 14, 2011 15:00:00"); //Reset on August 10, 2011 at 12:34 pm
		var baseline = baseDate.getTime();
		//var baseline = 1304010893474;
		var date = new Date();
		var time_passed = date.getTime() - baseline;
		var lives = addCommas(Math.ceil(120000 + time_passed * 0.0001));
		$('#estimated-lives').html(lives);
	}

	setInterval(function() {
		recalculate_lives();
	}, 1000);
	recalculate_lives();
})(jQuery)

function addCommas(nStr) {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
	x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function back2top(){
	$('html, body').animate({scrollTop:0},'slow');
}
