(function($){
$.fn.chatinvite = function(options){

 var defaults = {
 height: 200,
 width: 375,
 dropshadow: false,
 InvitationTimeIn: 30000,
 InvitationTimeOut: 180000,
 nothanks: true,
 notnow: true,
 buttonfire: true,
 buttonname: "btnChatInvite",
 inviteDiv: "sitechatinvite",
 skillLevel: ""
 }

 var options = $.extend(defaults, options);
 var id =  $(this);

 
 
  var enabled = false;
  var referringUrl = document.URL;
	
  var dmHost = (("https:" == document.location.protocol)? "https://" : "http://");
	var baseUrl = "/content/includes/scripts/FuzeProxy.asp?Mode="
  var chatUrl = dmHost + "service.lfcu.com/lockheedfcu/chat/UserPreChat.asp" + "?frominvite=1" + "&ref=" + referringUrl;
  var isMSRAvailableUrl = dmHost +  baseUrl + "JSON/RepIsOnline";
 $.getJSON("/content/includes/scripts/FuzeProxy.asp?Mode=GroupIsOnline&deptid=" + options.skillLevel, function(json){
 if(json.d == true){
 enabled = true;
 }else if(json.d == false){
 enable = false;
 }else{
 enabled = false;
 }});

 var createCookie = function(n,v,d){if(d.length < 0){document.cookie = n+"="+v+";expires="+d+"; path=/";}else{document.cookie = n+"="+v+";path=/";}};
 var deleteCookie = function(n){createCookie(n,"",-1);};
 var readCookie = function(n){
  var nEq = n + "=";
  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(nEq) == 0){return c.substring(nEq.length,c.length);}
   }
   return null;
   };


    return this.each(function(){
	obj = $(this);
	var body = obj.html();

	//Get the window height and width
	var winH = $(window).height();
	var winW = $(window).width();


	//Set the popup window to center
	$(id).css('top',  winH/2-$(id).height()/2);
	$(id).css('left', winW/2-$(id).width()/2);
	$(id).css('width', options.width);
	$(id).css('height', options.height);

	//If Dropshadow true set
	if(options.dropshadow == true){
	   $(id).css('border-bottom', '3px solid #666');
	   $(id).css('border-right', '3px solid #666');
	}
	//transition effect
	//display message based on InvitationTimeIn time.

	setTimeout(function(){if((readCookie('chatinvite') == null)&&(enabled == true)){$.ajax({type:"GET",url: baseUrl + "InsertChatOffer&url=" + referringUrl});$(id).fadeIn(1000);}}, options.InvitationTimeIn);


	if($(id).filter('visible')){
     setTimeout(function(){$(id).fadeOut(1000);}, (options.InvitationTimeIn + options.InvitationTimeOut));
    }


	 // set onclick event for close/nothanks/notnow link
	var closelink = $('.close', obj);
	var nothankslink = $('.nothanks', obj);
	var notnowlink = $('.notnow', obj);
	var yeslink = $('.yes',obj);

	closelink.click(function(e){
		e.preventDefault();
		$('.window').hide();
		createCookie('chatinvite','nothanks','');
	});

	nothankslink.click(function(e){
	    e.preventDefault();
	    $('.window').hide();
		createCookie('chatinvite','nothanks','');
	});

	notnowlink.click(function(e){
	    e.preventDefault();
		$('.window').hide();
	 	 createCookie('chatinvite','nothanks','');
	});

	yeslink.click(function(e){
	   e.preventDefault();
	   $('.window').hide();
		 $.ajax({type: "GET", url: baseUrl + "InsertChatOfferAccepted&url=" + referringUrl });
	   window.open(chatUrl + "&deptid=" + options.skillLevel, 'chatWindow', 'height=400,width=500,scrollbars=false,status=false,addressbar=false');

	});

	});
  };
})(jQuery);