var arVersion = navigator.appVersion.split("MSIE");
var version = parseFloat(arVersion[1]);
if(typeof HTMLElement!="undefined" && !HTMLElement.prototype.insertAdjacentElement) 
{ 
     HTMLElement.prototype.insertAdjacentElement = function(where,parsedNode) 
     { 
         switch (where) 
         { 
             case 'beforeBegin': 
                 this.parentNode.insertBefore(parsedNode,this) 
                 break; 
             case 'afterBegin': 
                 this.insertBefore(parsedNode,this.firstChild); 
                 break; 
             case 'beforeEnd': 
                 this.appendChild(parsedNode); 
                 break; 
             case 'afterEnd': 
             if (this.nextSibling) this.parentNode.insertBefore(parsedNode,this.nextSibling); 
                     else this.parentNode.appendChild(parsedNode); 
                 break; 
         } 
     } 

     HTMLElement.prototype.insertAdjacentHTML = function (where,htmlStr) 
     { 
         var r = this.ownerDocument.createRange(); 
         r.setStartBefore(this); 
         var parsedHTML = r.createContextualFragment(htmlStr); 
         this.insertAdjacentElement(where,parsedHTML) 
     } 

     HTMLElement.prototype.insertAdjacentText = function (where,txtStr) 
     { 
         var parsedText = document.createTextNode(txtStr) 
         this.insertAdjacentElement(where,parsedText) 
     } 
} 

function save_treasure(treasure_id)
{
	var user_name = $.cookie('goyeah_user_name');
	var uid = $.cookie('goyeah_uid');
	if(user_name == null && uid  == null)
	{
                $.openPopupLayer({
        		name: "myStaticPopup",
        		width: 550,
        		target: "_ajax_div"
      	});
    	$('#popupLayer_myStaticPopup').html(popLoginDiv);
    	fix_png();
	}
	else
	{		
		var rnd = Math.random();
		$.ajax({
				 type : "GET",
				 url : "index.php?control=treasure&action=save_treasure&treasure_id="+treasure_id+"&rnd="+rnd,
				 data : 'html',
				 success : function(msg){
					 	$.openPopupLayer({
						name: "myStaticPopup",
						width: 550,
						target: "save_treasure"
						});
					    $('#popupLayer_myStaticPopup').html(msg);
				 }
		});
	}
}
function show_treasure(index)
{
	//index= 1;
	var w = document.body.scrollWidth;
	var h = document.body.scrollHeight;
	
	x = parseInt(Math.random()*w+1);
	y = parseInt(Math.random()*h+1);
	if(x+50>w)
	{
		x = x - 50;
	}
	if(y + 50 > h)
	{
		y = y - 50;
	}
	if(y - 50 < 100)
	{
		y = y + 100;
	}
	var treasure = document.createElement("IMG");
	treasure.src = 'images/duobao/ico/m-0' + index + '.png';
	if ((version >= 5.5 && version < 7.0) && (document.body.filters)) 
	{
        treasure.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + treasure.src.replace('%23', '%2523').replace("'", "%27") + "', sizingmethod=scale)";
        treasure.src = "images/header/blank.gif";
	}
	treasure.id = 'treasure_ico_'+index;
	treasure.style.position = "absolute";
	treasure.style.left = x + "px";
	treasure.style.top = y + "px";
	treasure.style.cursor = "pointer";
	treasure.style.zIndex = 9999;
	document.getElementById('treasure_div').appendChild(treasure);
	//document.body.insertAdjacentElement("beforeend",treasure);
	$('#treasure_ico_'+index).bind('click', function(){save_treasure(this.id.replace('treasure_ico_', ''));});
}
$.ajax({
		 type : "GET",
		 url : "index.php?control=treasure&action=show_treasure&rnd="+Math.random(),
		 data : 'html',
		 success : function(msg){if(msg!= ''){show_treasure(msg);}}
});
