// JavaScript Document
( function($) {

    $(function() {
        OPALCO_CTA.init();
    
    });

    OPALCO_CTA = {

        init : function() {
				
					// if this is the add CTA page
		if ($('#cta-sample').length > 0) {
		
			// bkLib.onDomLoaded(nicEditors.allTextAreas);
			elements = jQuery("#cta_desc");
    var config = {
		buttons : {
		'bold' : {name : __('Click to Bold'), command : 'Bold', tags : ['B','STRONG'], css : {'font-weight' : 'bold'}, key : 'b'},
		'italic' : {name : __('Click to Italic'), command : 'Italic', tags : ['EM','I'], css : {'font-style' : 'italic'}, key : 'i'},
		'underline' : {name : __('Click to Underline'), command : 'Underline', tags : ['U'], css : {'text-decoration' : 'underline'}, key : 'u'},
		'left' : {name : __('Left Align'), command : 'justifyleft', noActive : true},
		'center' : {name : __('Center Align'), command : 'justifycenter', noActive : true},
		'right' : {name : __('Right Align'), command : 'justifyright', noActive : true},
		'justify' : {name : __('Justify Align'), command : 'justifyfull', noActive : true},
		'ol' : {name : __('Insert Ordered List'), command : 'insertorderedlist', tags : ['OL']},
		'ul' : 	{name : __('Insert Unordered List'), command : 'insertunorderedlist', tags : ['UL']},
		'subscript' : {name : __('Click to Subscript'), command : 'subscript', tags : ['SUB']},
		'superscript' : {name : __('Click to Superscript'), command : 'superscript', tags : ['SUP']},
		'strikethrough' : {name : __('Click to Strike Through'), command : 'strikeThrough', css : {'text-decoration' : 'line-through'}},
		'removeformat' : {name : __('Remove Formatting'), command : 'removeformat', noActive : true},
		'indent' : {name : __('Indent Text'), command : 'indent', noActive : true},
		'outdent' : {name : __('Remove Indent'), command : 'outdent', noActive : true},
		'hr' : {name : __('Horizontal Rule'), command : 'insertHorizontalRule', noActive : true}
	},
        iconsPath : "../wp-content/plugins/opalco-ctas/assets/images/nicEditorIcons.gif",
        buttonList : ['bold','italic','underline','left','center','right','justify','ol','ul','indent','outdent','link','unlink','xhtml'],
				iconList : {"xhtml":1,"bold":2,"center":3,"hr":4,"indent":5,"italic":6,"justify":7,"left":8,"ol":9,"outdent":10,"removeformat":11,"right":12,"save":13,"strikethrough":14,"subscript":15,"superscript":16,"ul":17,"underline":18,"image":19,"link":20,"unlink":21,"close":22,"arrow":23}
    };

    for (i = 0; i < elements.length; i++) {
        new nicEditor(config).panelInstance(elements[i].id);
    }
		
			$('#cta_title').keyup(function() { 
				$('h3.cta-title').html($(this).val());
			});
			$('#cta_desc').keyup(function() { 
				$('div.cta-text p').html($(this).val());
			});
			$(document).keyup(function() { 
				$('div.cta-text p').html($('div.nicEdit-main').html());
			});
			
			$('#cta_link_id').change(function() {
				$('.cta-link-url').html($(this).find('option:selected').attr('title'));
			});
			
			$('#cta_image_id').change(function() {
				$('.cta-image').html('<img src="'+$(this).val()+'" />');
			});
				
		}
				
        },

        delete_cta : function(cta_id, confirm_message) {
            var delete_group = confirm(confirm_message);
            if ( delete_group ) {
                $.post('../wp-content/plugins/opalco-ctas/assets/javascript/misc.php', { action : 'delete_cta', cta_id : cta_id }, function(ajaxReturn) {
                    $('#cta-' + cta_id).fadeOut('slow', function() {
                        $(this).remove();
                    });
                });
            }
        },

    };

})(jQuery);