﻿//Addon js per replace su tutte le occorrenze
String.prototype.totalReplace = function (find, replace) {
    var temp_array = this.split(find);
    return temp_array.join(replace);
};
window.openSmall = function (url, width, height) {
    this.Width = width == undefined ? 500 : width;
    this.Height = height == undefined ? 300 : height;
    this.Left = ($(window).width() - this.Width) / 2 + $(window).scrollLeft();
    this.Top = ($(window).height() - this.Height) / 2 + $(window).scrollTop();
    window.open(url, 'opup', 'width=' + this.Width + ',height=' + this.Height + ' ,toolbar=no, location=no,status=no,menubar=no,scrollbars=no,resizable=no,top=' + this.Top + ',left=' + this.Left);
};
window.alert = function (nText) {
    popover.Alert(nText);
};
String.prototype.capitalize = function () {
    return this.replace(/\w+/g, function (a) {
        return a.charAt(0).toUpperCase() + a.substr(1).toLowerCase();
    });
};
//Funzioni generali
function isNull(str) {
    if (str != undefined && str != null && str != '')
        return false;
    else
        return true;
}
function isEmailValid(strEmail) {
    validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
    // search email text for regular exp matches
    if (strEmail.search(validRegExp) == -1) {
        return false;
    }
    return true;
}
function Print() {
    window.print();
}
//fine funzioni generali

//classe per la gestione popup
function popupManager(ObjParams) {
    if (ObjParams == undefined) { var ObjParams = {} };
    this.Id = ObjParams.Id;
    this.ContainerClass = 'overPageContainer';
    this.VideoBoxId = 'video-box-cont';
    this.VideoDivId = 'insvideo';
    this.AlertBoxId = 'custom-alert';
    this.TissueId = 'overTissue';
    this.ImgClose = '/img/common/video-close.gif';
    this.GenericBoxClass = 'popover-generic-box';
    this.GenericCloseClass = 'popover-generic-box-close';
    this.AlertImg = '/img/common/custom-alert-exlamation.gif';
    this.YouTubeEmbed = '<iframe title="YouTube video player" width="640" height="385" src="http://www.youtube.com/embed/{ytId}" frameborder="0" allowfullscreen></iframe>';

    this.Alert = function (nText) {
        var overContainer = this.createOverPageContainer();
        var overTissue = this.createOverTissue();

        $(overContainer).append(overTissue);
        $(overContainer).append(this.createAlertBox(nText));

        $(document.body).append(overContainer);

        $('#' + this.AlertBoxId).center();
        $('.' + this.ContainerClass).hide().fadeIn(200);
    }
    this.createAlertBox = function (mText) {
        var br = $('<br />');
        $(br).addClass('clear');

        var boxCont = $('<div />');
        $(boxCont).attr('id', this.AlertBoxId);
        var boxTop = $('<div />');
        $(boxTop).addClass('top');
        var boxBody = $('<div />');
        $(boxBody).addClass('body');
        var boxCenter = $('<div />');
        $(boxCenter).addClass('center');
        var boxBottom = $('<div />');
        $(boxBottom).addClass('bottom');
        var img = $('<img />');
        $(img).attr('src', this.AlertImg);
        var p = $('<p />');
        $(p).html(mText);
        var btnOk = $('<div />');
        $(btnOk).addClass('btn-ok');

        var Obj = this;
        $(btnOk).click(function () { Obj.closeTissue(); });

        $(boxCenter).append(img);
        $(boxCenter).append(p);
        $(boxCenter).append(br);
        $(boxCenter).append(btnOk);
        $(boxBody).append(boxCenter);

        $(boxCont).append(boxTop);
        $(boxCont).append(boxBody);
        $(boxCont).append(br);
        $(boxCont).append(boxBottom);

        return boxCont;
    }
    this.openDiv = function (divObj) {
        var overContainer = this.createOverPageContainer();
        var overTissue = this.createOverTissue();
        $(overContainer).append(overTissue);

        var divClone = $(divObj).clone();
        $(divObj).remove();

        var genericBox = this.createGenericBox();
        $(genericBox).append(divClone);
        $(overContainer).append(genericBox);
        $(document.body).append(overContainer);
        var Obj = this;
        $('.' + this.GenericCloseClass).click(function () { Obj.closeTissue(); });
        $('.' + this.GenericBoxClass).center();
        $('.' + this.ContainerClass).hide().fadeIn(200);
    }
    this.openHtml = function (strHtml) {
        var overContainer = this.createOverPageContainer();
        var overTissue = this.createOverTissue();
        $(overContainer).append(overTissue);

        var Obj = this;
        $.get(strHtml, function (data) {
            var genericBox = Obj.createGenericBox();
            $(genericBox).append(data);
            $(overContainer).append(genericBox);
            $(document.body).append(overContainer);
            $('.' + Obj.GenericCloseClass).click(function () { Obj.closeTissue(); });
            $('.' + Obj.GenericBoxClass).center();
            $('.' + Obj.ContainerClass).hide().fadeIn(200);
        });
    }
    this.openYoutube = function (YouTubeId) {
        var overContainer = this.createOverPageContainer();
        var overTissue = this.createOverTissue();
        $(overContainer).append(overTissue);
        $(overContainer).append(this.createVideoBox());

        $(document.body).append(overContainer);

        var embedFrame = this.YouTubeEmbed.replace('{ytId}', YouTubeId);

        $('#' + this.VideoDivId).append(embedFrame);

        $('#' + this.VideoBoxId).center();
        $('.' + this.ContainerClass).hide().fadeIn(200);
    }
    this.openSmartDiv = function (divObj) {
        var overContainer = this.createOverPageContainer();
        var overTissue = this.createOverTissue();
        $(overContainer).append(overTissue);
        $(overContainer).append(this.createVideoBox());

        $(document.body).append(overContainer);

        $('#' + this.VideoDivId).append(divObj);

        $('#' + this.VideoBoxId).center();
        $('.' + this.ContainerClass).hide().fadeIn(200);
    }
    this.openVideo = function (strVideo) {
        var overContainer = this.createOverPageContainer();
        var overTissue = this.createOverTissue();
        var flashvars = {
            video: strVideo
        };
        var params = {};
        var attributes = {
            menu: 'false',
            wmode: 'transparent',
            quality: 'high'
        };
        $(overContainer).append(overTissue);
        $(overContainer).append(this.createVideoBox());

        $(document.body).append(overContainer);

        swfobject.embedSWF('/swf/video-player.swf', this.VideoDivId, '640', '385', '9.0.0', '/swf/expressInstall.swf', flashvars, params, attributes);
        $('#' + this.VideoBoxId).center();
        $('.' + this.ContainerClass).hide().fadeIn(200);
    }
    this.closeTissue = function () {
        $('.' + this.ContainerClass).fadeOut(200, function () { $(this).remove(); })
    }
    this.createOverPageContainer = function () {
        var overPage = $('<div />');
        $(overPage).attr('class', this.ContainerClass);

        var wWin = $(document).width();
        var wHei = $(document).height();

        $(overPage).css('height', wHei);
        $(overPage).css('width', wWin);

        return overPage;
    }
    this.createOverTissue = function () {
        var overTissue = $('<div />');
        $(overTissue).attr('id', this.TissueId);
        return overTissue;
    }
    this.createVideoBox = function () {
        var boxCont = $('<div />');
        $(boxCont).attr('id', this.VideoBoxId);
        var Box = $('<div />');
        $(Box).attr('id', 'video-box');
        var boxTop = $('<div />');
        $(boxTop).attr('id', 'video-box-top');
        var boxLeft = $('<div />');
        $(boxLeft).attr('id', 'video-box-left');
        var boxRight = $('<div />');
        $(boxRight).attr('id', 'video-box-right');
        var boxBottom = $('<div />');
        $(boxBottom).attr('id', 'video-box-bottom');
        var insvideo = $('<div />');
        $(insvideo).attr('id', this.VideoDivId);
        var a = $('<a />');
        $(a).attr('id', 'video-box-top-img');
        $(a).attr('href', 'javascript:void(0);');

        var Obj = this;
        $(a).click(function () { Obj.closeTissue(); });
        var img = $('<img />');
        $(img).attr('width', '23');
        $(img).attr('height', '22');
        $(img).attr('alt', 'chiudi');
        $(img).attr('src', this.ImgClose);

        $(a).append(img);
        $(boxTop).append(a);
        $(Box).append(boxTop);
        $(Box).append(boxLeft);
        $(Box).append(insvideo);
        $(Box).append(boxRight);
        $(Box).append(boxBottom);
        $(boxCont).append(Box);

        return boxCont;
    }
    this.createGenericBox = function () {
        var div = $('<div />');
        $(div).addClass(this.GenericBoxClass);
        return div;
    }
}
//fine classe per la gestione popup

