﻿$(document).ready(function () {

	loginImage();

	popUpRemarks();

	// colorbox
	$(".stoneviewer").colorbox({ iframe: true, width: 976, height: 826 });
	$(".watermarkFLV").colorbox({ iframe: true, width: 536, height: 546 });
	$(".certification-lightbox").colorbox({ ifram: true, width: 706, height: 806 });
	//$(".detail-images a").colorbox({ ifram: true, width: 706, height: 806 });
	$(".group1").colorbox({ rel: 'group1', width: 706, height: 806 });

	//use broken file image for missing thumbnails
	errorThumbHandler();
	// datatable
	$("#items-table").dataTable();


	// Convert labels to watermarks
//	$.watermark.options.className = 'myClass';
//	$('.wm').hide().each(function () {
//		var $text = $(this).text();
//		$(this).next('div.editor-field').find('input').watermark($text); //.addClass('wmed');
//	});

//	$(".email input").watermark('user@mail.com');

	$("#relist-date").datepicker();


	$(".image-print-icon").live('click', function () {
		var image = $(".cboxPhoto").attr("src");
		printImage(image);
	
	});

	// ajax notes
	ajaxNotes();
	showSideBar();
});


//add broken file link to the missing icons
function errorThumbHandler() {
	var myUrl = "/inc/image/file_broken.png";
	$('.thumb').one("error", function () {
		$(this).attr("src", myUrl);
	});

}

function missingImageHandler() {
	$('#ctl00_ContentPlaceHolder1_imgagtacert').one("error", function () {
		var newFilePath = $(this).attr("src") + ".jpg";
		$(this).attr("src", newFilePath);
	});
}

function showSideBar() {
	var urlStr = window.location.pathname;
	if (urlStr.indexOf("HowItWorks") != -1 || urlStr.indexOf("About") != -1) 
	{
		$("#sidebar").hide();
	}
	return true;
}


function ajaxNotes() {
	$("#add-note-submit").live('click', function () {
		var x = { noteText: $("#noteText").val(), isAjax: true };
		$.post($("#add-note-form").attr("action"), x, function (data) {
			if (data && data.success) {
				var y = $('<li>').append('<div class="note-text">' + data.text + '</div>').append('<div class="note-delete"><a>Delete</a></div>').append('<div class="note-date">' + data.date + '</div>');
				$(".detail-remarks ul").prepend(y);
			}
		});
		return false;
	});
}

function loginImage() {
	if (window.location.pathname == "/" || window.location.pathname == "/Home/LogIn") {
		$("#wrapper").addClass("home");
	}
}

function popUpRemarks() {
	$("#items-table tbody tr .r").delegate("a", "mouseover", function (event) {
		if ($(this).next(".pop-remarks").length > 0) {
			$(this).qtip({
				content: function () { return $(this).next(".pop-remarks").html(); },
				overwrite: false,
				position: {
					my: 'middle right',
					at: 'middle left',
					adjust: {
						y: 2,
						x: -2
					}
				},
				show: {
					event: event.type,
					ready: true
				}
			}, event);
			event.preventDefault();
		}
	});

	

}

function printImage(imagePath) {
	popup = window.open();
	popup.document.write("<img src=" + imagePath + " alt=''/>");
	popup.print();

	return true;
}
