// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

Ajax.Responders.register({
  onCreate: function() {
    if($('busy') && Ajax.activeRequestCount>0) {
      Effect.Appear('busy',{duration:0.5,queue:'end'});
    }
  },
  onComplete: function() {
    if($('busy') && Ajax.activeRequestCount==0)
      Effect.Fade('busy',{duration:0.5,queue:'end'});
  }
});

function toggleMenuItem(id) {
    menus = document.getElementsByClassName('submenu')
    menus.each(function(item) {
        if (item.visible()) {        
            Effect.Fade(item, {duration:0.5});
        }
    });
    
    $(id).visible() ? Effect.Fade(id, {duration:0.5}) : Effect.Appear(id, {duration:0.5});
}

/***********************************************
This callback is for image handling in the post editor.
**************************************************/
function blogExecCommandHandler(editor_id, elm, command, user_interface, value) {
    switch (command) {
        case "mceImage", "mceAdvImage":
            var url = "/site_images/imageselect";
            var pars = "";
            var imgAjax = new Ajax.Updater(
                "dlgplace",
                url,
                {
                    method: 'get',
                    parameters: pars,
                    onComplete: showFloatDialog
                })        
            return true;
    }
    return false
}

function showFloatDialog() {
    Effect.Appear('dlgplace');
}

function hideFloatDialog() {
    Effect.Fade('dlgplace');
}

function postInsertImage(src) {
    tinyMCE.execCommand('mceFocus', false, "mce_editor_0");
    tinyMCE.execCommand("insertimage", null, src);
    hideFloatDialog();    
}

function set_class_name(element, class_name) {
    new Element.ClassNames(element).set(class_name);
}

function fadeClick(url) {
	$$('body div').first().visualEffect('fade', {duration:'0.5'});
	setTimeout(function() {
		document.location = url;
	}, 1000);
}

function fadeIn() {
	$$('body div').first().visualEffect('appear', {duration:'0.5'});
}

function convertAllLinksToFadeOut() {
	$$('a').each( function(link) {
		// append to the attribute in case there's something else there. 
		// This should be the least damaging.
		att = link.getAttribute('onclick') || '';
		link.setAttribute('onclick', att + 'fadeClick(\'' + link["href"] + '\'); return false;');
	})
}

// function convertExternalLinks() {
// 	$$('a').each( function(link) {
// 		if link["href"]
// 		url = document.location["href"]
// 		if (url != "") {
// 			url_pieces = url.split('/')
// 			current_url = url_pieces[0] + '//' + url_pieces[2]
// 			redirect_to(current_url + url);
// 		}
// 		att = link.getAttribute('onclick') || '';
// 		link.setAttribute('onclick', att + 'fadeClick(\'' + link["href"] + '\'); return false;');
// 	})
// }
// 
function hideDocument() {
	$$('body div').first().hide();
}

// // Not tested.
// function appendToOnLoad(window, a_function) {
// 	if (window.onload)
// 		return function() {
// 			window.onload()
// 			a_function();
// 		}
// 	else
// 		window.onload = a_function;
// }
