var clip = null;
var CouponTipsId = "CouponTips";
var elmCouponTips = null;
var isFlash = !(window.clipboardData && window.clipboardData.setData); // IE?
ZeroClipboard.setMoviePath( cbmURL );
$(document).ready(function() {
	// setup single ZeroClipboard object for all our elements
	clip = new ZeroClipboard.Client();
	clip.setHandCursor( true );
	
	// assign a common mouseover function for all elements using $
	$('div.cpncode').mouseover( function() {
		setCouponTips(this);
	} );
	
	// add an event handler which fires after text is copied to the clipboard
	// and change the color of the underlying DOM element
	clip.addEventListener('complete', function(client, text) {
		var div = client.domElement;
		var j=$(div).parent().metadata({type:"elem",name:"script"})
		var k=j.aurl;
		if (k != undefined)
		{
			open_url("/go/?url="+k.replace(/mydotmy/g,"."));
		}
		hideCouponTips();
	} );
});
function setCouponTips(elm)
{
    if (isFlash)
    {
		// set the clip text to our innerHTML
		clip.setText( elm.innerHTML );
		
		// reposition the movie over our element
		// or create it if this is the first time
		if (clip.div) {
			clip.receiveEvent('mouseout', null);
			clip.reposition(elm);
		}
		else clip.glue(elm);
		
		// gotta force these events due to the Flash movie
		// moving all around.  This insures the CSS effects
		// are properly updated.
		clip.receiveEvent('mouseover', null);
		clip.addEventListener('onMouseOver', function (client) {
			showCouponTips(client.domElement);
		});
		clip.addEventListener('onMouseOut', function (client) {
			hideCouponTips();
		});
    }
    else
    {
        elm.onclick = function() { clickCoupon(elm); return false; };
        elm.onmouseout = function() { hideCouponTips(); };
    }
	showCouponTips(elm);
}
function showCouponTips(elm)
{
	$item = $(elm).parent().find('[name=CouponTips]');
	if ($item.length == 0)
	{
		elmCouponTips = document.getElementById(CouponTipsId);
	}
	else
	{
		elmCouponTips = $item.get(0);
	}

	if ((elmCouponTips != null) && (elmCouponTips.style.display != 'block'))
	{
		spanLoc = getCouponXY(elm);
		elmCouponTips.onclick = function() {  clickCoupon(elm); return false;};
		
		var offsetx = spanLoc.x;
		var offsety = spanLoc.y;
		if ($item.length != 0) {
			spanParentLoc = getCouponParentXY(elm);
			offsetx = spanLoc.x - spanParentLoc.x;
			offsety = spanLoc.y - spanParentLoc.y;
		}
		
		elmCouponTips.style.left = (offsetx + $(elm).width() + 0) + 'px';
		elmCouponTips.style.top = (offsety + $(elm).height()) + 'px';
		elmCouponTips.zIndex = '200';

		elmCouponTips.style.display = 'block';
	}
}
function getCouponParentXY(obj)
{
    var curLeft = curTop = 0;
    
    if (obj.offsetParent)
    {
        while (obj = obj.offsetParent)
        {
            curLeft += obj.offsetLeft;
            curTop += obj.offsetTop
        }
    }

    return { x : curLeft, y: curTop };
}
function getCouponXY(obj)
{
    var curLeft = curTop = 0;
    
    if (obj.offsetParent)
    {
        curLeft = obj.offsetLeft;
        curTop = obj.offsetTop;
        while (obj = obj.offsetParent)
        {
            curLeft += obj.offsetLeft;
            curTop += obj.offsetTop
        }
    }

    return { x : curLeft, y: curTop };
}
function clickCoupon(elm)
{
    if (!isFlash)
    {
        window.clipboardData.setData("Text", elm.innerHTML);
    }
	var j=$(elm).parent().metadata({type:"elem",name:"script"})
	var k=j.aurl;
	if (k != undefined)
	{
		open_url("/go/?url="+k.replace(/mydotmy/g,"."));
	}
    
    return true;
}

function open_url(url)
{
	var win = window.open(url, '_blank', '');
    if (!win)
    {
        document.location = url;
    }
    return;
}
function hideCouponTips()
{
	if (elmCouponTips != null)
	{
		elmCouponTips.style.display = 'none';
	}
}

