/* Sliding Menu */
$(document).ready(function(){

    // Manage Menu Toggle
    $('div[class^=slickbox]').hide();
    $('div[class^=add]').hide();
    // toggles the arrows in menu
    $('a[class^=box]').click(function() {
        var $this = $(this);
        var x = $this.attr("className");

        toggle = 'down';
        if($('.slick' + x).css('display') !='block')
            toggle = 'up';
        $(this).css('background-image','url(/images/'+ toggle +'Arrow.gif)');
        $('.slick' + x).toggle(400);
        return false;
    });
    // toggles add button
    $('a[class^=toggle]').click(function() {
        var $this = $(this);
        var x = $this.attr("className");
        $('.add' + x).toggle(400);
        return false;
    });

    // Hide Paid Icon on Click
    $('[id^=hide]').click(function(){
        $(this).hide();
        return false;
    });

    // Hide rows on aprrove
    $('[id^=approve]').click(function(){
        var row = $(this).closest("tr").get(0);
        $(row).hide();
        return false;
    });

		// WYSIWIG
    activateEditor();
		ajaxLinks();
		// $('.ajaxForm').validate({
		//    submitHandler: function(form) {
		//    	$(form).ajaxSubmit();
		//    }
		// });

    // Autocomplete
    $("input#donation_business_name").autocomplete({ 
        ajax: "businesses"
    });

    //LOADING POPUP
    //Click the button event!
    $('a[class^=Contact]').live('click', function(){
        var $this = $(this);
        var x = $this.attr("className");
        var popup = $("#popup" + x);
        //centering with css
        centerPopup(popup);
        //load popup

        $("#backgroundPopup2").css({
            "opacity": "0.7"
        });
        $("#backgroundPopup2").fadeIn("slow");
        $(popup).fadeIn("slow");
        popupStatus = 1;

        return false;
    });

    $('a[class^=Edit]').live('click', function(){
        var $this = $(this);
        var x = $this.attr("className");
        var popup = $("#popup" + x);
        $('[id^=flash_notice]').hide();
        $('[id^=edit_business]').show();
        //centering with css
        centerPopup(popup);
        //load popup
        loadPopup(popup);
        return false;
    });

    

    //SETTING UP OUR POPUP
    //0 means disabled; 1 means enabled;


    //loading popup with jQuery magic!
    function loadPopup(popup){
        //loads popup only if it is disabled

        $("#backgroundPopup").css({
            "opacity": "0.7"
        });
        $("#backgroundPopup").fadeIn("slow");
        $(popup).fadeIn("slow");
    }

    //disabling popup with jQuery magic!
    function disablePopup(){
        //disables popup only if it is enabled
        $.prompt('Any changes have not been saved, do you wish to close?', {
            buttons: {
                Yes: false,
                No: true
            },
            focus: 1,
            submit:function(v){
                if(v) return true;

                else
                    $("#backgroundPopup").fadeOut("slow");
                $(".popupEdit").fadeOut("slow");
                $.prompt.close();
                return false;
            }

        });
    }

    //centering popup
    function centerPopup(popup){
        var windowHeight = $(window).height();
        $(popup).centerInClient();

        $('#backgroundPopup').css({
            "height": windowHeight
        });
    }
    //CLOSING POPUP
    //Click the x event!
    $('a[class^=popupClose]').live('click', function(){
        disablePopup();
    });

    $('a[class^=popupContactClose]').live('click', function(){
        $("#backgroundPopup2").fadeOut("slow");
        $(".popupContact").fadeOut("slow");

    });

    //Click out event!
    $("#backgroundPopup").click(function(){
        disablePopup();
    });
    $("#backgroundPopup2").click(function(){
        $("#backgroundPopup2").fadeOut("slow");
        $(".popupContact").fadeOut("slow");

    });

    $('.error').hide();

    // Form Validation
    $('.hint').hint();

		// activate jscal2 calendars
		setup_triggers_for_calendars();
});

function activateEditor(root) {
	// WYSIWYG
	if (root == null) {
		root = YAHOO.util.Selector.document;
	}
  var editors = YAHOO.util.Selector.query('textarea.RTE', root);
  for (var i = 0; i < editors.length; i++) {
    var editor = new YAHOO.widget.SimpleEditor(editors[i], {
    handleSubmit:true,
    height: '200px',
    width: '450px',
    toolbar: {
      buttons: [
        {group:'textstyle', label: 'Font Style',
            buttons: [
              {type: 'push', label: 'Bold', value: 'bold'},
              {type: 'push', label: 'Italic', value: 'italic'},
              {type: 'push', label: 'Underline', value: 'underline'},
              {type: 'separator'}
            ]},
	    { group: 'indentlist', label: 'Lists', 
        	buttons: [ 
		      { type: 'push', label: 'Create an Unordered List', value: 'insertunorderedlist' }, 
		      { type: 'push', label: 'Create an Ordered List', value: 'insertorderedlist' } ,
              {type: 'separator'}
		]}, 
        {group:'insertitem', label: 'Insert',
            buttons: [
              { type: 'push', label: 'Insert Link', value: 'createlink', enabled: true},
              { type: 'push', label: 'Insert Image', value: 'insertimage' }
            ]}
      	]}
	});
	editor._defaultToolbar.titlebar = false;
 	/*if ($(editors[i]).hasClass('admin')) {
	    YAHOO.log('Set state to off..', 'info', 'example');
	 	YAHOO.log('Create the (editcode) Button', 'info', 'example');
	 
	 	editor.on('toolbarLoaded', function() {
 			var state = 'off';
 		        var codeConfig = {
 		            type: 'push', label: 'Edit HTML Code', value: 'editcode'
 		        };
 		        YAHOO.log('Create the (editcode) Button', 'info', 'example');
 		        this.toolbar.addButtonToGroup(codeConfig, 'insertitem');
 
 		        this.toolbar.on('editcodeClick', function() {
 		            var ta = this.get('element'),
 		                iframe = this.get('iframe').get('element');
 
 		            if (state == 'on') {
 		                state = 'off';
 		                this.toolbar.set('disabled', false);
 		                YAHOO.log('Show the Editor', 'info', 'example');
 		                YAHOO.log('Inject the HTML from the textarea into the editor', 'info', 'example');
 		                this.setEditorHTML(ta.value);
 		                if (!this.browser.ie) {
 		                    this._setDesignMode('on');
 		                }
 
 		                Dom.removeClass(iframe, 'editor-hidden');
 		                Dom.addClass(ta, 'editor-hidden');
 		                this.show();
 		                this._focusWindow();
		            } else {
 		                state = 'on';
 		                YAHOO.log('Show the Code Editor', 'info', 'example');
 		                this.cleanHTML();
 		                YAHOO.log('Save the Editors HTML', 'info', 'example');
 		                Dom.addClass(iframe, 'editor-hidden');
 		                Dom.removeClass(ta, 'editor-hidden');
 		                this.toolbar.set('disabled', true);
 		                this.toolbar.getButtonByValue('editcode').set('disabled', false);
 		                this.toolbar.selectButton('editcode');
 		                this.dompath.innerHTML = 'Editing HTML Code';
 		                this.hide();
 		            }
 		            return false;
 		        }, this, true);
 
 		        this.on('cleanHTML', function(ev) {
 		            YAHOO.log('cleanHTML callback fired..', 'info', 'example');
 		            this.get('element').value = ev.html;
 		        }, this, true);
 
 		        this.on('afterRender', function() {
 		            var wrapper = this.get('editor_wrapper');
		            wrapper.appendChild(this.get('element'));
 		            this.setStyle('width', '100%');
 		            this.setStyle('height', '100%');
 		            this.setStyle('visibility', '');
 		            this.setStyle('top', '');
 		            this.setStyle('left', '');
 		            this.setStyle('position', '');
 
 		            this.addClass('editor-hidden');
 		        }, this, true);
 		    }, editor, true);
	 }*/
		
		editor.render();
		
		$('select#event_id').change(function() {
			$.get($(this).attr("href"), $(this).serialize(), null, "script");
		});
	}
}

function setup_triggers_for_calendars(root) {
	// Calendar Pop up
	if (root != null) {
		root = '#' + root + ' ';
	} else {
		root = '';
	}
	
	$(root + 'img.calendar-trigger').each(function() {
		Calendar.setup({
      trigger    : this,
      inputField : $(this).prev('input')[0],
			dateFormat : "%Y-%m-%d at %l:%M%P",
			showTime	 : 12,
			date			 : new Date(),
			onSelect   : function() { this.hide() }
    });
	});
}
