$(document).ready(function() { 
	$('a.modal').nyroModal({
		bgColor: '#999999',
		minWidth: 350,
		minHeight: 0
	});	
    
	$(document).pngFix();


	$('#printBar').live( 'click', function() {
		var couponsList = [ ];
		$('input.clipCoupon:checked').each(function(idx, val) {
			couponsList.push( $(this).attr('cid'));
			$(this).attr('checked', false);
		});
     		window.open('/coupon/printbatch/ids/' + couponsList.join(','));
		$(this).remove();
		$('html, body').animate({scrollTop: 0}, 'slow');
	});
	$('input.clipCoupon').click( function() {
		if ( $(this).is(':checked')) {
			var that = this;
			$.getJSON('/coupon/canClip?cid=' + $(this).attr('cid'), function (data) {
				if (data === true) {
					if ( $('#printBar').length == 0) {
		 				$('#background').append ('<div id="printBar" style="cursor: pointer; width: 100%; padding: 8px 0 8px 0; text-align:center; position: fixed; bottom: 0px; background-color: #FFF797; color: #000; font-size: 180%;">Click here to print your coupons</div>');
					}
				} else {
					$(that).attr('checked', false);
					var position = $(that).parents('.list-coupon');
					var dialog = $('<div></div>')
						.html('In ' + data.days + ' day(s) you can clip it again.<br/><br/>Clipping limit for this coupon is:<br/>1 per customer per ' + data.recurrence + ' day(s)')
						.dialog({
							position: [ (position.offset().left+4 - $(window).scrollLeft()), (position.offset().top+4 - $(window).scrollTop()) ],
							width: 335,
							height: 180,
							autoOpen: true,
							modal: true,
							title: 'Already Clipped'
						});
					setTimeout(function() {
						dialog.dialog('close');
					}, 5000);
				}
			});

		} else {
			// Lets see if anything is checked, otherwise we'll hide the bar
			if ( $('input.clipCoupon:checked').length === 0) {
				$('#printBar').remove();
			}
		}
	});

});


function createNewListModal(id)
{
	$(function() {
	    $.nyroModalManual({
	      url: '/newsletter/createMailinglist?newsletter_id='+id,
		  bgColor: '#999999',
		  minWidth: 350,
		  minHeight: 0
	    });
	    
	    return false;
    });
}


// Initiate TinyMCE interface
function initTinyMce(elements, type) {

	if(type == 'advanced') {
		
		tinyMCE_GZ.init({
			plugins : 'imagemanager,spellchecker,inlinepopups,advimage,fullscreen,advlink,preview,media,searchreplace,contextmenu,paste,directionality,noneditable,visualchars,nonbreaking,xhtmlxtras',
			themes : 'advanced',
			languages : 'en',
			disk_cache : true,
			convert_urls : false,
			debug : false
		});
		
		tinyMCE.init({
			mode : "exact",
			elements : elements,
			theme : "advanced",
			convert_urls : false,
			// plugins : "table,imagemanager,spellchecker,inlinepopups,fullscreen,advimage,advlink,preview,media,advhr,searchreplace,contextmenu,paste,directionality,noneditable,visualchars,nonbreaking,xhtmlxtras",
			plugins : "table,spellchecker,inlinepopups,fullscreen,advimage,advlink,preview,media,advhr,searchreplace,contextmenu,paste,directionality,noneditable,visualchars,nonbreaking,xhtmlxtras",
			theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,link,unlink,bullist,numlist,|,formatselect,fontsizeselect,pastetext,pasteword",
			theme_advanced_buttons2 : "outdent,indent,|,undo,redo,|,link,unlink,cleanup,code,|,forecolor,backcolor,sub,sup,|,charmap,iespell,|,ltr,rtl,|,advhr,fullscreen,media,image,|,spellchecker,table",
			theme_advanced_buttons3 : "",
			theme_advanced_blockformats : "h1,h2,h3,h4,h5,h6",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_statusbar_location : "bottom",
		 	theme_advanced_resize_horizontal : false,
      forced_root_block : false,
      force_br_newlines : true,
      force_p_newlines : false,
  		extended_valid_elements : "object[type|src|id|pluginspage|bgcolor|allowScriptAccess|allowFullScreen|quality|name|play|scale|menu|salign|scriptAccess|wmode|height|width|flashvars],embed[type|src|id|pluginspage|bgcolor|allowScriptAccess|allowFullScreen|quality|name|play|scale|menu|salign|scriptAccess|wmode|height|width|flashvars],param[name|value],a[class|style|name|href|target|title|onclick],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name|style],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]"
		});

	} else if(type == 'simple') {
	
		tinyMCE_GZ.init({
			themes : 'simple',
			languages : 'en',
			disk_cache : true,
			debug : false
		});
		
		tinyMCE.init({
			mode : "exact",
			elements : elements,
			theme : "simple"
		});
	
	
	}
}

// Click to expand
$(document).ready(function()
  {
    //hide all of the element with class click-to-expand-body
    $(".click-to-expand-body").hide();
    //toggle the componenet with class click-to-expand-body
    $(".click-to-expand-head").click(function()
    {
      $(this).next(".click-to-expand-body").slideToggle(60);
    });
});

// Click to expand that partially shows hidden area
$(document).ready(function() {
  var adjustheight = 109;
  $(".expand-area-toggle").toggle(function() {
    $(this).prev().css('height', 'auto').css('overflow', 'visible');
    $(this).text("[less]");
  }, function() {
    $(this).prev().css('height', adjustheight).css('overflow', 'hidden');
    $(this).text("[more]");
  });
});

