/* $Id: targetgen.js 445 2008-12-12 22:13:58Z vincent $ */
$(document).ready(function(){
	var bindBehaviorsTargetGen = function() {
		$("#return_targetgen").click(function () { 
	      $("#target_results").slideUp();
			$("#header").show();
			$("#navbar").show();
			$("#wrapper").show();
			$("#footer").show();
			return false;
	    });
	};
	
	bindBehaviorsTargetGen();
	$("#targetgenaddmore").click(function(){
		var strData = $("#posttarget").serialize();
		$.post("/target-coupon-generator/add", strData, function(data){
			window.location = "/target-coupon-generator/add";
		});
	});

	 $('.uploadify').uploadify({
  			'uploader':  '/assets/images/uploadify.swf',
			'script':    '/target-coupon-generator/upload',
			'folder':    '/assets/target_gen',
			'cancelImg': '/assets/images/cancel.png',
			'sizeLimit': 1048576,
			'auto'           : true,
			onSelectOnce   : function(e, d) {
				selectTarget = 'url_' + e.target.id.substring(9, 10);
				return true;
			},
			onComplete   : function(event, queueID, fileObj, response, data) {
				$("#"+selectTarget).attr("value", "http://www.afullcup.com" + fileObj.filePath);
				return true;
			}
	});

	$(".target_box .couponbox").click(function (e) {
		var winW = $(window).width();
		var winH = $(window).height();
        var xPos = e.pageX;
		if (xPos > winW - 474) {
			xPos = xPos - 474;
		}
		var yPos = e.pageY;
		if (yPos > winH - 194) {
			yPos = yPos - 200;
		}
		var imgPath = $(this).attr("src");
		$("#targetBackgroundPopup").show();
		$("#target_replace").show();
		$('#target_replace').html('<div class="target_box_pop" style="z-index:1000;position:absolute;top:' + yPos + 'px; left:'+xPos+'px;"><img src="' + imgPath + '"/></div');
	});
	
	$("#posttarget .title").change(function(){
		var titlec = $(this).attr("value");
		var inputfield = $(this);
		var resultField = inputfield.parent().children(".description");
			$.ajax({
        url: "/target-coupon-generator/check/",
        data: ({checktitle: titlec}),
        cache: false,
        success: function(html){
          resultField.html(html);
          resultField.show();
        }
      });
	});
	
	$(".target_box .tdelete").click(function() {
			var target = $(this).parent();
			var targetid = $(this).parent().attr("id").substring(7);
			$("#confirmdelete").dialog({
				resizable: false,
				height:140,
				width:350,
				modal: true,
				buttons: {
					Delete: function() {
						$(this).dialog('close');
						$(this).dialog('destroy');
						$.post("/target-coupon-generator/delete/" + targetid, function(data){
						  $(target).animate({ backgroundColor: "#fbc7c7" }, "fast").animate({ opacity: "hide" }, "slow");
						});
					},
					Cancel: function() {
						$(this).dialog('close');
						$(this).dialog('destroy');
					}
				},
				title: 'Comfirm Action',
				position: ["top", 100],
				zIndex: 30000000
			});
	});

	$(".print_selected").click(function () {
		var strData = $("#targetgen").serialize();
		$.post("/target-coupon-generator/print", strData, function(data){
			$("#target_results").hide();
			$("#header").hide();
			$("#navbar").hide();
			$("#wrapper").hide();
			$("#footer").hide();
			$("#target_results").insertBefore("#header").html(data);
			$("#target_results").slideDown(function(){
				window.print();
			});
			bindBehaviorsTargetGen();
		});
	});
	
	$("#targetBackgroundPopup").click(function () {
		$("#targetBackgroundPopup").hide();
		$("#target_replace").hide();
	});
	
	$("#targetgen .promtreg").click(function(){
		$("#mustreg").dialog({
					resizable: false,
					height:140,
					modal: true,
					buttons: {
						Close: function() {
							$(this).dialog('close');
							$(this).dialog('destroy');
						},
						Register: function() {
							$(this).dialog('close');
							$(this).dialog('destroy');
							window.open('/register/');
						}
					},
					title: 'Registration Required',
					position: "center",
					zIndex: 30000000
		});
		return false;
	});
});