
var Sancho = {};

Sancho.init = function() {

	var ts = Math.round(new Date().getTime() / 1000);
	
	// get path
	$('script').map(function(i) { 
		if (this.src.match(/Sancho\.js(\?.*)?$/)) {
			Sancho.SCRIPT_URL = this.src.replace(/\/Sancho\.js(\?.*)?$/, '');
		}
	}); 
	// alert(Sancho.SCRIPT_URL); 
  
	// init sortables
	$('*[sancho_is_sortable=1]').parent().sortable({
		items: '*[sancho_is_sortable=1]',
		helper: 'clone',
		opacity: 0.6,
		placeholder: 'sancho_placeholder',
		forcePlaceholderSize: true,
		forceHelperSize: true,
		cursor: 'move',
		revert: 200,
		start: function(event, ui) {
			ui.helper.width($(this).width());
			ui.placeholder.height(ui.helper.height());
		},
		update: function() {
			Sancho.saveOrder($(this));
		}
	});
	$('*[sancho_is_sortable=1]').parent().disableSelection();
	$('*[sancho_is_sortable=1]').addClass('sancho_sortable');	
	//$('*[sancho_sortable_connect_with!=""]').parent().sortable('option', 'connectWith', $(this).attr('sancho_sortable_connect_with'));  
	/*
	$('*[sancho_is_sortable=1]').mouseenter(function(){
		$(this).children().children().css({'outline-width': '1px'});
	}).mouseleave(function(){
		$(this).children().children().css({'outline-width': '0px'});
	});
	*/

	
	// init modules avail on page
	// alert(Sancho.curPageId);
	var pid = Sancho.curPageId; // fix scope
	$.ajax({    
		type: "POST",    
		url: Sancho.SCRIPT_URL + '/get_modules.php',    
		data: 'cur_page_id=' + pid,
		cache: true,    
		async: true,    
		success: function(html) { 
    	// alert(html); 
	  	var modules = html.split(',');
      var script = '';
      for(var i=0; i<modules.length; i++) {
      	script = modules[i] + '_init();'
      	if (modules[i]) eval(script);
      }
      var te = Math.round(new Date().getTime() / 1000);
      // alert('I took: ' + (te-ts) + ' seconds');
		},    
		error: function(html) {    
			// alert('Unable to init modules. Please refresh the page.');   
		}    
	});  
  
}    

Sancho.saveOrder = function(elem) {
	var ajaxData = '';
	$('*[sancho_is_sortable="1"]').each(function() {;
  	if ($(this).attr('sancho_entity_id')) {
  		if (ajaxData) ajaxData += ','    
    	ajaxData += $(this).attr('sancho_entity_id');
    }
  });
  
  // alert(ajaxData);
	
	elem.block({
		message: '<span style="color:#fff;font:11px \'Trebuchet MS\';word-wrap:break-word;line-height:16px;"><img src="/CAMARGUE/images/loader.gif" style="display:inline;border:0px;">&nbsp;&nbsp;&nbsp;saving,&nbsp;please&nbsp;wait...</span>',
		baseZ: 9998,
		css: { 
			color:          '#fff', 
			width:          '80%', 
			border:         '0px solid #aaa', 
			backgroundColor:'transparent', 
			cursor:         'wait' 
			}
	});

  $.ajax({    
		type: "POST",    
		url: Sancho.SCRIPT_URL + '/save_content_order.php',    
		data: 'contents_str=' + ajaxData,
		cache: true,    
		async: true,    
		success: function(html) {    
			  // alert(html);
		  	//$('#sancho_save_order_notice').hide();
		  	$(elem).unblock();
				// top.Camargue.refreshNode("*[sancho_entity_id='" + refreshElem.attr('sancho_entity_id') + "']");
		},    
		error: function(html) {    
			//$('#sancho_save_order_notice').text('Error saving order!');
			$(elem).unblock();
			elem.block({ message: '<span style="color:red;font:11px \'Trebuchet MS\';word-wrap:break-word;">SAVING&nbsp;ERROR!</span>',
			baseZ: 9998,
			css: { 
		        color:          '#fff', 
		        width:          '80%', 
		        border:         '0px solid #aaa', 
		        backgroundColor:'transparent', 
		        cursor:         'wait' 
		    }
		    });
			setTimeout($(elem).unblock(), 2000);
		}    
	});
	// alert(strOrder);
};
