function rotateImage(){

		$img_number = pad( Math.floor((20-1)*Math.random()) + 1);
		var $img = "cap_home_"+$img_number+".jpg";

		var image = document.createElement('img');
		image.src = "/images/"+$img;
		image.onload = function() {
		
			var image2 = document.createElement('img');
			image2.src = "/images/blurred/"+$img;
			image2.onload = function() {
				
				$('.body_bg').fadeIn(3000,function(){
					$('.body_bg').css('background-image', 'url( /images/' + $img + ')');
					$('.body_bg').css(' background-position', 'top left');
				});
				
				
				$('.body_bg2').fadeOut(3000,function(){
					$('.body_bg2').css('background-image', 'url( /images/' + $img + ')');
					$('.body_bg2').css(' background-position', 'top left');
					$('.body_bg2').fadeIn();
				});
				
				
					/*
					$('.body_bg').fadeTo('slow', 0, function()
					{
						$('.body_bg').css('background-image', 'url( /images/' + $img + ')');
						$('.body_bg2').css('background-image', 'url( /images/' + $img + ')');
					}).delay(500).fadeTo('slow', 1);
					*/
					
					/*$('.bg-wrap').fadeTo(1500, 0, function()
					{
						//$('.bg-wrap').css('background-image', 'url( /images/blurred/' + $img + ')');
					}).fadeTo(1500, 1);*/
					
					setTimeout( "rotateImage()", 8 * 1000);
				
				
				};
			
		};		
		
}

function pad (str) {
  return str.toString().length < 2 ? "0" + str : str;
}

function getImage(url) {        
    var image = document.createElement('img'); // new Image(1, 1); 
    image.src = url;
    image.onload = function() {};
}

var $img_number = pad( Math.floor((20-1)*Math.random()) + 1);

function callheight(){
	get_height = $(document).height()-115;
	//alert(get_height);
	$(".two .box").css('height',get_height+'px');
	$("#slider").css('height',get_height+'px');
	$(".three .box").css('height',get_height+'px');	
}

$(document).ready(function(){


$(".loading").bind("ajaxStart", function(){
    $(this).fadeIn('fast');
}).bind("ajaxStop", function(){
    $(this).fadeOut('fast');
});


if ( $(".loginMessage .error").text() !== "" ){
	$('#login').slideDown();
}

$('.loginLoginForm').bind("submit", function(){

	    $.post( "/login", $(this).serialize(),function( data2 ) {

	    var status = $( data2 ).find( '.status' );
	    console.info( $(status).text() );
	    
	    if ( $(status).text() == "Loggedin" ){
		$("#teachers").unbind('click');
		$("#teachers").attr('href', "#!/teachers" );
		$("#teachers").trigger('click');
	    }else{
	    
		$("#teachers").unbind('click');
		$("#teachers").click(function(e){
			$('#login').slideToggle();
			e.preventDefault();
		});
	    }
	    
		$('.loginLoginForm').html( data2 );
	});
  	e.preventDefault();
	return false; 
});


callheight();
	$(window).resize(function(){
		callheight();
    });
	$(window).scroll(function(){
		callheight();
    });



rotateImage();

// teachers
$("#teachers").click(function(e){
	
	$('#login').slideToggle();
	e.preventDefault();
});


//Modify by Muthukumar.G for X button issue

$('.two .x').click( function(e){
	navigationTwo(e);
    $(".two").css("display", "none");
    $(".three").css("display", "none");

});

$('.three .x').click( function(e){
	navigationThree(e);
	$(".three").css("display", "none");	
});

$('.one .header a').click( function(e){
	$(".two").css("display", "block");	
	$(".three").css("display", "none");	

});


function navigationTwo(e){
	location.href = "/#!/home";	
	e.preventDefault ? e.preventDefault() : e.returnValue = false;
}

function navigationThree(e){
	location.href = $(".two nav li.selected a:first").attr('href');	
	e.preventDefault ? e.preventDefault() : e.returnValue = false;
}



//End

function stripTrailingSlash(str) {
    if(str.substr(-1) == '/') {
        return str.substr(0, str.length - 1);
    }
    return str;
}

function select_links(){

	var base_url = window.location.hash;

	var url_primary = "";
	var url_secondary = "";
	var url_tertiary = "";

	var urls = base_url.split('/');

	if(urls[1]){
		
		url_primary = "/" + urls[1];
	}
	if(urls[2]){
		
		url_secondary = url_primary + "/" + urls[2];
	}
	if(urls[3]){
		
		url_tertiary = url_secondary + "/" + urls[3];
	}

	// select proper link
	$('nav a').each(function(){
		
		var href = $(this).attr('href');
		if(
			href == "/#!" + url_primary ||
			href == "/#!" + url_secondary ||
			href == "/#!" + url_tertiary		
		)
			$(this).parent().addClass('selected');
	});	
}

Path.root("#!/home");
Path.map("#!/:primary(/:secondary)(/:tertiary)").to(function(){

	var url = "";
	var menu_two = false;
	var copy_two = false;
	var menu_three = false;
	var copy_three = false;

	if(this.params['primary']){
		
		menu_two = true;
		copy_two = true;
	}
	if(this.params['tertiary']){
		
		menu_three = true;
		copy_three = true;
	}

	// clear all selected links
	$('nav ul>li.selected').removeClass('selected');

	// clear all content areas
//$('#menu-two').html('');
	$('#copy-two').html('');
	$('#menu-three').html('');
	$('#copy-three').html('');

	// home
	if(this.params['primary'] == 'home'){
		menu_two = false;
		copy_two = false;
		menu_three = false;
		copy_three = false;
	}

	// get menu-two
if(false){ //menu_two
		
		var menu_two_url = "/"+this.params['primary'];
		$.ajax({
			url: menu_two_url,
			dataType: 'html',
			success: function(data){

				if(data){
					data = $(data).filter(".menu_two").html();
					$('#menu-two').html( $(data) );	

					$("#menu-two").find('a').each(function(){
$(this).attr('href', function(){
    str= $(this).attr('href');
	if(str.substr(-1) == '/') {
        str = str.substr(0, str.length - 1);
    }
		return "/#!/" + str;
});
					});

					$('.two').show();
					$('section.two nav.header').removeClass('off');

				}
				else{
					$('section.two nav.header').addClass('off');
				}
				select_links();
			}
		});
	}
	// get copy-two
	if(copy_two){
		
		var secondary = this.params['secondary'] ? this.params['secondary'] : '' ;
		
var copy_two_url = "/"+this.params['primary']+"/"+secondary+"";
//console.log("copy_two_url: "+copy_two_url);
		$.ajax({
			url: copy_two_url,
			dataType: "html",
			success: function(data){

var menu = $( $(data).filter(".site_menu")).find('ul ul').html();
//var menu = $(data).filter(".site_menu").html();
//alert( menu );
if(menu){

//var menu = $(data).filter(".menu_two");
//alert( "-"+$(menu).html().trim()+"-" );

$('#menu-two').html( $(menu) ).wrap("<ul />");	

					$("#menu-two").find('a').each(function(){
$(this).attr('href', function(){
    str= $(this).attr('href');
	if(str.substr(-1) == '/') {
        str = str.substr(0, str.length - 1);
    }
		return "/#!/" + str;
});
					});

					$('.two').show();
					$('section.two nav.header').removeClass('off');

				}
				else{
					$('#menu-two').html('');
					$('section.two nav.header').addClass('off');
				}
select_links();


//alert("Nav response: " + $(data).filter(".content").html()  );
				$('#copy-two').append( $(data).filter(".content") );
//console.info( "Aloha: "+$('#alohaContent').html() );
			},
			complete: function(){
			//alert(typeof $('#menu-two a:first').attr('href'));  && typeof $('#menu-two a:first').attr('href') != "undefined"
//console.info("Complete");
//console.info( "Aloha2: "+$('#alohaContent').is(":empty") );
if ( $('#alohaContent').html() == ""  && typeof $('#menu-two a:first').attr('href') != "undefined" ){
					window.location = $('#menu-two a:first').attr('href');
				}
$('#copy-two').trigger("content_loaded");
			}

		});



	}

	// get menu-three
	if(menu_three){
		
		var menu_three_url = "/"+this.params['secondary'];
		$.ajax({
			url: menu_three_url,
			success: function(data){

				if(data){
					$('#menu-three').html( $(data).filter(".menu").html() );
					$('section.three nav.header').removeClass('off');
				}
				else{
					if($('section.two nav.header').hasClass('off'))
						$('section.three nav.header').addClass('off');
				}
				select_links();

			}
		});
	}
	// get copy-three
	if(copy_three){
		$(".three").css("display","block");		
		var copy_three_url = "/"+this.params['primary']+"/"+this.params['secondary']+"/"+this.params['tertiary']+"";
		$.ajax({
			url: copy_three_url,
			dataType: 'html',
			success: function(data){
//$('#copy-three').html( $(data).filter(".content").html() );
$('#copy-three').append( $(data).filter(".content") );
$('#copy-three').append( $(data).filter("script")   );
$('#copy-three').trigger("content_loaded");
			}
		});
	}

	var margin_right = '-870px';
	if(menu_two || copy_two){
		margin_right = '-470px';
	}
	if(menu_three || copy_three){
		margin_right = '-44px';
	}
	
	$('#slider').animate({
		marginRight: margin_right,
	});
		
	select_links();

});
Path.listen();

/*
$('.two .x').click(function(event){
	$('.two').hide();
	$('.two').animate({width:'0'},350, function(){$(this).hide()});
	if ($(".three").is(":visible")) 
		$('.three').hide();
		$('.three').animate({width:'0'},350, function(){$(this).hide()});

	event.preventDefault()
});
$('.three .x').click(function(event){
	$('.three').hide();
//	$('.three').animate({width:'0'},350, function(){$(this).hide()});

	event.preventDefault()
});
*/


// placeholder
if(!Modernizr.input.placeholder){

	$('[placeholder]').focus(function() {
	  var input = $(this);
	  if (input.val() == input.attr('placeholder')) {
		input.val('');
		input.removeClass('placeholder');
	  }
	}).blur(function() {
	  var input = $(this);
	  if (input.val() == '' || input.val() == input.attr('placeholder')) {
		input.addClass('placeholder');
		input.val(input.attr('placeholder'));
	  }
	}).blur();
	$('[placeholder]').parents('form').submit(function() {
	  $(this).find('[placeholder]').each(function() {
		var input = $(this);
		if (input.val() == input.attr('placeholder')) {
		  input.val('');
		}
	  })
	});

}



   $(".donate_button").live("click", function() {

	$(".donate_form").attr("target", "donate-frame");
		$( "#dialog-donate" ).dialog({
			resizable: true,
			height: $(document).height(),
			width: "640px",
			modal: true,
			buttons: {
				Close: function() {
					$( this ).dialog( "close" );
				}
			}
		});
	});




});
