

/*  =============
	Get querystring from URL
	============= */
function  getQuerystring(key, url, default_)
{
  if (default_==null) default_="";
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  if(url==null){
	  url = window.location.href;
  }
  var qs = regex.exec(url);

  if(qs == null)
    return default_;
  else
    return qs[1];
}

/*  ==============
	DOM Ready
	============= */

$(function() {

/* ===== baner ===== */		
	 $('#banner div').cycle({
		fx: 'fade',
		timeout: 8000
	});
	
/* ===== projects ===== */	
	$("#projects div.carousel").jCarouselLite({
		visible: 1,
		scroll: 1,
		btnNext: "#projects .next",
		btnPrev: "#projects .prev",
		auto: 3000,
      speed: 800
	});
	
/* ===== video ===== */	
	$("#video div.carousel").jCarouselLite({
		visible: 1,
		scroll: 1,
		btnNext: "#video .next",
		btnPrev: "#video .prev",
      speed: 500
	});	
	
/* ===== stories ===== */
	$("#stories div.carousel").jCarouselLite({
		visible: 1,
		scroll: 1,
		auto: true,
		auto: 3000,
      speed: 800
		
	});	
	
/* ===== YOU TUBE IMAGE ===== */
	$("a.iframe").colorbox({iframe:true, innerWidth:'90%', innerHeight:'90%'});
	$("a.youtube").colorbox({iframe:true, innerWidth:480, innerHeight:385});
	$('a.youtube').each(function(){
		 ytid = getQuerystring('v',$(this).attr('href'));
		 if(ytid){
		 	$(this).attr('href','http://www.youtube.com/v/'+ytid).html('<img src="http://img.youtube.com/vi/'+ytid+'/2.jpg" width="230" height ="150" /><span class="play"></span>');
		 } 
	})
	
/* ===== DONATE ===== */
	$('#donate .open').live("click", function () {
		$(this).removeClass('open').addClass('close');									 
		$('#container').animate({opacity: "0.1"}, 600 );
      $(this).parent().animate({ 
        marginLeft: "-800px"
      }, 1200 );
	  
    });
	
	$('#donate .close').live("click", function () {
		$(this).removeClass('close').addClass('open');	
		$(this).parent().animate({ 
        marginLeft: "-29px"
      }, 1200 );
		$('#container').animate({opacity: "1"}, 1000 );
    });	
	
	
/* ===== Clear on focus ===== */
	var clearMePrevious = "";
	// clear input on focus
	$('.clearFocus').focus(function(){
		if($(this).val()==$(this).attr('title')){
			clearMePrevious = $(this).val();
			$(this).val("");
		}
	});
	// if field is empty afterward, add text again
	$('.clearFocus').blur(function(){
		if($(this).val()==""){
			$(this).val(clearMePrevious);
		}
	});	
	
/* ===== Contact Us ===== */
	$("#footer form").submit(function() {
		frm = $(this);
		$.post(frm.attr("action"), { email: frm.find("input[name=email]").val(), subject: frm.find("input[name=subject]").val(), message: frm.find("textarea[name=message]").val()},
		function(data){
			$(frm).parent().find("div.msg").html(data);
   		});
		return false
	})		
		
});
