/**
 * create date: 2009-8-31
 * @requires jQuery v1.3+
 */
$(function(){
	var date = new Date(), timestamp = Date.parse(date);
	var rand = getRand(1000, 9999);
	var flag = String(timestamp + date.getMilliseconds()) + rand;
	var requestUrl = "/mini/audi/index.php?controller=puzzles&action=getlucky&flag=" + flag;
	var returnUrl = "/mini/audi/index.php?controller=puzzles&action=puzzles&flag=" + flag;
	if (false) {
	//if ($.cookie("user_logged") == "oui") {
		var puzz = $.cookie("puzzlestime");
		if (!$.cookie("puzzlestime")) {
			puzz = 0;
		}
		if ((timestamp - parseInt(puzz)) > 0) {
			rand = getRand(1, 60);
			if (rand==10||rand==20||rand==30||rand==40||rand==50||rand==60) {
				//if(rand%2==0){
					var requestUrl = "/mini/09xmascar/index.php?controller=puzzles&action=getpuzzles&flag=" + flag;
					var returnUrl = "/mini/09xmascar/index.php?controller=puzzles&action=puzzles&flag=" + flag;
				//}
				$.cookie("puzzlestime", timestamp);
				$.post(requestUrl, {
					check: "pass"
				}, function(json){
					if (json.result != "0") {
						var btnText = (json.result == 1) ? "你已收藏" : "收入拼版";
						//var url = "/mini/audi/index.php?controller=puzzles&action=puzzles&flag=" + flag;
						var html = "<div class=\"jigsaw-container\">" +
								   "<a href=\"" + returnUrl +	"\" target=\"_blank\">" +
								   "<img src=\"" + json.src + "\" alt=\"图像\" title=\"" + btnText + "\" />" +
								   "<span>" + btnText +	"</span>" +
								    "<em>×</em>"+
								   "</a>" +
								   "<p>" + json.text + "</p>" +
								   "</div>";
						$("#main,#wrapper").append(html);
						positionFixed(".jigsaw-container", 100, 5);
						$(".jigsaw-container").show().find("a").hover(function(){
							$(this).next("p").show("slow");
						}, function(){
							$(this).next("p").hide();
						}).click(function(){
							$(".jigsaw-container").hide();
						}).find("em").click(function(e){
							e.preventDefault();
							e.stopPropagation();
							$(".jigsaw-container").hide();
						});
						
						setTimeout(function(){
							$(".jigsaw-container").hide();
						}, 300000);
					}
				}, "json");
			}
		}
	//}
	}
});

function getRand(firstValue, lastValue){
	var choices = lastValue - firstValue + 1;
	return Math.floor(Math.random() * choices + firstValue);
}

function positionFixed(o, top, left){
	// check is IE6
	if ($.browser.msie && $.browser.version == "6.0") 
		$(window).scroll(function(){
			$(o).css({
				top: (document.documentElement.scrollTop + top) + "px",
				left: (document.documentElement.scrollLeft + left) + "px"
			})
		});
	else {
		var left, top;
		left = ($("#main").size() == 0) ? $("#wrapper").offset().left : $("#main").offset().left;
		top = ($("#main").size() == 0) ? $("#wrapper").offset().top : $("#main").offset().top;
		$(o).css({
			position: "fixed",
			top: (top + 100) + "px",
			left: (left + 5) + "px"
		});
	}
}
