this.screenshotPreview = function(){	
	/* CONFIG */
	
	xOffset = 10;
	yOffset = 30;
	
	// these 2 variable determine popup's distance from the cursor
	// you might want to adjust to get the right result
	
	/* END CONFIG */
	$("a.screenshot").hover(function(e) {
		this.t = this.title;
		this.title = "";	
		var c = (this.t != "") ? "<br/>" + this.t : "";
		$("body").append("<p id='screenshot' style='position:absolute'><img src='"+ this.rel +"' alt='url preview'/>"+ c +"</p>");
		$("#screenshot")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");
    }, function() {
		this.title = this.t;
		$("#screenshot").remove();
	});
	$("a.screenshot").mousemove(function(e){
		$("#screenshot")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});
};

$(document).ready(function() {
	screenshotPreview();

    var email_tip = "Please Enter Your E-Mail";
    $("#id_email").focus( function() {
        if ($(this).val() == email_tip) {
            $(this).val("");
        }
    }).blur( function() {
        if ($(this).val() == "") {
            $(this).val(email_tip);
        }
    });

	$("#id_btn_fav_add").click(function() {
		var event = $(this);
		$.getScript(AJAX_ADD_FAV_SHOP_URL, function(data) {
			event.parent().replaceWith("<div class='fvadd_okn'>Saved</div>");
		});
	});
	
	var openWindow = function(newUrl){
		window.open(newUrl,'', 'Width=900,Height=800,toolbar=yes, menubar=yes, scrollbars=yes,resizable=yes,location=yes,status=yes,z-look=no');
	}
	$(".btn_click > a").each(function(){
		var temp = $(this);
		var tt = temp.attr("rel");
		temp.click(function(){
			var dealId = temp.next("input:hidden").val();
			var isvoucher = temp.next().next("input:hidden").val();					
			var t = temp.parent().next(".codeshow");
			var newUrl = "/redirect/"+dealId;
			if(t.css("display")=="none"&&tt.toLowerCase()==="false"){
				openWindow(newUrl);
			}
			
			if(t.css("display")=="none"&&tt.toLowerCase()==="true"){
				openWindow(newUrl);
				window.focus();	
			}
			t.toggle();
			temp.hide();
		});
		
	});
	
	$(".showhide").click(function(){$(".codeshow").show();$(".showhide2").show();$(this).hide();$(".btn_click").hide();
									openWindow("/out/"+$(this).attr("rel"));window.focus();
								   });

	$(".showhide2").click(function(){$(".codeshow").hide();$(".showhide").show();$(this).hide();$(".btn_click").show();});


	$(".vote_c").find("a").each(function() {
		var temp = $(this);
		//				alert(temp.attr("rel"));
		temp.click(function(){
			$.getJSON("/deal_vote" + "?id="+parseInt(temp.attr("rel")),function(data){
				alert("Thank you ! Hope you enjoy shopping !");
				temp.children("span").text(data.deal.vote);
			});	
		});	
	});
	
	$(".re_expired").each(function() {
		var temp = $(this);
		temp.click(function() {
			$.getJSON("/deal_expired" + "?id="+parseInt(temp.attr("rel")),function(data){
				alert("Thank you for letting us know the expiration ! Hope you enjoy our service !");
			});
		});
	});

	$("a.workok").each(function() {
		var temp = $(this);
		var dealId=temp.attr("rel");
		temp.click(function(){$(".vote_c").find("a[rel='"+dealId+"']").click();});
	});

	$("a.notwork").each(function() {
		var temp = $(this);
		var dealId=temp.attr("rel");
		temp.click(function() {
			$.get("/deal_expired" + "?id=" + parseInt(temp.attr("rel")));
			temp.parents('li').fadeOut(1000);
		});
		
	});	


	$("#u_de_dealType").change(function(){
		var type = $(this).attr("value");
		if(type=="Codes"){
			$("#u_de_code").children("span").html("Code:");
			$("#u_de_code").children("input").attr("name","code");
		}else if(type=="Deal"){
			$("#u_de_code").children("span").html("Url:");
			$("#u_de_code").children("input").attr("name","url");		
		}	
	});

	
	var TEXT_OUT_FLAG = false;
	
	$("#u_de_discount").keydown(function(e){
		var code = e.keyCode;
		var text = $(this).val()!='undefined'? $(this).val():'';
		var num = text.replace(/[^\x00-\xff]/g, 'xx').length;
		if(!TEXT_OUT_FLAG){
			$(".u_de_error").hide();
		}
		if(num>980){
				TEXT_OUT_FLAG = true;
				$(".u_de_comment").hide();
				$(".u_de_error").show();
				$(".u_de_success").hide();
				$(".u_de_error > p").html('the Discount too long <br> please make it shorter!');
				
				if(code!=8) return false;
		}else{
			TEXT_OUT_FLAG = false;
		}
	
	});


	function valida(data){
		for(i=0;i<data.length-1;i++){
			// alert the value
			//alert(data[i].value);
			if (data[i].value=='' || data[i].value==null)
			{
				$(".u_de_comment").hide();
				$(".u_de_error").show();
				$(".u_de_success").hide();
				$(".u_de_error > p").html('Please describe the <span style="text-transform:capitalize">'+data[i].name+' </span><br> But Thank you anyway!');
				return false;
			}
		}
		return true;
	}


	function showRequest(formData, jqForm, options) { 
		// formData is an array; here we use $.param to convert it to a string to display it 
		// but the form plugin does this for you automatically when it submits the data 
		//var queryString = $.param(formData); 
	 
		// jqForm is a jQuery object encapsulating the form element.  To access the 
		// DOM element for the form do this: 
		// var formElement = jqForm[0]; 
	 
		//alert('About to submit: \n\n' + queryString); 
	 
		// here we could return false to prevent the form from being submitted; 
		// returning anything other than false will allow the form submit to continue 
		//return true; 
		$('.u_de_comment').html('Submitting...');
		$(".u_de_success").hide();
		$(".u_de_error").hide();
		flag = valida(formData);
		return flag;

	};

	function showResponse(responseText, statusText, xhr, $form)  { 
		// for normal html responses, the first argument to the success callback 
		// is the XMLHttpRequest object's responseText property 
	 
		// if the ajaxSubmit method was passed an Options Object with the dataType 
		// property set to 'xml' then the first argument to the success callback 
		// is the XMLHttpRequest object's responseXML property 
	 
		// if the ajaxSubmit method was passed an Options Object with the dataType 
		// property set to 'json' then the first argument to the success callback 
		// is the json data object returned by the server 
	 
//		alert(responseText.is_success);
		if(responseText.is_success){
			$(".u_de_comment").hide();
			$(".u_de_error").hide();
			$(".u_de_success ").show();
		}else{
			$(".u_de_comment").hide();
			$(".u_de_error").show();
			$(".u_de_error > p").html('Sorry , the deal/code submit unsuccessfully '+responseText.reason+' <br> But Thank you anyway!');
		}
		
		
	};

	$('#user_submit_deal').ajaxForm({ 
			// dataType identifies the expected content type of the server response 
		beforeSubmit:  showRequest,
		dataType:  'json', 
		resetForm: true,
		success:   showResponse 
	}); 

	// load comments by ajax
	$('a.commentsloader[rel]').click(function() {
		var deal_id = $(this).attr('rel');
		$.getJSON('/deal/show-comments?id='+deal_id, function(comment_list) {
			if (!comment_list || comment_list.length == 0) { // no comments found
				return false;
			}
			var area = $('#id_comments_area_'+deal_id);
			if (area.is(':visible')) { // already shown
				return false;
			}
			var ul = $('<ul/>');
			for (i in comment_list) {
				var comment = comment_list[i];
				var img_head = $('<div class="img_head"></div>');
				var comment_con = $('<div class="comment_con"></div>').append(comment.comment);
				var r_time = $('<div class="r_time">').append(comment.user_name+' posted this '+comment.submit_date+' ago');
				var li = $('<li/>').append(img_head).append(comment_con).append(r_time).append($('<div class="clear"></div>'));
				ul.append(li);
			}
			var div_comment_c = $('<div class="ment_c"/>').append(ul);
			var closebtn = $('<div class="nunb_c"><a href="#">Close</a></div>');
			closebtn.click(function() {
				area.hide('slow');
				area.empty();
				
				return false;
			});
			var div_comment_t = $('<div class="comment_t"/>').append(div_comment_c).append(closebtn);
			area.append(div_comment_t);
			area.show('slow');
		});
		return false; // stop href="#"
	});
});
