function Special(){
	var cropper;
	var max_width = 640;
	var max_height = 480;

	this.crop_x = 0;
	this.crop_y = 0;
	this.crop_w = 0;
	this.crop_h = 0;
	
	this.clearForm = function(){
		$(':text').val("");
		$('#special_id').val("");
		$('#copy-en').val("");
		$('#copy-fr').val("");
		$('#image-en').val("");
		$('#image-fr').val("");
	}
	this.buildCropper = function(img_info,lang){
		var min_width = img_info['min_width'];
		var min_height = img_info['min_height'];
		
		$('#crop_target').load(function(){
			var x1 = (img_info['width']/2) - (min_width/2) < 0 ? 0 : (img_info['width']/2) - (min_width/2);
			var y1 = (img_info['height']/2) - (min_height/2) < 0 ? 0 : (img_info['height']/2) - (min_height/2);
			var x2 = x1 + min_width;
			var y2 = y1 + min_height;
			
			if(cropper !== undefined){
				cropper.destroy();
			}
			
			$("#crop_window").dialog({
				modal: true,
				width: max_width + 28,
				height: max_height + 110,
				buttons:{
					"Crop Image":function(){
						var sels = cropper.tellSelect();
						$.ajax({
							type: "POST",
							url: "/include/api/special/crop.php",
							data: {
								return_type:'JSON',
								img:img_info['full_path'],
								x:sels.x,
								y:sels.y,
								w:sels.w,
								h:sels.h,
								cw:min_width,
								ch:min_height
							},
							success: function(r){
								var resp = JSON.parse(r);
								if(resp['success']){
									$("#crop_window").dialog('close');
									$('#image-'+lang).val(resp["full_path"]);
									$("#special_image-"+lang).html('<img src="'+resp['image']+'" alt="Special Image" width="300" />');
								}
								else{
									alert(resp['msg']);
								}						
							}
						});
					}
				},
				zIndex: 9400
			});
			
			cropper = $.Jcrop('#crop_target',{
				minSize: [min_width,min_height],
				aspectRatio: min_width/min_height,
				setSelect: [x1,y1,x2,y2],
				boxWidth:max_width,
				boxHeight:max_height
			});
		});
		$('#crop_target').attr("src",img_info['image']);
	};
	this.deleteSpecial = function(special_id){
		if(special_id){
			$.ajax({
				type: "POST",
				url: "/include/api/special/delete.php",
				data: {
					return_type:'JSON',
					id: special_id
				},
				success: function(r){
					var resp = JSON.parse(r);
					$('#'+special_id).remove();
					var tbody = $('#specials_table tbody');
					if(tbody.children().length == 0){
						tbody.append('<tr class="nodrag nodrop"><td colspan="5">There are no specials available at this time.</td></tr>')
					}
				}
			});		
		}
		
	};
	function centerWindow(){
		$('.popup_window').show();
		var top = (($(window).height() - $('.popup_window').height()) / 2) + $(window).scrollTop();
		if(top < 0){
			top = 0;
		}
		var left = (($(window).width() - $('.popup_window').width()) / 2) + $(window).scrollLeft();
		if(left < 0){
			left = 0;
		}
		$('.popup_window').css("top", top + "px");
		$('.popup_window').css("left", left + "px");
	}
}

Special.prototype.textareas = [];

Special.prototype.remove = function(special_id){
	if(confirm('Deleting this Special cannot be undone.  If you are sure you want to do this, click OK.  Otherwise, click Cancel')){
		this.deleteSpecial(special_id);
	}
}

Special.prototype.reorder = function(table,row){
	var rows = table.tBodies[0].rows;
	var cnt = 0;
	var ids = [];
	for(var i = 0;i<rows.length;i++){
		if(rows[i].className.indexOf('nodrop nodrag') < 0){
			ids.push(rows[i].id);
			cnt++;
		}			
	}

	if(ids.length){
		$.ajax({
			type:'POST',
			url: '/include/api/special/reorder.php',
			cache: false,
			data: {
				return_type: 'JSON',
				ids:ids.join(',')
			},
			success: function(r){}
		});
	}
}

Special.prototype.actToggle = function(obj){
	var status = $(obj).attr('active') == '1' ? 0 : 1;
	var id = $(obj).parent().parent().attr('id');
	$.ajax({
	   type: "POST",
	   url: "/include/api/special/activate.php",
	   data: {
			return_type:'JSON',
			id: id,
			status:status
		},
	   success: function(msg){
			if(status){
				$(obj).attr('active',1);
				$(obj).attr('src','/images/buttons/Active-On.png');
				$(obj).attr('alt','Deactivate');
				$(obj).attr('title','Deactivate');
			}
			else{
				$(obj).attr('active',0);
				$(obj).attr('src','/images/buttons/Active-Off.png');
				$(obj).attr('alt','Activate');
				$(obj).attr('title','Activate');
			}			
	   }
	 });
}
Special.prototype.hpToggle = function(obj){
	var status = $(obj).attr('homepage') == '1' ? 0 : 1;
	var id = $(obj).parent().parent().attr('id');
	$.ajax({
	   type: "POST",
	   url: "/include/api/special/homepage.php",
	   data: {
			return_type:'JSON',
			id: id,
			status:status
		},
	   success: function(msg){
			if(status){
				$(obj).attr('homepage',1);
				$(obj).attr('src','/images/buttons/Home-On.png');
				$(obj).attr('alt','Hide on Homepage');
				$(obj).attr('title','Hide on Homepage');
			}
			else{
				$(obj).attr('homepage',0);
				$(obj).attr('src','/images/buttons/Home-Off.png');
				$(obj).attr('alt','Show on Homepage');
				$(obj).attr('title','Show on Homepage');
			}			
	   }
	 });
}

Special.prototype.buildUploader = function(dealer_id,lang){
	var cont = this;
	$('#upload_button-'+lang).uploadify({
		uploader:"/scripts/jquery/other/uploadify.swf",
		script:"/admin/upload/special.php",
		cancelImg:"/images/stock/16x16/Cancel.png",
		buttonText:"Upload Image",
		auto:true,
		fileDataName:'image',
		scriptData:{
			dealer_id:dealer_id
		},
		onComplete:function(e,id,file,r,data){
			var resp = JSON.parse(r);
			if(resp["success"]){
				cont.buildCropper(resp,lang);
			}
			else{
				alert(resp["msg"]);
			}
		}
	});
}

Special.prototype.getInventoryInfo = function(obj,langs){
	var cont = this;
	var inventory_id = $(obj).val();
	
	if(inventory_id > 0){
		$.ajax({
			type: "POST",
			url: "/include/api/inventory/getVehicle.php",
			data: {
				return_type:'JSON',
				inventory_id: inventory_id
			},
			success: function(r){
				var resp = JSON.parse(r);
				if(resp["success"]){
					cont.buildCropper(resp,langs[0]);
					$("#title-"+langs[0]).val(resp['year']+' '+resp['make']+' '+resp['model']);
					if(langs.length > 1){
						for(i=1;i<langs.length;i++){
							$("#title-"+langs[i]).val('');
							$("#image-"+langs[i]).val('');
							$("#copy-"+langs[i]).val('');
						}					
					}
				}
				else{
					alert(resp["msg"]);
				}
			}
		});
	}
}
