var photographyData;
var photographyCurrent = 0;
var photographyTotal;


$("nav").show();

$("#link-photography, #link-about, #link-websites").attr("href", "javascript: void(0);");

$("#link-photography").bind('click', function(e){
	changePage("photography");
});

$("#link-about").bind('click', function(e){
	changePage("about");
});

$("#link-websites").bind('click', function(e){
	changePage("web");
});

$("#link-admin").bind('click', function(e){
	changePage("admin");
});

$("#link-add-content").bind('click', function(e){
	changePage("add-content");
});

$("#link-manage-content").bind('click', function(e){
	changePage("manage-content");
});

function changePage(page){
	if(!$("#prev-bar").is(":animated")){
	$("#photography-outer, #prev-bar, #next-bar, #login-box, #add-content, #manage-content, #about-me, #edit-content").fadeOut('500');
	setTimeout(function(){
		if(page == "photography"){
			loadPhotoGal(0);
		} else if (page == "web") {
			loadPhotoGal(2);		
		} else if (page == "about") {
			$("#about-me").fadeIn('500');
		} else if (page == "admin") {
			$("#login-box").fadeIn('500');
			$("#login-box input[name=user]").focus();
		} else if (page == "add-content") {
			$("#upload input[type=file], #upload input[type=text], #upload textarea ").val("");
			$("#add-content").fadeIn('500');
		} else if (page == "manage-content") {
			manageContent();
			$("#manage-content").fadeIn('500');
		} else if (page == "edit-content") {
			$("#edit-content").fadeIn('500');
		}
	}, 500);
	}
}

function loadPhotoGal(type){
//	if (navigator.appName == "Microsoft Internet Explorer"){
//		$("#photography-outer").fadeIn('500');
//	}
	$.ajax({                                      
      url: 'extras/getAllPhotos.php',                  //the script to call to get data          
      data: "type="+type,                        //you can insert url argumnets here to pass to api.php
                                       //for example "id=5&parent=6"
      dataType: 'json',                //data format      
      success: function(data)          //on recieve of reply
      {
		
		photographyData = data;
		photographyCurrent = 0;
		
		
			var img = new Image();
			$(img).load(function(){
				$("#photography, #photography figure").css("max-width", this.width+"px");
				$("#main-image").replaceWith(this);
				if (photographyData[photographyCurrent].description != ""){
					$("#photography figcaption").html(photographyData[photographyCurrent].description).show();
				} else {
					$("#photography figcaption").hide();
				}
				if (photographyData[photographyCurrent].title != ""){
					$("#photography #imgTitle").html(photographyData[photographyCurrent].title).show();
				} else {
					$("#photography #imgTitle").hide();
				}
				$("#photography-outer").stop().fadeIn('fast');
			}).attr("src", data[0].path+data[0].baseName+data[0].ext).attr("id", "main-image");
			
		
		
		$('#thumbs').empty();
		photographyTotal = 0;
		$.each(data, function(i, photo){
			var tblRow = '<li><img src="'+photo.path+photo.baseName+'-tiny'+photo.ext+'" id="'+ i +'" width="20" height="20" class="tiny"></li>';
			$(tblRow).appendTo("#thumbs");
			photographyTotal++;
		});
		
		$("#photography #thumbs .tiny").hover( function(e){
			var newthis = $(this);
            timer = setInterval(function() {
				clearInterval(timer);
				var squareUrl = $(newthis).attr("src").replace('tiny', 'square');
				
				var img = new Image();
				$(img).load(function(){
					$(newthis).parent().append('<ul class="temp"><li></li></ul>').find('.temp li').append(this).end().find('.temp').hide().fadeIn('500');
				}).attr('src', squareUrl);				
				
				}, 400);
		}, function(e){
			clearInterval(timer);
			$("#photography #thumbs").find('.temp').fadeOut('150', function(e){
				$(this).remove();
			});

		});
		
		$("#photography #thumbs .tiny").bind('click', function(e){
			photographyCurrent = $(this).attr("id");
			photographyChangeMain();
		});
		$("#next-bar").unbind('click').fadeIn('500').bind('click', function(e){
			if(photographyCurrent < (photographyTotal-1)){
			photographyCurrent++;
			} else {
				photographyCurrent = 0;
			}
			photographyChangeMain();
		});
		
		$("#prev-bar").unbind('click').fadeIn('500').bind('click', function(e){
			if(photographyCurrent > 0){
			photographyCurrent--;
			} else {
				photographyCurrent = (photographyTotal-1);
			}
			photographyChangeMain();
		});		
		
	  }
	  });}

function photographyChangeMain(){
	$("#main-image").fadeOut('500');
	$("#photography").fadeOut('500', function(){
		var img = new Image();
		$(img).load(function(){
			$("#photography, #photography figure").css("max-width", this.width+"px");
			$("#main-image").replaceWith(this);
			if (photographyData[photographyCurrent].description != ""){
				$("#photography figcaption").html(photographyData[photographyCurrent].description).show();
			} else {
				$("#photography figcaption").hide();
			}
			if (photographyData[photographyCurrent].title != ""){
				$("#photography #imgTitle").html(photographyData[photographyCurrent].title).show();
			} else {
				$("#photography #imgTitle").hide();
			}
			$("#photography").stop().fadeIn('500');
			$(this).hide().fadeIn('500');
		}).attr("src", photographyData[photographyCurrent].path+photographyData[photographyCurrent].baseName+photographyData[photographyCurrent].ext).attr("id", "main-image");
	});
			

};




var wallpaperData;
var wallpaperCurrent = 0;
var wallpaperTotal;

$.ajax({                                      
      url: 'extras/getAllPhotos.php',                  //the script to call to get data          
      data: "type=1",                        //you can insert url argumnets here to pass to api.php
                                       //for example "id=5&parent=6"
      dataType: 'json',                //data format      
      success: function(data)          //on recieve of reply
      {
		wallpaperData = data;
		wallpaperTotal = 0;
		$.each(data, function(i, wallpaper){
			wallpaperTotal++;
		});
		changeBG();
		
	  }
	});
	
var bgTimer;
function changeBG(){
	wallpaperCurrent=Math.floor(Math.random()*(wallpaperTotal));
	var img = new Image();
	$(img).load(function(){
		$("#background-image #two").replaceWith(this);
		 $(this).fadeIn(6000, function(e){
				$("#background-image #one").attr("src", $(this).attr("src"));
				clearTimeout(bgTimer);
				bgTimer = setTimeout(changeBG, 20000);
			});
	}).attr("src", wallpaperData[wallpaperCurrent].path+wallpaperData[wallpaperCurrent].baseName+wallpaperData[wallpaperCurrent].ext).attr("id", "two").hide();	
}

$("#login-submit").bind('click', function(e){
	loginVerify();
	e.preventDefault();
});

function loginVerify(){
	var user = $("#login-box input[name=user]").val();
	var pass;
	
	pass = $().crypt ({
		method: 'md5',
		source:	$("#login-box input[name=passw]").val()
	});
	$.ajax({                                      
      url: 'extras/login-check.php',                  //the script to call to get data          
      data: "user="+user+"&passw="+pass,                        //you can insert url argumnets here to pass to api.php
                                       //for example "id=5&parent=6"
      success: function(data)          //on recieve of reply
      {
		if(data != "error"){
			window.location="admin.php";
		} else {
			$("#login-box").fadeOut('500', function(){
				$("#login-box input[name=passw]").val("").focus();
				$("#login-box").fadeIn('500');
			});
		}
	  }
	});
}


var uem = "techbeast2k"
var hst = "yahoo"
$('.email').html('<a href="mailto:'+ uem + '@' + hst + '.com">' +uem + '@' + hst + '.com</a>');









