﻿/// <reference path="~/Scripts/External/jquery-1.4.2.js"/>

// first, declare the two namespaces if they do not already exist
if (Adlib == null || typeof (Adlib) != "object") { var Adlib = new Object(); }
if (Adlib.View == null || typeof (Adlib.View) != "object") { Adlib.View = new Object(); }

Adlib.View.Options = function () {

    return {

        showData: function (xml, xpath, target) {

            var options = '<option value="">All</option>';

            $j(xml).find(xpath + "[active='true']").each(function () {

                var title = $j(this).attr('name');
                if (title == null) {
                    title = $j(this).text();
                }

                var url = $j(this).find('url').text();

                options += '<option value="' + title + '">' + title + '</option>';

            });

            $j(target).html(options);
        },

        processing: function (process, msg) {
            $j('h2.clientError').fadeOut('fast');
            $j('div.clientList').fadeOut('fast');
            $j('h2.clientSuccess').fadeOut('fast');
            $j('h2.clientLoading').fadeOut('fast');

            switch (process) {
                case "loading":
                    $j('h2.clientLoading').fadeIn('fast');
                    break;
                case "error":
                    $j('h2.clientError').html(msg);
                    $j('h2.clientError').fadeIn('fast');
                    break;
                case "success":
                    $j('h2.clientSuccess').html(msg);
                    $j('h2.clientSuccess').fadeIn('fast');
                    $j('div.clientList').fadeIn('fast');
                    break;
            }
        },

        showCustomers: function () {
            Adlib.View.Options.processing('loading');

            //var baseUrl = 'osearch/search?a=content_type=customer_item';
            var query = 'osearch/search?a=content_type=customer_item';
            var country = $j("select#ctlCountry option:selected").val();
            var province = $j("select#ctlProvince option:selected").val();
            var city = $j("select#ctlCity option:selected").val();
            var sectors = $j("select#ctlSectors option:selected").val();
            var sector = $j("select#ctlSector option:selected").val();
            var product = $j("select#ctlProduct option:selected").val();
            //set up the query for place
            var queryPlace = country;
            if (country != '') {
                $j("select#ctlProvince").fadeIn('slow');
            } else {
                $j("select#ctlProvince").fadeOut('slow');
            }
            if (province != '') {
                queryPlace = province;
                $j("select#ctlCity").fadeIn('slow');
            } else {
                $j("select#ctlCity").fadeOut('slow');
            }
            if (city != '') { queryPlace = city; }
            if (queryPlace != '') { query += " AND keywords='" + queryPlace + "'"; }

            //set up the query for sectors
            var querySector = sectors;
            if(sectors != '') {
                $j("select#ctlSector").fadeIn('slow');
            } else {
                $j("select#ctlSector").fadeOut('slow');
            }
            if (sector != '') { querySector = sector; }
            if (querySector != '') { query += " AND keywords='" + querySector + "'"; }

            //set up the query for products
            var queryProduct = product;
            if (queryProduct != '') { query += " AND keywords='" + queryProduct + "'"; }

            $j.ajax({
                type: "GET",
                url: query,
                dataType: "xml",
                success: function (xml) {
                    var results = '';

                    $j(xml).find('item').each(function () {
                        var title = $j(this).find('title').text();
                        var description = $j(this).find('description').text();
                        var link = $j(this).find('link').text();

                        results += '<p><a href="' + link + '"><u>' + title + '</u></a><br/>' + description + '</p>';

                    });

                    $j('div.clientList').html(results);

                    if (results == '') {
                        Adlib.View.Options.processing('error', 'No results for: ' + country + " " + province + " " + city + " " + sectors + " " + sector + " " + product);
                    } else {
                        Adlib.View.Options.processing('success', 'Results for: ' + country + " " + province + " " + city + " " + sectors + " " + sector + " " + product);
                    }

                },
                complete: function (xhr, status) {
                    //console.log(xhr);
                    //console.log(status);
                },
                error: function (xhr, status, err) {
                    Adlib.View.Options.processing('error');
                  //  //console.log(status);
                }

            });
           // //console.log(query);
        },

        Init: function (lang) {
            $j(document).ready(function () {
                var language = lang;
                var varxmlPlace = '';
                var varxmlSector = '';

                //Set the country
                $j("select#ctlCountry").change(function () {
                    Adlib.View.Options.showData(varxmlPlace, "country[name='" + $j("select#ctlCountry option:selected").text() + "'] province", 'select#ctlProvince');
                    //reset city
                    $j('select#ctlCity').html('<option value="">All</option>');
                    Adlib.View.Options.showCustomers();
                });

                //set the province
                $j("select#ctlProvince").change(function () {
                    Adlib.View.Options.showData(varxmlPlace, "country[name='" + $j("select#ctlCountry option:selected").text() + "'] province[name='" + $j("select#ctlProvince option:selected").text() + "'] city", 'select#ctlCity');
                    Adlib.View.Options.showCustomers();
                });

                //set the city
                $j("select#ctlCity").change(function () {
                    //Adlib.View.Options.showData(varxmlPlace, "country[name='" + $j("select#ctlCountry option:selected").text() + "'] province[name='" + $j("select#ctlProvince option:selected").text() + "'] city", 'select#ctlCity');
                    Adlib.View.Options.showCustomers();
                });

                //get the xml for the places
                $j.ajax({
                    type: "GET",
                    url: "Content/XML/country-city.xml",
                    dataType: "xml",
                    success: function (xml) {
                        varxmlPlace = xml;
                        Adlib.View.Options.showData(varxmlPlace, 'country', 'select#ctlCountry');
                    }
                });

                //Set the sectors
                $j("select#ctlSectors").change(function () {
                    Adlib.View.Options.showData(varxmlSector, "sector[name='" + $j("select#ctlSectors option:selected").text() + "'] item[lang='" + lang + "']", 'select#ctlSector');
                    //reset city
                    Adlib.View.Options.showCustomers();
                });

                //set the sector-sub category
                $j("select#ctlSector").change(function () {
                    //Adlib.View.Options.showData(varxmlPlace, "country[name='" + $j("select#ctlCountry option:selected").text() + "'] province[name='" + $j("select#ctlProvince option:selected").text() + "'] city", 'select#ctlCity');
                    Adlib.View.Options.showCustomers();
                });

                //get the xml for sectors
                $j.ajax({
                    type: "GET",
                    url: "Content/XML/sectors.xml",
                    dataType: "xml",
                    success: function (xml) {
                        varxmlSector = xml;
                        Adlib.View.Options.showData(varxmlSector, "sector[lang='" + lang + "']", 'select#ctlSectors');
                    }
                });

                //set the Product 
                $j("select#ctlProduct").change(function () {
                    Adlib.View.Options.showCustomers();
                });

                //get the xml for sectors
                $j.ajax({
                    type: "GET",
                    url: "Content/XML/sectors.xml",
                    dataType: "xml",
                    success: function (xml) {
                        varxmlSector = xml;
                        Adlib.View.Options.showData(varxmlSector, "product[lang='" + lang + "']", 'select#ctlProduct');
                    }
                });

            });
        }

    };
} ();


