var showprocessing = true;
var showmodalprocessing = false;
var hideprocessing= true;

function showproc()
{   
    if(showprocessing == true)
    {
        $("#divStaticProcessingImg").css("visibility","visible");
        $("#divStaticProcessingImg").css("display","block");
        if(showmodalprocessing==true)
        {
            showmodal("divStaticProcessingImg");
            showmodalprocessing = false;
        }
       centerdiv();
       $(window).bind("scroll", centerdiv);
    }
 }

function hideproc()
{   
    if(hideprocessing == true)
    {
        $("#divStaticProcessingImg").css("visibility","hidden");
        $("#divStaticProcessingImg").css("display","none");
        hidemodal();
        showprocessing = true;
        showmodalprocessing = false;
        $(window).unbind("scroll", centerdiv);
    }
    hideprocessing = true;
}
 
function centerdiv()
{
    var div ="divStaticProcessingImg";
    
    var left = ($(window).width()/2) - ($("#" +div).width()/2)
    
    var top = ($(window).height()/2) - ($("#" +div).height()/2)
    
    $("#" +div).css("left",left +  $("#" +div).scrollLeft());
    
    $("#" +div).css("top",top +  $(document).scrollTop());
}

function jquerycallserver(method,args,receivedata)
{
    $.ajax({
    type: "POST",
    url: method,
    data: args,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    beforeSend:showproc,
    complete:hideproc,
    error: function(XMLHttpRequest, textStatus, errorThrown)
    {
        alert(XMLHttpRequest.responseText);
    },
    success: function(msg)
    {
        if(typeof(msg.d)!= "undefined")
        {
            msg = msg.d;
        }
        receivedata(msg);
    }
    });
}

function jquerycallback()
{
    var source = this.event.srcElement;
//    
//    var validator = $(this.event.srcElement).attr("clientvalidatormethod");
//    
//    if(null != validator && validator!="")
//    {
//        var result = eval(validator);
//        
//        if(result == false)
//        {
//            return false;
//        }
//    }
    
    var criteria = "{'criteria':'" + buildcriteria(this.event.srcElement) + "'}";
    
    var method = $(this.event.srcElement).attr("servicepath") + "/" + $(this.event.srcElement).attr("methodname");
    
    var f = $(this.event.srcElement).attr("clientcallbackmethod");

    jquerycallserver(method,criteria,function f(msg){eval(f + '(msg);')});
    
    return false;
}

function buildcriteria(eventTarget)
{
    var criteria = "";
    criteria ="<criteria>"
    
    var controls;
    
    var container = $(eventTarget).attr("callbackdatacontainer");

    controls = $("#" + container +" *");
    
    for(var i=0;i<controls.length;i++)
    {
        if(null == $(controls[i]).attr("PropertyName"))
        {
            continue;
        }
        
        var propname = $(controls[i]).attr("PropertyName");
        
        criteria = criteria  + "<" + propname + ">";
        
        if($(controls[i]).attr("ControlType") == "Label")
        {
            criteria = criteria + $(controls[i]).text();
        }
        else
        {
            criteria = criteria + $(controls[i]).val();
        }

        criteria = criteria  + "</" + propname + ">";
    }
    criteria = criteria  + "</criteria>"
    
    return criteria;
}

function getfile(url,receivedata,onerror)
{
    $.ajax({
       url: url,
       cache:false,
       dataType: ($.browser.msie) ? "text" : "text",
       success: receivedata,
       error: onerror
     });
}

/*Used in temporary results start*/
function imagelazypartialload()
{
    $(window).bind("scroll",partialloadvisibleimages);
    
    loadvisibleimages();
}

function partialloadvisibleimages()
{
    var hotelimages =$(getresultholder()).find(".hotelimage");
    if(hotelimages.length == 0){ $(window).unbind("scroll",partialloadvisibleimages); return;}
    hotelimages.each(function() 
    {
        if(!belowthefold(this))
        {
            $(this).attr("src",$(this).attr("rel"));
            
            $(this).removeClass("hotelimage");
        }
    });
}
/*Used in temporary results end*/

function imagelazyload()
{
    $(window).bind("scroll",loadvisibleimages);
    
    loadvisibleimages();
}

function loadvisibleimages()
{
    var hotelimages =$("#searchresults").find(".hotelimage");
    if(hotelimages.length == 0){ $(window).unbind("scroll",loadvisibleimages); return;}
    hotelimages.each(function() 
    {
        if(!belowthefold(this))
        {
            $(this).attr("src",$(this).attr("rel"));
            
            $(this).removeClass("hotelimage");
        }
    });
}

function belowthefold(element) 
{
    var fold = $(window).height() + $(window).scrollTop();
    
    return fold <= $(element).offset().top;
};

function resizemodal()
{
    var h =$(document).height();var w;
    
    $.each($.browser, function(i) {
      if($.browser.msie){
         w =$(document).width()-22;
      }else{
         w =$(document).width();
      }
    });
    
    $("#modaldiv").height(h);
    
    $("#modaldiv").width(w);
}


function showmodal(div)
{
    var left = ($(window).width()/2) - ($("#" +div).width()/2)
    
    var top = ($(window).height()/2) - ($("#" +div).height()/2)
    
    $("#" +div).css("left",left);
    
    $("#" +div).css("top",top);
    
    var h =$(document).height();var w;
    
    $.each($.browser, function(i) {
      if($.browser.msie){
         w =$(document).width()-22;
      }else{
         w =$(document).width();
      }
    });
    
    
    var modal = $("<div id='modaldiv' style='position:absolute;top:0;left:0;height:"+h+";width:"+w+";'></div>")
    
    modal.css("opacity",0.5);

	modal.css("background-color","black");
    
    modal.appendTo(document.body)
}

function hidemodal()
{
    if($("#modaldiv").length > 0)
    {
        $("#modaldiv").remove();
    }
}

function makedivcenter(div)
{    
    var left = ($(window).width()/2) - ($("#" +div).width()/2)
    
    var top = ($(window).height()/2) - ($("#" +div).height()/2)
    
    $("#" +div).css("left",left +  $("#" +div).scrollLeft());
    
    $("#" +div).css("top",top +  $(document).scrollTop());
    $("#" +div).css("z-index",10000);
}

function ShowPopup(PageName, Width, Height) {
    if ($('#popup').length > 0) {
        $('#popup').remove();
    }

    var popup = "<div id='popup' style='position:absolute' ><iframe src='" + PageName + "' frameborder='0' scrolling='no' /></div>";

    $(document.body).append(popup);
    
    $('#popup').show();
}

function HidePopup() {
    $('#popup').remove();
}

function resizeIframe(height, width) {

    $('#popup iframe').height(height);

    $('#popup iframe').width(width);

    makedivcenter('popup');
}
