// Get cumulative width of all children
function getWidthOfChildren(strTarget) {

    var intWidth = 0;
    $(strTarget).children().each(function() {
        intWidth += $(this).outerWidth(true);
    });

    return intWidth;
}

// Make all children unselectable (i.e. double click on superoffer page number)
function makeChildrenUnselectable(varTarget) {
    alert('function makeChildrenUnselectable should no longer be here, please email to info@weekendjeweg.nl');
    $(varTarget).children().each(function() {
        this.onselectstart = function() { return false; };
        $(this).attr('unselectable', 'on').css('-moz-user-select', 'none');
    });
}

function openCalendarNew(strUrl) {
    popupShow(false, strUrl, false, 400);
}

function checkCalenderNewMouse() {
    if (!blnMouseOverPopup) closeCalendarNew();
}

function closeCalendarNew() {
    popupHide();
}

function openWindow(strUrl, intHeight, intWidth) {
    alert('function openWindow should no longer be here, please email to info@weekendjeweg.nl');
    var name = intHeight+intWidth;
    window.open(strUrl, name, 'height=' + intHeight + ',width=' + intWidth + ',location=no,menubar=no,status=no,toolbar=no,resizable=yes');
    return true;
}


/**
 * Function to register a pageview through google, with the current page and a variable also possible to track using another object
 * @param    string    the variable we want to register
 * @param    object    the object to be used (default _gaq if not defined)
 * @return    void
 */
function trackPageview(variable, object) {

    if (object == undefined) {
        if (typeof _gaq != 'undefined' && typeof _gaq == 'object') {
            object = _gaq;
        } else {
            return false;
        }
    } else if (typeof object != undefined && typeof object == 'object') {

    } else {
            return false;
    }

    if (variable.charAt(0) == '/') {
        object.push(['_trackPageview', variable]);
    } else {
        var url_root = document.location.pathname + document.location.search;

        if(url_root.split("#").length > 1) {
            url_root = url_root.split("#").join("?");
        }

        if (url_root.split("?").length > 1) {
            url_root += '&'+variable;
        } else {
            url_root += '?'+variable;
        }

        object.push(['_trackPageview', url_root]);
    }

    return true;
}

/**
 * Function to register a event through google, with variables
 * @param    string    category
 * @param    string    action
 * @param    string    label
 * @param    string    extra option
 * @param    object    the object to be used (default _gaq if not defined)
 */
function trackEvent(category, action, label, extra, object) {
    
    if (object == undefined) {
        if (typeof _gaq != 'undefined' && typeof _gaq == 'object') {
            object = _gaq;
        } else {
            return false;
        }
    } else if (typeof object != undefined && typeof object == 'object') {
        
    } else {
        return false;
    }

    object.push(['_trackEvent', category, action, label, extra]);

    return true;
}


/**
 * Sets a COOKIE value
 * @param   string  The name of the COOKIE
 * @param   string  The value for the COOKIE
 * @param   date    The date at which the COOKIE expires, or leave blank to use the current session's enddate
 * @param   string  The path for which the COOKIE is valid, or leave blank to use the current document's path
 * @param   string  The domain for which the COOKIE is valid, or leave blank to use the current document's domain
 * @param   boolean Whether or not the COOKIE should use secure transmission
 * @return  void
 */
function setCookie(strName, strValue, datExpires, strPath, strDomain, blnSecure){
    strPath = '/';
    document.cookie = strName + "=" + escape(strValue) +
                      ((datExpires) ? "; expires=" + datExpires.toGMTString() : "") +
                      ((strPath)    ? "; path=" + strPath : "") +
                      ((strDomain)  ? "; domain=" + strDomain : "") +
                      ((blnSecure)  ? "; secure" : "");
    return true;
}

/**
 * Returns the string value of a COOKIE, or null if it was not set
 * @param   string  The name of the COOKIE
 * @return  string  The value of the COOKIE
 */
function getCookie(strName) {

    var objCookie = document.cookie;

    if (!objCookie) {
        return;
    }

    var strPrefix = strName + "=";
    var intStart = objCookie.indexOf("; " + strPrefix);
    
    if (intStart == -1) {
        intStart = objCookie.indexOf(strPrefix)
        if (intStart != 0) {
            return null;
        }
    } else {
        intStart += 2;
    }

    var intEnd = objCookie.indexOf(";", intStart);
    
    if (intEnd == -1) {
        intEnd = objCookie.length;
    }

    return unescape(objCookie.substring(intStart + strPrefix.length, intEnd));
}

/**
 * Deletes a COOKIE
 * @param   string  The name of the cookie
 */
function deleteCookie(strName, strPath, strDomain) {
    strPath = '/';
    if (getCookie(strName)){
        document.cookie = strName + "=" +
                          ((strPath) ? "; path=" + strPath : "") +
                          ((strDomain) ? "; domain=" + strDomain : "") +
                          "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}


function fancy_alert(text, timeout, height) {
    alert('function fancy_alert should no longer be here, please email to info@weekendjeweg.nl');
}

/**
 * Converts a XHtml url to javascript
 * @param   string  The url to convert
 * @return  string  The converted url
 */
function urlToJs(strUrl){
    alert('function urlToJs should no longer be here, please email to info@weekendjeweg.nl');
    return strUrl.replace('&amp;', '&');
}

var ajaxIframeObject;
function ajaxRequest(strUrl){
    alert('function ajaxRequest should no longer be here, please email to info@weekendjeweg.nl');
    // Pass the request to the server
    if (!document.createElement) {return true};

    // If the document supports dynamic creation of elements, create the iframe object
    if (!ajaxIframeObject && document.createElement) {
        try {
            var tempIFrame=document.createElement('iframe');
            tempIFrame.setAttribute('id','ajaxFrame');
            tempIFrame.style.border = '0px';
            tempIFrame.style.width  = '800px';
            tempIFrame.style.height = '400px';
            ajaxIframeObject = document.body.appendChild(tempIFrame);

            if (document.frames) {
                // this is for IE5 Mac, because it will only
                // allow access to the document object
                // of the IFrame if we access it through
                // the document.frames array
                ajaxIframeObject = document.frames['ajaxFrame'];
            }
        } catch(exception) {
            // This is for IE5 PC, which does not allow dynamic creation
            // and manipulation of an iframe object. Instead, we'll fake
            // it up by creating our own objects.
            iframeHTML='\<iframe id="ajaxFrame" style="';
            iframeHTML+='border:0px;';
            iframeHTML+='width: 0px;';
            iframeHTML+='height: 0px;';
            iframeHTML+='"><\/iframe>';
            document.body.innerHTML+=iframeHTML;
            ajaxIframeObject = new Object();
            ajaxIframeObject.document = new Object();
            ajaxIframeObject.document.location = new Object();
            ajaxIframeObject.document.location.iframe = document.getElementById('ajaxFrame');
            ajaxIframeObject.document.location.replace = function(location) {
                this.iframe.src = location;
            }
        }
    }

    if (navigator.userAgent.indexOf('Gecko') !=-1 && !ajaxIframeObject.contentDocument) {
        // we have to give NS6 a fraction of a second
        // to recognize the new IFrame
        setTimeout('ajaxRequest(\''+strUrl+'\')',10);
        return false;
    }

    if (ajaxIframeObject.contentDocument) {
        // For NS6
        IFrameDoc = ajaxIframeObject.contentDocument;
    } else if (ajaxIframeObject.contentWindow) {
        // For IE5.5 and IE6
        IFrameDoc = ajaxIframeObject.contentWindow.document;
    } else if (ajaxIframeObject.document) {
        // For IE5
        IFrameDoc = ajaxIframeObject.document;
    } else {
        return true;
    }

    if (IFrameDoc.location.replace) {
        IFrameDoc.location.href = strUrl;
        // IFrameDoc.location.replace(strUrl);
    }
    else {
        IFrameDoc.location.href = strUrl;
    }
}

/**
 * Resizes the browser window to fit to contents
 */
function autoFitWindow(){
    alert('function autoFitWindow should no longer be here, please email to info@weekendjeweg.nl');

    if (self.scrollWidth){
        intWidth = self.scrollWidth;
        intHeight = self.scrollHeight;
    } else if (document.documentElement && document.documentElement.scrollWidth){
        intWidth = document.documentElement.scrollWidth;
        intHeight = document.documentElement.scrollHeight;
    } else if (document.body){
        intWidth = document.body.scrollWidth;
        intHeight = document.body.scrollHeight;
    }

    if (window.offsetHeight){
        intWidth  += window.offsetWidth  - window.clientWidth;
        intHeight += window.offsetHeight - window.clientHeight;
    } else if (window.document && window.document.offsetHeight){
        intWidth  += window.document.offsetWidth  - window.document.clientWidth;
        intHeight += window.document.offsetHeight - window.document.clientHeight;
    } else if (window.document.documentElement && window.document.documentElement.offsetHeight){
        intWidth  += window.document.documentElement.offsetWidth  - window.document.documentElement.clientWidth;
        intHeight += window.document.documentElement.offsetHeight - window.document.documentElement.clientHeight;
    }
    window.resizeTo(intWidth + 6, intHeight + 106);

}

/**
 * Global variables
 */
var TB_Ajax_IFrame_Count  = 0;
var TB_Ajax_Request_Stack = new Array();

/**
 * TB_Ajax_Request
 * ---------------
 * Class to make ajax requests
 */
function TB_Ajax_Request(){

    // Properties
    this.intRequestId    = TB_Ajax_Request_Stack.length;
    this.objRequest      = false;
    this.objIFrame       = false;
    this.blnIFrame       = false;
    this.blnLoaded       = false;
    this.objCallback     = false;
    this.arrCallbackArgs = new Array();
    this.strData         = "";

    // Methods
    this.get          = TB_Ajax_Request_Get;
    this.create       = TB_Ajax_Request_Create;
    this.useIFrame    = TB_Ajax_Request_UseIFrame;
    this.checkState   = TB_Ajax_Request_CheckState;
    this.setCallback  = TB_Ajax_Request_SetCallback;
    this.doCallback   = TB_Ajax_Request_DoCallback;
    this.IFrameLoaded = TB_Ajax_Request_IFrameLoaded;

    // Code
    TB_Ajax_Request_Stack[this.intRequestId] = this;
}

function TB_Ajax_Request_UseIFrame(){

    this.blnIFrame = true;

}

function TB_Ajax_Request_SetCallback(strFunction){

    this.objCallback = strFunction;

    for(var i=1;i<arguments.length;i++){
        this.arrCallbackArgs[this.arrCallbackArgs.length] = arguments[i];
    }
}

function TB_Ajax_Request_DoCallback(){

    strCallback = "this.objCallback(this.strData";

    for (var i=0;i<this.arrCallbackArgs.length;i++) {
        strCallback += ",this.arrCallbackArgs[" + i + "]";
    }

    strCallback += ")";
    eval(strCallback);
}

function TB_Ajax_Request_IFrameLoaded(){
    alert('function TB_Ajax_Request_IFrameLoaded should no longer be here, please email to info@weekendjeweg.nl');

    this.blnLoaded = true;
    if (this.objIFrame.document) this.strData = this.objIFrame.document.getElementsByTagName("body")[0].innerHTML;
    else strData = this.objIFrame.contentDocument.getElementsByTagName("body")[0].innerHTML;
    this.doCallback();
}

function TB_Ajax_Request_CheckState(){

    if (this.objRequest.readyState != 4) return;
    this.blnLoaded = true;

    this.strData = this.objRequest.responseText;
    delete this.objRequest;
    this.doCallback();
}

function TB_Ajax_Request_Get(strUrl){

    strUrl += (strUrl.indexOf('?') != -1 ? '&' : '?') + 'TB_AJAX_REQUEST_ID=' + this.intRequestId;


    // Use an IFrame?
    if (this.blnIFrame == true){
        strUrl += '&TB_AJAX_REQUEST_IFRAME=1';
        if (this.objIFrame.document)
            this.objIFrame.document.location.href = strUrl;
        else this.objIFrame.src = strUrl;
    }

    // Or XMLHttp object?
    else {
        this.objRequest.onreadystatechange = new Function("TB_Ajax_Request_Stack[" + this.intRequestId + "].checkState();");
        this.objRequest.open('GET', strUrl);
        // .substr(0, strUrl.indexOf('?')), true);
        this.objRequest.send(strUrl.substr(strUrl.indexOf('?')+1));
    }
}


function TB_Ajax_Request_Create(){

    // Use true AJAX?
    if (this.blnIFrame == false){

        // Create the XMLHttpRequest object
        try{
            this.objRequest = new XMLHttpRequest();
        }

        // In IE this will result in an error because it uses an ActiveX component instead
        catch(e){

            // So try to create the ActiveX object
            try{
                this.objRequest = new ActiveXObject("Msxml2.XMLHTTP");
            }

            // In IE 5.5 this also fails, because another ActiveX component is used
            catch(e){

                // So create the older ActiveX object
                try{
                    this.objRequest = new ActiveXObject("Microsoft.XMLHTTP");
                }

                // If this also failed, try to use an IFrame instead
                catch(e) {
                    this.useIFrame();
                    this.create();
                }
            }
        }
    }

    // Or fake it using an IFrame?
    else {

        // We'll need to dynamically create an iframe
        if (!document.createElement) return false;

        try {

            // Try to create the IFrame dynamically
            this.objIFrame = document.createElement('iframe');
            this.objIFrame.setAttribute('id', 'TB_AJAX_IFRAME_OBJECT_' + TB_Ajax_IFrame_Count);
            this.objIFrame.style.border = '0px';
            this.objIFrame.style.height = '0px';
            this.objIFrame.style.width  = '0px';

            this.objIFrame = document.body.appendChild(this.objIFrame);
            if (document.frames) this.objIFrame = document.frames['TB_AJAX_IFRAME_OBJECT_' + TB_Ajax_IFrame_Count];

            var i = 0;
            while(i<1000) i++;
            TB_Ajax_IFrame_Count++;
        }

        // This will fail on IE5
        catch (e) {

            document.body.innerHTML += '\<iframe id="TB_AJAX_IFRAME_OBJECT_'
                                     + TB_Ajax_IFrame_Count
                                     + '" style="border:0px;width:0px;height:0px;"><\/iframe>';

            this.objIFrame                           = new Object();
            this.objIFrame.document                  = new Object();
            this.objIFrame.document.location         = new Object();
            this.objIFrame.document.location.replace = function(location) {this.iframe.src = location;}

            if (document.getElementById){
                this.objIFrame.document.location.iframe = document.getElementById('TB_AJAX_IFRAME_OBJECT_' + TB_Ajax_IFrame_Count);
            } else if (document.all){
                this.objIFrame.document.location.iframe = document.all['TB_AJAX_IFRAME_OBJECT_' + TB_Ajax_IFrame_Count];
            } else if (document.layers){
                this.objIFrame.document.location.iframe = document.layers['TB_AJAX_IFRAME_OBJECT_' + TB_Ajax_IFrame_Count];
            }

            TB_Ajax_IFrame_Count++;
        }

    }
}

function ajaxLoadIFrame(strUrl, strCallback){

    var objAjax = new TB_Ajax_Request();

    objAjax.useIFrame();
    objAjax.create();

    if (strCallback) {
        objAjax.setCallback(strCallback);
    }

    objAjax.get(strUrl);
}

function ajaxLoadRequest(strUrl, strCallback){

    var objAjax = new TB_Ajax_Request();
    objAjax.create();
    if (strCallback) objAjax.setCallback(strCallback);
    objAjax.get(strUrl);
}

function clickFirstChildLink(objObject) {
    alert('function clickFirstChildLink should no longer be here, please email to info@weekendjeweg.nl');

    if (typeof($) == 'function') {
        objLink = $(objObject).find('A');
        strLink = objLink.attr('href');
        strTarget = objLink.attr('target');
        if (typeof(strTarget) == 'undefined') strTarget = '_self';
        switch (strTarget) {
            case '_self':
                self.location.href = strLink;
                break;
            case '_top':
                top.location.href = strLink;
                break;
            case '_parent':
                parent.location.href = strLink;
                break;
            case '_new':
            case '_blank':
                window.open(strLink, '_new');
                break;
            default:
                if (typeof(top.frames[strTarget]) != 'undefined') {
                    top.frames[strTarget].location.href = strLink;
                } else {
                    document.location.href = strLink;
                }
                break;
        }
    }
    return false;
}

function getUrlVars() {

    var vars = [],
        param,
        hash,
        varsFirst = (window.location.href.indexOf('?') + 1),
        varsSecond = (window.location.href.indexOf('#') + 1);

    if (varsSecond > 0) {
        var params = window.location.href.slice(varsFirst, varsSecond).split('&');
    } else {
        var params = window.location.href.slice(varsFirst).split('&');
    }

    var hashes = window.location.href.slice(varsSecond).split('&');

    for (var i = 0; i < params.length; i++) {
        param = params[i].split('=');
        vars.push(param[0]);
        vars[param[0]] = param[1];
    }
    
    if (varsSecond > 0) {
        for (var i = 0; i < hashes.length; i++) {
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }
    }

    return vars;
}
