﻿
// DETECTS IF NAMESPACE OF THE APP EXISTS, CREATES THE NAMESPACE OF THE APP.
if (!SB) var SB = {};
else if (SB && typeof (SB) != "object")
    throw new Error("SB is not an Object type");

/* SB.SERCULTUR
* All user inteface under this class name
*/
SB.SERCULTUR = {};

/* SB.SERCULTUR.ValidateForm
* VALIDATES THE MODULE SIDEBAR FOR SEARCH MOVIES, THIS FUNCTION IS CALLED BY THE DROPDOWNS AND BY THE BUTTON. 
*
* Params: [DOM Element] btn - string to now if is the button correct
*
*/
SB.SERCULTUR.ValidateForm = function(btn) {

    var selectLocation = $("select#location option:selected").val(),
        selectMovie = $("select#movie option:selected").val(),
        selectMovieTheather = $("select#movieTheaters option:selected").val();

    if (selectLocation == 0 && selectMovie == 0 && selectMovieTheather == 0) {

        $('.msgError').html('Tem que escolher um dos parametros');

        return false;

    } else {

        $('.msgError').html('');

        // IF THE CALLER IS THE BUTTON, DEFINE THE NEW WINDOW LOCATION.
        if (btn == 'button') {

            var oldWindowLocation = String(window.location),
				urlLang = '',
				urlParams = '';

            if (oldWindowLocation.search("/pt/")) {
                urlLang = "/pt/o-que-queres-fazer-hoje/cinema/pesquisa/";
            }

            // EXAMPLE: /pt/cinema/pesquisa/5-0-0/.
            urlLocation = selectLocation + '-' + selectMovie + '-' + selectMovieTheather + '/';

            window.location = urlLang + urlLocation;
        }

        return true;
    }
}

/* SB.SERCULTUR.GetDistricFromSercultur
* GET ALL DISTRICT FROM SERCULTUR
*
* Params: [DOM Element] district - string of district cinema
* Params: [DOM Element] movie - string of movie cinema
* Params: [DOM Element] room - string of room cinema
*
*/
SB.SERCULTUR.GetDistricFromSercultur = function (district, movie, room) {

    if (district == '') {
        district = 0;
    }
    if (movie == '') {
        movie = 0;
    }
    if (room == '') {
        room = 0;
    }

    var method = "GET",
		url = "/base/services/getSerculturInfo/" + district + "/" + movie + "/" + room,
		aLocation = [],
		aNumberLocation = [],
        options = '';

    $.ajax({
        type: method,
        url: url,
        contentType: "application/json; charset=utf-8",
        dataType: "json",

        success: function (data) {
            var locals = data.Locals;

            //Ordenar por Nome de Localidade
            locals.sort(function (a, b) {
                var nameA = a.Value.toLowerCase(),
                nameB = b.Value.toLowerCase();
                if (nameA < nameB) //sort string ascending
                    return -1
                if (nameA > nameB)
                    return 1
                return 0 //default return value (no sorting)
            })

            $.each(locals, function (i) {

                aLocation[i] = locals[i].Value;
                aNumberLocation[i] = locals[i].Key;

                for (j = 0; j < aNumberLocation.length; j++) {
                    if (aNumberLocation[j] == district) {
                        districtName = locals[i].Value;
                        options += '<option value="' + district + '" selected="selected">' + districtName + '</option>';

                        $('#selectlocation').html(districtName)
                        return false;
                    }
                }
                if (district == '') {
                    options += '<option value="' + aNumberLocation[i] + '">' + aLocation[i] + '</option>';
                }

            })

            $('select#location option').filter(':gt(0)').remove();

            inserBeforeElement = $('select#location option:first')

            optionSelected = $('select#location option:selected').val();

            inserBeforeElement.after(options);

            $('.wait').find('.loading').remove();

            $('div.sidebarSearchMovie div.dropdownSearchWrap').css('opacity', '1');

        }


    })
}

/* SB.SERCULTUR.GetMoviesFromSercultur
* GET ALL MOVIES FROM SERCULTUR, OR BY DISTRICT
*
* Params: [DOM Element] district - string of district cinema
* Params: [DOM Element] movie - string of movie cinema
* Params: [DOM Element] room - string of room cinema
* Params -> a1=País, a2=Género, a3=Classificação, a4=Distrito, a5=Concelho, a6=CicloFestival, a7=DiasProgramação, a8=Destaques, a9=Opcionais, a10=Espaço Académico
*/
SB.SERCULTUR.GetMoviesFromSercultur = function (district, movie, room) {

if (district == '') {
        district = 0;
    }
    if (movie == '') {
        movie = 0;
    }
    if (room == '') {
        room = 0;
    }

    var method = "GET",
		url = "/base/services/getSerculturInfo/" + district + "/" + movie + "/" + room,
        options = '',
        aMovies = [],
        aNumberMovies = [],
		iLimitCharacters2 = 33,
        options;

    $.ajax({
        type: method,
        url: url,
        contentType: "application/json; charset=utf-8",
        dataType: "json",

        success: function (data) {

            var movies = data.Movies;

            //Ordenar por Nome de Localidade
            movies.sort(function (a, b) {
                var nameA = a.Value.toLowerCase(),
                nameB = b.Value.toLowerCase();
                if (nameA < nameB) //sort string ascending
                    return -1
                if (nameA > nameB)
                    return 1
                return 0 //default return value (no sorting)
            })


            $.each(movies, function (i) {

                var movieName = movies[i].Value,
					movieNumber = movies[i].Key;

                aMovies[i] = movieName;
                aNumberMovies[i] = movieNumber;

                for (j = 0; j < aNumberMovies.length; j++) {
                    if (aNumberMovies[j] == movie) {
                        MovieName = movies[i].Value;
                        options += '<option value="' + movie + '" selected="selected">' + MovieName + '</option>';

                        $('#selectMovie').html(MovieName)
                        return false;
                    }
                }
                if (movie == '') {
                    options += '<option value="' + aNumberMovies[i] + '">' + aMovies[i] + '</option>';
                }


            });

            $('select#movie option').filter(':gt(0)').remove(); //clear options from DOM

            inserBeforeElement = $('select#movie option:first');

            inserBeforeElement.after(options);

            optionSelected = $('select#movie option:selected').val();

            $('div.sidebarSearchMovie div.dropdownSearchWrap').css('opacity', '1');

            $('.wait').find('.loading').remove();
        }
    });
}


/* SB.SERCULTUR.GetMovieTheatersFromSercultur
* GET ALL MOVIE THEATERS FROM SERCULTUR, OR BY DISTRICT
*
* Params: [DOM Element] district - string of district cinema
* Params: [DOM Element] movie - string of movie cinema
* Params: [DOM Element] room - string of room cinema
* PARAMS -> a1=País, a2=Género, a3=Classificação, a4=Distrito, a5=Concelho, a6=CicloFestival, a7=DiasProgramação, a8=Destaques, a9=Opcionais, a10=Espaço Académico
*/
SB.SERCULTUR.GetMovieTheatersFromSercultur = function (district, movie, room) {

if (district == '') {
        district = 0;
    }
    if (movie == '') {
        movie = 0;
    }
    if (room == '') {
        room = 0;
    }

    var method = "GET",
		url = "/base/services/getSerculturInfo/" + district + "/" + movie + "/" + room,
        options = "",
        aMovieTheaters = [],
        aNumberMovieTheaters = [];

    $.ajax({
        type: method,
        url: url,
        contentType: "application/json; charset=utf-8",
        dataType: "json",

        success: function (data) {
            var rooms = data.Rooms;

            //Ordenar por Nome de Localidade
            rooms.sort(function (a, b) {
                var nameA = a.Value.toLowerCase(),
                nameB = b.Value.toLowerCase();
                if (nameA < nameB) //sort string ascending
                    return -1
                if (nameA > nameB)
                    return 1
                return 0 //default return value (no sorting)
            })


            $.each(rooms, function (i) {

                var movieTheaters = rooms[i].Value,
					movieTheatersNumber = rooms[i].Key;

                aMovieTheaters[i] = movieTheaters;
                aNumberMovieTheaters[i] = movieTheatersNumber;

                for (j = 0; j < aNumberMovieTheaters.length; j++) {
                    if (aNumberMovieTheaters[j] == room) {
                        RoomName = rooms[i].Value;
                        options += '<option value="' + room + '" selected="selected">' + RoomName + '</option>';

                        $('#selectmovieTheaters').html(RoomName)
                        return false;
                    }
                }
                if (room == '') {
                    options += '<option value="' + aNumberMovieTheaters[i] + '" >' + aMovieTheaters[i] + '</option>';
                }


            });

            $('select#movieTheaters option').filter(':gt(0)').remove();

            inserBeforeElement = $('select#movieTheaters option:first');
            inserBeforeElement.after(options);

            optionSelected = $('select#movieTheaters option:selected').val();

        }

    });
}





