﻿
// 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.GOOUT
* All user inteface under this class name
*/
SB.GOOUT = {};

/* SB.GOOUT.ValidateForm
* VALIDATES THE MODULE SIDEBAR FOR SEARCHING RESTAURANTS AND NIGHT SPOTS , THIS FUNCTION IS CALLED BY THE DROPDOWNS AND BY THE BUTTON. 
*
* Params: [DOM Element] btn - string to now if is the button correct
*
*/
SB.GOOUT.ValidateForm = function (btn) {

    var selectType = $("select#type option:selected").val(),
        selectDistrict = $("select#district option:selected").val(),
        selectPrice = $("select#price option:selected").val();

    if (selectType == 0 && selectDistrict == 0 && selectPrice == 0) {

        $('.msgError').html('Tem que escolher um dos parametros');

        return false;

    } else {

        $('.msgError').html('');

        var oldWindowLocation = String(window.location),
			urlLang = '',
			urlParams = '';

        if (oldWindowLocation.search("/pt/")) {
            urlLang = "/pt/o-que-queres-fazer-hoje/guia-restaurantes-e-noite.aspx";
        }

        urlLocation = '?quick_type=' + selectType + '&quick_district=' + selectDistrict + '&quick_price=' + selectPrice; //+ '/';

        window.location = urlLang + urlLocation;

    }
}

/* SB.GOOUT.GetTypeFromGoOut
* GET ALL THE TYPES FROM GOOUT
*/
SB.GOOUT.GetTypeFromGoOut = function () {

    var method = "GET",
		url = "/base/services/getAppGenerationTypes",           
		typeName = '',
        options = '';

    $.ajax({
        type: method,
        url: url,
        contentType: "application/json; charset=utf-8",
        dataType: "json",

        success: function (data) {
            var types = data.result;

            for (var type in types) {
                typeName = types[type];

                options += '<option value="' + typeName + '">' + typeName + '</option>';
            }

            $('select#type option').filter(':gt(0)').remove();

            inserBeforeElement = $('select#type option:first')

            //optionSelected = $('select#type option:selected').val();

            inserBeforeElement.after(options);

            $('.wait').find('.loading').remove();

            $('div.sidebarSearchMovie div.dropdownSearchWrap').css('opacity', '1');
        }
    });
}

/* SB.GOOUT.GetLocalFromGoOut
* GET ALL THE LOCALS FROM GOOUT
*/
SB.GOOUT.GetLocalFromGoOut = function () {

    var method = "GET",
		url = "/base/services/getAppGenerationLocals",
        options = '',
        localName = '',
        options = '';

    $.ajax({
        type: method,
        url: url,
        contentType: "application/json; charset=utf-8",
        dataType: "json",

        success: function (data) {
            var locals = data.result;

            $.each(locals, function (index, value) {

                for (var name in value) {
                    localName = value[name];
                }

                options += '<option value="' + localName + '">' + localName + '</option>';
            });

            $('select#district option').filter(':gt(0)').remove(); //clear options from DOM

            inserBeforeElement = $('select#district option:first');

            inserBeforeElement.after(options);

            //optionSelected = $('select#district option:selected').val();

            $('div.sidebarSearchMovie div.dropdownSearchWrap').css('opacity', '1');

            $('.wait').find('.loading').remove();
        }
    });
}


/* SB.GOOUT.GetPriceFromGoOut
* GET THE PRICE FROM GOOUT
*/
SB.GOOUT.GetPriceFromGoOut = function () {

    var method = "GET",
		url = "/base/services/getAppGenerationPrices",
        priceValue = '',
        options = "";

    $.ajax({
        type: method,
        url: url,
        contentType: "application/json; charset=utf-8",
        dataType: "json",

        success: function (data) {
            var prices = data.result;

            for (var price in prices) {
                priceValue = prices[price];

                options += '<option value="' + priceValue + '">' + priceValue + '</option>';
            }

            $('select#price option').filter(':gt(0)').remove();
            inserBeforeElement = $('select#price option:first');
            inserBeforeElement.after(options);

            //optionSelected = $('select#price option:selected').val();
        }
    });
}





