/*
 *
 *	jQuery Timer plugin v0.1
 *		Matt Schmidt [http://www.mattptr.net]
 *
 *	Licensed under the BSD License:
 *		http://mattptr.net/license/license.txt
 *
 */

jQuery.timer = function (interval, callback)
{
    /**
  *
  * timer() provides a cleaner way to handle intervals
  *
  *	@usage
  * $.timer(interval, callback);
  *
  *
  * @example
  * $.timer(1000, function (timer) {
  * 	alert("hello");
  * 	timer.stop();
  * });
  * @desc Show an alert box after 1 second and stop
  *
  * @example
  * var second = false;
  *	$.timer(1000, function (timer) {
  *		if (!second) {
  *			alert('First time!');
  *			second = true;
  *			timer.reset(3000);
  *		}
  *		else {
  *			alert('Second time');
  *			timer.stop();
  *		}
  *	});
  * @desc Show an alert box after 1 second and show another after 3 seconds
  *
  *
  */

    var interval = interval || 100;

    if (!callback)
        return false;

    _timer = function (interval, callback) {
        this.stop = function () {
            clearInterval(self.id);
        };

        this.internalCallback = function () {
            callback(self);
        };

        this.reset = function (val) {
            if (self.id)
                clearInterval(self.id);

            var val = val || 100;
            this.id = setInterval(this.internalCallback, val);
        };

        this.interval = interval;
        this.id = setInterval(this.internalCallback, this.interval);

        var self = this;
    };

    return new _timer(interval, callback);
};



function acceptChat( id )
{
    $( '#chatDiv' ).remove();

    window.open( '/sglivechat/f.php/chat/acceptInvite?id=' + id ,'chat_' + id, 'width=680,height=520,location=0,toolbar=0,resizable=1');    
}

function rejectChat( id )
{
    $( '#chatDiv' ).fadeOut("slow", removeInvite );
    
    $.get('/sglivechat/f.php/chat/rejectInvite?id=' + id );
}

function removeInvite()
{
    $( '#chatDiv' ).remove();

}

function scrollInvite()
{
    $("#chatDiv").css("position", "fixed");
    $("#chatDiv").css("top", "200px");
    $("#chatDiv").css("border", "3px solid #FDC463");
    return;

    if ( $('chatDiv') ) $('chatDiv').setStyle({
        top: 8 + document.viewport.getScrollOffsets().top + 'px'
    });
}


function openInvite()
{

    $('#chatDiv').show();
    scrollInvite();
}

if ( typeof( skipChat ) == "undefined" )
{
    document.write( '<div id="chatDiv" style="position: absolute; left: 100px; border: 2px solid #666; border-top: 0; background: url(/sglivechat/images/header_bgr.jpg) repeat-x top;background-color: #fff; width: 330px;"><img src="/sglivechat/images/sg_logo.jpg" width=182 height=37 class="logo" /><div style="clear: both; padding: 10px; ">Chat Invitiation: may I help you with anything? <a href="javascript:void(0);" onclick="acceptChat(410)">Yes</a>&nbsp;&nbsp;&nbsp;<a href="javascript:void(0);" onclick="rejectChat(410)">No</a></div></div>' );

    $(document).ready(function () {
        $('#chatDiv').hide();

        
        $(window).scroll(function(){
            scrollInvite();
        });




    });



}

function getTimeZone()
{
    var dt = new Date();   
    return (dt.getTimezoneOffset()/60)*(-1);
}
function processResponse(ajax)
{
    json = ajax.responseJSON;
  
// if ( true || json ) Alert( json );
}






$.post('/sglivechat/f.php/tracker/update?v=2&lt=' + escape(new Date()) + '&tz='+getTimeZone()+'&r='+encodeURIComponent( document.referrer ) );

var sglcu = $.timer( 5000, function(timer) {
    $.getScript('/sglivechat/f.php/tracker/update?v=2&lt=' + escape(new Date()), processResponse );
} );

var sglcs = $.timer( 1000 * 3600, function(timer) {
    sglcu.stop();
    sglcs.stop();
} );



