var lpMTagConfig = {
'lpServer' : "supportchat.apple.com",
'lpNumber' : "18469873",
'lpProtocol' : (document.location.toString().indexOf("https:")==0) ? "https" : "http",
'sendCookies' : "true"
}

function lpAddMonitorTag(src){if(typeof(src)=='undefined'||typeof(src)=='object'){src=lpMTagConfig.lpMTagSrc?lpMTagConfig.lpMTagSrc:'/hcp/html/mTag.js';}if(src.indexOf('http')!=0){src=lpMTagConfig.lpProtocol+"://"+lpMTagConfig.lpServer+src+'?site='+lpMTagConfig.lpNumber;}else{if(src.indexOf('site=')<0){if(src.indexOf('?')<0)src=src+'?';else src=src+'&';src=src+'site='+lpMTagConfig.lpNumber;}};var s=document.createElement('script');s.setAttribute('type','text/javascript');s.setAttribute('charset','iso-8859-1');s.setAttribute('src',src);s.setAttribute('id','lpChatScript');document.getElementsByTagName('head').item(0).appendChild(s);}


//creating dynamic button object - We use the same override button for all buttons, since the actions we're taking are the same
lpMTagConfig.dbBtOverride = new Object();

//function called when button starts
lpMTagConfig.dbBtOverride.dbStart = function (objName) {
	//save the button name, we need to delete the object it references later
	lpMTagConfig.lastBtName = objName;
                       
	objRef = eval(objName);
	if (objRef == null) return true;
	try {
		objRef.imgDefaultName = objRef.imgDefaultName + '?d=' + (new Date()).getTime();
	}
	catch (e) {
		if (lpConnLib.DebugDisplay) lpMTagDebug.Display('DynBut override failed setting default Image name' ,'ERROR');
	};
	
	return true;
};

//function called when state changes
lpMTagConfig.dbBtOverride.dbStateChange = function (objName, status) {
	objRef = eval(objName);
	
	var chatLangDir = '/en';
	if (typeof(lastChatLanguage) != 'undefined' && lastChatLanguage != '') {
		if (lastChatLanguage == 'french') {
			chatLangDir = '/fr';
		}
		else if (lastChatLanguage == 'german') {
			chatLangDir = '/de';
		}
	}
	//override busy image
	objRef.imgBusyName = 'http://images.apple.com/support/mobileme'+chatLangDir+'/images/reponline.gif';
	
	if (status == 'online') {
		$('chatonlinenote').show();
		$('chatbusynote').hide();
		$('chatofflinenote').hide();
		$('helpemail').hide();
		(lastChatStatus == 'chat') ? $('helpchat').show() : $('helpchat').appear({duration:1.0});
		logButtonState("Chat Online Exposed");
		lastChatStatus = 'chat';
	}
	else if (status == 'busy') {
		$('chatonlinenote').hide();
		$('chatbusynote').hide();
		$('chatofflinenote').show();
		$('helpchat').hide();
		(lastChatStatus == 'email') ? $('helpemail').show() : $('helpemail').appear({duration:1.0});
		logButtonState("Chat Busy Exposed");
		lastChatStatus = 'email';
	}
	else if (status == 'offline') {
		$('chatonlinenote').hide();
		$('chatbusynote').hide();
		$('chatofflinenote').show();
		$('helpchat').hide();
		(lastChatStatus == 'email') ? $('helpemail').show() : $('helpemail').appear({duration:1.0});
		logButtonState("Chat Offline Exposed");
		lastChatStatus = 'email';
	}
	else {
		$('chatonlinenote').hide();
		$('chatbusynote').hide();
		$('chatofflinenote').show();
		$('helpchat').hide();
		(lastChatStatus == 'email') ? $('helpemail').show() : $('helpemail').appear({duration:1.0});
		logButtonState("Email Exposed");
		lastChatStatus = 'email';
	}
	return true;
};

// override busy state
lpMTagConfig.dbBtOverride.busyAction = function (objName) {
	objRef = eval(objName);
	var chatWinURL = objRef.getActionURL("Busy");
	chatWinURL = chatWinURL.replace(/forceOffline/,"SESSIONVAR%21BusyClickOverride");
	window.open(chatWinURL,'chat'+lpMTagConfig.lpNumber,'width=472,height=320,status=0,resizable=0,menubar=no,scrollbars=no,location=no');
};

// SiteCatalyst tracking for button states
function logButtonState(bs) {
	SCReporting.trackClick(lastTopic + ' - ' + bs);
}

//Variables Arrays - By Scope
if (typeof(lpMTagConfig.pageVar)=='undefined') lpMTagConfig.pageVar = new Array();
if (typeof(lpMTagConfig.sessionVar)=='undefined') lpMTagConfig.sessionVar = new Array();
if (typeof(lpMTagConfig.visitorVar)=='undefined') lpMTagConfig.visitorVar = new Array();


// Function that sends variables to LP - By Scope
function lpAddVars(scope,name,value) {
	//This is optional, depends if client wants to pass 0 or blank values
	if (value != 0 && value != "") {
		switch (scope){
			case "page": 
			lpMTagConfig.pageVar[lpMTagConfig.pageVar.length] = escape(name)+"="+escape(value);
			break;
			case "session": 
			lpMTagConfig.sessionVar[lpMTagConfig.sessionVar.length] = escape(name)+"="+escape(value);
			break;
			case "visitor": 
			lpMTagConfig.visitorVar[lpMTagConfig.visitorVar.length] = escape(name)+"="+escape(value);
			break;
		}
	}
}


// Apple added functions to support multiple chats
var lastChatSkill = '';
var lastChatLanguage = '';
var lastChatStatus = '';
var lastTopic = '';
var lastCountry = '';

function clearChatVars() {
	lpMTagConfig.pageVar = new Array();
	lpMTagConfig.sessionVar = new Array();
	lpMTagConfig.visitorVar = new Array();
	lpMTagConfig.dynButton = new Array();

	//remove existing hidden button object
	internalBt = eval(lpMTagConfig.lastBtName);
	if(internalBt!=null) {
		delete internalBt;
	}
}

function createChatButton(skill, language, topic, country) {
	lastChatSkill = skill;
	lastChatLanguage = language;
	lastTopic = topic;
	lastCountry = country;
	clearChatVars();
	if ($('lpChatScript')!=null) $('lpChatScript').remove();
	lpAddVars('page','unit','itunes-'+skill);
	lpAddVars('page','itunes_Section','<Section>');
	lpAddVars('page','itunes_PageName',topic);
	lpAddVars('session','language',language.toLowerCase());
	lpAddVars('session','country',country);
	lpAddVars('visitor','itunes_VisitorID','<Visitor ID>');
	lpMTagConfig.dynButton = [{'name':'chat-itunes-'+skill+'-'+language.toLowerCase(),'pid':'chat-button', 'ovr':'lpMTagConfig.dbBtOverride'}];
	if (typeof(lpMTag)!='undefined') {
		clearTimeout(lpMTag.lpLoopTimer);		
	}	
	lpAddMonitorTag();
}
