﻿/// <reference path="../Frameworks/jquery-1.3.2.min-vsdoc.js" />

(function () {

    var 

    FOF = window.FOF = window.F = function (selector) {
        return new FOF.fn.init(selector);
    };

    var _FOFiFrames = new Array();

    FOF.fn = FOF.prototype = {

        init: function (selector) {
            this.selector = selector;
        },

        // The current version of FOF being used
        FOF: "0.1",
        selector: "",

        iFrameCreate: function (name) {

            //Public properties
            this.name = name;
            this.source;
            this.onLoad;

            //private fields
            var 
            frameStr = '',
            indexExist = -1;

            //prevent loading same iFrame twice
            for (i = 0; i <= _FOFiFrames.length - 1; i++) {
                if (_FOFiFrames[i].name == name) {
                    indexExist = i;
                    break;
                }
            }

            var createFrame = function (name, source, load, selector) {

                frameStr = '<div id="' + name + '" class="frameDefault" >';
                frameStr += '<div id="' + name + 'Drag">Drag</div>';
                frameStr += '<iframe frameborder="0" width="300" height="200" scrolling="auto" src="' + source + '"></iframe>';
                frameStr += '<br />';
                frameStr += '<a>Close</a>';
                frameStr += '</div>';

                //Load iframe
                $(selector).prepend(frameStr);

                $('#' + name + ' a').click(function () {
                    $('#' + name).fadeOut(400);
                });

                //attach user defined function
                if (typeof (load) != 'undefined')
                    $(selector + ' iframe').bind('load', load);

                //$(selector + ' iframe').attr('src', source);

                //Options
                $('#' + name).draggable({ handle: '#' + name + 'Drag' });

            }

            this.display = function () {

                //window.open(this.source, this.name);

                if (indexExist == -1) {
                    createFrame(this.name, this.source, this.onLoad, this.selector);
                    //_FOFiFrames.push(this);
                }
                else {
                    if (typeof _FOFiFrames[indexExist].onLoad == "function")
                    //_FOFiFrames[indexExist].onLoad();
                        $(this.selector + ' iframe').attr('src', this.source);

                }

                $('#' + this.name).fadeIn(400);
            }

            return this;
        }
    };

    FOF.fn.init.prototype = FOF.fn;

})();

//Load the following function asap (generally after loading HTML and before loading picture)
$(document).ready(
	            function () {

	                //
	                var admin = $('div.divGroupAdmin')

	                if (admin.length > 0)
	                    initPage();

	            }
         );    //end $(document).ready

function initPage() {
    //binding elements
    $('a.hlPageManageOn').bind('click', PageManageOn);
    $('a.hlPageManageoff').bind('click', PageManageOff);
    $('a.hlPageManageWebPart').bind('click', PageManageWebPart);
    $('.lnkSave').bind('click', PageManageSave);
    $("div.WebPartListDesign ul li a").bind('click', initTab);
    $('.WebPartBoxBannerAdminDelete').bind('click', WebPartDelete);
    $('div.WebPartCatalogClose a').bind('click', WebPartCatalogClose);

    $('.WebPartBoxBannerAdmin').css('cursor', 'move');

    // Prevent clicking enter launch global search when editing webpart title
    $(".WebPartBoxBannerAdminTitle").keypress(function (event) {
        if (event.which == 13) {
            event.preventDefault();
        }
    });

    //Add the sortable capapcity to the div with ccs class 'block' into div with css class WebPartBoxBannerAdmin      
    $("div.groupWrapper").sortable({
        placeholder: 'sortHelper',
        connectWith: 'div.groupWrapper',
        handle: 'div.WebPartBoxBannerAdmin',
        forcePlaceholderSize: true,
        tolerance: 'pointer',
        activate: function (event, ui) { $("div.groupWrapper").toggleClass("sortableactive"); },
        deactivate: function (event, ui) { $("div.groupWrapper").toggleClass("sortableactive"); }
    });

    //Drag & drop web part catalog
    $("div.draggable").draggable({
        opacity: 0.7,
        handle: 'div.WebPartCatalogHeader'
    });

    $("div.WebPartCatalog div.block").draggable({
        connectToSortable: 'div.groupWrapper',
        helper: 'clone'
    });
};

//Functions

var PageManageOn = function (e) {
    $('div.WebPartBoxBannerAdminCanHide').slideDown(800);
    $('div.WebPartBoxDetailAdminCanHide').css("border-width", "1px");
    $('a.hlPageManageoff').css("display", "block");
    $('a.hlPageManageWebPart').css("display", "block");
    $('a.hlPageManageOn').css("display", "none");
    $('input.lnkCancel').css("display", "block");
    $('input.lnkSave').css("display", "block");
    $('select.ddlStyle').css("display", "block");
    $('div.divGroupAdmin').addClass("divGroupAdminOpen");
};

var PageManageOff = function (e) {
    $('div.WebPartBoxBannerAdminCanHide').slideUp(800);
    $('div.WebPartBoxDetailAdminCanHide').css("border-width", "0px");
    //if ($('div.WebPartCatalog').css('display') != 'none') 
    $('div.WebPartCatalog').slideUp(800);
    $('a.hlPageManageoff').css("display", "none");
    $('a.hlPageManageWebPart').css("display", "none");
    $('input.lnkCancel').css("display", "none");
    $('input.lnkSave').css("display", "none");
    $('select.ddlStyle').css("display", "none");
    $('a.hlPageManageOn').css("display", "block");
    $('div.divGroupAdmin').removeClass("divGroupAdminOpen");
};

var PageManageWebPart = function (e) {
    $('div.WebPartCatalog').slideDown(800);
};

var WebPartCatalogClose = function (e) {
    $('div.WebPartCatalog').slideUp(800);
};

var WebPartDelete = function (e) {
    var headerId = $(this).parent().parent().get(0).id;
    $('#' + headerId).parent().get(0).id = 'WPRemove';
    var zoneId = $('#WPRemove').parent().get(0).id
    $('#' + zoneId + ' #WPRemove').remove();
    return false;
};

var initTab = function (e) {

    var divId = new String(this.id);
    divId = divId.substring(4);

    divId = 'div.' + divId;

    switch ($('select.ddlStyle').val()) {
        case 'WebPartListByZone':
            $('div.div3ColumnsByZone').css("display", "none");
            $(divId).css("display", "block");
            break;
        case 'WebPartListByMenu':
            $('.div3ColumnsByMenu .block').css("display", "none");
            $(divId).css("display", "block");
            break;
        default:

    }
};


var PageManageSave = function serialize(s) {

    var StringToSave = '';
    var webPartTitle = '';
    var test = '';

    //For each webpart zone
    $("div.groupWrapper").each(
	                    function () {

	                        StringToSave += "/" + this.id + "=";
	                        //For each webpart in webpart zone
	                        $("#" + this.id + "> div > div").each(
	                        function () {
	                            //It's a link
	                            if (this.id.indexOf('CatalogExistingItem') > -1) {
	                                $(this.children[1].children).each(
	                                    function () {
	                                        if (this.nodeName == 'SPAN')
	                                            StringToSave += this.className + "|";
	                                    }
	                                )
	                            }
	                            //it 's a new web part
	                            else {
	                                //Adding the web part id and the web part title (first input in the WebPartBoxBannerLeftPart div -header-...)
	                                try {
	                                    webPartTitle = "#" + $("#" + this.children[0].id + "> DIV.WebPartBoxBannerLeftPart > input")[0].value;
	                                } catch (err) {
	                                    webPartTitle = ''; //this.children[0].innerText;
	                                }

	                                StringToSave += this.id + webPartTitle + "|";
	                            }
	                        }
	                     )

	                        //suppress the last coma
	                        StringToSave = StringToSave.slice(0, -1);

	                        //$("div.PageManageHeader> input").attr("value", $("div.PageManageHeader> input").attr("value") + "/" + this.id + "=" + $("#" + this.id).sortable("toArray"));
	                        //test += "/" + this.id + "=" + $("#" + this.id).sortable("toArray");
	                    }

	                );

    $("div.PageManageHeader> input").attr("value", StringToSave);
    //alert(StringToSave);

};


function openIframe(iFrame) {

    var page = new F('body').iFrameCreate('frameDefault');
    //page.source = 'http://www.google.com';
    page.source = iFrame.replace("~", "")
    page.onLoad = function () {
        //alert('loaded');
    }
    page.display();
}        

