﻿/// <reference path="jquery.intellisense.js"/>

var _IdLanguage = null;

jQuery.address.init(function(event) {
    
}).change(function(event) {
    LoadAddress(event);
});

function LoadAddress(event) {

    _IdLanguage = jQuery.url.attr('query');    
    
    if (event.pathNames.length == 0) {    
        window.location = '#/Home/';
        return;
    }

    var MenuOp = null;
    var section = event.pathNames[0].toLowerCase();
    switch (section) {
        case 'home':
            LoadUrlHtml('frontoffice/Home.aspx', { idLanguage: _IdLanguage }, 'POST');
            break;
        case 'empresa':
            MenuOp = 1;
            var idContent = 3;
            LoadUrlHtml('frontoffice/Content.aspx', { idContent: idContent, idLanguage: _IdLanguage }, 'POST');
            break;
        case 'noticias':
            MenuOp = 1;
            var idContent = 3;
            LoadUrlHtml('frontoffice/Content.aspx', { idContent: idContent, idLanguage: _IdLanguage }, 'POST');
            break;
        case 'poliuretano':
            MenuOp = 2;
            var idContent = 4;
            LoadUrlHtml('frontoffice/Content.aspx', { idContent: idContent, idLanguage: _IdLanguage }, 'POST');
            break;
        case 'produtos':
            MenuOp = 3;            
            if (event.pathNames.length == 1)
                LoadUrlHtml('frontoffice/Categories.aspx', { idLanguage: _IdLanguage }, 'POST');
            else            
                LoadUrlHtml('frontoffice/Products.aspx', { idProductCategory: event.pathNames[1], idLanguage: _IdLanguage }, 'POST');
            break;
        case 'produto':
            MenuOp = 3;
            LoadUrlHtml('frontoffice/Product.aspx', { idProduct: event.pathNames[1], idLanguage: _IdLanguage }, 'POST');            
            break;
        case 'localizacao':
            MenuOp = 4;
            var idContent = 5;
            LoadUrlHtml('frontoffice/Content.aspx', { idContent: idContent, idLanguage: _IdLanguage }, 'POST');
            break;
        case 'contactos':
            MenuOp = 5;
            var idContent = 6;
            LoadUrlHtml('frontoffice/Content.aspx', { idContent: idContent, idLanguage: _IdLanguage }, 'POST');
            break;
        case 'arquitectos-projectistas':
            MenuOp = 3;
            var idContent = 7;
            LoadUrlHtml('frontoffice/Content.aspx', { idContent: idContent, idLanguage: _IdLanguage }, 'POST');
            break;
        case 'construtores':
            MenuOp = 3;
            var idContent = 8;
            LoadUrlHtml('frontoffice/Content.aspx', { idContent: idContent, idLanguage: _IdLanguage }, 'POST');
            break;
        case 'distribuidores':
            MenuOp = 3;
            var idContent = 9;
            LoadUrlHtml('frontoffice/Content.aspx', { idContent: idContent, idLanguage: _IdLanguage }, 'POST');
            break;
        case 'donos-de-obra':
            MenuOp = 3;
            var idContent = 10;
            LoadUrlHtml('frontoffice/Content.aspx', { idContent: idContent, idLanguage: _IdLanguage }, 'POST');
            break;                      
        case 'mapa':
            var idContent = 11;
            LoadUrlHtml('frontoffice/Content.aspx', { idContent: idContent, idLanguage: _IdLanguage }, 'POST');
            break;
        case 'pesquisa':
            LoadUrlHtml('frontoffice/Search.aspx', { Value: event.pathNames[1], idLanguage: _IdLanguage }, 'POST');
            break;
        case 'simulador':
            MenuOp = 4;
            LoadUrlHtml('frontoffice/Simulators.aspx', { idLanguage: _IdLanguage }, 'POST');
            break;            
    }
    ActiveMenuOp(MenuOp);
}

function ActiveMenuOp(idOption) {
    var options = jQuery('#Menu a');
    for (var i = 0; i != options.length; ++i) {
        jQuery(options[i]).removeClass('menuItemActive');
    }
    if (idOption != null)
        jQuery(options[idOption - 1]).addClass('menuItemActive');
}

function LoadUrlHtml(URL, Vars, Method, onLoaded) {

    jQuery.ajax({
        url: 'frontoffice/Loading.htm',
        type: 'GET',
        dataType: 'html',
        success: function(data, textStatus) {
            jQuery('#Content').html(data);

            jQuery.ajax({
                url: URL,
                type: Method,
                data: Vars,
                dataType: 'html',
                success: function(data, textStatus) {
                    jQuery('#Content').html(data);

                    var title = jQuery('#TitlePage');

                    if (title.length != 0 && jQuery(title).html().length > 0) {
                        jQuery.address.title(jQuery(title).html());
                    }
                    if (onLoaded != undefined) onLoaded();

                    var page = jQuery.url.attr('file')+ '?' + jQuery.url.attr('query') + '#' + jQuery.url.attr("anchor");                    
                    pageTracker._trackPageview(page);
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    jQuery('#Content').html(textStatus + '<br/>' + errorThrown);
                }
            });
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            jQuery('#Content').html(textStatus + '<br/>' + errorThrown);
        }
    });
}


function DeleteProduct(idProduct) {
    jQuery.ajax({
        url: 'Product.ashx',
        type: 'POST',
        data: 'a=DeleteProduct&idProduct=' + idProduct,
        dataType: 'json',
        success: function(data, textStatus) {

        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert(textStatus);
        }
    });
}

function DeleteProductCategory(idProductCategory) {
    jQuery.ajax({
        url: 'Product.ashx',
        type: 'POST',
        data: 'a=DeleteProductCategory&idProductCategory=' + idProductCategory,
        dataType: 'json',
        success: function(data, textStatus) {

        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert(textStatus);
        }
    });
}

function Search(value) {
    window.location = '#/Pesquisa/' + value + '/';
}