if(typeof(webchat_button) === "undefined") webchat_button = document.getElementById("webchat_button");
else if(!window.ActiveXObject) alert("variable 'webchat_button' already existed.");
	
function isAvailable(){
	new Ajax("/yoyoor/isWebChatOnline.jsp",
	{
		method: 'get',
		timeout: (15*1000*60), 
		onTimeout: buildOfflineWebChatImg ,
		onComplete : function(response){
			if(response.indexOf('show="available"') > -1 || response.indexOf("show='available'") > -1){
				buildOnlineWebChatImg();
				//var iframe = $("webchat_area_iframe");
				//if(iframe) iframe.addEvent('load', function(){
					waitPopWinChat_(5);
				//});
			}else{
				buildOfflineWebChatImg();
			}
		}
	}
	).request();	
}

function toggleChat(){
	var maxh = 250;
	var area = $('webchat_area');
	var close_bt = $('web_chat_close_bt');
	var elh = parseInt(area.getStyle('height'));
	if(elh <= 0){
		area.setStyle('display', "block");
	}
	
	var fx = area.effects({duration:500, wait:false, transition: Fx.Transitions.Back.easeOut, 
	onStart:function(){if(window.ie && elh <= 0){area.setStyle('height', maxh+"px");} if(elh > 0){close_bt.setStyle('display', "none");}},
	onComplete:function(){if(window.ie && elh > 0){area.setStyle('height', 0+"px");} if(elh <= 0){close_bt.setStyle('display', "block");}}
	});
	
	if(window.ie){
		fx.start({'opacity': elh <= 0?1:0});
	}
	else fx.start({height: elh <= 0?maxh:0,'opacity': elh <= 0?1:0});
}

function buildOnlineWebChatImg(){
	var link = document.getElementById("webchat_link");
	link.innerHTML = "<img border='0' src='/images/helpdesk_a.gif' style='cursor:hand' title='Need assistance? Click here to talk to Yoyoor representative.' />";
	link.onclick = function(){
		openChat();
		return false;
	}
}

function buildOfflineWebChatImg(){
	var link = document.getElementById("webchat_link")
	link.innerHTML = "<img border='0' src='/images/helpdesk_b.gif' title='Sorry, Yoyoor representative is not available right now.' />";
	link.removeAttribute("href");
}

function waitPopWinChat(i){
    	setTimeout('toggleChat()',i * 1000 );
}

function waitPopWinChat_(i){
    	setTimeout('openChat()',i * 1000 );
}

var libraryh3lp_chat_win = null;
function openChat(){
    var wid=350,heig=350;
    var x = (screen.width-wid), y = (screen.height-heig);

    if(!libraryh3lp_chat_win || libraryh3lp_chat_win.closed){
        libraryh3lp_chat_win = window.open('http://libraryh3lp.com/chat/all@chat.libraryh3lp.com?skin=1467&theme=yipee&title=Yoyoor%20Live%20Chat%20Helpdesk&identity=Yoyoor', 'chat', 'resizable=1,width=' + wid + ',height=' + heig +',top='+y+', left='+x);
        if(libraryh3lp_chat_win){
            libraryh3lp_chat_win.focus();
        }
    }else{
        if(libraryh3lp_chat_win){
            libraryh3lp_chat_win.focus();
        }
    }

}