// CUFON
//Cufon.replace('.dinot', {fontFamily: 'dinot'});

$(document).ready(function() {
	$('.confirmation').live('click', function(){
		var url=$(this).find("a:first").attr("href");

		$.confirm({
			'title'		: 'Delete Confirmation',
			'message'	: 'You are about to delete this item. <br />It cannot be restored at a later time! Continue?',
			'buttons'	: {
				'Yes'	: {	'action': function(){
					window.location = url;
				}},
				'No'	: {	'action': function(){

				} }
			}
		});
	
		return false;
	
	});
});

function deleteImage(id){

	$.confirm({
		'title'		: 'Delete Confirmation',
		'message'	: 'You are about to delete this item. <br />It cannot be restored at a later time! Continue?',
		'buttons'	: {
			'Yes'	: {	'action': function(){
				$.ajax({
					url: baseURL + 'admin/products/delete_image',
					type: "POST",
					data: 'id=' + id,
					success: function(feedback){
						if(feedback == 'ok'){
							$('#item_' + id).remove();
						}
					}
				});
			}},
			'No'	: {	'action': function(){} }
		}
	});
	
	return false;
}

function deleteGaleryImage(id){

	$.confirm({
		'title'		: 'Delete Confirmation',
		'message'	: 'You are about to delete this item. <br />It cannot be restored at a later time! Continue?',
		'buttons'	: {
			'Yes'	: {	'action': function(){
				$.ajax({
					url: baseURL + 'admin/gallery/delete',
					type: "POST",
					data: 'id=' + id,
					success: function(feedback){
						if(feedback == 'ok'){
							$('#item_' + id).remove();
						}
					}
				});
			}},
			'No'	: {	'action': function(){} }
		}
	});
	
	return false;
}

function setFeaturedImage(id){
	$.ajax({
		url: baseURL + 'admin/products/set_featured_image',
		type: "POST",
		data: 'id=' + id,
		success: function(feedback){
			$('#info-message-up').html();
			$('#info-message-up').css('display', 'block');
			$('#info-message-up').html(feedback).delay(3000).fadeOut();
		}
	});
}




function sortable_table(html_table_id, db_table_name, db_table_id_column, db_table_where_column, db_table_where_value, db_table_position_column, child){
	if(typeof(db_table_where_column) == 'undefined'){db_table_where_column = '';}
	if(typeof(db_table_where_value) == 'undefined'){db_table_where_value = '';}
	if(typeof(db_table_position_column) == 'undefined'){db_table_position_column = 'position';}
	if(typeof(child) == 'undefined'){child = '';}

	$(document).ready(function() {
		var fixHelper = function(e, ui) {
			ui.children().each(function() {
				$(this).width($(this).width());
			});
			return ui;
		};
		
		function forceHelper(e,ui) {
			$(".ui-state-highlight").html("<td colspan='100%'>&nbsp;</td>");
		}
	
		$("#"+html_table_id+" tbody.sort "+child).sortable({
			opacity: '0.7',
			placeholder: 'ui-state-highlight',
			helper: fixHelper,

		      change: function(e, ui) {
		         forceHelper(e,ui);
				
				
		      },
			  
			  start: function(e, ui){
			  	/* 
				 // ajax loader for category tree
				if(html_table_id=='sort_categories'){
					

						$('#tree').html('<img src="'+baseURL+'assets/images/admin/ajax-loader.gif" style="margin: 20px 0 0 60px;"/>');
						
				
				}
				*/
			  },
			  
			update: function(e, ui){
				serial = $(this).sortable('serialize');
				
				if(html_table_id=='sort_categories'){
					

						$('#tree').html('<img src="'+baseURL+'assets/images/admin/ajax-loader.gif" style="margin: 20px 0 0 60px;"/>');
						
				
				}
				
				$.ajax({
					url: baseURL + "admin/position",
					type: "POST",
					data: serial+'&table_name='+db_table_name+'&column_name='+db_table_id_column+'&where_column='+db_table_where_column+'&where_value='+db_table_where_value+'&position_column='+db_table_position_column,
					success: function(feedback){
						$('#info-message-up').html();
						$('#info-message-up').css('display', 'block');
						$('#info-message-up').html(feedback).delay(3000).fadeOut();
					
						
						//$('#tree').empty();
						// refresh category tree
						if(html_table_id=='sort_categories'){
							$.get(baseURL + "admin/categories/ajax_update_category_tree", function(data) {
								$('#tree').html(data);
								$("#tree").treeview({
									collapsed: true,
									animated: "fast",
									control:"#sidetreecontrol",
									persist: "location"
								});
							});
						}
						
						$("#"+html_table_id+" tbody tr:first").click();
						
				
						
					}
				});						
			}
		});
		

		
		return false;
	});
	
	

		

		
		
}





function show_tooltip(tooltip_parent){

	$('<div class="tooltip2"><span>Double-click to edit</span><div class="arrow2"></div></div>').appendTo('body').hide();

	var div_width = $(tooltip_parent).width();
	var tooltip_width = $('.tooltip2').width();
	
	var pos = $(tooltip_parent).offset(); 
	var top = pos.top;
	
	if(div_width >= tooltip_width){
		var left_plus = (div_width-tooltip_width)/2;	
		var left = pos.left + left_plus;		
	}else{
		var left_plus = (tooltip_width-div_width)/2;	
		var left = pos.left - left_plus;
	}

	$('.tooltip2').css({
		display: 'block',
		top: top - 40,			
		left: left
	});
}

