﻿/// <reference name="MicrosoftAjax.debug.js" />
/// <reference name="MicrosoftAjaxTimer.debug.js" />
/// <reference name="MicrosoftAjaxWebForms.debug.js" />

/// <reference path="jquery-1.2.6-vsdoc.js"/>

Type.registerNamespace('SCOTT.CSC');

SCOTT.CSC.Extend = function(destination, source)
{
    /// <summary>Extends one object with the properties from a source object</summary>
    /// <param name="destination">destination</param>
    /// <param name="source">source</param>
    /// <returns type="object">object</returns>
    for (var property in source.prototype) 
    {
        if (destination.prototype[property] === undefined)
        {
            destination.prototype[property] = source.prototype[property];
        }
    }
    return destination;
};

// mock console.log (Firebug feature in Firefox)
if (!window['console'])
{
    window['console'] = {};
    console.log = function(){};
}

// utility functions for static access

SCOTT.CSC.Utility = {};

SCOTT.CSC.Utility.ShowMask = function() 
{
    // create the mask if it is not on the page yet
    if ($('div#Mask').length === 0)
    {
        var maskContent = '<div id="Mask" style="display: none;"></div>';
        $('form:first').prepend(maskContent);
    }
    var mask = $('#Mask');
    mask.css('position', 'absolute').
        css('width', $(document).width()).
        css('height', $(document).height()).
        css('top', 0).
        css('left', 0).
        css('z-index', '50').
        css('overflow', 'hidden').
        show().
        fadeTo(100, 0.65);
    if ($.browser.msie && $.browser.version < 6)
    {
        mask.css('filter', 'alpha(opacity=0.65)');
    }
};

SCOTT.CSC.Utility.HideMask = function() 
{
    $('#Mask').hide();
};

SCOTT.CSC.Utility.RequireAuthentication = function(onSuccess, onFailure)
{
    var csc = SCOTT.CSC.GetApplication();
    var isUserAuthenticated = csc.IsUserAuthenticated();
    
    if (!isUserAuthenticated)
    {
        csc.ShowAutoLogin(onSuccess, onFailure);
        return false;
    }
    else
    {
        return true;
    }
};

SCOTT.CSC.Utility.HtmlEncode = function(s) 
{
    var str = new String(s);
    str = str.replace(/&/g, '&amp;');
    str = str.replace(/</g, '&lt;');
    str = str.replace(/>/g, '&gt;');
    str = str.replace(/"/g, '&quot;');
    return str + '';
};

SCOTT.CSC.Utility.HtmlDecode = function(s) 
{
    var str = new String(s);
    str = str.replace(/&amp;/g, '&');
    str = str.replace(/&lt;/g, '<');
    str = str.replace(/&gt;/g, '>');
    str = str.replace(/&quot;/g, '"');
    return str + '';
};

SCOTT.CSC.Utility.CenterElement = function(selector)
{
    // Position
    var wWidth = $(window).width();
    var wHeight = $(window).height();
    var width = $(selector).width();
    var height = $(selector).height();
    
    var topPos = parseInt((wHeight / 2) - (height / 2), 10) + $(window).scrollTop() + 'px';
    var leftPos = parseInt((wWidth / 2) - (width / 2), 10) + 'px';
    
    $(selector).
        css("position", 'absolute').
        css("top", topPos).
        css("left", leftPos).
        css("z-index", "100");
};


SCOTT.CSC.Utility.IsFunction = function(callback) 
{
    return (callback && typeof (typeof(callback) === 'function'));
};

SCOTT.CSC.Core = function() {};
SCOTT.CSC.Core.prototype =
{
    Name : 'SCOTT.CSC.Core',
    __typeName : 'SCOTT.CSC.Core',
    ShowError : function(message, stackTrace)
    {
        // do nothing
    },
    HandleError : function(exception, context)
    {
        //exception._exceptionType;
        //exception._message;
        //exception._stackTrace;
        //exception._statusCode;
        //exception._timedOut;
        if (context !== undefined)
        {
            context.ShowError(exception._message, exception._stackTrace);
        }
        else
        {
            alert('Error: ' + exception._message);
        }
    },
    InitializeServices : function()
    {
        this.AuthenticationService = Sys.Services.AuthenticationService;
        this.AuthenticationService.set_defaultUserContext(this);
        this.AuthenticationService.set_defaultFailedCallback(this.HandleError);
        
        this.UsersService = SCOTT.CSC.WebServices.Users;
        this.UsersService.set_defaultUserContext(this);
        this.UsersService.set_defaultFailedCallback(this.HandleError);
        
        this.TipsService = SCOTT.CSC.WebServices.Tips;
        this.TipsService.set_defaultUserContext(this);
        this.TipsService.set_defaultFailedCallback(this.HandleError);
        
        this.TagsService = SCOTT.CSC.WebServices.Tags;
        this.TagsService.set_defaultUserContext(this);
        this.TagsService.set_defaultFailedCallback(this.HandleError);
    },
    Initialize : function()
    {
        this.InitializeServices();
        this.AttachEvents();
        this.Display();
        
    },
    AttachEvents : function()
    {
    },
    Display : function()
    {
    },
    ShowMask: SCOTT.CSC.Utility.ShowMask,
    HideMask: SCOTT.CSC.Utility.HideMask,
    CenterElement : SCOTT.CSC.Utility.CenterElement,
    RequireAuthentication : SCOTT.CSC.Utility.RequireAuthentication,
    HtmlEncode : SCOTT.CSC.Utility.HtmlEncode,
    HtmlDecode : SCOTT.CSC.Utility.HtmlDecode,
    IsFunction : SCOTT.CSC.Utility.IsFunction
};

SCOTT.CSC.BusinessObject = function() 
{
};

SCOTT.CSC.BusinessObject.prototype =
{
    Clone : function()
    {
        //var newObj = eval(uneval(this));
        var newObj = $CloneObject(this);
        return newObj;
    }
};
