// JavaScript Document
function createDiv(id, content) {
	var newdiv = $(document.createElement("div"));
	newdiv.attr("id", id);
	newdiv.html(content);
	$('body').append(newdiv);
}

function destoryDiv(id) {
	$(id).remove();
}

function showDetails(type,id) {
	content = '<p>loading...</p>';
	createDiv('daikin_ui_detail', content);
	$("#daikin_ui_detail").load("project_details.php?t=" + type + "&id=" + id, function() {
		$.blockUI({
				  message: $('#daikin_ui_detail'),
				  css: {
					  padding: '0px',
					  margin: '0px',
					  cursor: 'default',
					  top: ($(window).height() - $('#daikin_ui_detail > table:first').height()) / 2 + 'px',
					  left: ($(window).width() - $('#daikin_ui_detail > table:first').width()) / 2 + 'px',
					  width: $('#daikin_ui_detail > table:first').width()
				  },
				  overlayCSS: {
					  backgroundColor: '#3CF',
					  opacity: .5
				  }
				  });
		$(document).bind('keydown', function(e) {
									var key = e.charCode? e.charCode : e.keyCode ? e.keyCode : 0;
									if (key == 27) {
										hideDetails();	
									}
								   });
																						});

}

function hideDetails() {
	$(document).unbind('keydown');
	$.unblockUI({
				onUnblock: function(){destoryDiv('#daikin_ui_detail');}
				});
}

function showPhoto(id) {
	content = '';
	createDiv('daikin_ui_detail', content);
	$("#daikin_ui_detail").load("media_details.php?id=" + id, function() {
		$.blockUI({
				  message: $('#daikin_ui_detail'),
				  css: {
					  padding: '0px',
					  margin: '0px',
  					  cursor: 'default',
					  top: ($(window).height() - $('#daikin_ui_detail > table:first').height()) / 2 + 'px',
					  left: ($(window).width() - $('#daikin_ui_detail > table:first').width()) / 2 + 'px',
					  width: $('#daikin_ui_detail > table:first').width()
				  },
				  overlayCSS: {
					  backgroundColor: '#3CF',
					  opacity: .5
				  }
				  });
		$(document).bind('keydown', function(e) {
									var key = e.charCode? e.charCode : e.keyCode ? e.keyCode : 0;
									if (key == 27) {
										hideDetails();	
									}
								   });
																						});

}

function moveleft(e) {
	p = e.data.px;
	disableButton();
	cssleft = $(".daikin_product_content").css("left").replace(/px/, "");
	w = $(".daikin_product_content > table:first").width();
	if (eval(cssleft) < 0) {
		offsetl = eval(cssleft.replace(/-/, ""));
	} else {
		offsetl = 0;
	}
	if (offsetl < p) {
		offset = "+=" + offsetl + "px";
	} else {
		offset = "+=" + p + "px";
	}
	if (offsetl > 0)
	{
		$(".daikin_product_content").animate({"left": offset}, 500, function(){enableButton(p);});
	} else {
		$.blockUI({ message: $('#daikin_ui_message') });
		setTimeout($.unblockUI, 2000);
		setTimeout(function(){enableButton(p);}, 2000);
	}
}

function moveright(e) {
	p = e.data.px;
	disableButton();
	cssleft = $(".daikin_product_content").css("left").replace(/px/, "");
	w = $(".daikin_product_content > table:first").width();
	if (eval(cssleft) < 0) {
		offsetr = w - p - (eval(cssleft.replace(/-/,"")));
	} else {
		offsetr = w - p;
	}
	if (offsetr < p) {
		offset = "-=" + offsetr + "px";
	} else {
		offset = "-=" + p + "px";
	}
	if (offsetr > 0) {
		$(".daikin_product_content").animate({"left": offset}, 500 , function(){enableButton(p);});
	} else {
		$.blockUI({ message: $('#daikin_ui_message')});
		setTimeout($.unblockUI, 2000);
		setTimeout(function(){enableButton(p);}, 2000);
	}
}

function disableButton() {
	$("#moveright").unbind('click');
	$("#moveleft").unbind('click');
}

function enableButton(p) {
	$("#moveright").bind("click", {px: p}, moveright);
	$("#moveleft").bind("click", {px: p}, moveleft);
}
function search_product() {
	err = '';
	if (document.getElementById('d').value == '') {
		err = 'please enter keyword.\n請輸入搜查字';
	}
	if (document.getElementById('d').value.length < 3) {
		err = 'please enter keyword with more than 2 characters.\n請輸入多於2個字元的搜查字';
	}
	if (err != '') {
		alert(err);
	} else {
		document.search_form.submit();
	}
}