﻿// ********** BACK OFFICE - CMT ********** //

// opens a rad window only for campaign
function openCampaignRadWindow(windowTitle, windowWidth, windowHeight, windowUrl) {
    parent.loadWindow(windowTitle, windowWidth, windowHeight, windowUrl);
}

// Close rad window of master page
function closeRadWindow() {
    var oWnd = GetRadWindowManager().GetWindowByName('RadWindowMaster');
    oWnd.Close();
}

// Close the parent rad window
function closeParentRadWindow() {
    parent.closeWindow();
}

// Close the parent rad window without refreshing page
function closeParentRadWindowWithoutRefresh() {
    parent.closeWindowWithoutRefresh();
}

function popupRadWindow(windowTitle, windowWidth, windowHeight, windowUrl, isModalWindow) {

    // Get a refence on the radWindow
    var oWnd = GetRadWindowManager().GetWindowByName('RadWindowMaster');

    // Set the radWindow Title        
    oWnd.set_title(windowTitle);

    // Set the radWindow behaviour
    oWnd.isModal = isModalWindow;
    oWnd.set_behaviors(Telerik.Web.UI.WindowBehaviors.Move + Telerik.Web.UI.WindowBehaviors.Close + Telerik.Web.UI.WindowBehaviors.Reload)

    // Set the radWindow size
    oWnd.setSize(windowWidth, windowHeight);

    // Set the radWindow url
    oWnd.setUrl(windowUrl);

    // Display radWindow
    oWnd.Show();
}

//Jquery for confirmation Message
function popupJQuery(windowTitle, windowWidth, windowHeight, message) {
    $('#JQueryConfirmation').modal({ onClose: modalClose });
}

function modalClose(dialog) {
    dialog.data.fadeOut('slow', function() {
        dialog.container.fadeOut('slow', function() {
            $.modal.close();
        });
    });
}

// ********** FRONT OFFICE ********** //

/*** FLASH OBJECTS ***/

var ExpressInstallURI = "/BAIFiles/BAI/Swf/expressInstall.swf";

// Embed a Flash object with Flash 9 ExpressInstall
// Usage:
// $("#divId").swfobject({
//    src:"footer_carousel.swf",
//    width:940, height:110,
//    wmode:"transparent",
//    flashvars:{ xmlpath:"path/to/xml" }
// });
$.fn.swfobject = function(htmlOptions, pluginOptions) {
    if (!$.fn.flash.hasFlash(9))
        return this.flash({
            src: ExpressInstallURI,
            width: Math.max(htmlOptions.width, 260),
            height: Math.max(htmlOptions.height, 168)
        },
            { version: 9, expressInstall: true }
        );

    if (!htmlOptions.id) htmlOptions.id = "swf" + (this.attr("id") || "movie");
    if (!htmlOptions.name) htmlOptions.name = htmlOptions.id;
    return this.flash(htmlOptions, pluginOptions);
}
if ($.fn.flash)
$.fn.flash.update = function(htmlOptions) {
    var url = String(location).split('?');
    url.splice(1, 0, '?hasFlash=true&');
    url = url.join('');
    var msg = '<p>Ce module nécessite le lecteur Adobe Flash. '
	    + '<a href="http://www.adobe.com/go/getflashplayer">Télécharger le lecteur Flash</a>. '
	    + 'Vous avez déjà Flash 9 ? <a href="' + url + '">cliquez ici.</a></p>';
    this.innerHTML = '<span class="alt">' + this.innerHTML + '</span>';
    jQuery(this)
		.addClass('flash-update')
		.prepend(msg);
}

/*** TOOL METHODS ***/

function openWindowFromDropDown(id) {
    var dropdownIndex = document.getElementById(id).selectedIndex;
    var dropdownValue = document.getElementById(id)[dropdownIndex].value;
    if (dropdownIndex!=0)
    return window.open(dropdownValue);
}

function printWindow(autoPrint) {
    window.print();
    if (autoPrint) {
        window.onAfterPrint = setTimeout('top.close();', 0);
    }
}

function openPopUpWindow(url, left, top, width, height) {
    
    if (left = '0') left = (screen.width - width) / 2;
    if (top = '0') top = (screen.height - height) / 2;

    var popUpWin = 0;

    if (popUpWin) {
        if (!popUpWin.closed) popUpWin.close();
    }

    popUpWin = window.open(url, null, 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=yes,width=' + width + ',height=' + height + ',left=' + left + ', top=' + top + ',screenX=' + left + ',screenY=' + top + '');
}

function reloadPage() {
    __doPostBack(null, null);
}

//
function SetEnterKeyClickEvent(defaultButtonId, _event) {
    var evnt;
    var srcElementType;

    if (document.all) {
        //IE
        evnt = window.event;
        srcElementType = evnt.srcElement.type;

    } else {
        // Non IE
        evnt = _event;
        srcElementType = evnt.target.type;
    }

    if (evnt.keyCode == 13 && srcElementType != 'textarea') {
        evnt.cancelBubble = true;
        evnt.returnValue = false;

        targetElement = document.getElementById(defaultButtonId);
        targetElement.click();
    }
    return false;
}

// clear input field default value
function make_blank(TheValue, myField) {
    var theField = $(eval("document.aspnetForm." + myField));
    if (theField.val() == TheValue) {
        theField.val("");
    }
}

// clear input field default value
function make_blank_password(TheValue, myField) {
    var theField = $(eval("document.aspnetForm." + myField));
    if (theField.val() == TheValue) {
        theField.val("");
        if (theField.get(0).outerHTML) {
            var src = theField.get(0).outerHTML.split('"text"').join('""');
            src = src.substr(0, src.length - 1) + ' type="password">';
            theField.replaceWith(src);
            $(eval("document.aspnetForm." + myField)).focus();
        }
        else theField.attr("type", "password");
    }
}


/*** RELOAD PAGE HEADER (and viewstate) ***/

function reloadHeader() {
    $.get(window.location.href, null, onHeaderLoad);
}
function onHeaderLoad(data, status) {
    if (status != "success") return;
    replaceViewstate(data);
    var H_MARKER = '<div class="maHeader">';
    var hs = data.indexOf(H_MARKER) + H_MARKER.length;
    if (hs < H_MARKER.length) return;
    var N_MARKER = '<div class="maNav">';
    var he = data.indexOf(N_MARKER, hs);
    if (he < 0) return;
    var html = data.substr(hs, he - hs);
    he = html.lastIndexOf("</div>") - 6;
    if (he < 0) return;
    html = html.substr(0, he);
    $("div.maHeader").html(html);
}
function replaceViewstate(data) {
    var VS_MARKER = 'id="__VIEWSTATE" value="';
    var vss = data.indexOf(VS_MARKER) + VS_MARKER.length;
    if (vss < VS_MARKER.length) return false;
    var vse = data.indexOf('"', vss + 1);
    if (vse < 0) return false;
    var viewstate = data.substr(vss, vse - vss);
    $("#__VIEWSTATE").val(viewstate);
}

/*** AUTOMATIC BEHAVIORS ***/

$(function() {

    $(".jsReadMore")
        .css("cursor", "pointer")
        .mouseover(function(e) {
            if (e.target.nodeName == "A" || e.target.parentNode.nodeName == "A") return;
            $(this).addClass("hover");
        })
        .mouseout(function(e) {
            if (e.target.nodeName == "A" || e.target.parentNode.nodeName == "A") return;
            $(this).removeClass("hover");
        })
        .click(function(e) {
            if (e.target.nodeName == "A" || e.target.parentNode.nodeName == "A") return;
            e.preventDefault();
            var url = $(this).find(".readMore a").attr("href");
            if (e.shiftKey) window.open(url);
            else document.location.href = url;
        });

});


/*** MESSAGE BOX ***/

function showConfirmation(message) {
    showPanel("CONFIRM_LAYER");
    panelContent.find(".inner").html(message);
}

/*** POPUP LAYER ***/

var panelContent;
var panelFrame;

function setIFramePaneltitle(src) {
    $("#POPUP_LAYER .top img").attr("src", src);
}

function showIFramePanel(src, titleImage) {
    $("#POPUP_LAYER .inner").empty().append($('<iframe frameborder="0" />'));
    panelFrame = $("#POPUP_LAYER iframe");
    panelFrame.css({ visibility: "hidden", height: 100 });

    if (titleImage) {
        if (titleImage.indexOf("/") < 0)
            setIFramePaneltitle("/BAIFiles/BAI/Images/PopupLayer/" + titleImage);
        else
            setIFramePaneltitle(titleImage);
    }
    else setIFramePaneltitle("/BAIFiles/BAI/Images/blank.gif");

    showPanel("POPUP_LAYER", 100);

    setTimeout(function() {
        panelFrame.attr("src", src).load(resizeFrame);
    }, 100);
    return false;
}

function resizeFrame(e) {
    var iframe = panelFrame.get(0);
    var doc = iframe.contentDocument || iframe.Document;
    var docHeight = Math.max(100, Math.min(400, $(doc.body).height() + 10));
    var totalHeight = docHeight + 95;
    var pageTop = getScroll();
    var dims = getPageDimensions();
    var endY = pageTop + (dims.height - totalHeight) / 2;

    panelFrame.css({ height: docHeight, visibility: "hidden" });
    panelContent
        .animate({ top: endY }, { duration: 600, complete: resizeEnd })
        .find(".inner").animate({ height: docHeight }, { duration: 600 });
}
function resizeEnd() {
    setTimeout(fadeIFrame, 50);
}
function fadeIFrame() {
    panelFrame.css({ visibility: "visible" });
}

function showPanel(idPanel, innerHeight) {
    var panel = $("#" + idPanel);
    panel.appendTo($("form")); // over page content

    panel.find(".bg").css({ opacity: 0.3, height: $("body").height() });
    panel.find(".btClose").click(hidePanel);
    panel.show();

    panelContent = panel.find(".content");
    if ($.browser.msie > 0 && $.browser.msie < 7) panelContent.addClass("ie6");
    if (innerHeight)
        panelContent.find(".inner").css({ height: innerHeight });
    autoCenter();
    $(window).scroll(autoCenter);
    setTimeout(fadePanel, 50);
}
function fadePanel() {
    if (panelContent)
        panelContent.fadeIn(200);
}

function hidePanel(e) {
    if (e) e.preventDefault();
    if (panelContent) {
        panelContent.parents(".popupPanel").find(".bg").css({ opacity: 0 });
        panelContent.fadeOut(200, removePanel);
        panelContent = null;
    }
    if (panelFrame) {
        panelFrame.unbind("load", resizeFrame);
        panelFrame.attr("src", "");
    }
    return false;
}
function removePanel() {
    var panel = $(this).parents(".popupPanel");
    panel.hide().find(".inner").css({ height: 200 });
    panel.find("frame2").attr("src", "").css({ height: 200, visibility: "hidden" });
}

function autoCenter() {
    if (panelContent) {
        var pageTop = getScroll();
        var dims = getPageDimensions();
        var endY = pageTop + (dims.height - panelContent.find(".inner").height() - 95) / 2;
        panelContent.css({ top: endY });
    }
}

function getScroll() {
    if (document.all) return top.document.documentElement.scrollTop;
    else return top.pageYOffset;
}
function getPageDimensions() {
    var dimensions = { width: 0, height: 0 };
    if ($.browser.msie) {
        dimensions.width = document.documentElement.clientWidth;
        dimensions.height = document.documentElement.clientHeight;
    }
    else if (window.innerWidth && window.innerHeight) {
        dimensions.width = window.innerWidth;
        dimensions.height = window.innerHeight;
    }
    return dimensions;
}
/* END POPUP LAYER */


