﻿


(function() {

    var blockdrag = false;
    var singleaction = 0;
    var Dom = YAHOO.util.Dom;
    var Event = YAHOO.util.Event;
    var DDM = YAHOO.util.DragDropMgr;

    YAHOO.example.DDApp = {
        init: function() {

            //the rows line denotes how many widgets you may have on each panel. i have set this to 10 for the time being but
            //will need upgraded to match the available widgets.
            //if controls are added to the website you will NEED to change ctl08 to the relavent number
            //
            var rows = 10, cols = 2, i, j;
            for (i = 1; i < cols + 1; i = i + 1) {
                new YAHOO.util.DDTarget("iPortalCore_ctl00_ul" + i);
            }

            for (i = 1; i < cols + 1; i = i + 1) {
                for (j = 1; j < rows + 1; j = j + 1) {
                    new YAHOO.example.DDList("li" + i + "_" + j);
                }
            }

            Event.on("showButton", "click", this.showOrder);
            Event.on("switchButton", "click", this.switchStyles);
        },

        showOrder: function() {
            var parseList = function(ul, title) {
                var items = ul.getElementsByTagName("li");
                var out = "" // = title + ": ";
                for (i = 0; i < items.length; i = i + 1) {
                    if (items[i].className != "") {
                        out += items[i].className + ",";
                    }

                }
                return out;
            };

            var ul1 = Dom.get("iPortalCore_ctl00_ul1"), ul2 = Dom.get("iPortalCore_ctl00_ul2");



            var list1;
            var list2;

            list1 = parseList(Dom.get("iPortalCore_ctl00_ul1"), "List 1");
            list1 = list1.substring(0, list1.length - 1);
            list2 = parseList(Dom.get("iPortalCore_ctl00_ul2"), "List 2");
            list2 = list2.substring(0, list2.length - 1);

            var list1order;

            list1order = list1.split(",");

            //document.getElementById('iPortalCore_Dbtest').value = '';

            var widgetlist
            widgetlist = ''
            

            var sortorder1 = 0
            for (loopme = 0; loopme < list1order.length; loopme++) {
                if (list1order[loopme] != 'panellist') {
                    //updateexistingwidgets(list1order[loopme], 'SortOrder', sortorder1, 1);
                    //document.getElementById('iPortalCore_Dbtest').value = document.getElementById('iPortalCore_Dbtest').value + list1order[loopme] + ',' + sortorder1 + ',' + 1 + '|'
                    widgetlist = widgetlist + list1order[loopme] + ',' + sortorder1 + ',' + 1 + '|';
                    sortorder1 = sortorder1 + 1;
                }
            }

            var list2order;

            list2order = list2.split(",");


            var sortorder2 = 0
            for (loopme2 = 0; loopme2 < list2order.length; loopme2++) {
                if (list2order[loopme2] != 'panellist') {
                    //updateexistingwidgets(list2order[loopme2], 'SortOrder', sortorder2, 2);
                    //document.getElementById('iPortalCore_Dbtest').value = document.getElementById('iPortalCore_Dbtest').value + list2order[loopme2] + ',' + sortorder2 + ',' + 2 + '|'
                    widgetlist = widgetlist + list2order[loopme2] + ',' + sortorder2 + ',' + 2 + '|';
                    sortorder2 = sortorder2 + 1;
                }

            }

            UpdateUser('', '', '', '', '', '', '', '', widgetlist);

            //document.getElementById("iPortalCore_reloadpanel").click();

        },

        switchStyles: function() {
            Dom.get("iPortalCore_ctl00_ul1").style.border = 'dotted 1px gray';

        }
    };


    YAHOO.example.DDList = function(id, sGroup, config) {

        YAHOO.example.DDList.superclass.constructor.call(this, id, sGroup, config);

        //alert(this + "," + id + "," + sGroup + "," + config);
        // this.logger = this.logger || YAHOO;
        var el = this.getDragEl();
        var el2 = this.getEl();
        //changed
        if (this.getEl()) {
            //alert(this.getEl().innerHTML);
            var strong = this.getEl().getElementsByTagName('Div')[2];
            YAHOO.util.Dom.generateId(strong);
            this.setHandleElId(strong.id);
        }
        //alert(el2.innerHTML);
        //end

        Dom.setStyle(el, "opacity", 0.67); // The proxy is slightly transparent

        this.goingUp = false;
        this.lastY = 0;
    };


    YAHOO.extend(YAHOO.example.DDList, YAHOO.util.DDProxy, {

        startDrag: function(x, y) {
            //this.logger.log(this.id + " startDrag");

            // make the proxy look like the source element
            var dragEl = this.getDragEl();
            var clickEl = this.getEl();

            var height = dragEl.clientHeight;
            var width = dragEl.clientWidth;

            Dom.setStyle(clickEl, "visibility", "hidden");

            dragEl.innerHTML = clickEl.innerHTML;



            dragEl.innerHTML = clickEl.innerHTML;
            if (height < 100) {
                dragEl.height = height + 5;
                Dom.setStyle(dragEl, "overflow", "hidden");
            }


            if (dragEl.innerHTML.indexOf('widget_header_external') > -1) {

                //cover firefox
                dragEl.innerHTML = dragEl.innerHTML.replace('class="widget_header_external"', 'Style="background-image:url(./iportalimage/iPortalLargeHeader-Red.png); color:white;font-size:1em; font-weight:bold; padding:4px 0px 3px 10px; height:23px;"')
                dragEl.innerHTML = dragEl.innerHTML.replace('class="mainwindowexternal"', 'Style=" height:' + height + '; color: #000;font-family:helvetica; font-size:0.8em;"')
                dragEl.innerHTML = dragEl.innerHTML.replace('class="mainweather"', 'Style="position:relative;"')
                dragEl.innerHTML = dragEl.innerHTML.replace('class="bbc_header"', 'Style="position:relative; top:-3px;margin-left:-5px;width:624px;height:1px;background-color:#000;"')




                //everything else

                dragEl.innerHTML = dragEl.innerHTML.replace('class=widget_header_external', 'Style="background-image:url(./iportalimage/iPortalLargeHeader-Red.png); color:white;font-size:1em; font-weight:bold; padding:4px 0px 3px 10px; height:23px;"')
                dragEl.innerHTML = dragEl.innerHTML.replace('class=mainwindowexternal', 'Style=" height:' + height + ';color: #000; font-family:helvetica;   background-repeat:no-repeat; height:12px;"')
                dragEl.innerHTML = dragEl.innerHTML.replace('class=mainweather', 'Style="position:relative;"')
                dragEl.innerHTML = dragEl.innerHTML.replace('class=bbc_header', 'Style="position:relative; top:-3px;margin-left:-5px;width:624px;height:1px;background-color:#000;"')





            }
            else if (dragEl.innerHTML.indexOf('widget_header_wakefield') > -1) {
                //cover firefox
                Dom.setStyle(dragEl, "backgroundColor", "#FFF");
                dragEl.innerHTML = dragEl.innerHTML.replace('class="widget_header_wakefield"', 'Style="background-image:url(./iportalimage/iPortalLargeHeader-Green.png); color:white;font-size:1em; font-weight:bold; padding:4px 0px 3px 10px; height:23px;"')
                dragEl.innerHTML = dragEl.innerHTML.replace('class="mainwindowcouncil"', 'Style="color: #194568;  position:relative; top:-5px; background-repeat:no-repeat; height:23px;"')


                //everything else
                Dom.setStyle(dragEl, "backgroundColor", "#FFF");
                dragEl.innerHTML = dragEl.innerHTML.replace('class=widget_header_wakefield', 'Style="background-image:url(./iportalimage/iPortalLargeHeader-Green.png); color:white;font-size:1em; font-weight:bold; padding:4px 0px 3px 10px; height:23px;"')
                dragEl.innerHTML = dragEl.innerHTML.replace('class=mainwindowcouncil', 'Style="color: #194568;  position:relative; top:-5px; background-repeat:no-repeat; height:23px;"')

            }
            else {

                //cover firefox
                Dom.setStyle(dragEl, "backgroundColor", "#FFF");
                dragEl.innerHTML = dragEl.innerHTML.replace('class="widget_header"', 'Style="background-image:url(./iportalimage/iPortalLargeHeader-Blue.png); color:white;font-size:1em; font-weight:bold; padding:4px 0px 3px 10px; height:23px;"')
                dragEl.innerHTML = dragEl.innerHTML.replace('class="mainwindow"', 'Style="color: #194568;  padding: 10px 5px 10px 5px; background-image:url(./Images/Wigit-Header-Shadow.gif);  position:relative; top:-8px; background-repeat:no-repeat; height:23px;"')



                //eveything else
                Dom.setStyle(dragEl, "backgroundColor", "#FFF");
                dragEl.innerHTML = dragEl.innerHTML.replace('class=widget_header', 'Style="background-image:url(./iportalimage/iPortalLargeHeader-Blue.png); color:white;font-size:1em; font-weight:bold; padding:4px 0px 3px 10px; height:23px;"')
                dragEl.innerHTML = dragEl.innerHTML.replace('class=mainwindow', 'Style="color: #194568;  padding: 10px 5px 10px 5px; background-image:url(./Images/Wigit-Header-Shadow.gif);  position:relative; top:-8px; background-repeat:no-repeat; height:23px;"')
                dragEl.innerHTML = dragEl.innerHTML.replace(/class=ListPanel/g, 'border-bottom-style:none!important; style="padding-top:10px; font-size:0.9em; "')
                dragEl.innerHTML = dragEl.innerHTML.replace(/class=Date/g, 'style="width:75px; display:block; float:left ;"')

                dragEl.innerHTML = dragEl.innerHTML.replace(/class=TextBig/g, 'style="position:relative; top:-6px; width: 500px; font-size:1em; line-height:1.9em; "')
                dragEl.innerHTML = dragEl.innerHTML.replace(/class=Text/g, 'style="border-bottom-style:none!important; display:block; float:left ;  width: 200px; font-size:1em; line-height:1.3em; padding-bottom:10px;  "')
                dragEl.innerHTML = dragEl.innerHTML.replace(/<LI class=panellist/g, '<LI style=" list-style-type:none; "')
                //alert(dragEl.innerHTML.substr(1000,1500));

            }



            //alert(width);

            if (width < 410) {
                //cover firefox
                dragEl.innerHTML = dragEl.innerHTML.replace('class="Big_Data"', 'Style="display:none; "')
                dragEl.innerHTML = dragEl.innerHTML.replace('class="Small_Data"', 'Style="display:block;"')

                //everythin else
                dragEl.innerHTML = dragEl.innerHTML.replace('class=Big_Data', 'Style="display:none;"')
                dragEl.innerHTML = dragEl.innerHTML.replace('class=Small_Data', 'Style="display:block;"')

            }
            else {
                //cover firefox
                dragEl.innerHTML = dragEl.innerHTML.replace('class="Big_Data"', 'Style="display:block;"')
                dragEl.innerHTML = dragEl.innerHTML.replace('class="Small_Data"', 'Style="display:none;"')

                //everythin else
                dragEl.innerHTML = dragEl.innerHTML.replace('class=Big_Data', 'Style="display:block;"')
                dragEl.innerHTML = dragEl.innerHTML.replace('class=Small_Data', 'Style="display:none;"')
            }

            Dom.setStyle(dragEl, "width", width + 4 + "px");


            // Dom.setStyle(dragEl, "border", "1px solid gray");

            Dom.get("iPortalCore_ctl00_ul1").style.border = '2px dashed #1C5487';
            Dom.get("iPortalCore_ctl00_ul2").style.border = '2px dashed #1C5487';

            var leftheight
            var rightheight

            leftheight = Dom.get("iPortalCore_ctl00_ul1").clientHeight;
            rightheight = Dom.get("iPortalCore_ctl00_ul2").clientHeight;

            //alert(leftheight + "," + rightheight);

            if (leftheight > rightheight) {
                Dom.get("iPortalCore_ctl00_ul2").style.paddingBottom = (leftheight - rightheight) + "px";
            }
            else {
                Dom.get("iPortalCore_ctl00_ul1").style.paddingBottom = (rightheight - leftheight) + "px";
            }

            if (dragEl.innerHTML.indexOf('!-!NoBigData!-!') > -1) {
                blockdrag = true;
            }
            else {
                blockdrag = false;
            }

            DDM.refreshCache();

        },

        endDrag: function(e) {
            var srcEl = this.getEl();
            var proxy = this.getDragEl();

            // Show the proxy element and animate it to the src element's location
            Dom.setStyle(proxy, "visibility", "");
            var a = new YAHOO.util.Motion(
            proxy, {
                points: {
                    to: Dom.getXY(srcEl)
                }
            },
            0.2,
            YAHOO.util.Easing.easeOut
        )
            var proxyid = proxy.id;
            var thisid = this.id;

            // Hide the proxy and show the source element when finished with the animation
            a.onComplete.subscribe(function() {
                Dom.setStyle(proxyid, "visibility", "hidden");
                Dom.setStyle(thisid, "visibility", "");
            });
            a.animate();

            document.getElementById('showButton').click();

            Dom.get("iPortalCore_ctl00_ul1").style.border = 'none';
            Dom.get("iPortalCore_ctl00_ul2").style.border = 'none';
            Dom.get("iPortalCore_ctl00_ul2").style.paddingBottom = '10px';
            Dom.get("iPortalCore_ctl00_ul1").style.paddingBottom = '10px';
            DDM.refreshCache();



        },

        onDragDrop: function(e, id) {

            // If there is one drop interaction, the li was dropped either on the list,
            // or it was dropped on the current location of the source element.

            if (blockdrag == false) {
                if (DDM.interactionInfo.drop.length === 1) {

                    // The position of the cursor at the time of the drop (YAHOO.util.Point)
                    var pt = DDM.interactionInfo.point;

                    // The region occupied by the source element at the time of the drop
                    var region = DDM.interactionInfo.sourceRegion;

                    // Check to see if we are over the source element's location.  We will
                    // append to the bottom of the list once we are sure it was a drop in
                    // the negative space (the area of the list without any list items)
                    if (!region.intersect(pt)) {
                        var destEl = Dom.get(id);
                        var destDD = DDM.getDDById(id);
                        destEl.appendChild(this.getEl());
                        destDD.isEmpty = false;
                    }




                    DDM.refreshCache();
                }

            }
            else {

                if (singleaction == 0) {
                    //alert("This widget cannot be dragged to the larger side.");
                    document.getElementById('overlay').style.display = 'block';
                    AssignPosition(document.getElementById('overlay'));
                    singleaction = 1
                }
                DDM.refreshCache();
            }
        },

        onDrag: function(e) {
            singleaction = 0;
            if (blockdrag == false) {
                // Keep track of the direction of the drag for use during onDragOver
                var y = Event.getPageY(e);
                if (y < this.lastY) {
                    this.goingUp = true;
                } else if (y > this.lastY) {
                    this.goingUp = false;
                }
                this.lastY = y;
            }
        },

        onDragOver: function(e, id) {
            var srcEl = this.getEl();
            var destEl = Dom.get(id);
            // We are only concerned with list items, we ignore the dragover
            // notifications for the list.
            if (destEl.nodeName.toLowerCase() == "li") {
                var orig_p = srcEl.parentNode;
                var p = destEl.parentNode;

                if (p.innerHTML.indexOf('li1_1') > -1) {
                    //alert("moving to left");
                    if (srcEl.innerHTML.indexOf('!-!NoBigData!-!') > -1) {
                        blockdrag = true;
                    }
                }
                else {
                    blockdrag = false;
                }

                if (blockdrag == false) {
                    if (this.goingUp) {
                        p.insertBefore(srcEl, destEl); // insert above
                    } else {
                        p.insertBefore(srcEl, destEl.nextSibling); // insert below
                    }
                    DDM.refreshCache();
                }
            }
        }

    });

    Event.onDOMReady(YAHOO.example.DDApp.init, YAHOO.example.DDApp, true);

})();

