function upload_image(){
	alert('pop it!');
}

$(document).ready(function() {
   $('.listingThumb').live('click',
   		function(){
   			var index = $(this).find('img').attr('id').split('_');
   			var newSrc = $(this).find('img').attr('src').replace('thumbs','previews');
   			var newSrc = newSrc.replace('thumbnail','preview');
   			
   			$('#listingImageOptions').find('input[name=index]').val(index[1]);
			$('#listingImageOptions').find('a').attr('href','javascript:remove_listing_image(' + index[1] + ')');
			$('#listingPicPreviewImage').attr('src',newSrc);
			
			if($(this).hasClass('mainListingImage')){
				$('#mainCheckBox').attr('checked',true);
			}else{
				$('#mainCheckBox').attr('checked',false);
			}
   		}
   );
   
   
   
   
   
 });

function store_image(fileName,baseURLPath,galIndex){
	
	var data = {command:'addListingImage',listing_index:galIndex,file_name:fileName,base_url:baseURLPath};
	
	ajax_call(data, 'json','addListingImage'); 
	
}

function set_main_image(index,galIndex){
	var data = {command:'setMainImage',record_index:index,gal_index:galIndex};	
	ajax_call(data, 'json','setMainImage'); 
}

function delete_image(index,galIndex){
	var data = {command:'deleteListingImage',record_index:index,gal_index:galIndex};	
	ajax_call(data, 'json','deleteListingImage'); 
}

function edit_listing(index){
	$('#body').append(
		'<div id="block" class="pageBlock"></div>'
	);
	$('#body').append( 
		'<div id="listingEditorDetail"></div>'
	);
	
	$('#listingEditorDetail').css('top', 40);
	$('#listingEditorDetail').css('left', (($(document).width()/2) - ($('#listingEditorDetail').width()/2))-20);
	
	ajax_call('recordIndex=' + index + '&command=loadListingData', 'json','loadListingData'); 
}

function load_admin_thumbs(index){
	ajax_call('recordIndex=' + index + '&command=loadAdminThumbs', 'json','loadAdminThumbs'); 
}

function close_listing_editor(){
	$('#listingEditorDetail').remove();
	$('#block').remove();
	var data = {command:'refreshListingData'};
	ajax_call(data, 'json', 'refreshListingData');
}

/*################################################################################################################
------------------------------ ------------------------------------  AJAX VALIDATE FUNCTIONS ----------------------
/*################################################################################################################*/

function confirm_listing_delete(obj){
	$(obj).parents('tr').css('color','#ff6666');
	
	var answer = confirm('Delete Listing???');
	if (answer){
		$(obj).parents('tr').css('color','#000');
		return true;
	}else{
		$(obj).parents('tr').css('color','#000');
		return false;
	}
}

/*################################################################################################################
--------------------------------------------------------  AJAX RETURN FUNCTIONS ---------------------------------
################################################################################################################*/
function setListingStatus(returnData){
	alert(returnData['status']);
}
function setListingFeatured(returnData){
	alert(returnData['status']);
}
function refreshListingData(returnData){
	$('#adminListings').html(returnData['html']);
}

function deleteListingImage(returnData){
	$('#picList').html(returnData['html']);
	alert(returnData['status']);
}

function addListingImage(returnData){
	// <-- nothin'
}


function addNewListing(returnData){
	$('#adminListings').html(returnData['html']);
}

function loadListingData(returnData){
	$('#listingEditorDetail').html(returnData['html']);
}

function deleteListing(returnData){
	$('#adminListings').html(returnData['html']);
	alert(returnData['status']);
}

function updateListing(returnData){
	alert(returnData['status']);
}

function setListingImageMain(returnData){
	var index = $('#listingImageOptions').find('input[name=index]').val();
	$('.mainListingImage').removeClass('mainListingImage');
	
	if(returnData['status'].indexOf('NOT') != -1){
		// <-- nothin...
	}else{
		$('#thumb_' + index).addClass('mainListingImage');
	}
		alert(returnData['status']);
}

function loadAdminThumbs(returnData){
	$('#picList').html(returnData['html']);
}

function arrangeThumbs(returnData){
	//alert(returnData['status']);
}

function setMainImage(returnData){
	alert(returnData['status']);
}
