// JavaScript Document

function getText(field,target){
  $.get('includes/_getText.php',
				{ field:field },
				function(data){
					$(target).html(data);	
				}
	);				
}
function getPiercerInfo(){ // Retrieves the database info (id/bio/picture) for all artists whose type is set to piercer 
  $.get('includes/_getPiercerInfo.php',
				{},
				function(data){
					$.each(data,function(x,obj){
						$('<div>').addClass('artist').appendTo('#leftCol');	
						$('<h2>').addClass('border').appendTo('.artist:last-child');
						$('<img>').attr('src','images/artists/names/'+obj.name).appendTo('.artist:last-child h2.border');
						$('<div>').attr('id','artistImgWrap').appendTo('.artist:last-child');
						$('<img>').attr({ src:'images/artists/'+obj.picture, id:'artistImg' }).appendTo('.artist:last-child #artistImgWrap');
						$('<img>').attr('src','images/artistBgBottom.jpg').appendTo('.artist:last-child');
						$('<p>').addClass('artistBio').html(obj.text).appendTo('.artist:last-child');
						$('<a>').addClass('artistMore').attr('href','artist.php?id='+obj.id).appendTo('.artist:last-child');
					});
				},
	'json');
}
function loadAlbum(artistID,album){ // Retrieves the img gallery from the database for the corresponding artist and displays it
  $('#thumbsContainer').empty();
  $.get('includes/_getAlbum.php',
		{ artistID: artistID, album: album },
		function(data){
		  $.each(data,function(x,obj){
		    $('<img>').addClass('thumb').attr({'src':'images/artists/'+artistID+'/thumbs/'+obj.image}).appendTo('#thumbsContainer');
				$('.thumb:last-child').click(function(){
					$('#imgTitle, #imgCaption').stop('true','true');									
					$('#viewer').attr('src','../images/artists/'+artistID+'/'+obj.image);									
					$('#imgTitle, #imgCaption').fadeOut('slow',function(){								
						$('#imgTitle').text(obj.name);
						$('#imgCaption').text(obj.caption);
						$(this).fadeIn('slow');
					});    
				});
		  });
		  $('.thumb:first-child').click();
		},
  'json');
}

function getPosts(){ // Retrieves news items from the DB and displays them
  $.get('includes/_getPosts.php',
				{},
				function(data){
					$.each(data, function(x,obj){
					  $('<div>').addClass('post').appendTo('#news');
						$('<span>').addClass('postTitle').text(obj.title).appendTo('.post:last-child');
						$('<span>').addClass('postDate').text('posted: '+obj.date).appendTo('.post:last-child');
						$('<p>').addClass('postBody').html(obj.text).appendTo('.post:last-child');
						$('<span>').addClass('postMore').text('more').appendTo('.post:last-child p').click(function(){
							$('#overlay').fadeIn();
							getPost(obj.id);																																													
						});
					});
				},
	'json');
}
function getPost(id){ // Retrieves post from the DB that matches the supplied id
	$.get('includes/_getPost.php',
				{ id:id },
				function(data){
					$('#box p').html(data.text);
					$('#box .postTitle').text(data.title);
					$('#box .postDate').text(data.date);
					$('#box').show();
					center('#box');
				},
  'json');
}

function getArtistInfo(artistID){ // Retrieves the artist information (bio/pic) from the database and displays them
  $.get('includes/_getArtistInfo.php',
		{ id:artistID },
		function(data){
		  $('#artistName').attr('src','images/artists/names/'+data.name);	
		  $('#bioText').html(data.bio);
			$('<img>').attr({ src:'images/artists/'+data.picture, id:'artistImg' }).appendTo('#artistImgWrap');
			if(data.type == 'piercer'){
				$('#viewerNav #nav li:contains("Tattoo")').text('Piercing');
				$('#viewerNav #nav li:contains("Art")').text('Life');
			}
		},
  'json');
}
function center(element){
	try{
		element = $(element);
	}catch(e){
		return;
	}

	var my_width  = 0;
	var my_height = 0;

	if ( typeof( window.innerWidth ) == 'number' ){
		my_width  = window.innerWidth;
		my_height = window.innerHeight;
	}else if ( document.documentElement && 
			 ( document.documentElement.clientWidth ||
			   document.documentElement.clientHeight ) ){
		my_width  = document.documentElement.clientWidth;
		my_height = document.documentElement.clientHeight;
	}
	else if ( document.body && 
			( document.body.clientWidth || document.body.clientHeight ) ){
		my_width  = document.body.clientWidth;
		my_height = document.body.clientHeight;
	}

	element.css("position","absolute");
	element.css("z-index",99);

	var scrollY = 0;

	if ( document.documentElement && document.documentElement.scrollTop ){
		scrollY = document.documentElement.scrollTop;
	}else if ( document.body && document.body.scrollTop ){
		scrollY = document.body.scrollTop;
	}else if ( window.pageYOffset ){
		scrollY = window.pageYOffset;
	}else if ( window.scrollY ){
		scrollY = window.scrollY;
	}
	
	var setX = ( my_width  - element.width()  ) / 2;
	var setY = ( my_height - element.height() ) / 2 + scrollY;

	setX = ( setX < 0 ) ? 0 : setX;
	setY = ( setY < 0 ) ? 0 : setY;
	
	var newX = setX + "px";
	var newY = setY + "px";
	
	element.css("left",setX);
	element.css("top",setY);
	element.css("display","block");
}
// Admin Scripts ##########################################################################

function getBio(artistID){ // Retrieves bio from the database according to the artistID
  $.get('includes/_getBio.php',
		{ id: artistID },
		function(data){
		  $('#bio').text(data);	
		}
  );
}

function updateBio(artistID){ // Updates the bio for the artist logged in
  var bio = $('#bio').val();
  $.post('includes/_updateBio.php',
		 { id:artistID, bio:bio },
		 function(data){
		 }
  );
}

function getHomePgText(){ 
  $.get('includes/_getHomePgText.php',
		{ },
		function(data){
		  $('#homePgText').text(data);	
		}
  );
}

function updateHomePg(){ 
  var txt = $('#homePgText').val();
  $.post('includes/_updateHomePgText.php',
		 { txt:txt },
		 function(data){
		 }
  );
}

function getSpecialText(){ // Retrieves bio from the database according to the artistID
  $.get('includes/_getSpecialText.php',
		{ },
		function(data){
		  $('#specialText').text(data);	
		}
  );
}

function updateSpecialText(){ // Updates the bio for the artist logged in
  var txt = $('#specialText').val();
  $.post('includes/_updateSpecialText.php',
		 { txt:txt },
		 function(data){
		 }
  );
}

function getPierceText(artistID){ // Retrieves bio from the database according to the artistID
  $.get('includes/_getPierceText.php',
		{ id: artistID },
		function(data){
		  $('#pierceText').text(data);	
		}
  );
}

function updatePierceText(artistID){ // Updates the bio for the artist logged in
  var txt = $('#pierceText').val();
  $.post('includes/_updatePierceText.php',
		 { id:artistID, txt:txt },
		 function(data){
		 }
  );
}


function loadAlbumAdmin(artistID,album){ // Loads the gallery editor for the corresponding artist in the admin
  $('#thumbsContainer').empty();
  $.get('includes/_getAlbum.php',
		{ artistID: artistID, album: album },
		function(data){
		  $.each(data,function(x,obj){
		    $('<img>').addClass('thumb').attr({'src':'../images/artists/'+artistID+'/thumbs/'+obj.image}).appendTo('#thumbsContainer');
			  $('.thumb:last-child').click(function(){
					$('.thumb.current').removeClass('current');									
					$('#galleryEdit input[name="name"]').val(obj.name);
					$('#galleryEdit option[value="'+obj.album+'"]').attr('selected','selected');
					$('#galleryEdit textarea[name="caption"]').text(obj.caption);
					$('#galleryEdit input[name="imgID"]').val(obj.id);
					$(this).addClass('current');
				});
		  });
		},
  'json');
}

function clearEditImg(){ // Clears the addImg editor in the admin
  $('#galleryEdit').find('input[name="name"],input[name="imgID"]').val('');
  $('#galleryEdit').find('textarea[name="caption"]').text('');
  $('#galleryEdit').find('select option:first-child').attr('selected','selected');
  $('.thumb.current').removeClass('current');	
}

function clearNewsEdit(){ // Clears the addImg editor in the admin
  $('#newsEdit').find('input[name="title"],input[name="id"]').val('');
  $('#newsEdit').find('textarea').text('');
  $('.news.current').removeClass('current');	
}

function deleteImg(){ // Deletes the selected image from the current artist's gallery
  var imgID = $('#galleryEdit input[name="imgID"]').val();
  var imgName = $('#galleryEdit input[name="name"]').val();
  if(imgID != ''){
	var answer = confirm('Are you sure you wish to delete: '+imgName+ '?');
	if(answer){
	  $.post('includes/_deleteImg.php',
			 { id: imgID },
			 function(data){
			   $('.thumb.current').remove();
			   clearEditImg();
			 }
	  );
	}
  }
}

function getNews(){
  $.get('includes/_getNews.php',
				{ },
				function(data){
					$('#newsContainer').empty();
					$.each(data,function(x,obj){
						$('<div>').addClass('news').appendTo('#newsContainer').click(function(){
							getNewsItem(obj.id);																																			
						});
						$('<span>').addClass('newsDate').text(obj.date).appendTo('.news:last-child');
						$('<span>').addClass('newsTitle').text(obj.title).appendTo('.news:last-child');
					});
				},
	'json');
}

function getNewsItem(itemID){
	$.get('includes/_getNewsItem.php',
				{ id:itemID },
				function(data){
					$('#newsEdit input[name="title"]').val(data.title);
					$('#newsEdit textarea').text(data.text);
					$('#newsEdit input[name="id"]').val(data.id);
				},
	'json');
}

function addNews(){
	$.post('includes/_addNews.php',
				$('#newsEdit').serialize(),
				function(data){
					$('#newsEdit').clear();
					getNews();
				}
  );
}

function deleteNews(){
  $.post('includes/_deleteNews.php',
				 $('#newsEdit').serialize(),
				 function(data){
					 $('#newsEdit').clear();
					 getNews();
				 });
}

function deleteRotatorImg(img){
	$.post('includes/_deleteRotatorImg.php',
				 { img:img },
				 function(data){
					 getRotator();
				 });
}

function getRotator(){
	$.get('../includes/_getHomePgImgs.php',
				{ dir : 'images/rotator/'},
				function(data){
					//$('#rotatorImages').empty();
					$.each(data,function(x,obj){
					  $('<img>').attr('src','../images/rotator/'+obj).appendTo('#rotatorImages').click(function(){
							$('#imageRotator > img.current').removeClass('current');
							$(this).addClass('current');
							
							var del = confirm('Do you really want to delete '+obj+' ?');
							if(del){
								deleteRotatorImg(obj);
							}
						});
					});
				},
	'json');
}
