triggeredCode = false;

// wrapper function to keep Safari/Chrome from triggering multiple alerts on click
function codeboxAlert(msg) {
	if (!triggeredCode) {
		triggeredCode = true;
		alert(msg);
		window.setTimeout(function() { triggeredCode = false; }, 500);
	}
}

// function to apply proper positioning CSS to the invisible Flash elements so they sit atop the parent elements
function applyCSS(e) {
	var parentElem = $('#' + e.id.replace('_applet',''));
	$('#' + e.id).css({
		position: 'absolute',
		top: parentElem.offset().top,
		left: parentElem.offset().left,
		width: parentElem.outerWidth(),
		height: parentElem.outerHeight(),
		zIndex: 10
	});
}

function bindCopyLinks() {
	// create Flash widgets and overlay on top of HTML elements
	$('.action_clipboard').css('display','block');
	$('.coupon_generic, .coupon_featured').each(function() {
		coupon_id = $(this).attr('id').replace('c','');
		// get coupon code for Flash widget
		if($(this).find('.codebox_code').length > 0) {
			code = encodeURI($(this).find('.codebox_code').text());
		}
		$(this).find('.codebox_button, .action_clipboard').each(function() {
			var flash_container = $('<div>', {
				id: $(this).attr('class') + '_applet' + $(this).attr('id').replace($(this).attr('class'), ''),
				css: {
					position: 'absolute',
					top: $(this).offset().top,
					left: $(this).offset().left,
					width: $(this).outerWidth(),
					height: $(this).outerHeight(),
					zIndex: 10
				}
			}).appendTo($('body'));
			swfobject.embedSWF('/CopyToClipboard.swf',
							   flash_container.attr('id'),
							   flash_container.outerWidth(),
							   flash_container.outerHeight(),
							   '9.0.0',
							   false,
							   {
									'coupon_code': code
							   },
							   {
									'allowscriptaccess': true,
									'allowfullscreen': false,
									'quality': 'high',
									'scale': 'exactfit',
									'wmode': 'transparent'
							   },
							   {
									'styleclass': $(this).attr('class') + '_applet'
							   },
							   applyCSS);
		});
		$(this).find('.codebox_code').each(function() {
			swfobject.embedSWF('/codefield_mrscoupons.swf',
							   $(this).attr('id'),
							   $(this).outerWidth(),
							   $(this).outerHeight(),
							   '9.0.0',
							   false,
							   {
									'w': $(this).outerWidth(),
									'h': $(this).outerHeight(),
									'coupon_code': code
							   },
							   {
									'allowscriptaccess': true,
									'allowfullscreen': false,
									'quality': 'high',
									'scale': 'exactfit',
									'wmode': 'transparent'
							   },
							   {
									'styleclass': 'codebox_code_applet',
									'id': 'codebox_code_applet' + $(this).attr('id').replace('codebox_code_', '')
							   });
		});
	});
}



