﻿
var MiniQ = {
    _bOpenMiniQue: false, // used from add/remove dvd titles button and from show mini que to either open or close mini list
    _oMiniQue: null,
    Q_LIST_ID: 'qList',
    OPEN_CLOSE_ID: 'qOpenClose',
    NO_TITLES_MESSAGE: 'Your list does not contain any titles.',
    NO_TITLES_IN_LIST_DIVID: 'qDivNoTitles',
    NO_TITLES_IN_LIST_SPANID: 'qNoTitles',
    DRAG_ADVERT_ID: 'qListDrag',
    AJAX_GIF_LOAD_ID: 'qGifLoad',
    _NoTitles: 0,

    openMiniQ: function() {
        // this function opens mini list by using
        // sepecial effects. It also changes the open/close 
        // button and hides the Mini List summary info box

        // typeElement(oQOpenArr).className = 'arrUp';

        new Effect.SlideDown(this._oMiniQue, { duration: 1.0, afterFinish: function() {
            MiniQ.showHideOpenText();
        }
        });

        // If the advert on how to change the position in the queue
        // has not been hidden by the user, then show it
        if (!CPUtility.readCookie(this.DRAG_ADVERT_ID)) {
            Effect.SlideDown(this.DRAG_ADVERT_ID, { duration: 1.0 });
        }
        return true;
    },

    showHideOpenText: function() {
        $(this.OPEN_CLOSE_ID).show();
    },

    closeMiniQ: function() {

        // this function closes mini list by using
        // sepecial effects. It also changes the open/close 
        // button and shows the Mini List summary info box
        // typeElement(oQOpenArr).className = 'arrDown';
        new Effect.SlideUp(MiniQ._oMiniQue, { duration: 1.0, afterFinish: function() {
            MiniQ.showHideOpenText();
        }
        });

        // If the advert on how to change the position in the queue
        // has not been hidden by the user, then show it
        if (!CPUtility.readCookie(MiniQ.DRAG_ADVERT_ID)) {
            Effect.SlideUp(MiniQ.DRAG_ADVERT_ID, { duration: 1.0 });
        }
        // Show Mini list summary box
        Effect.Appear(MiniQ.NO_TITLES_IN_LIST_DIVID);
        MiniQ._oMiniQue = null; // set that no mini list is open
        return true;
    },
    showQList: function(e) {
        if (document.getElementById) {
            //CPUtility.eraseCookie(this.DRAG_ADVERT_ID); // To be removed Sets advert to appear every time

            // If the Mini List is not opened then first check if
            // there are any titles in customers list by checking the customer
            // Info box values. If there are no films in list there is no need 
            // for mini list to be displayed

            var elText = $(this.OPEN_CLOSE_ID);
            if (elText.innerHTML == 'show') {
                $(elText).hide();
                elText.innerHTML = 'hide';
            } else {
                if (elText.innerHTML == 'hide') {
                    $(elText).hide();
                    elText.innerHTML = 'show';
                }
            }

            if (!this._oMiniQue) {
                this._bOpenMiniQue = true;
                this._oMiniQue = $(this.Q_LIST_ID); // set what list we are working with
                $(this.NO_TITLES_IN_LIST_DIVID).hide()
                $(this.AJAX_GIF_LOAD_ID).show()
                this.getMiniQ(); // get mini list data 
                return true;
            }

            //  Close Mini list
            if (this._oMiniQue) {
                this.closeMiniQ();
                return false;
            }
        }
    },
    getMiniQ: function() {
        // Gets the mini list data from database

        var _noInList = $(this.NO_TITLES_IN_LIST_SPANID);
        if (_noInList) {
            if (parseInt(_noInList.innerHTML) == 0) {
                alert(this.NO_TITLES_MESSAGE);
                return false
            }
        }
        if (FilmAjax.GetMiniQ(7, getMiniQ_CallBack)) {

        }
        else {
            alert("No Data Found");
            return false;
        }



        function getMiniQ_CallBack(response) {

            // The function returnes HTML code as it should appear
            // in the formatted HTML element and place it straight to 
            // DVD box. It also sets the bulleted list dragable
            // so the user can change position of the movie titles
            // The function opens the mini list if it is closed and if
            // it has been requested to be opened

            $(MiniQ.AJAX_GIF_LOAD_ID).hide();
            var oDiv = $(MiniQ.Q_LIST_ID);
            if (response.error != null) {
                alert(response.error);
                return false;
            }

            if (response.value == "") {
                oDiv.innerHTML = "";
                MiniQ.closeMiniQ();
                return false;
            }

            var oUL = null
            var sULLists = '';
            oDiv.innerHTML = "";
            oDiv.innerHTML = response.value;

            Sortable.create('q1', { containment: ['q1', 'q2'], dropOnEmpty: true, ghosting: false, constraint: false, hoverclass: 'over',
                onUpdate: function(sortable) { MiniQ.updateMiniQ(Sortable.serialize(sortable)); new Effect.Highlight(MiniQ._oMiniQue.id, {}); }
            });
            Sortable.create('q2', { containment: ['q1', 'q2'], dropOnEmpty: true, ghosting: false, constraint: false, hoverclass: 'over',
                onUpdate: function(sortable) { '' }
            });

            if (MiniQ._bOpenMiniQue == true) {
                MiniQ.openMiniQ();
            } else {
                if (!MiniQ._oMiniQue) {
                    $(MiniQ.Q_LIST_ID).hide();
                }
            }

        }

    },

    updateMiniQ: function(s) {
        if (s) {
            if (FilmAjax.UpdateMiniQ(s, updateMiniQ_CallBack)) {

            }
            else {
                alert("No Data Found");
                return false;
            }
        }
        function updateMiniQ_CallBack(response) {
            //if the server side code threw an exception
            if (response.error != null) {
                alert(response.error); //we should probably do better than this
                return false;
            }

            //if we didn't get what we expect, or didn't get any matches  
            if (response.value == "") {
                alert("Action Failed");
                return false;
            }
            MiniQ._bOpenMiniQue = false;
            MiniQ.getMiniQ();


        }
    },
    hideQListDrop: function() {
        CPUtility.createCookie(this.DRAG_ADVERT_ID, 1, 1000);
        Effect.SlideUp(this.DRAG_ADVERT_ID, { duration: 1.0 })
    },

    removeDVD: function(ID) {
        if (ID) {
            if (FilmAjax.removeDVDFromList(ID, removeDVD_CallBack)) {

            }
            else {
                alert("An error occured!");
                return false;
            }
        }

        function removeDVD_CallBack(response) {

            if (response.error != null) {
                alert(response.error);
                window.status = ''
                return false;
            }

            if (response.value == "") {
                alert("Action Failed");
                window.status = ''
                return false;
            }

            if (response.value == 0) {
                alert("There has been a problem adding this title to your list.\nTry again and if the problem persist please contact us.");
                return false;
                window.status = ''
            }
            MiniQ._bOpenMiniQue = false;
            MiniQ.getNoDVDsInList();
            if (MiniQ._oMiniQue) {
                MiniQ.getMiniQ();
            }
        }
    },

    setNoDVDsInList: function(n) {
        // If the mini list box exist in the current page then
        // update then get current number of DVDs in customers list
        // and update it with the input value
        var el = $(this.NO_TITLES_IN_LIST_DIVID);
        if (el) {
            new Effect.Highlight(this.NO_TITLES_IN_LIST_DIVID)
            var elSpan = $(this.NO_TITLES_IN_LIST_SPANID)
            elSpan.innerHTML = n;
        }

        // If the customer info box on the top of the page 
        // exists then get current number of DVDs in customers list
        // and update it with the input value
        var el2 = $(CPUtility.GLOBAL_NO_TITLES_IN_LIST_ID);
        if (el2) {
            el2.innerHTML = n;
        }

    },
    getNoDVDsInList: function() {
        FilmAjax.GetNoDVDsInList(function(r) { 
            if (r.error != null) {
                    return false;
                }

                if (r.value == "") {
                    return false;
                }

                if (r.value == 0) {
                    return false;
                }
                MiniQ.setNoDVDsInList(r.value);
            }) 
    },
    checkNoTitles: function() {
        var noT = jQuery('#' + this.NO_TITLES_IN_LIST_SPANID).html();
        noT = parseInt(noT);

        FilmAjax.GetMiniNoTitles(noT, function(r) {
            if (r.error != null) {
                return false;
            }

            if (r.value == -2) {
                alert(MiniQ.NO_TITLES_MESSAGE);
                return false;
            }
            if (r.value == -1) {
                PopUp.logIn(5);
                return false;
            }

            if (r.value == 0) {
                MiniQ.showQList();
            }

        });
    }
}

jQuery(document).ready(function() {
    jQuery("a[JQ=7]").click(function(event) {
        event.preventDefault();
        MiniQ.checkNoTitles(event);
    });
});