$j = jQuery.noConflict();

$j(document).ready(function() {
    $j("button,input:submit,input:button").button();
    $j("button,input:submit,input:button").css("position","static");

    $j.datepicker.regional['nl'] = {
        closeText: 'Sluiten',
        prevText: '←',
        nextText: '→',
        currentText: 'Vandaag',
        monthNames: ['januari', 'februari', 'maart', 'april', 'mei', 'juni',
        'juli', 'augustus', 'september', 'oktober', 'november', 'december'],
        monthNamesShort: ['jan', 'feb', 'maa', 'apr', 'mei', 'jun',
        'jul', 'aug', 'sep', 'okt', 'nov', 'dec'],
        dayNames: ['zondag', 'maandag', 'dinsdag', 'woensdag', 'donderdag', 'vrijdag', 'zaterdag'],
        dayNamesShort: ['zon', 'maa', 'din', 'woe', 'don', 'vri', 'zat'],
        dayNamesMin: ['zo', 'ma', 'di', 'wo', 'do', 'vr', 'za'],
        weekHeader: 'Wk',
        dateFormat: 'dd-mm-yy',
        firstDay: 1,
        isRTL: false,
        showMonthAfterYear: false,
        yearSuffix: ''
    };
    $j.datepicker.setDefaults($j.datepicker.regional['nl']);

    var windowLocation=""+window.location;
    //voor acceptatie warning    
    if (windowLocation.indexOf("acc.provinciaalgeoregister")>=0)
        $j("#acceptatieWarning").css("display","block");
});
getIEVersionNumber = function() {
    var ua = navigator.userAgent;
    var MSIEOffset = ua.indexOf("MSIE ");
    if (MSIEOffset == -1) {
        return -1;
    } else {
        return parseFloat(ua.substring(MSIEOffset + 5, ua.indexOf(";", MSIEOffset)));
    }
}
var ieVersion = getIEVersionNumber();

function generatePopup(url, title, width, height, dialogOptions) {
    if(url != '') {
        dialogOptions = dialogOptions || {};
        if(!document.getElementById('popupWindowIframe')) {
            var popupDiv = document.createElement('div');
            popupDiv.id = 'popupWindowIframe';
            popupDiv.title = title;
            var popupIframe = null;
            if(ieVersion!=-1 && ieVersion < 9) {
                popupIframe = document.createElement('<iframe name="popupIframe" frameborder="0" scrolling="no">');
            } else {
                popupIframe = document.createElement('iframe');
                popupIframe.name = 'popupIframe';
            }
            popupIframe.id = 'popupIframe';
            popupDiv.appendChild(popupIframe);
            document.body.appendChild(popupDiv);

            popupIframe.src = url;
            
            $j("#popupWindowIframe").dialog($j.extend({
                height: height,
                width: width,
                modal: true,
                draggable: true,
                resizable: false,
                bgiframe: true
            }, dialogOptions));
        } else {
            //TODO: clear werkt niet:
            $j('#popupIframe').contents().find("body").html("");
            
            $j("#popupWindowIframe").dialog("option","width", width );
            $j("#popupWindowIframe").dialog("option","height", height );
            $j("#popupWindowIframe").dialog("option", dialogOptions);
            document.getElementById('popupIframe').src = url;
            $j("#popupWindowIframe").attr("title", title);
            $j("#popupWindowIframe").dialog('open');
            $j("#ui-dialog-title-popupWindowIframe").text(title);
        }
        return $j("#popupWindowIframe");
    }
    return null;
}
function closePopup(){
    $j("#popupWindowIframe").dialog('close');
}
var startIndex;
var numberOfReturned;
var totalMatched;
function updatePagingDescription(sIndex,count,total){
    startIndex=Number(sIndex);
    numberOfReturned=Number(count);
    totalMatched=Number(total);   
    var lastIndex= startIndex+numberOfReturned-1;
    if (lastIndex > total){
        lastIndex=total;
    }else if (lastIndex < 0){
        lastIndex=0;
    }
    $j("#pagingDescription").html(""+startIndex+" - "+lastIndex+" van "+totalMatched);
    if (startIndex==1){
        $j("#previous").button("disable");
    }else{
        $j("#previous").button("enable");
    }
    if (startIndex-1+numberOfReturned == totalMatched){
        $j("#next").button("disable");
    }else{
        $j("#next").button("enable");
    }
}
function getAantalRecords(){
    return Number($j("#aantalRecords").val());
}
function correctStartIndex(sIndex){
    var maxResults=getAantalRecords();
    if (sIndex-1+maxResults > totalMatched){
        sIndex=totalMatched+1-maxResults;
    }
    if (sIndex<1)
        sIndex=1;
    else if (sIndex > totalMatched){
        sIndex=totalMatched-getAantalRecords();
        if (sIndex < 1){
            sIndex=1;
        }
    }
    return sIndex;
}

function showLoading(jqueryLoadingObject){
    $j("#loadScreen_"+jqueryLoadingObject.attr("id")).remove();
    var loadScreen=$j("<div></div>");
    loadScreen.attr("id","loadScreen_"+jqueryLoadingObject.attr("id"));
    loadScreen.addClass("loadingScreen");
    //loadScreen.html('<img class="loadingBar" src="images/loading.gif" border="0" alt="laadbalk">');
    loadScreen.html('&nbsp;');
    var width=jqueryLoadingObject.css("width");
    var height=jqueryLoadingObject.css("height");
    var top=jqueryLoadingObject.css("top");
    var left=jqueryLoadingObject.css("left");
    var zIndex = jqueryLoadingObject.css("zIndex");
    if (width=="auto")
        width="100%";
    if (height=="auto")
        width="100%";
    if (top=="auto")
        top="0";
    if (left=="auto")
        left="0"
    if (zIndex == "auto")
        zIndex = 999;
    else
        zIndex = zIndex + 1;
    loadScreen.css({heigth: height,
        width: width,
        margin: "auto",
        top: top,
        left: left,
        zIndex: zIndex
    });
    /*loadScreen.height(jqueryLoadingObject.height());
    loadScreen.width(jqueryLoadingObject.width());
    loadScreen.offSet(jqueryLoadingObject.offSet());
    loadScreen.zIndex(jqueryLoadingObject.zIndex()+1);*/
    var thisParent=jqueryLoadingObject;
    if(jqueryLoadingObject.parent()!=undefined){
        thisParent=jqueryLoadingObject.parent();
    }
    loadScreen.fadeTo('slow', 0.2);
    thisParent.append($j(loadScreen));
}
function hideLoading(jqueryLoadingObject){    
    $j("#loadScreen_"+jqueryLoadingObject.attr("id")).fadeTo('slow', 0,function(){
        $j("#loadScreen_"+jqueryLoadingObject.attr("id")).remove();
    });
}
function log() {
    if (true) {
        var message = arguments[0];
        if (arguments.length > 1)
            message = Array.prototype.join.call(arguments, '; ');

        if (window.console && window.console.log) {
            console.log(message);
        } else if (window.opera && window.opera.postError) {
            window.opera.postError(message);
        }
    }
}

jQuery.fn.log = function(message) {
    if (true) {
        if (window.console && window.console.log) {
            console.log("%s: %o", message, this);
        } else if (window.opera && window.opera.postError) {
            window.opera.postError(message);
        }
    }
    return this;
};

function isEqual(a, b)
{
  var result = true;

  function typeTest(a, b) {return (typeof a == typeof b)}

  function test(a, b) {
    if (!typeTest(a, b)) return false;
    if (typeof a == 'function' || typeof a == 'object') {
      for (var p in a) {
        result = test(a[p], b[p]);
        if (!result) return false;
      }
      return result;
    }
    return (a == b);
  }
  return test(a, b);
}
