jQuery(document).ready(function($){

//--------------------------------------------------------------------------------------------
// START Helper functions
//--------------------------------------------------------------------------------------------

	jQuery.fn.centerIntercept = function () {
    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
	}
	
	
	jQuery.fn.sayHello = function (version) {
    alert(version);
	}

	function setCookie(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=/";
	}
	
	function getCookie(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 null;
	}
	
	function deleteCookie(name) {
			setCookie(name,"",-1);
	}

//--------------------------------------------------------------------------------------------
// END Helper functions
//--------------------------------------------------------------------------------------------
	var theDate = new Date();
	var theNow = theDate.getTime();
	
	var intercept1Name = "velaroProactiveChatInvite";
	var intercept1StartTime = 630; // Start time for possible intercepts
	var intercept1EndTime = 1700; // End time for possible intercepts
	var intercept1StartDay = 1; // Start day for possible intercepts
	var intercept1EndDay = 5; // End dat for possible intercepts
	var intercept1PageViewsTarget = 2; // this number of page views must be seen for the intercept
	var intercept1Timer = 0;  // 1000 = 1 second, the user must be on the site for this amount of time for the intercept
	var intercept1Timer2 = 2000;  // 1000 = 1 second, when the user meets the above requirements, wait this long on the target page to show the intercept.
	var cookieTTL = 1;  // days for the intercept cookie to live.
	var intercept1GWOaccount = 'UA-325904-2';
	var intercept1GWOActive = true;
	var intercept1GWOTestNumber = 1811461440;
	var intercept1GWOutmxSection = 'proactiveChatIntercept';
		
	
	jQuery.fn.velaroProactiveChatInvite = function(version) {
		
		if ( (version != 0) && intercept1GWOActive && (theServerTime >= intercept1StartTime && theServerTime <= intercept1EndTime) && (theServerDay >= intercept1StartDay && theServerDay <= intercept1EndDay) ) {
			
			
// START velaroProactiveChatInviteAgree
			function velaroProactiveChatInviteAgree() {
				if ( $('#valeroProactiveChatInterceptBoxWrapper').is(':visible') ) {
					$('#valeroProactiveChatInterceptBoxWrapper').remove();
					this.newWindow = window.open('http://service.velaro.com/visitor/requestchat.aspx?siteid=8588&showwhen=inqueue&deptid=15356', 'OnlineChatSoftware', 'toolbar=no,location=no,directories=no,menubar=no,status=no,scrollbars=no,width=440,height=550,resizable=yes,replace=no');this.newWindow.focus();
					// track pageview for accepted proactive chat.
					_gaq.push(["_setAccount", "UA-325904-1"]);
			    	_gaq.push(["_trackPageview", "/proactiveChat"]);
					/*if (intercept1GWOActive) {
						// if GWO1 is active, then trigger goal when the yes button is clicked.
						_gaq.push(['gwo._setAccount', intercept1GWOaccount]);
						_gaq.push(['gwo._trackPageview', '/' + intercept1GWOTestNumber + '/goal']);
					}*/
				}
			}
// END velaroProactiveChatInviteAgree

// START velaroProactiveChatInviteDecline
			function velaroProactiveChatInviteDecline() {
				if ( $('#valeroProactiveChatInterceptBoxWrapper').is(':visible') ) {
					$('#valeroProactiveChatInterceptBoxWrapper').remove();
				}
			}
// END velaroProactiveChatInviteDecline

		
// START velaroProactiveChatInviteGo
			this.velaroProactiveChatInviteGo = function() {
				
				switch (version) {
					case 0:
						break;
					case 1:
						var valeroProactiveChatInterceptContainer = '<style>'+'\n';
						valeroProactiveChatInterceptContainer = valeroProactiveChatInterceptContainer + '#valeroProactiveChatInterceptBoxWrapper { border:1px solid #fff; width:450px; height:272px; background:#fff; position:absolute; top:0; left:-451px; z-index:999; }'+'\n';
						valeroProactiveChatInterceptContainer = valeroProactiveChatInterceptContainer + '#valeroProactiveChatInterceptBox { border:16px solid #494949; width:418px; height:240px; background:#fff; }'+'\n';
						valeroProactiveChatInterceptContainer = valeroProactiveChatInterceptContainer + '#valeroProactiveChatInterceptLogo { margin-top:5px; }'+'\n';
						valeroProactiveChatInterceptContainer = valeroProactiveChatInterceptContainer + '#valeroProactiveChatInterceptBox p { font-family: Helvetica, Arial, Sans-serif; color:#454545; font-size:16px; line-height:21px; margin:20px 10px 0; padding:0; width:398px; }'+'\n';
						valeroProactiveChatInterceptContainer = valeroProactiveChatInterceptContainer + '#valeroProactiveChatInterceptYes { margin:45px 0 0 100px; float:left; display:inline; cursor:pointer; }'+'\n';
						valeroProactiveChatInterceptContainer = valeroProactiveChatInterceptContainer + '#valeroProactiveChatInterceptNo { margin:45px 100px 0 0; float:right; display:inline; cursor:pointer; }'+'\n';
						valeroProactiveChatInterceptContainer = valeroProactiveChatInterceptContainer + '</style>'+'\n';
						valeroProactiveChatInterceptContainer = valeroProactiveChatInterceptContainer + '<div id=\"valeroProactiveChatInterceptBoxWrapper\">'+'\n';
						valeroProactiveChatInterceptContainer = valeroProactiveChatInterceptContainer + '	<div id=\"valeroProactiveChatInterceptBox\">'+'\n';
						valeroProactiveChatInterceptContainer = valeroProactiveChatInterceptContainer + '		<img id=\"valeroProactiveChatInterceptLogo\" src=\"http://www.discountid.com/Images/didChatHeader3.jpg\" width=\"386\" height=\"65\" alt=\"\" border=\"0\" />'+'\n';
						valeroProactiveChatInterceptContainer = valeroProactiveChatInterceptContainer + '  	<p>Hello, a sales associate is available to assist you, can we assist you with anything?</p>'+'\n';
						valeroProactiveChatInterceptContainer = valeroProactiveChatInterceptContainer + '  	<img id=\"valeroProactiveChatInterceptYes\" src=\"http://www.discountid.com/skin/frontend/discountid/did_theme_three/images/did_chat_now.gif\" width=\"85\" height=\"19\" alt=\"\" border=\"0\" /><img id=\"valeroProactiveChatInterceptNo\" src=\"http://www.discountid.com/skin/frontend/discountid/did_theme_three/images/did_no_thanks.gif\" width=\"85\" height=\"19\" alt=\"\" border=\"0\" />'+'\n';
						valeroProactiveChatInterceptContainer = valeroProactiveChatInterceptContainer + '	</div>'+'\n';
						valeroProactiveChatInterceptContainer = valeroProactiveChatInterceptContainer + '</div>'+'\n';
						$("body").append(valeroProactiveChatInterceptContainer);
						
						_gaq.push(["_setAccount", "UA-325904-1"]);
						_gaq.push(["_trackPageview", "/proactiveChatInitialized"]);
						
						$('#valeroProactiveChatInterceptBoxWrapper').css({'top':( $(window).height()-($('#valeroProactiveChatInterceptBoxWrapper').height()*1.5))/2+$(window).scrollTop()+'px'}).delay(intercept1Timer2).animate({'top':( $(window).height()-($('#valeroProactiveChatInterceptBoxWrapper').height()*1.5))/2+$(window).scrollTop()+'px', 'left':($(window).width()-$('#valeroProactiveChatInterceptBoxWrapper').width())/2+$(window).scrollLeft()+"px"},2500);

						_gaq.push(['gwo._setAccount', intercept1GWOaccount]);
						_gaq.push(['gwo._trackPageview', '/' + intercept1GWOTestNumber + '/test']);

						$('#valeroProactiveChatInterceptYes').click(function(){velaroProactiveChatInviteAgree()});
						$('#valeroProactiveChatInterceptNo').click(function(){velaroProactiveChatInviteDecline()});
						break;
					default:
						break;
				}
				// set expired cookie
				setCookie(intercept1Name + 'Status', 'expired', cookieTTL);
				// clean up old cookie data
				deleteCookie(intercept1Name + 'PageViews');
				deleteCookie(intercept1Name + 'Timer');
			}
// END velaroProactiveChatInviteGo
			var intercept1StatusCookieVal = getCookie(intercept1Name + 'Status');
			if (intercept1StatusCookieVal !== "" && intercept1StatusCookieVal !== null) {
		// User has a cookie value
				pageViews = getCookie(intercept1Name + 'PageViews');
				pageViews++;
				pageTimer = getCookie(intercept1Name + 'Timer');
				if (intercept1StatusCookieVal != "expired") {
					if (pageViews >= intercept1PageViewsTarget) {
						// page count target is reached or exceeded, so proceed with the timer test.
						// no need to update cookie page counter b/c it is at the goal and has no value in incrementing.
						timeElapsed = theNow - pageTimer;
						if (timeElapsed >= intercept1Timer) {
						// timer is reached, show invite
							this.velaroProactiveChatInviteGo();
						} else {
						// timer is not reached, start timer to show intercept.
							this.velaroProactiveChatInviteGo();
						}
					} else {
						// The target page count is not reached, so update the cookie page counter, then stop.
						setCookie(intercept1Name + 'PageViews', pageViews, cookieTTL);
					}
				}
			} else {
		// user has no cookie
				if (intercept1PageViewsTarget == 1) {
					// timer is not reached, start timer to show intercept.
					this.velaroProactiveChatInviteGo();
				}
				setCookie(intercept1Name + 'Status', 'Active', cookieTTL);
				setCookie(intercept1Name + 'PageViews', 1, cookieTTL);
				setCookie(intercept1Name + 'Timer', theNow, cookieTTL);
			}
		} else if ( (version == 0) && intercept1GWOActive && (theServerTime >= intercept1StartTime && theServerTime <= intercept1EndTime) && (theServerDay >= intercept1StartDay && theServerDay <= intercept1EndDay) ) {
				_gaq.push(["_setAccount", "UA-325904-1"]);
			    _gaq.push(["_trackPageview", "/noProactiveChatRightTime"]);
		} else {
			
		}// end if for version 0 which is the control, so nothing should happen.
	} // END function velaroProactiveChatInvite(version)
	
//	if ( intercept1GWOActive && (theServerTime >= intercept1StartTime && theServerTime <= intercept1EndTime) && (theServerDay >= intercept1StartDay && theServerDay <= intercept1EndDay) ) {
		
		// Initialize / start test.
//		_gaq.push(['gwo._setAccount', intercept1GWOaccount]);
//		_gaq.push(['gwo._trackPageview', '/' + intercept1GWOTestNumber + '/test']);
		
//		velaroProactiveChatInvite(1);
//	} // END if theServerTime is during business hours.
	
	


});











