﻿//jQuery stuff
$(document).ready(function() {
    //Scroll to top function
    $('a.top').click(function() {
        $('html, body').animate({ scrollTop: 0 }, 'slow');
    });

    if (parent != null && parent.transport != null) {

    }
    else {
        if ($('.pnlPhotosPhoto a').size() > 0) {
            $('.pnlPhotosPhoto a').lightBox({
                txtImage: "",
                txtOf: "/",
                fixedNavigation: true
            });
        }
    }
    
    // On vérifie si tout d'abord des éléments avec la classe 'annPhotos' sont trouvés
    if ($('.pnlPhotosPhoto').size() > 0) {
        $('.pnlPhotosPhoto').cycle({
            fx: 'fade',
            timeout: 0,
            speed: 800,
            prev: '.prev',
            next: '.next',
            after: onAfter
        });
    }

   
});

function onAfter(curr, next, opts) {
    var title = '&nbsp;';
    if (this.title != '') title = this.title;
    
    $('.pnlPhotosInfo').html(title);
  
    var caption = (opts.currSlide + 1) + ' / ' + opts.slideCount;
    $('.pnlPhotosCnt').html(caption);
}

function lightboxizePhotosMembers() {
    // On vérifie si tout d'abord des éléments avec la classe 'annPhotos' sont trouvés
    var numElements = $('.pnlPhotosPhoto').size();
    if (numElements > 0) {
        $('.pnlPhotosPhoto').cycle({
            fx: 'fade',
            timeout: 0,
            speed: 800,
            prev: '.prev',
            next: '.next',
            after: onAfter
        });
    }

    numElements = $('.pnlPhotosPhoto a').size();
    if (numElements > 0) {
        $('.pnlPhotosPhoto a').lightBox({
            txtImage: "",
            txtOf: "/",
            fixedNavigation: true
        });
    }
}

////////////////////////////////////////////////////

function getDomainUrlPart(url) {
    var expr = /^((http[s]?|ftp):\/)?\/?([^:\/\s]+)(:([^\/]*))?((\/\w+)*\/)([\w\-\.]+[^#?\s]+)(\?([^#]*))?(#(.*))?$/;
    var regex = new RegExp(expr);

    var retval = regex.exec(url);
    

    var addr = retval[3];

    var ret = null;
    var idx = addr.indexOf('.');
    if (idx != -1) {
        ret = addr.substr(idx + 1, addr.length - (idx + 1));
    }

    return ret;
}

function redirect(objRef) {
    var accesID = objRef.previousSibling.previousSibling.value;

    var isDebug = false;

    if (window.location.hostname.indexOf('debug.') != -1 || window.location.hostname.indexOf('intranet.') != -1 || window.location.search.indexOf('affname=') != -1) {
        isDebug = true;
    }

    if (accesID != "" && objRef.previousSibling.previousSibling.attributes.getNamedItem('__disabledCssClass').value != objRef.previousSibling.previousSibling.className && objRef.previousSibling.previousSibling.value != objRef.previousSibling.previousSibling.attributes.getNamedItem('__watermarkLabel').value) { // & !isNaN(accesID)
        if (isDebug == true) {
            if (accesID.indexOf('B') == 0 || accesID.indexOf('b') == 0)
                document.location.href = "/Home.aspx?buildingId=" + accesID;
            else
                document.location.href = "/Search/Details.aspx?ID=" + accesID;
        }
        else {
            var b = true;
            for (var i = 0; i < accesID.length; i++) {
                if (isNaN(accesID[i])) {
                    b = false;
                    break;
                }
            }
            
            if (b)
                document.location.href = "/" + accesID;
            else
                document.location.href = "/Search/Details.aspx?ID=" + accesID;
        }
    }
}

function Enter(objRef, e, ctrlId, namingContainer) {
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    else return true;
    if (keycode == 13) {
        //do stuff
        if (objRef.value != "") {
            StopEvent(e)

            if (objRef.id == 'ctl01_ID' || objRef.id == 'ctl00_ContentPlaceHolder1_ID') {
                if (objRef.attributes.getNamedItem('__disabledCssClass').value != objRef.className && objRef.value != objRef.attributes.getNamedItem('__watermarkLabel').value)
                    redirect(objRef.nextSibling.nextSibling);
            }
            else if (ctrlId != undefined && namingContainer != undefined) {
                if (objRef.id == namingContainer + '_Login1_' + ctrlId) {
                    var imgBtn = document.getElementById(namingContainer + '_Login1_btn_LoginImageButton');
                    imgBtn.click();
                }
            }
            //e.cancelBubble = true;
            return false;
        }
    }
}

function txtAidFocused(objRef) {
    if (objRef.value == objRef.attributes.getNamedItem('__watermarkLabel').value) {
        objRef.className = '';
        objRef.value = '';
    }
}

function txtAidBlured(objRef) {
    if (objRef.value == '') {
        objRef.className = objRef.attributes.getNamedItem('__disabledCssClass').value;
        objRef.value = objRef.attributes.getNamedItem('__watermarkLabel').value;
    }
}

// Prevents event bubble up or any usage after this is called.
// pE - event object
function StopEvent(pE) {
    if (!pE)
        if (window.event) pE = window.event;
    else
        return;
    if (pE.cancelBubble != null)
        pE.cancelBubble = true;
    if (pE.stopPropagation)
        pE.stopPropagation();
    if (pE.preventDefault)
        pE.preventDefault();
    if (window.event)
        pE.returnValue = false;
    if (pE.cancel != null)
        pE.cancel = true;
}  // StopEvent

// Array implementation

Array.prototype.contains = function(value) {
    var retval = false;
    for (var i = 0; i < this.length; i++) {
        if (this[i] == value) {
            retval = true;
            break;
        }
    }

    return retval;
}

Array.prototype.indexOf = function(obj) {
    var found = false;
    var i;

    for (i = 0; i < this.length; i++) {
        if (this[i] == obj) {
            found = true;
            break;
        }
    }

    if (!found) i = -1;

    return i;
}

// Cookie enable state verification

/*var cookieEnabled = (navigator.cookieEnabled) ? true : false

//if not IE4+ nor NS6+
if (typeof navigator.cookieEnabled == "undefined" && !cookieEnabled) {
    document.cookie = "testcookie"
    cookieEnabled = (document.cookie.indexOf("testcookie") != -1) ? true : false
}

if (!cookieEnabled && window.location.pathname.toLowerCase() != '/cookiedisabled.aspx') { //if cookies are disabled on client's browser
    window.location.href = '/CookieDisabled.aspx';
}*/

// Popup
function doLayout(fg, bg) {
    var scrollLeft = (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
    var scrollTop = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);

    //var clientBounds = $common.getClientBounds();
    var clientWidth = $(window).width();
    var clientHeight = $(window).height();

    // Setup the location of the background element
    //this._layoutBackgroundElement();

    var xCoord = 0;
    var yCoord = 0;

    var foregroundelementwidth = fg.offsetWidth ? fg.offsetWidth : fg.scrollWidth;
    xCoord = ((clientWidth - foregroundelementwidth) / 2);
    // workaround for drag behavior which calls setlocation which in turn
    // changes the position of the panel to be absolute and requiring us
    // to add the scrollLeft so that it is positioned correctly.
    if (fg.style.position == 'absolute') {
        xCoord += scrollLeft;
    }
    fg.style.left = xCoord + 'px';

    var foregroundelementheight = fg.offsetHeight ? fg.offsetHeight : fg.scrollHeight;
    yCoord = ((clientHeight - foregroundelementheight) / 2);
    // workaround for drag behavior which calls setlocation which in turn
    // changes the position of the panel to be absolute and requiring us
    // to add the scrollLeft so that it is positioned correctly.
    if (fg.style.position == 'absolute') {
        yCoord += scrollTop;
    }
    fg.style.top = yCoord + 'px';

    // make sure get location agrees with the location of the foreground element
    //this._layoutForegroundElement(xCoord, yCoord);


    // layout background element again to make sure it covers the whole background
    // in case things moved around when laying out the foreground element
    if (bg != undefined) {
        bg.style.width = Math.max(Math.max(document.documentElement.scrollWidth, document.body.scrollWidth), clientWidth) + 'px';
        bg.style.height = Math.max(Math.max(document.documentElement.scrollHeight, document.body.scrollHeight), clientHeight) + 'px';
    }
}
