var CJSDavsUtils = {
        
        OpenWindow: function(url, width, height)
        {
                var w = screen.width, h = screen.height;
                if(this.IsOpera())
                {
                        w = document.body.offsetWidth;
                        h = document.body.offsetHeight;
                }
                window.open(url, '', 'status=no,scrollbars=yes,resizable=no,width='+width+',height='+height+',top='+Math.floor((h - height)/2-14)+',left='+Math.floor((w - width)/2-5));
                return false;
                
        },
        IsIE: function()
        {
                return document.attachEvent && navigator.userAgent.toLowerCase().indexOf('opera') == -1;
        },
        IsOpera: function()
        {
                return navigator.userAgent.toLowerCase().indexOf('opera') != -1;
        }                 
};

toggleForwardDate = function(box)
{
        var calendarContainer = $("#date_backward");
        if(box.checked)
        {
                calendarContainer.css("visibility", "visible");
                //calendarContainer.show();
        }
        else
        {
                calendarContainer.css("visibility", "hidden");
                //calendarContainer.hide();
        }
}
searchLocation = function(n, title)
{
        $("#location_container").val(n);
        openSearchWindow();
}
createOverlay = function(cont, width, height)
{
        if(document.getElementById("lw_overlay") === null)
        {
                $('<div id="lw_overlay"></div>').appendTo(document.body);
                $("#lw_overlay").addClass("lw_overlayBG").click(closeSeachWindow);
        }
        if(document.getElementById("lw_window") === null)
        {
                $('<div id="lw_window"></div>').appendTo(document.body);
                $(cont).appendTo("#lw_window");
        }
        $(cont).css({marginLeft: '-' + parseInt((width / 2),10) + 'px', width: width + 'px'});
        if ( !($.browser.msie && $.browser.version < 7)) { // take away IE6
                $(cont).css({marginTop: '-' + parseInt((height / 2),10) + 'px'});
        }
        $(cont).show();        
        $("#lw_window").show();
}
removeOverlay = function(cont)
{
        $(cont).fadeOut("fast",function(){$("#lw_window").hide(); $("#lw_overlay").remove()});
        $(document).unbind('keydown');
} 
openSearchWindow = function()
{
        //var width = 630;
        //var height = 320;                
        createOverlay($("#lw"), 655, 320);
        
        $("#lw .form").hide();
        //$("#lw").css({marginLeft: '-' + parseInt((width / 2),10) + 'px', width: width + 'px'}).show();
        $("#lw .form").css("height", "270px").slideDown("fast");
        //if ( !($.browser.msie && $.browser.version < 7)) { // take away IE6
          //      $("#lw").css({marginTop: '-' + parseInt((height / 2),10) + 'px'});
        //}
        $(document).keyup(function (e) {
                keycode = e.keycode ? e.keycode : e.which;
                if(keycode == 27)
                        closeSeachWindow();
        });
}
closeSeachWindow = function()
{
        removeOverlay($("#lw"));
}
openRulesWindow = function()
{
        createOverlay($("#rules"), 655, 100);
        $(document).keyup(function (e) {
                keycode = e.keycode ? e.keycode : e.which;
                if(keycode == 27)
                        closeRulesWindow();
        });        
}
closeRulesWindow = function()
{
        removeOverlay($("#rules"));
}
acceptRules = function (box, submit)
{
        if($(box).attr("checked")) {
                $(".form-button").slideDown();
                $(submit).attr("disabled", false);
        }
        else {
                $(".form-button").hide();
                $(submit).attr("disabled", true);
        }
}
showError = function (error)
{
        var e = $("#error_msg_container");
        if(error && error != e.html())
        {
                if(e.html() == "")
                        e.hide().html(error).slideDown();
                else
                        e.html(error);
        }
}
sendBookForm = function()
{
        var msg = $("#error_msg_container");
        //msg.hide();
        var container = $("#bookform_container");
        $(container).showWaitWindow();
        
        $form = $("#form1");
        $('<input type="hidden" name="AJAX_MODE" value="Y" />').appendTo($form);
        
        var str = $($form).serialize();
        $.post(ajaxRequestPath, str, function(res) {
                container.html(res).closeWaitWindow();
                if( msg.html() != "" ) msg.slideDown();
        });        
        return;
        
        //jsAjaxUtil.SendForm(document.getElementById("form1"), function (res) {container.html(res).closeWaitWindow();});
        
        $("#form1").sendFrameForm(ajaxRequestPath, function (res) {
                        container.html(res).closeWaitWindow();
                        if( msg.html() != "" ) msg.slideDown();
        });
        return false;
}
jQuery.fn.sendFrameForm = function(url, callback) {
        if ( url )
                // If it's a function
                if ( jQuery.isFunction( url ) ) {
                        // We assume that it's the callback
                        callback = url;
                        url = null;
                }
                else {
                        this.attr("action", url);        
                }                
        $('<input type="hidden" name="AJAX_MODE" value="Y" />').appendTo(this);
        
        var t = (new Date()).getTime().toString().substr(8);
        var f = $('<iframe src="javascript:\'\'" id="iframe' + t + '" name="iframe' + t + '" />')
                        .css("display", "none")
                                .appendTo(document.body);
                                
        this.attr("target", "iframe" + t);
        f.bind("load", function () {                
                
                //setTimeout(function() {var r = $(f[0].contentWindow.document.body).html(); alert(r)}, 10000);        
                var r = $(f[0].contentWindow.document.body).html();
                //fixing another strange bug. Now for FF
                var TimerID = setTimeout("document.body.removeChild(document.getElementById('iframe" + t + "'));", 100);
                if( $.isFunction(callback) ) callback(r);
        });
}
jQuery.fn.showWaitWindow = function(options)
{
        var options = jQuery.extend({Class:'waitwindowlocalshadow', MinHeight:0, MinWidth:0}, options);
        return this.each(function()
        {
                var w = jQuery(this).width();
                var h = jQuery(this).height();

                function GetWindowSize()
                {
                        var innerWidth, innerHeight;

                        if (self.innerHeight) // all except Explorer
                        {
                                innerWidth = self.innerWidth;
                                innerHeight = self.innerHeight;
                        }
                        else if (document.documentElement && document.documentElement.clientHeight) // Explorer 6 Strict Mode
                        {
                                innerWidth = document.documentElement.clientWidth;
                                innerHeight = document.documentElement.clientHeight;
                        }
                        else if (document.body) // other Explorers
                        {
                                innerWidth = document.body.clientWidth;
                                innerHeight = document.body.clientHeight;
                        }

                        var scrollLeft, scrollTop;
                        if (self.pageYOffset) // all except Explorer
                        {
                                scrollLeft = self.pageXOffset;
                                scrollTop = self.pageYOffset;
                        }
                        else if (document.documentElement && document.documentElement.scrollTop) // Explorer 6 Strict
                        {
                                scrollLeft = document.documentElement.scrollLeft;
                                scrollTop = document.documentElement.scrollTop;
                        }
                        else if (document.body) // all other Explorers
                        {
                                scrollLeft = document.body.scrollLeft;
                                scrollTop = document.body.scrollTop;
                        }

                        var scrollWidth, scrollHeight;

                        if ( (document.compatMode && document.compatMode == "CSS1Compat"))
                        {
                                scrollWidth = document.documentElement.scrollWidth;
                                scrollHeight = document.documentElement.scrollHeight;
                        }
                        else
                        {
                                if (document.body.scrollHeight > document.body.offsetHeight)
                                        scrollHeight = document.body.scrollHeight;
                                else
                                        scrollHeight = document.body.offsetHeight;

                                if (document.body.scrollWidth > document.body.offsetWidth ||
                                        (document.compatMode && document.compatMode == "BackCompat") ||
                                        (document.documentElement && !document.documentElement.clientWidth)
                                )
                                        scrollWidth = document.body.scrollWidth;
                                else
                                        scrollWidth = document.body.offsetWidth;
                        }

                        return  {"innerWidth" : innerWidth, "innerHeight" : innerHeight, "scrollLeft" : scrollLeft, "scrollTop" : scrollTop, "scrollWidth" : scrollWidth, "scrollHeight" : scrollHeight};
                };

                function GetRealPos(el)
                {

                        if (el.getBoundingClientRect)
                        {
                                var obRect = el.getBoundingClientRect();
                                var obWndSize = GetWindowSize();
                                var arPos = {
                                        left: obRect.left + obWndSize.scrollLeft,
                                        top: obRect.top + obWndSize.scrollTop,
                                        right: obRect.right + obWndSize.scrollLeft,
                                        bottom: obRect.bottom + obWndSize.scrollTop
                                };
                                return arPos;
                        }


                        var res = Array();

                        res["left"] = el.offsetLeft;
                        res["top"] = el.offsetTop;
                        var objParent = el.offsetParent;

                        while(objParent && objParent.tagName != "BODY")
                        {
                                res["left"] += objParent.offsetLeft;
                                res["top"] += objParent.offsetTop;
                                objParent = objParent.offsetParent;
                        }
                        return res;
                };

                arPosition = GetRealPos(this);


                if(w < options.MinWidth)w = options.MinWidth;
                if(h < options.MinHeight)h = options.MinHeight;

                jQuery("body").append("<div id='ajaxLoader_"+this.id+"' style='display:none; top:"+arPosition["top"]+"px; left:"+arPosition["left"]+"px; width:"+w+"px;height:"+h+"px; position:absolute; z-index:1000;' class='"+options.Class+"'></div>");
                $("#ajaxLoader_"+this.id).css("display","block");

        });
}

jQuery.fn.closeWaitWindow = function()
{
        return this.each(function()
        {
                $('#ajaxLoader_'+this.id).remove();
        });
}
