
ADORABLE.pricingOptions = function()
{
	var options = $("form.inputForm select[name='ad_options']");
	var price 	   = $("#formRow_ad_price");
	var negotiable = $("form.inputForm .formRow:contains('Negotiable')");
	if (options.val() == 1)
	{
		price.show();
		this.getNegotiable();
	} else {
		price.hide();
		this.getNegotiable();
	}
	options.change(
		function ()
		{
			if (options.val() == 1)
			{
				price.show();
				negotiable.show();
			} else {
				price.hide();
				negotiable.hide();
			}
		}
	);
}


ADORABLE.getNegotiable = function()
{
	var negotiable = $("form.inputForm .formRow:contains('Negotiable')");
	var price 	   = $("form.inputForm input[name='ad_price']");
	var options = $("form.inputForm select[name='ad_options']");
	if (price.val() == 0)
	{
		negotiable.hide();
	} else {
		if(options.val() == 0)
		{
			negotiable.css('background-color', '#eee');
			negotiable.show();
		} else {
			negotiable.hide();
		}
	}
	price.change(
		function ()
		{
			if (price.val() == 0)
			{
				negotiable.hide();
			} else {
				negotiable.css('background-color', '#eee');
				negotiable.show();
			}
		}
	);
}

ADORABLE.deleteImage = function(ad_id, img_id)
{
	if(confirm('Are You sure?'))
	{
	$.ajax({
		type: "POST",
		url: ADORABLE.homepageUrl + "async/classifieds/deleteimage.php",
		data: 'img_id=' + img_id + 'ad_id=' + ad_id,
		success: function (msg){
		    $("#img_" + img_id).remove();
		    $("#actionReport").html(msg).fadeOut(1200);
		  }		
	});		
    $("#actionReport").html('');
    $("#actionReport").show();
	}
}


ADORABLE.setDefaultImage = function(ad_id, img_id)
{
	$.ajax({
		type: "POST",
		url: ADORABLE.homepageUrl + "async/classifieds/set_default_image.php",
		data: 'img_id=' + img_id + '&ad_id=' + ad_id,
		success: function (msg){
		    $("#actionReport").html(msg).fadeOut(1200);
			  $(".def").each(function (i) {
				if (this.id == "i"+img_id) {
				  this.innerHTML = "default";
				} else {
				  current = new String(this.id);
				  current = current.replace(/i/g,""); 
				  this.innerHTML = '<a class="act" href="javascript:void(0);" onclick="javascript:ADORABLE.setDefaultImage(\'' + ad_id + '\',\'' +  parseInt(current) + '\');">set as default</a>';
				}
			  });
		  }		
	});		
    $("#actionReport").html('');
    $("#actionReport").show();
}


ADORABLE.editImageComment = function(ad_id, cid, elem)
{
	if(can_edit == 1)
	{
		can_edit = 0;
		var default_text = "no comment set";
		var div = $(elem);
		var original = div.text();
		if(div.text().trim() != default_text)
		{
			var textarea =  '<textarea id="comment">' + div.text().trim() + '</textarea>';
		} else {
			var textarea =  '<textarea id="comment"></textarea>';
		}
		var submitdiv = '<div id="submit" class="btn">submit</div>';
		var resetdiv  = '<div id="reset" class="btn">reset</div>';
		div.html(textarea + submitdiv + resetdiv);
		var comment = $('#comment');
		var submit = $("#submit");
		var reset = $("#reset");
		var comment = $('#comment');
		submit.click( function()
		{
			if(comment.val().length > 255)
			{
				alert('Text is too long. Maximum allowed number of characters is 255.');
			} else {
				var div = $('#comment_' + cid);
				$.ajax({
					type: "POST",
					url: ADORABLE.homepageUrl + "async/classifieds/update_comment.php",
					data: 'ad_id=' + ad_id + '&cid=' + cid + '&comment=' + escape(comment.val()),
					success: function(msg){
						if(msg == 'error') 
						{
							div.html(div.text());
							$("#actionReport").html('').show();
					    	$("#actionReport").html('An error occured.').fadeOut(1200);
						} else {
							div.html(msg);
							$("#actionReport").html('').show();
					    	$("#actionReport").html('Comment updated successfully.').fadeOut(1200);
						}
					  }		
				});	
				can_edit = 1;
			}
		});
		reset.click( function() {
			div.html(original);
			can_edit = 1;
		});
	}
}


String.prototype.trim = function() {
	a = this.replace(/^\s+/, '');
	return a.replace(/\s+$/, '');
};


ADORABLE.showLargeItem = function() {
	if($("#showLargeItemHandler a").text() == "show large")
	{
		$("#largeitem").show('slow');
		$("#showLargeItemHandler a").html("hide");
	} else  {
		$("#largeitem").hide('slow');
		$("#showLargeItemHandler a").html("show large");
	}
};

ADORABLE.toggleShowHide = function(handle, element, txtShow, txtHide, showIcons) {
	if($(element).css('display') == "none")
	{
		$(element).show();
		if(showIcons == 1)
		{
			$(handle).html('<img src="' + ADORABLE.homepageUrl + 'images/ifc/icons/close.gif" alt="' + txtHide + '" /> ' + '<span>' + txtHide + '</span>');
		} else {
			$(handle).html(txtHide);
		}
	} else  {
		$(element).hide();
		if(showIcons == 1)
		{
			$(handle).html('<img src="' + ADORABLE.homepageUrl + 'images/ifc/icons/show.gif"  alt="' + txtShow + '" /> ' + '<span>' + txtShow + '</span>');
		} else {
			$(handle).html(txtShow);
		}
	}
};


ADORABLE.getNewClassifiedItems = function()
{
	$.ajax({
		type: "POST",
		url: ADORABLE.homepageUrl + "async/classifieds/newitems.php",
		data: '',
		success: function(msg){
		    $("#newitems").html(msg);
		  }		
	});	
}


ADORABLE.showItemImage = function(img_id)
{
	var imgLarge = $("#imgLarge");
	imgLarge.show();
	imgLarge.html('<div id="loading"><img src="' + ADORABLE.homepageUrl + 'images/items-loading.gif" alt="Loading, please wait..." />Loading. Please, wait...</div>');
	$.ajax({
		type: "POST",
		url: ADORABLE.homepageUrl + "async/classifieds/show_large_image.php",
		data: 'img_id='+img_id,
		success: function(msg){
		    imgLarge.html(msg);
		  }		
	});
	imgLarge.click( function() {
		imgLarge.hide();
	});
	
}


ADORABLE.showTooltip = function()
{
	$("ul.imgList li").hover(
      function () {
        $(this).prepend($('<div class="tooltip">click on the image to enlarge</div>'));
      }, 
      function () {
        $(this).find("div:last").remove();
      }
    );	
}

ADORABLE.reportAd = function(ad_id, reason_id)
{
	$("#keepClean").html('<div id="loading"><img src="' + ADORABLE.homepageUrl + 'images/items-loading.gif" alt="Loading, please wait..." />Loading. Please, wait...</div>');
	$.ajax({
		type: "POST",
		url: ADORABLE.homepageUrl + "async/classifieds/report_ad.php",
		data: 'ad_id='+ ad_id + '&reason_id=' + reason_id,
		success: function(msg){
		    $("#keepClean").html(msg);
		  }		
	});
}



$(window).load(function()
{
	ADORABLE.getNewClassifiedItems();
	ADORABLE.showTooltip();
	ADORABLE.pricingOptions();
	ADORABLE.getNegotiable();
});