﻿/// <reference name="MicrosoftAjax.js"/>

var VMG_Service =
{
    GetProductById: function(txtBoxId) {
        $('#divResults').hide();
        $('#divLoading').show();
        try {
            if (txtBoxId == null) {
                $.ajax({
                    type: "POST",
                    url: "jsonwebservice.asmx/GetProductsJson",
                    data: "{'prefix':'','test':''}",
                    contentType: "application/json; charset=utf-8",
                    success: VMG_Service.AjaxCallSucceed,
                    dataType: "json",
                    failure: VMG_Service.AjaxCallFailed
                });
            }
            else {
                $.ajax(
                {
                    type: "POST",
                    url: "jsonwebservice.asmx/GetProductsJson",
                    data: "{'prefix':'" + $('#' + txtBoxId).val() + "','test': '" + $('#' + txtBoxId).val() + "' }",
                    contentType: "application/json; charset=utf-8",
                    success: VMG_Service.AjaxCallSucceed,
                    dataType: "json",
                    failure: VMG_Service.AjaxCallFailed
                });
            }
        }
        catch (e) {
            alert('failed to call web service. Error: ' + e);
            $('#divLoading').hide();
        }
    },
    AjaxCallSucceed: function(response) {
        $('#divLoading').hide();
        var products = eval('(' + response.d + ')');
        VMG_Service.PareseResult(products);
    },
    AjaxCallFailed: function(error) {
        $('#divLoading').hide();
        alert('error: ' + error);
        $('#divResults').hide();
    },
    PareseResult: function(products) {
        var lists = '';
        for (var i = 0; i < products.length; i++) {
            if (lists == '') {
                lists = products[i].ProductName;
            }
            else {
                lists = lists + " <br />" + products[i].ProductName;
            }
        }
        $('#divResults').html(lists);
        $('#divResults').show();
    },

    Actions:
    {
        showHelp: function() {
            var request = new Sys.Net.WebRequest();
            request.set_httpVerb("GET");
            request.set_url('help.aspx');
            request.add_completed(function(executor) {
                if (executor.get_responseAvailable()) {
                    var helpDiv = $get('HelpDiv');
                    var helpLink = $get('HelpLink');

                    var helpLinkBounds = Sys.UI.DomElement.getBounds(helpLink);

                    helpDiv.style.top = (helpLinkBounds.y + helpLinkBounds.height) + "px";

                    var content = executor.get_responseData();
                    helpDiv.innerHTML = content;
                    helpDiv.style.display = "block";
                }
            });

            var executor = new Sys.Net.XMLHttpExecutor();
            request.set_executor(executor);
            executor.executeRequest();
        }
    }
};


function Listen(event, _url, _name) {


    var flashvars = {
        mp3File: escape(_url),
        mp3Name: escape(_name)
    };
    var params = {
        wmode: 'transparent',
        quality: 'best'
    };
    var attributes = {};
    swfobject.embedSWF("/FlashPlayer/mp3Player_white.swf", "FlashContent", "322", "80", "9.0.0", true, flashvars, params, attributes);


    $('.FlashBox').css('left', $(window).width() / 2 - $('.FlashBox').width() / 2).css('top', event.pageY - 50 - $('.FlashBox').height()).fadeIn();



};

function FlashBoxClose() {
    $("#FlashContent").html('');
    $('.FlashBox').fadeOut();

};

function ToggleDisplay(id) {
    var status = $('#' + id).css("display");
    if (status == 'none') {
        $('#' + id).slideDown(500);
    } else {
        $('#' + id).fadeOut('fast');
    }

};



Cookie = {

    Add: function(name, value, days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            var expires = "; expires=" + date.toGMTString();
        }
        else var expires = "";
        document.cookie = name + "=" + value + expires + "; path=/";
    },
    Get: function(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') c = c.substring(1, c.length);
            if (c.indexOf(nameEQ) == 0) {
                return c.substring(nameEQ.length, c.length);
            }
        }
        return "";
    },
    Remove: function(name) {
        this.Add(name, "", -1);
    }
}

