var slideSpeed = 300; 
var fadeSpeed = 400;
var itemGuid = 0;
$(document).ready(function(){
	$('#nav li a').click(function(){
		var div = $(this).attr('href');
		var offset = $(div).offset();
		var position = $(div).position();
		var offsetTop = parseInt(offset.top)-192;
		
		if($.browser.safari) { var scroller = 'body'; } else { var scroller = 'html'; } 
		
		$(scroller).animate({ scrollTop: offsetTop }, slideSpeed);

		return false;	
	});
	$('.adEditFader').click(function(){ ad_fader('down'); });
	$('.adCaptionEdit').live('focus',function(){
		var guid = $(this).attr('id').replace('ce','');
		$('#m'+guid+' .adCaptionControls').fadeIn(200);
	}).live('blur',function(){
		var guid = $(this).attr('id').replace('ce','');
		$('#m'+guid+' .adCaptionControls').fadeOut(200);
	});
});



// image functions
function ad_image(type,guid,btn) {
	debug(type);
	debug(guid);
	debug(btn);
	
	switch(type) {
		case 'edit': 	
			ad_fader('up');
			itemGuid = guid;
		break;
		case 'up':
			ad_move('up',guid);
		break;
		case 'down':
			ad_move('down',guid);
		break;
		case 'delete':
			if(confirm('Are you sure you want to delete this image?')) {
				ad_delete(guid);	
			} 
		break;
	}
	return false; 
}


// fade bg in 
function ad_fader(dir) {
	if(dir == 'up') {
		$('.adEditFader').fadeIn(fadeSpeed,function(){ ad_grabImages(); });	
	} else if(dir == 'down2') {
		$('.adEditImgForm').fadeOut(fadeSpeed,function(){ $(this).remove(); $('.adEditLoading').remove(); });	
	} else {
		$('.adEditWindow').fadeOut(fadeSpeed,function(){ $('.adEditFader').fadeOut(fadeSpeed); });	
	}	
}

// close image
function ad_closeImage() {
	ad_fader('down');
	return false; 	
}

// close image
function ad_closeImage2() {
	ad_fader('down2');
	return false; 	
}

// grab images
function ad_grabImages() {
	debug(itemGuid);
	$.ajax({
		type: 'POST',
		url: '?adImage=getList',
		data: 'guid='+itemGuid,
		success: function(data){
			$('#adEditImageList').empty().append(data);
			$('.adEditWindow').fadeIn(fadeSpeed);  
			debug(data);
		},
		complete: function(request,status){ 
			debug('complete');
		},
		dataType: 'html'
	});
}

// move 
function ad_move(dir,guid) {
	debug(itemGuid);
	$.ajax({
		type: 'POST',
		url: '?adImage=move',
		data: 'parent='+itemGuid+'&guid='+guid+'&dir='+dir,
		success: function(data){
			$('#adEditImageList').empty().append(data);
			$('.adEditWindow').fadeIn(fadeSpeed);  
			debug(data);
		},
		complete: function(request,status){ 
			debug('complete');
		},
		dataType: 'html'
	});
}

// delete 
function ad_delete(guid) {
	debug(itemGuid);
	$.ajax({
		type: 'POST',
		url: '?adImage=delete',
		data: 'guid='+guid,
		success: function(data){
			$('#m'+guid).fadeOut(fadeSpeed,function(){ $(this).remove(); });
			debug(data);
		},
		complete: function(request,status){ 
			debug('complete');
		},
		dataType: 'html'
	});
}

function ad_addImage() {
	debug(itemGuid);
		
	$.ajax({
		type: 'POST',
		url: '?adImage=addForm',
		data: 'guid='+itemGuid,
		success: function(data){
			$('body').append(data);
			$('.adEditImgForm').fadeIn(fadeSpeed);
			debug(data);
			ad_setupForm('adFormForm','adImage=add');
		},
		complete: function(request,status){ 
			debug('complete');
		},
		dataType: 'html'
	});
	return false;
}

// setups the ajax form stuff 
function ad_setupForm(guid,url) {
	var url = '?'+url;
	var options = {
		//target:        '#output2',   // target element(s) to be updated with server response 
		beforeSubmit:  showRequest,  // pre-submit callback 
		success:       showResponse,  // post-submit callback 
		
		// other available options: 
		url:       url         // override for form's 'action' attribute 
		//type:      type        // 'get' or 'post', override for form's 'method' attribute 
		//dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
		//clearForm: true        // clear all form fields after successful submit 
		//resetForm: true        // reset the form after successful submit 
		
		// $.ajax options can be used here too, for example: 
		//timeout:   3000 	
	};
	$('#'+guid).unbind('submit').submit(function(){
		//for(instance in CKEDITOR.instances) CKEDITOR.instances['imageEditsbItemNotes'].updateElement();
		
		$(this).ajaxSubmit(options);
		$('.adEditLoading').fadeIn(fadeSpeed);
		return false;	
	});
}


// pre-submit callback 
function showRequest(formData, jqForm, options) { 
	debug('showRequest');
	debug(formData);
	debug(jqForm);
	debug($(jqForm[0]).attr('id'));
	return true; 
} 
 
// post-submit callback 
function showResponse(responseText, statusText, xhr, $form)  { 
	debug('showResponse');
	debug(responseText);
	debug(statusText);
	debug(xhr);
	debug($form);
	
	var returns = responseText.replace(/\n/g,'').replace('<head></head><body>','').replace('</body>','').split('}&amp;{');
	debug(returns);
	switch(returns[0]) {
		case 'done':
			ad_showDone(returns[1]);
		break;	
	}
} 


function ad_showDone(data) {
	/*$('#adFormForm').fadeOut(fadeSpeed,function(){ 
		$(this).remove();
		$('.adEditImgForm .adEditSection').append('Image Uploaded'); 
	});*/
	$('#adEditImageFormMessage').fadeIn(fadeSpeed,function(){
		$('#adEditImageList').empty().append(data);
		$('.adEditLoading').fadeOut(fadeSpeed);
	});
}

function ad_showImageForm() {
	$('#adEditImageFormForm #ad_Image').val('');
	$('#adEditImageFormMessage').fadeOut(fadeSpeed);
	return false;
}

function ad_caption(guid,type) {
	console.log(guid);
	console.log(type);
	switch(type) {
		case 'cancel':
			var value = $('#ceorig'+guid).val();
			$('#ce'+guid).val(value);
		break;
		case 'save':
			var value = $('#ce'+guid).val();
			console.log(value);
			$.ajax({
				type: 'POST',
				url: '?adImage=updateCaption',
				data: 'guid='+guid+'&caption='+value,
				dataType: 'html'
			});
		break;	
	}
	return false;	
}

function debug(data) {
	//console.log(data);	
}





