var clip = null;
var linktag = null;
var gotolink = null;
var pos;

var x;
var y;
var mouseY = null;

ZeroClipboard.setMoviePath('/jscripts/swf/ZeroClipboard.swf');
// setup single ZeroClipboard object for all our elements
clip = new ZeroClipboard.Client();
clip.setHandCursor( true );

$().mousemove(function(e){
	x = e.pageX;
	y = e.pageY;
	if (y >= (mouseY+20) || y <= (mouseY-20)) {
		$("#codeTip").fadeOut(200);
	};
});

// create tooltip element for coupon code hovers
var codeTipText = 'Click to Copy Code to Clipboard.  At Checkout, Press Ctrl+V to Paste Coupon Code.';
$('#main').after('<div id="codeTip">'+codeTipText+'</div>');

// assign a common mouseover function for all elements using jQuery
$('a.corange').mouseover( function() {
	gotolink = $(this).attr("href")
	mouseY = y;
 // set the clip text to our innerHTML
	clip.setText( this.innerHTML );

	linktag = $(this).Attributes;


	// reposition the movie over our element
	// or create it if this is the first time
	if (clip.div) {
		clip.receiveEvent('mouseout', null);
		clip.reposition(this);
	} else clip.glue(this);

	// gotta force these events due to the Flash movie
	// moving all around.  This insures the CSS effects
	// are properly updated.
	clip.receiveEvent('mouseover', null);
	//alert(pos.left + " " + pos.top);

});


clip.addEventListener( 'onMouseOver', my_mouse_over_handler );
function my_mouse_over_handler( client ) {
	$("#codeTip").css({"left": x + "px","top": y + 20 + "px"}).fadeIn(200);
	//alert( "mouse is over movie" );
}
clip.addEventListener( 'onMouseOut', my_mouse_out_handler );
function my_mouse_out_handler( client ) {
	$("#codeTip").fadeOut(200);
	clip.hide();
	//alert( "mouse has left movie" );
}
clip.addEventListener( 'onMouseUp', my_mouse_up_handler );
function my_mouse_up_handler( client ) {
	//alert("You are now being redirected to the store website. The coupon code has been copied to your clipboard.");
	var storeWin=window.open(gotolink);
  if (storeWin) {
		return false;
	} else {
		document.location = gotolink;
	}
}
