/* amped js*/

/* the defaults */
var layoutIds = {
	fader 			: 'adFader',
	holder 			: 'adEditWindow',
	btnClose 		: 'adMediaClose',
	header			: 'adMediaHeader',
	media				: 'adMedia',		
	loader			: 'adLoader',
	loaderData	: 'adLoaderData'
}

var layout = '<div class="'+layoutIds.fader+'"></div>';
var adFadeSpeed = 200;
var animationSpeed = 200;
var slideSpeed = 400;
var editor = null;
var loadingObj = {
	type			: 'loading',
	header		: 'Searching for ...',
	width			: 300,
	height		: 140,
	hideClose : 1
};
/* end defaults */

$(function(){
	
	// setting up the gallery
	$('.mediaItem').adGallery();

	// edit button click
	$('.adBlockEdit').live('click',function(){
		adDebug('edit');
		var clicked = $(this);
		//if(clicked.ha
		ad_fade({dir:'up',div:'.'+layoutIds.fader});	
		var data = {
			dbTable 		: 	clicked.siblings('.adEditBtndbTable').val(),
			guid				: 	clicked.siblings('.adEditBtnguid').val(),
			parent			: 	clicked.siblings('.adEditBtnparent').val(),
			source			:		clicked.siblings('.adEditBtnsource').val(),
			position		:		clicked.siblings('.adEditBtnposition').val()
		}
		var loadObj = {
			type			: 	'editWindow',
			width			:		555,
			height		:		368,
			data			:		data
		}
		ad_load(loadObj);
		return false;	
	});
	
	// delete button click 
	$('.adBlockDelete').live('click',function(){
		adDebug('delete');
		var confirmItem = confirm('Are you sure you want to delete this item?');
		if(confirmItem) {
			ad_delete($(this));	
		}	
		return false;
	});
	
	// add button click 
	$('.adAddSection .adSystemBtn').live('click',function(){
		ad_fade({dir:'up',div:'.'+layoutIds.fader});	
		var clicked = $(this);
		var data = clicked.attr('href').replace(/\?/,'');
		var loadObj = {
			type			: 	'addWindow',
			width			:		555,
			height		:		368,
			data			:		data,
			header		:		'Add Item'
		}
		ad_load(loadObj);
		return false;	
	});
	
	// close button
	$('.adEditWindowClose, .adSystemCancel').live('click',function(){
		ad_fade({dir:'down',div:'.'+layoutIds.fader,remove:1});
		ad_fade({dir:'down',div:'.adEditWindow',remove:1});
		return false;
	});
	
});

// fades in items
function ad_fade(obj) {
	if($('.'+layoutIds.fader).length < 1) $('body').append(layout);
	if(obj.dir == 'up') {
		$(obj.div).fadeIn(adFadeSpeed);
	}
	if(obj.dir == 'down') {
		if(obj.remove) {
			$(obj.div).fadeOut(adFadeSpeed,function(){ $(this).remove(); });	
		} else {
			if(obj.reload) {
				$(obj.div).fadeOut(adFadeSpeed,function(){ window.location.reload(); });	
			} else {
				$(obj.div).fadeOut(adFadeSpeed);	
			}
		}
	}
}

// loader
function ad_load(obj) {
	/*
		type 			// which type to pull back and display
		header 		// header text
		width 		// width of the box
		height 		// height of the box
	*/
	adDebug(obj);
	var type = obj.type;
	var header = obj.header;
	var width, height;
	obj.width ? width = obj.width : width = 400;
	obj.height ? height = obj.height : height = 400;
	var widthInner = width-12;
	var heightInner = height-42;

	if(type != 'addWindow') {
		var items = 'dbTable='+obj.data.dbTable+'&guid='+obj.data.guid+'&parent='+obj.data.parent+'&source='+obj.data.source+'&position='+obj.data.position;
	} else {
		var items = obj.data;	
	}
			
	var offsetX = Math.floor(width / 2);
	var offsetY = Math.floor(height / 2);
	
	switch(type) {
		case 'editWindow':
		case 'addWindow':
			$.ajax({
				type: 'POST',
				url: '?adJax='+type,
				data: 'data='+header+'&'+items,
				success: function(data){ 
					adDebug('sucess'); 
					adDebug(data);
					$('body').append(data);
					$('.adEditWindow').hide().css({ width : width, height : height, marginLeft : -offsetX, marginTop : -offsetY });
					$('.adEditWindowInner').width(widthInner).height(heightInner);
					$('.adEditWindow').fadeIn(adFadeSpeed);
					$('.adRichTextInput').each(function(){ 
						var itemId = $(this).attr('id');
						var instance = CKEDITOR.instances[itemId];
						if(instance) CKEDITOR.remove(instance);
						CKEDITOR.replace(this,{filebrowserUploadUrl: 'inc/ckpost.php'});
					});
					type == 'addWindow' ? form = 'add' : form = 'update';
					ad_setupForm($('.adSystemContentForm'),'adForm='+form);
				},
				complete: function(request,status){ 
					adDebug('complete');
				},
				dataType: 'html'
			});
		
		break;
	}	
	$('.'+layoutIds.holder).clearQueue().animate({
		width 			: width,
		marginLeft 	: -offsetX 	
	},animationSpeed,function(){
		$(this).animate({
			height		: height,
			marginTop : -offsetY	
		}, animationSpeed,function(){ 
			adDebug(type);
		});	
	});
}

function ad_delete(clicked) {
	var dbTable = clicked.siblings('.adEditBtndbTable').val();
	var guid = clicked.siblings('.adEditBtnguid').val();
	
	$.ajax({
		type: 'POST',
		url: '?adForm=delete',
		data: 'dbTable='+dbTable+'&guid='+guid,
		success: function(data){ 
			window.location.reload();
		},
		dataType: 'html'
	});
}

function ad_setupForm(form,url) {
	var url = '?'+url;
	var options = {
		beforeSubmit	:  showRequest,  
		success				:  showResponse, 
		url						:  url  
	};
	form.unbind('submit').submit(function(){
		//for(instance in CKEDITOR.instances) CKEDITOR.instances['imageEditsbItemNotes'].updateElement();
		
		$(this).ajaxSubmit(options);
		return false;	
	});
}


// pre-submit callback 
function showRequest(formData, jqForm, options) { 
	adDebug('showRequest');
	adDebug(formData);
	adDebug(jqForm);
	adDebug($(jqForm[0]).attr('id'));
	return true; 
} 
 
// post-submit callback 
function showResponse(responseText, statusText, xhr, $form)  { 
	adDebug('showResponse');
	adDebug(responseText);
	adDebug(statusText);
	adDebug(xhr);
	adDebug($form);
	switch(responseText) {
		case 'complete':
			ad_fade({dir:'down',div:'.adEditWindow',reload:1});
		break;	
	}
} 


// debugger
adDebug = function(data){
	if(window.console) {
		//console.log(data);
	} else {
		// alert(data);
	}	
}


/* end amped js*/
