// Show confirmation box before deleting item
function confirm_delete(thing,message) {
	var message = (message == null) ? "" : message;
	the_message = "Are you sure you want to delete this " + thing + "?";
	if (message!="") {
		the_message += "\n\n" + message;
	}
	var answer = confirm(the_message);
	if (answer) {
		return true;
	} else {
		return false;
	}
}

// Hide status message after few seconds
function status_message() {
	$("#status-message").animate({opacity: 1.0}, 3000);
	$("#status-message").fadeOut('slow', function() { 
		$(this).css({'display' : 'none'});
	});
}

$(document).ready(function() { 
	$("#orders-table").dataTable({
		"aaSorting": [[ 1, "desc" ]],
		"bSort": true,
		"sPaginationType": "full_numbers"
	});
	
	$("#giftcards-table").dataTable({
		"bSort": true,
		"sPaginationType": "full_numbers"
	});
	
	$("#galleries").dataTable( {
		"bSort": true,
		"sPaginationType": "full_numbers"
	});
	
	var path = location.href
	path = path.split('/');
	path = path[(path.length-1)];
	
	if (path == 'home.html') {
		 //背景切换
		 rotateBg();
		 //在线客服漂浮窗口
		floatBlock = $('#onLineService');
		floatBlock.css("top", 0);
		floatBlock.css("visibility", "visible");
		setInterval("blockFloat()", 30);
	}
});

function rotateBg() {
if (typeof count == 'undefined') {
	count = 0;
}
if (count == 0) {
	$('#hidden3').fadeOut("slow")
	$('#hidden0').fadeIn("slow");
	count++;
	loadBg(count + 1, $('#hidden1'));
} else if (count == 1) {
	$('#hidden0').fadeOut("slow")
	$('#hidden1').fadeIn("slow");
	count++;
	loadBg(count + 1, $('#hidden2'));
} else if (count == 2) {
	$('#hidden1').fadeOut("slow")
	$('#hidden2').fadeIn("slow");
	count++;
	loadBg(count + 1, $('#hidden3'));
} else if (count == 3) {
	$('#hidden2').fadeOut("slow");
	$('#hidden3').fadeIn("slow");
	count++;
} else {
	var mCount = count % 4;
	if (mCount == 0 ) {
		$('#hidden3').fadeOut("slow");
		$('#hidden0').fadeIn("slow");
	} else {
		$('#hidden' + (mCount - 1 )).fadeOut("slow");
		$('#hidden' + mCount).fadeIn("slow");
	}
	count++;
}
	movement = setTimeout("rotateBg()",3500);
}

function loadBg(imageIndex, hidden) {
	var temp = hidden.css("background-image");
	var unchange = temp.substring(0, temp.indexOf(".jpg") - 1);
	hidden.css("background-image", unchange + imageIndex + ".jpg\")");
}

//漂浮窗口全局控制变量,这些变量是blockFloat函数使用的
var xPos = 20;
var yPos = 0;
var step = 1;
var yon = 0;
var xon = 0;
//全局变量，代表漂浮窗口或者图片,在使用时初始化
var floatBlock;

function blockFloat() {
	var width = ($("body").attr("clientWidth") - 800) / 2;
	var height = $("body").attr("clientHeight");
	var Hoffset = floatBlock.attr("offsetHeight");
	var Woffset = floatBlock.attr("offsetWidth");
	floatBlock.css("left", xPos + $("body").attr("scrollLeft"));
	floatBlock.css("top", yPos + $("body").attr("scrollTop"));
	if (yon) {
		yPos = yPos + step;
	} else {
		yPos = yPos - step;
	}
	
	if (yPos < 0) {
		yon = 1;
		yPos = 0;
	}
	
	if (yPos >= (height - Hoffset)) {
		yon = 0;
		yPos = (height - Hoffset);
	}
	
	if (xon) {
		xPos = xPos + step;
	} else {
		xPos = xPos - step;
	}
	
	if (xPos < 0) {
		xon = 1;
		xPos = 0;
	}
	
	if (xPos >= (width - Woffset)) {
		xon = 0;
		xPos = (width - Woffset);
	}
}