$(".truncated").each(function(){$(this).after('<span class="expander">&#8230; <a href="#">expand</a></span>')});
$(".expander a").live("click",function(b){b.preventDefault(),$(this).parent().prev().show(),$(this).parent().hide()});

/**
 * ZeroClipboard
 * */
var clip = null;
var CouponTipsId = "CouponTips";
var elmCouponTips = null;
var isFlash = !(window.clipboardData && window.clipboardData.setData); // IE?
ZeroClipboard.setMoviePath( "http://www.couponsjunction.com/template/js/ZeroClipboard.swf" );
$(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 jQuery
	$('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;
		if (div.parentNode.href != undefined)
		{
			open_url(div.parentNode.href)
		}
		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 = jQuery(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 + jQuery(elm).width() + 0) + 'px';
		elmCouponTips.style.top = (offsety + jQuery(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);
    }
    open_url(elm.parentNode.href);
    
    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';
	}
}
/**
 * END ZeroClipboard
 * */
