var products={};

var api;
var currentPictId=1;

products.init=function() {
	
	
	$('table.datas').dataTable();
	$('table.datas tbody ').sortable();
		
}

products.editproducts=function() {
	
}

products.editData=function(productId) {
	//alert(productId);
	$.post('/index.php',{'jfunc':'products.adminEdit','contentId':$('#contentId').val(),'productId':productId},function(data) {
		//alert(data);
		data=$.parseJSON(data);
		
		$('#dataEdit').show();
		$('#dataListing').hide();
		$('#productId').val(data.productId);
		$('#title_fr').val(data.title_fr);
		$('#title_en').val(data.title_en);
		$('#description_fr').html(data.description_fr);
		$('#description_en').html(data.description_en);
		
		$('#url').val(data.url);
		
		/*$('#priceCatId').val(data.priceCatId);
		$('#year').val(data.year);
		$('#designer').val(data.designer);
		$('#manufacturer').val(data.manufacturer);
		$('#width').val(data.width);
		$('#height').val(data.height);
		$('#diameter').val(data.diameter);
		$('#weight').val(data.weight);
		$('#country').val(data.country);
		*/
		
		//$('#defaultPict').val(data.defaultPict);
		
		$.each(data.categories, function(key, val) {
			$('#cat_'+val).attr('checked','checked');
	  	});
	  	if(typeof(data.picts)!='undefined') {
	  		//alert('r');
		  	p='';
		  	c=1
		  	$.each(data.picts, function(key, val) {
		  		p+="<li><a onclick=\"products.setToCrop('tpict_"+c+"','/datas/products/"+data.productId+"/"+val+"')\"><img src='/datas/products/"+data.productId+"/thumbs/"+val+"' id='tpict_"+c+"' /></a>";
		  		p+="<input type='radio' name='defaultPict' value='"+val+"' /></li>";
		  		if(c==1) {
		  			$('#pictCrop').attr('src','/datas/products/'+data.productId+'/'+val);
		  		}
				c++;
		  	});
			$('#picts').html(p);
			products.setCrop();
		}
		else {
			$('#pictCrop').attr('src','/datas/products/'+data.productId+'/');
			
		}
		
		$.each($('input:radio[name=defaultPict]'),function(key,val) {
			//alert($(this).attr('value'));
			if($(this).attr('value')==data.defaultPict) {
				$(this).attr('checked','checked');
			}
		});
		
		
		if(data.promote==1) {
			$('#promote').attr('checked','checked');
		}
		if(data.online==1) {
			$('#online').attr('checked','checked');
		}
		
		$('#editTabs').tabs();
		
		
		
		$('#file_upload').uploadify({
		    'uploader'  : '/admin/js/uploadify.swf',
		    'script'    : '/admin/js/uploadify.php',
		    'cancelImg' : '/admin/css/images/cancel.png',
		    'folder'    : '/datas/products/'+$('#productId').val(),
		    'multi'		: true,
		    'auto'      : true,
		     'onComplete'  : function(event, ID, fileObj, response, data) {
		      //alert('There are ' + data.fileCount + ' files remaining in the queue.'+fileObj.filePath);
		     // alert(response);
		    },
		     'onError'     : function (event,ID,fileObj,errorObj) {
		      alert(errorObj.type + ' Error: ' + errorObj.info);
		    }
		 });
				
	});
}


products.deleteData=function(productId) {
	if(confirm('Are you sure ?')) {
		$.post('/index.php',{'jfunc':'products.adminDelete','productId':productId},function(data) {
		window.location.reload();
	});
	}
}

products.saveData=function(productId) {
	datas=$('#form').serialize();
	//alert(datas);
    $.post('/index.php',{'jfunc':'products.adminSave','productId':productId,'datas':datas},function(data) {
		//alert(data);
		//window.location.reload();
		admin.addEditWindow('products');
	});
	
	
}

products.changeCategory=function(obj,contentId) {
	
	  $.post('/index.php',{'jfunc':'products.adminChangeCategory','categoryId':obj.value,'contentId':contentId},function(data) {
	//	alert(data);
		window.location.reload();
		//admin.addEditWindow('products');
	});
}


products.newData=function() {
	categoryId=$('#categoryId').val();
		$.post('/index.php',{'jfunc':'products.adminNew','categoryId':categoryId},function(dataId) {
			//alert(dataId);
			products.editData(dataId);
		});
	
}





function showPreview(c){ 
	//alert(currentPictId);
	//alert($('#productId').val());
	//alert(c.x+'/'+c.y+'/'+c.x2+'/'+c.y2+'/'+c.w+'/'+c.h);
	$.post('/index.php',{'jfunc':'products.resizeThumb','pict':$('#pictCrop').attr('src'),'c':c,'width':$('#pictCrop').width(),'height':$('#pictCrop').height(),'productId':$('#productId').val()},function(dataId) {
			//alert(dataId);
			
	});
}


products.setCrop=function(pictId) {
     api = $.Jcrop('#pictCrop',{ aspectRatio: 150/110, onSelect: showPreview,setSelection: [0,0,150,110]     });
}

   
    
    
    

products.setToCrop=function(pictId,url) {
	currentPictId=pictId;
	api.destroy();
    $('#pictCrop').attr('src',url);
    products.setCrop();
}


