/**
 cor-ajax-base.js
 @package      Core
 @copyright    Copyright (c) 2007 - 2009 Ragnar.cz. All rights reserved
 @license      GNU/GPL http://www.gnu.org/copyleft/gpl.html

 Albireo is free software!
*/
/////////////////////////////redundant functions//////////////////////////////////////
    // MSIE doesn't know XMLHttpRequest that opens Ajax in rest of browsers,
    // so we will create it from ActiveXObject
    if ( typeof XMLHttpRequest == "undefined" ) {
    	XMLHttpRequest = function () {
    	    return new ActiveXObject (
    	    	// MSIE 5 uses for Ajax object different from next versions
    	    	navigator.userAgent.indexOf ( "MSIE 5" ) >= 0 ?
    	    	"Microsoft.XMLHTTP" : "Msxml2.XMLHTTP"
    	    );
    	};
    }

    // is called for checking http states in ajax triggers
    function httpSuccess ( r ) {

    	try {
    	    // no status means local file and the request succeeded
    	    return !r.status && location.protocol == "file:" ||
    	    	// all status from 200 to 299 is good
    	    	( r.status >= 200 && r.status < 300 ) ||
    	    	// document hasn`t changed, successful end
    	    	r.status == 304 ||
    	    	// browser Safari sends blank status for unchanged documents
    	    	navigator.userAgent.indexOf ( "Safari" ) >= 0 && typeof r.status == "undefined";
    	} catch ( e ) {}

	// exception means unsuccessful end
    	return false;
    }
/////////////////////////////redundant functions//////////////////////////////////////
    // next function will change element by Ajax response
    function changeIdByAjax ( html, id ) {

    	if ( html.indexOf ( "<body>" ) != -1 ) {

    	    //$("#"+id).css ( "display", "none" );
    	    $("#"+id).fadeIn ( 500 );
    	    // we work with xml response as text becouse browsers (aside from Firefox) can`t append xml tags as html into document
    	    var response = html.substring ( html.indexOf ( "<body>" )+6, html.indexOf ( "</body>" ) );
            $("#"+id).html ( response );

    	} else {
            $("#"+id).html ( '<p><strong class="warning">Response has no contents!</strong>' );
        }

    }

    // function for showing and hiding messages. It`s good for combinating with ajax
    function showAndHide ( id ) {
            $("#"+id).css ( "display", "none" );
    	    $( "#"+id  ).fadeIn ( "slow" );
    	    setTimeout ( function () {
                $( "#"+id ).fadeOut ( "slow" );
            }, 6000 );
    }

    function ajaxError ( id ) {
        $("#"+id).html("Ajax error in communication with server");
    }

    function additionalContents ( target, author ) {
        if ($("#"+target).is(":hidden")) {
            $("#"+target).slideDown("slow");
            contents = $(author).html();
            $(author).html(contents.replace("arrow-right.gif","arrow-down.gif"));
        } else {
            $("#"+target).slideUp("slow");
            contents = $(author).html();
            $(author).html(contents.replace("arrow-down.gif","arrow-right.gif"));
        }
    }