﻿function insertRCIRIntoWymEditor(rcirPH) {    
    WYMeditor.INSTANCES[0].insert(rcirPH);
}

function GetCurrentContentCode() {
    if ((typeof (currentContentItemCode) == 'undefined') || (currentContentItemCode == null)) {
        if (window.opener != null) {
            if ((typeof (window.opener.currentContentItemCode) == 'undefined') || (window.opener.currentContentItemCode == null)) {
                return window.opener.opener.currentContentItemCode;
            }
            else {
                return window.opener.currentContentItemCode
            }
        }
        else
            return "";
    }
    else
        return currentContentItemCode;
}



function initJQueryBinding() {

    $.extend({
        getCurrentUrlVars: function () {
            var vars = [], hash;
            var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
            for (var i = 0; i < hashes.length; i++) {
                hash = hashes[i].split('=');
                vars.push(hash[0]);
                vars[hash[0]] = hash[1];
            }
            return vars;
        },
        getCurrentUrlVar: function (name) {
            return $.getCurrentUrlVars()[name];
        }
    });


    $.extend({
        getUrlVars: function (theUrl) {
            var vars = [], hash;
            var hashes = theUrl.slice(theUrl.indexOf('?') + 1).split('&');
            for (var i = 0; i < hashes.length; i++) {
                hash = hashes[i].split('=');
                vars.push(hash[0]);
                vars[hash[0]] = hash[1];
            }
            return vars;
        },
        getUrlVar: function (theUrl, name) {
            return $.getUrlVars(theUrl)[name];
        }
    });

    //window.alert(GetCurrentContentCode());
    // Renders RCIRs via AJAX taking a link URL as argument
    jQuery.fn.renderRCIR = function () {
        return this.each(function () {
            var rcirUrl = new String(this);
            var rciCode = $.getUrlVars(rcirUrl)['RCICode'];
            var rcirCode = $.getUrlVars(rcirUrl)['ReferenceCode'];
            var forceViewMode = $.getUrlVars(rcirUrl)['ForceViewMode'];
            //window.alert(rciCode + " / " + rcirCode + " / " + forceViewMode);
            if (rciCode != null && rciCode.length >= 19 && rcirCode != null && rcirCode.length >= 19) {
                var referenceUrl = '../../ContentItems/Public/ContentItems_ReferenceView.ashx?RCICode=' + rciCode + '&ReferenceCode=' + rcirCode + '&ForceViewMode=' + forceViewMode;
                var newHtml = $.ajax({ url: referenceUrl, async: true }).responseText;
                $(this).replaceWith(newHtml);
            }
        });
    };



   
    $("a.ImageThumbnail").fancybox({
        'type': 'image',
        'transitionIn': 'elastic',
        'transitionOut': 'elastic',
        'titlePosition': 'inside',
        'titleFormat': function (title, currentArray, currentIndex, currentOpts) {
            return  (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '');
        }
    });
      
    $("a.ImageThumbnailNoBG").fancybox({
        'type': 'image',
        'transitionIn': 'elastic',
        'transitionOut': 'elastic',
        'titlePosition': 'inside',
        'titleFormat': function (title, currentArray, currentIndex, currentOpts) {
            return (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '');
        }
    });
        
   
    //$('a.Reference').renderRCIR();

    $('.Date').datepicker({ dateFormat: 'dd/mm/yy' });
    $('alt="Date"').datepicker({ dateFormat: 'dd/mm/yy' });

    // $(".Date").mask({ mask: "##/##/####" });


    function buildWymButton(wym, btnClass, btnName, btnIconUrl, btnText, btnHandler, previousSiblingMatch) {
        //construct the button's html
        var btnHtml = "<li class='" + btnClass + "'>"
                     + "<a name='" + btnName + "' href='#'"
                     + " style='background-image:"
                     + " url(" + btnIconUrl + ")'>"
                     + btnText
                     + "</a></li>";
        addWymButton(wym, btnClass, btnHtml, btnHandler, previousSiblingMatch);
    }

    function addWymButton(wym, btnClass, btnHtml, btnHandler,previousSiblingMatch) {

        if (previousSiblingMatch == null) {
            //add the button to the tools box
            jQuery(wym._box)
            .find(wym._options.toolsSelector + wym._options.toolsListSelector)
            .append(btnHtml);
        }
        else {
            //add the button to the tools box
            $(previousSiblingMatch).after(btnHtml);             
        }
        //handle click event
        jQuery(wym._box).find('li.' + btnClass + ' a').click(btnHandler);
    }

    // FULL EDITOR
    jQuery('.wymeditor').wymeditor({
        stylesheet: '../../../stylesheet.ashx?stylesheets=%7e%2fModules%2fjQuery%2fPublic%2fEditableText%2feditableText.css%2c%7e%2fApp_Themes%2fDefault%2f0_Tags.css%2c%7e%2fApp_Themes%2fDefault%2f1_Base.css%2c%7e%2fApp_Themes%2fDefault%2f1_MasterPage.css%2c%7e%2fApp_Themes%2fDefault%2f2_BoxesAndContainers.css%2c%7e%2fApp_Themes%2fDefault%2f2_Search.css%2c%7e%2fApp_Themes%2fDefault%2f3_Menus.css%2c%7e%2fApp_Themes%2fDefault%2f3_Modules_Calendar.css%2c%7e%2fApp_Themes%2fDefault%2f3_Modules_ContentItems.css%2c%7e%2fApp_Themes%2fDefault%2f3_Modules_ContentItems_News.css%2c%7e%2fApp_Themes%2fDefault%2f3_Modules_ContentItems_Project.css%2c%7e%2fApp_Themes%2fDefault%2f3_Modules_ContentItems_TwoLevelsContents.css%2c%7e%2fApp_Themes%2fDefault%2f3_Modules_ContentItems_WebBonesCart.css%2c%7e%2fApp_Themes%2fDefault%2f3_Modules_Tagging.css%2c%7e%2fApp_Themes%2fDefault%2f3_Toolbars.css%2c%7e%2fApp_Themes%2fDefault%2f3_Tools.css%2c%7e%2fApp_Themes%2fDefault%2f4_Custom.css%2c%7e%2fApp_Themes%2fDefault%2f5_Modificators.css%2c%7e%2fApp_Themes%2fDefault%2fjquery-ui-.custom.css%2c%7e%2fApp_Themes%2fDefault%2fjquery-ui-1.8.2.custom.css%2c%7e%2fApp_Themes%2fDefault%2fwymeditor.css',      //styles to load
        lang: 'it',
        skin: 'default',                //activate silver skin
        postInit: function (wym) {
            // 'add internal reference' button
            buildWymButton(wym, 'WymAddInternalReferenceBtn', 'AddInternalReferenceBTN', '../../../Modules/System/Images/XhtmlEditor/XhtmlEditor_RCIR_InternalReferenceLink.png', 'Aggiungi referenza (immagini, video, links interni, etc) dalla pagina corrente',
             function () {
                 window.open('../../../Modules/ContentItems/Admin/ContentItems_GetInternalReferencePlaceHolder.aspx?RCICode=' + GetCurrentContentCode() + '&JSCallbackFunctionName=insertRCIRIntoWymEditor', 'InsertInternalRCIR', 'toolbar=false,location=false,directories=false,status=false,menubar=false,scrollbars=yes');
                 return false;
             },
             '.wym_tools_unlink'
             );

            // 'add shared reference' button
            buildWymButton(wym, 'WymAddSharedReferenceBtn', 'AddSharedReferenceBTN', '../../../Modules/System/Images/XhtmlEditor/XhtmlEditor_RCIR_SharedReferenceLink.png', 'Aggiungi referenza (immagini, video, links interni, etc) tra le risorse condivise',
             function () {
                 window.open('../../../Modules/ContentItems/Admin/ContentItems_GetSharedReferencePlaceHolder.aspx?RCICode=' + GetCurrentContentCode() + '&JSCallbackFunctionName=insertRCIRIntoWymEditor', 'InsertSharedRCIR', 'toolbar=false,location=false,directories=false,status=false,menubar=false,scrollbars=yes');
                 return false;
             },
             '.wym_tools_unlink'
             );

            // 'add other page reference' button
            buildWymButton(wym, 'WymAddOtherPageReferenceBtn', 'AddOtherPageReferenceBTN', '../../../Modules/System/Images/XhtmlEditor/XhtmlEditor_RCIR_ExternalReferenceLink.png', 'Aggiungi un link ad una pagina interna al sito',
             function () {
                 window.open('../../../Modules/ContentItems/Admin/ContentItems_GetOtherPageReferencePlaceHolder.aspx?RCICode=' + GetCurrentContentCode() + '&JSCallbackFunctionName=insertRCIRIntoWymEditor', 'InsertOtherPageRCIR', 'toolbar=false,location=false,directories=false,status=false,menubar=false,scrollbars=yes');
                 return false;
             },
             '.wym_tools_unlink'
             );

            // 'add filesystem reference' button
            buildWymButton(wym, 'WymAddFSReferenceBtn', 'AddFSReferenceBTN', '../../../Modules/System/Images/XhtmlEditor/XhtmlEditor_RCIR_FileLink.png', 'Aggiungi una referenza ad un file nel sito',
             function () {
                 window.open('../../../Modules/ContentItems/Admin/ContentItems_GetFSReferencePlaceHolder.aspx?RCICode=' + GetCurrentContentCode() + '&JSCallbackFunctionName=insertRCIRIntoWymEditor', 'InsertFSRCIR', 'toolbar=false,location=false,directories=false,status=false,menubar=false,scrollbars=yes');
                 return false;
             },
             '.wym_tools_unlink'
             );

            // 'add box' button
            buildWymButton(wym, 'WymAddBoxBtn', 'AddBoxBTN', '../../../Modules/System/Images/application_xp.png', 'Aggiungi box con titolo',
             function () {
                 wym.insert('<div class="ToolBox ui-widget"><div class="ToolBoxTitle ui-widget-header ui-corner-top">Titolo</div><div class="ToolBoxBody ui-widget-content">Contenuti</div></div>');
                 return false;
             }
             );

            // 'add box' button
            buildWymButton(wym, 'WymAddBoxBtn2', 'AddBoxBTN2', '../../../Modules/System/Images/application_xp.png', '222',
             function (aaa) {
                 //window.alert(aaa);

                 //wym.insert('<div class="ToolBox ui-widget"><div class="ToolBoxTitle ui-widget-header ui-corner-top">Titolo</div><div class="ToolBoxBody ui-widget-content">Contenuti</div></div>');
                 wym.wrap('<u style="font-size:20px;display:inline">', '</u>');
                 return false;
             }
             );

            wym.hovertools();          //activate hovertools
            wym.resizable();           //and resizable plugins
            wym.fullscreen();
        }
    });

    // SIMPLE EDITOR
    jQuery('.wymeditorSimple').wymeditor({
        stylesheet: '../../../stylesheet.ashx?stylesheets=%7e%2fModules%2fjQuery%2fPublic%2fEditableText%2feditableText.css%2c%7e%2fApp_Themes%2fDefault%2f0_Tags.css%2c%7e%2fApp_Themes%2fDefault%2f1_Base.css%2c%7e%2fApp_Themes%2fDefault%2f1_MasterPage.css%2c%7e%2fApp_Themes%2fDefault%2f2_BoxesAndContainers.css%2c%7e%2fApp_Themes%2fDefault%2f2_Search.css%2c%7e%2fApp_Themes%2fDefault%2f3_Menus.css%2c%7e%2fApp_Themes%2fDefault%2f3_Modules_Calendar.css%2c%7e%2fApp_Themes%2fDefault%2f3_Modules_ContentItems.css%2c%7e%2fApp_Themes%2fDefault%2f3_Modules_ContentItems_News.css%2c%7e%2fApp_Themes%2fDefault%2f3_Modules_ContentItems_Project.css%2c%7e%2fApp_Themes%2fDefault%2f3_Modules_ContentItems_TwoLevelsContents.css%2c%7e%2fApp_Themes%2fDefault%2f3_Modules_ContentItems_WebBonesCart.css%2c%7e%2fApp_Themes%2fDefault%2f3_Modules_Tagging.css%2c%7e%2fApp_Themes%2fDefault%2f3_Toolbars.css%2c%7e%2fApp_Themes%2fDefault%2f3_Tools.css%2c%7e%2fApp_Themes%2fDefault%2f4_Custom.css%2c%7e%2fApp_Themes%2fDefault%2f5_Modificators.css%2c%7e%2fApp_Themes%2fDefault%2fjquery-ui-.custom.css%2c%7e%2fApp_Themes%2fDefault%2fjquery-ui-1.8.2.custom.css%2c%7e%2fApp_Themes%2fDefault%2fwymeditor.css',      //styles to load
        lang: 'it',
        skin: 'default',                //activate silver skin
        postInit: function (wym) {                                  
            wym.hovertools();          //activate hovertools
            wym.resizable();           //and resizable plugins
            wym.fullscreen();
        }
    });


  //  window.alert(jQuery('.wymeditor').insert);

    $("button, input:submit, input:file").button();

    $('.Accordion').accordion({ autoHeight: false });
    $('.AccordionCollapsible').accordion({ autoHeight: false, collapsible: true, active: false });
    $('.TabsNested').tabs();
    $('.TabsMain').tabs();
    $('.EditBarsContainerTabs').tabs();
    $('ul.ButtonsList li').hover(
		function () { $(this).addClass('ui-state-hover'); },
		function () { $(this).removeClass('ui-state-hover'); }
	);

    $('.HideOnLoad').hide();






    $('#CarouselTOP').infiniteCarousel({
        transitionSpeed: 2000,
        displayTime: 5000,
        textholderHeight: .25,
        displayProgressBar: 1,
        displayThumbnailBackground: 1,
        thumbnailWidth: '72px',
        thumbnailHeight: '70px',
        thumbnailFontSize: '3em'


    });

    $('#CarouselBOTTOM').infiniteCarousel({
        transitionSpeed: 2000,
        displayTime: 5000,
        textholderHeight: .25,
        displayProgressBar: 1,
        displayThumbnailBackground: 1,
        thumbnailWidth: '80px',
        thumbnailHeight: '80px',
        thumbnailFontSize: '3em'
    });

    // $('#CarouselBOTTOM').infiniteCarousel();
    //$('div.vcard').addClass('ui-widget ui-widget-content');
    //$('div.vcard span.n').addClass('ui-widget-header'); 

    // $(".Throbber").throbber({ image: "../../../Modules/System/Images/ajax-loader.gif"});
    jQuery.fn.center = function () {
        this.css("position", "absolute");
        this.css("top", ($(window).height() - this.height()) / 2 + $(window).scrollTop() + "px");
        this.css("left", ($(window).width() - this.width()) / 2 + $(window).scrollLeft() + "px");
        return this;
    }


    $("#showHiddenParagraphs").click(function () {
        $(this).hide();
        $("#hiddenParagraphsContainer").slideToggle(1000);

    });

    $(".EditBarsContainerTabsActivator").click(function () {
        $(this).next().slideToggle(500); // toolbar     
    });


    $('.RssFeedContainer').each(function () {
        var rssUrl = $(this).attr('title');
        //window.alert(rssUrl);
        $(this).rssReader({
            targeturl: '../../../Modules/System/Public/System_RssProxy.ashx?r=' + rssUrl,
            items: 5,
            Maxlength: 80,
            loadingImg: '../../../Modules/System/Images/ajax-loader.gif',
            LinkText:'[Link]',
            ContainerMatch: '.RssFeedContainer'
        });
    });

    $('.RssFeedContainerInPage').each(function () {
        var rssUrl = $(this).attr('title');
        //window.alert(rssUrl);
        $(this).rssReader({
            targeturl: '../../../Modules/System/Public/System_RssProxy.ashx?r=' + rssUrl,
            items: 15,
            Maxlength: 80,
            loadingImg: '../../../Modules/System/Images/ajax-loader.gif',
            LinkText: '[Link]',
            ContainerMatch: '.RssFeedContainerInPage'
        });
    });  
                      
}


//----------------
function postReloadContentOnly() {
    initJQueryBinding();
    // window.alert('fff');
}

//----------------
$(document).ready(function () {
    initJQueryBinding();

    webBonesEnvironment.onSubmit = function () {
        if (typeof (Page_IsValid) != "undefined" && !Page_IsValid)
            return;

        $("#myThrobber").center();
        $("#myThrobber").show();
        $.throbberShow({ parent: "#myThrobber", image: "../../../Modules/System/Images/ajax-loader.gif" });
    }


    webBonesEnvironment.onUnload = function () {
        $("#myThrobber").hide();
        $.throbberHide();
    }

    $(window).unload(webBonesEnvironment.onUnload);
}
);

