if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
	var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
	if (ieversion>=6 && ieversion<7) {
		var ie6_user = true; 
	}
}

var lang = 'en'; // default selected language

var rnInstance = 'williamhill-lang.custhelp.com';

// pass the URL of the current parent page to the iframe using location.hash
//src = 'https://williamhill-lang.custhelp.com/app/answers/list/';
/* # + encodeURIComponent(document.location.href); */
//document.getElementById("iframe_mainFrame").src = src;

function send(msg) {
XD.postMessage(msg, src, frames[0]);
return false;
}

// setup a callback to handle the dispatched MessageEvent. if window.postMessage is supported the passed
// event will have .data, .origin and .source properties. otherwise, it will only have the .data property.
XD.receiveMessage(function(message){
//window.alert(message.data + " received on "+window.location.host); 
// listen for a message passed from the iFrames
//window.addEventListener("message", receiveMessage, false);
//function receiveMessage(event){
	//if (event.origin !== "*")
	//return;
	
if ( jQuery('#helpWindow').hasClass('selected_option_e') == true ) {
	// show main iFrame
	jQuery('#helpWindow_vwCtrl ul li.helpWindow_vwCtrl_option_a span').click();
}
// do nothing if the provided and current urls match
if (message.data != jQuery('#iframe_mainFrame').attr('src')) {jQuery('#iframe_mainFrame').attr('src',message.data);}
//}

}, 'https://' + rnInstance);

// use this object to determine which view mode should be used
var viewModes = new Object;
viewModes.selected_option_a = 'helpWindow_vwCtrl_option_a';
viewModes.selected_option_b = 'helpWindow_vwCtrl_option_b';
viewModes.selected_option_c = 'helpWindow_vwCtrl_option_c';
viewModes.selected_option_d = 'helpWindow_vwCtrl_option_d';
viewModes.selected_option_e = 'helpWindow_vwCtrl_option_e';
// the popup window
var helpPopup;

jQuery(document).ready(function($) {
	var pathname = window.location.pathname;
	var hostname = window.location.hostname;
	var segmentsArr = pathname.split('/');
	if (segmentsArr.length >=2) {
		if (hostname.indexOf("sports") >= 0) { //sportsbook
			lang = (segmentsArr[2].length >= 2) ? segmentsArr[2].substring(0, 2) : 'en';
			if (segmentsArr[2].indexOf('zh') == 0) { // chinese
    				lang = (segmentsArr[2].substring(3, 5) == 'cn') ? 'cn' : 'zh';
			}
		} else {
			lang = (segmentsArr[1].length == 2) ? segmentsArr[1] : 'en';
		}
	}
	var localLang = lang;
	if (lang == 'at') { localLang = 'de'; }
	if (lang == 'ja') { localLang = 'jp'; }
	if (lang == 'cs') { localLang = 'cz'; }
	if (lang == 'sl') { localLang = 'si'; }
	if (lang == 'sr') { localLang = 'rs'; }
	if (lang == 'br') { localLang = 'pt'; }
	if (lang == 'es') { localLang = 'sp'; }
	if (lang == 'cn') { localLang = 'lang-sch'; }
	if (lang == 'zh') { localLang = 'lang-tch'; }
	if (lang != 'en' && lang != 'ca' && lang != 'ie' && lang != 'tr' && lang != 'no') {
		rnInstance = 'williamhill-' + localLang + '.custhelp.com';
	}
	
	$( "#helpWindow" ).draggable({ 
		handle	: '#helpWindow_hdr',
		stop	: function (event,ui) {
					var className = getViewMode();
					var leftVal = ($('#helpWindow')) ? $('#helpWindow').css('left') : '';
					var topVal = ($('#helpWindow')) ? $('#helpWindow').css('top') : '';
					var cookieInfo = rnEncodeCookieInfo(className, leftVal , topVal , '#', '#', 'true', lang);
					rnCreateCookie('helpWindowConfig', cookieInfo, 1);
				  }
	});
	
	$(window).resize(function() {
		if(this.resizeTO) clearTimeout(this.resizeTO);
			this.resizeTO = setTimeout(function() {repositionFrame();}, 500);
	});
	try {
		var options = (rnReadCookie('helpWindowConfig')).split('|');
		var cookieLang = (typeof options[6] === "undefined") ? 'en' : options[6];
		var cookieVis = (typeof options[6] === "undefined") ? 'false' : options[5];
		if (cookieVis == 'true' && cookieLang == lang) {showHelp();} else if (cookieVis == 'true' && cookieLang != lang) {rnEraseCookie('helpWindowConfig');}
	} catch (err) {
		// ok, so there is noting to do here - no need to make a song and dance about it!
	}

	$('#helpWindow .helpWindow_close a').click( function () {
		var className = getViewMode();
		var leftVal = ($('#helpWindow')) ? $('#helpWindow').css('left') : '';
		var topVal = ($('#helpWindow')) ? $('#helpWindow').css('top') : '';
		var cookieInfo = rnEncodeCookieInfo(className, leftVal, topVal, '#', '#', 'false', lang);
		$('#helpWindow').fadeOut(500);
		rnCreateCookie('helpWindowConfig', cookieInfo, 1);		
	});
	$('#helpWindow .helpWindow_detach a').click( function () {
		var randomNumber=Math.floor(Math.random()*11);
		//helpPopup = window.open( 'http://www.'+document.domain+'/whHelp/?v='+randomNumber, 'helpPopup', 'menubar=0,resizable=0,width=990,height=510,status=0,location=0' );
		if (lang != 'en') {
			var localLang = lang;
			if (lang == 'br') { localLang = 'pt'; }			
			helpPopup = window.open( 'http://www.williamhill.com/'+localLang +'/whHelp/?v='+randomNumber, 'helpPopup', 'menubar=0,resizable=0,width=990,height=510,status=0,location=0' );
		} else {
		        helpPopup = window.open( 'http://www.williamhill.com/whHelp/?v='+randomNumber, 'helpPopup', 'menubar=0,resizable=0,width=990,height=510,status=0,location=0' );
		}
		var className = getViewMode();
		var leftVal = ($('#helpWindow')) ? $('#helpWindow').css('left') : '';
		var topVal = ($('#helpWindow')) ? $('#helpWindow').css('top') : '';
		var cookieInfo = rnEncodeCookieInfo(className,leftVal, topVal, '#', '#', 'false', lang);
		rnCreateCookie('helpWindowConfig', cookieInfo, 1);
		$('#helpWindow').fadeOut(500);
		jQuery('#iframe_mainFrame').attr("src","");
	});
	
	// fix for lack of IE6 position:fixed support
	if (window.ie6_user != undefined) {
		$(window).scroll(function() {
			$('#helpWindow').css('top', $(this).scrollTop() + "px");
		});
	}
	
});

jQuery('#helpWindow_vwCtrl ul li span').click( function () {
	changeDisplay(jQuery(this).parent('li').attr('class'));
	jQuery('.vwCtrl_option_active').each( function () {jQuery(this).removeClass('vwCtrl_option_active');});
	jQuery(this).addClass('vwCtrl_option_active');
	
});

var RNDomain = 'https://williamhill-lang.custhelp.com/app/';
var baseRNUrl = RNDomain + 'answers/detail/a_id/';
var targetUrl = '';
var frameWidth = 0;

// Shows the help window. Does exactly what it says on the tin, one could say.
function showHelp(whereFrom,productID,metaID,anchor,subFrame,viewMode) {
//console.log('call showHelp  with productID:' + productID);
	
	if (typeof productID === "undefined" || productID == 'undefined' || productID == '') {
		switch(document.body.id) {						
			case'root_site':
				productID = '1';
				break;
			case 'bingo_site':
				productID = '504';
				break;
			case 'casino_site':
				productID = '4';
				break;
			case 'poker_site':
				productID = '5';
				break;
			case 'liveDealer_site':
				productID = '1044';
				break;
			case 'vegas_site':
				productID = '739';
				break;
			case 'skill_site':
				productID = '503';
				break;
			case 'games_site':
				productID = '23';
				break;
			default:
				productID = '1';
				break;
		}
	}
	
	// if an article/anchor location has been requested, make sure they are shown	
	if (metaID != '' && metaID != null) {
		targetUrl = 'https://' + rnInstance + '/app/answers/detail/a_id/'+metaID+'/';
		//we need the article before we can load an anchor
		if (anchor != '' && anchor != null) {
			// load all 'targeted' articles in the 'most popular' view - K.I.S.S.
			targetUrl = targetUrl+'#'+anchor;
		}
	} else {
		targetUrl = 'https://' + rnInstance + '/app/?lastpage=true';
	}
	
	if (/Android|iPhone/i.test(navigator.userAgent)) {
		// Rightnow show the mobile version, so no frameset is required
		var popUrl = (targetUrl == '') ? src+'p/'+productID : targetUrl;
		helpPopup = window.open( popUrl , 'helpPopup', 'menubar=0,resizable=0,width=990,height=510,status=0,location=0' );			
		return;
	} else if (/webOS|iPad|iPod|BlackBerry/i.test(navigator.userAgent) || ( (window.screen.availHeight < 550 && window.screen.availWidth < 1000) || (jQuery(window).width() < 1000 && jQuery(window).height() < 550) ) ) {
		// load popup window with frameset
		var randomNumber=Math.floor((Math.random()*10000000)+1); 
		if (lang == 'en') {
		     var helpPopupUrl = 'http://www.williamhill.com/whHelp/' + ((metaID != '' && metaID != null) ? metaID + '/' : '') + '?v='+randomNumber;
		} else {
			var localLang = lang;
		     var helpPopupUrl = 'http://www.williamhill.com/'+localLang +'/whHelp/' + ((metaID != '' && metaID != null) ? metaID + '/' : '') + '?v='+randomNumber;
		}
		
		helpPopup = window.open(helpPopupUrl, 'helpPopup', 'menubar=0,resizable=0,width=990,height=510,status=0,location=0' );
		return;
	} else {
		// show the standard overlay version
		jQuery('#iframe_mainFrame').attr('src',targetUrl);
	}
	
	// check to see if the modal or popup window is already displayed
	if ((helpPopup == null) || (helpPopup.closed) || whereFrom=='popup'){
	
		var cookieValue = rnReadCookie('helpWindowConfig');
		if(cookieValue != null) {
			var options = cookieValue.split('|');
			if (options[2] == '0px') {jQuery('#helpWindow').css('top','50%');} else {
				//jQuery('#helpWindow').css('top',options[2]);
				jQuery('#helpWindow').css('top','50%');
			}
			if (options[1] == '0px') {jQuery('#helpWindow').css('left','50%');} else {
				//jQuery('#helpWindow').css('left',options[1]);
				jQuery('#helpWindow').css('left','50%');
			}
			if (viewMode != '' && viewMode != null) {
				switch (viewMode) {
					default:
					case 'vw1':
						jQuery('#helpWindow').find('.helpWindow_vwCtrl_option_a>span').click();
						break;
					case 'vw2':
						jQuery('#helpWindow').find('.helpWindow_vwCtrl_option_b>span').click();
						break;
					case 'vw3':
						jQuery('#helpWindow').find('.helpWindow_vwCtrl_option_c>span').click();
						break;
					case 'vw4':
						jQuery('#helpWindow').find('.helpWindow_vwCtrl_option_d>span').click();
						break;
					case 'vw5':
						jQuery('#helpWindow').find('.helpWindow_vwCtrl_option_e>span').click();
						break;
				}
			} else {jQuery('#helpWindow').find('.'+viewModes[options[0]]+'>span').click();}
		}
	
		jQuery('#helpWindow').fadeIn(500, function () {
			window.frameWidth = jQuery('#helpWindow').outerWidth();
			repositionFrame();
			if (jQuery('#iframe_mainFrame').attr("src") == '') {jQuery('#iframe_mainFrame').attr("src",src+'p/'+productID);}
		});
		if (whereFrom == 'popup') {
			// reload the page so that we can pick up the contents of the cookie set in the popup window
			jQuery('#iframe_mainFrame').attr("src", jQuery('#iframe_mainFrame').attr("src"));
		}
		var className = getViewMode();
		var leftVal = ($('#helpWindow')) ? $('#helpWindow').css('left') : '';
		var topVal = ($('#helpWindow')) ? $('#helpWindow').css('top') : '';
		var cookieInfo = rnEncodeCookieInfo(className, leftVal, topVal, '#', '#', 'true', lang);
		rnCreateCookie('helpWindowConfig', cookieInfo, 1);
	} else {helpPopup.focus();}
}

// Shows the liveChat page (with pre-loaded variables)
function showLiveChat(viewMode,firstName,surname,email) {
	if ((firstName != '' && firstName != null) && (surname != '' && surname != null) && (email != '' && email != null)) {
		var baseURL = 'https://' + rnInstance + '/app/chat/chat_landing_sidebar/';
		if (viewMode != '' && viewMode != null) {
			switch (viewMode) {
				default:
				case 'vw1':
					jQuery('#helpWindow').find('.helpWindow_vwCtrl_option_a>span').click();
					break;
				case 'vw2':
					jQuery('#helpWindow').find('.helpWindow_vwCtrl_option_b>span').click();
					break;
				case 'vw3':
					jQuery('#helpWindow').find('.helpWindow_vwCtrl_option_c>span').click();
					break;
				case 'vw4':
					jQuery('#helpWindow').find('.helpWindow_vwCtrl_option_d>span').click();
					break;
				case 'vw5':
					jQuery('#helpWindow').find('.helpWindow_vwCtrl_option_e>span').click();
					break;
			}
		} else {jQuery('#helpWindow').find('.helpWindow_vwCtrl_option_e>span').click();}
		jQuery('#helpWindow').fadeIn(500, function () {
			window.frameWidth = jQuery('#helpWindow').outerWidth();
			repositionFrame();
		});
		var targetURL = baseURL+'first_name/'+firstName+'/last_name/'+surname+'/email/'+email+'/#' + encodeURIComponent(document.location.href);
	} else {
		return false;
	}
}

function changeDisplay(viewMode) {
	// configuration options
	var animSpeed = 500; // use this for the animations that change view modes
	jQuery('#helpWindow').removeClass('selected_option_a selected_option_b selected_option_c selected_option_d selected_option_e');
	if (viewMode != 'helpWindow_vwCtrl_option_e') {
		//jQuery('#helpWindow_vwCtrl .helpWindow_vwCtrl_title').text('Change View: ');
	}
	switch (viewMode) {
		case 'helpWindow_vwCtrl_option_a':
			jQuery('#helpWindow').addClass('selected_option_a');
			jQuery('#helpWindow_vwCtrl .helpWindow_vwCtrl_title').show();
			jQuery('#helpWindow_hdr .helpWindow_title').show();
			jQuery('#helpWindow').animate({width:'996px'},animSpeed, function () {
				window.frameWidth = '996';
				repositionFrame();
			});
			jQuery('#helpWindow_hdr').animate({width:'968px'},animSpeed);
			jQuery('#helpWindow_ftr').animate({width:'745px'},animSpeed);
			jQuery('#helpWindow_content').animate({width:'990px'},animSpeed);
			jQuery('#helpWindow_vwCtrlExtsn').animate({width:'235px'},animSpeed);
			jQuery('#helpWindow_mainFrame').css('display','block').delay(1).animate({width:'988px'},animSpeed);
			jQuery('#helpWindow_ftrExtsn').animate({width:'223px'},animSpeed);
			jQuery('#helpWindow_vwCtrl').animate({width:'754px'},animSpeed);
			break;
		case 'helpWindow_vwCtrl_option_b':
			jQuery('#helpWindow').addClass('selected_option_b');
			jQuery('#helpWindow_vwCtrl .helpWindow_vwCtrl_title').show();
			jQuery('#helpWindow_hdr .helpWindow_title').show();
			jQuery('#helpWindow').animate({width:'762px'},animSpeed, function () {
				window.frameWidth = '762';
				repositionFrame();
			});
			jQuery('#helpWindow_hdr').animate({width:'734px'},animSpeed);
			jQuery('#helpWindow_ftr').animate({width:'734px'},animSpeed);
			jQuery('#helpWindow_content').animate({width:'756px'},animSpeed);
			jQuery('#helpWindow_vwCtrlExtsn').animate({width:'0px'},animSpeed);
			jQuery('#helpWindow_mainFrame').css('display','block').delay(1).animate({width:'754px'},animSpeed);
			jQuery('#helpWindow_ftrExtsn').animate({width:'0px'},animSpeed);
			jQuery('#helpWindow_vwCtrl').animate({width:'754px'},animSpeed);
			break;
		case 'helpWindow_vwCtrl_option_c':
			jQuery('#helpWindow').addClass('selected_option_c');
			jQuery('#helpWindow_vwCtrl .helpWindow_vwCtrl_title').show();
			jQuery('#helpWindow_hdr .helpWindow_title').show();
			jQuery('#helpWindow').animate({width:'890px'},animSpeed, function () {
				window.frameWidth = '890';
				repositionFrame();
			});
			jQuery('#helpWindow_hdr').animate({width:'842px'},animSpeed);
			jQuery('#helpWindow_ftr').animate({width:'619px'},animSpeed);
			jQuery('#helpWindow_content').animate({width:'864px'},animSpeed);
			jQuery('#helpWindow_vwCtrlExtsn').animate({width:'235px'},animSpeed);
			jQuery('#helpWindow_mainFrame').css('display','block').delay(1).animate({width:'862px'},animSpeed); /*861*/
			jQuery('#helpWindow_ftrExtsn').animate({width:'223px'},animSpeed);
			jQuery('#helpWindow_vwCtrl').animate({width:'628px'},animSpeed);
			break;
		case 'helpWindow_vwCtrl_option_d':
			jQuery('#helpWindow').addClass('selected_option_d');
			jQuery('#helpWindow_vwCtrl .helpWindow_vwCtrl_title').show();
			jQuery('#helpWindow_hdr .helpWindow_title').show();
			jQuery('#helpWindow').animate({width:'635px'},animSpeed, function () {
				window.frameWidth = '635';
				repositionFrame();
			});
			jQuery('#helpWindow_hdr').animate({width:'607px'},animSpeed);
			jQuery('#helpWindow_ftr').animate({width:'607px'},animSpeed);
			jQuery('#helpWindow_content').animate({width:'629px'},animSpeed);
			jQuery('#helpWindow_vwCtrlExtsn').animate({width:'0px'},animSpeed);
			jQuery('#helpWindow_subFrame').delay(1).animate({width:'0px'},animSpeed);
			jQuery('#helpWindow_mainFrame').css('display','block').delay(1).animate({width:'627px'},animSpeed);
			jQuery('#helpWindow_ftrExtsn').animate({width:'0px'},animSpeed);
			jQuery('#helpWindow_vwCtrl').animate({width:'628px'},animSpeed);
			break;
		case 'helpWindow_vwCtrl_option_e':
			jQuery('#helpWindow').addClass('selected_option_e');
			jQuery('#helpWindow_vwCtrl .helpWindow_vwCtrl_title').hide();
			jQuery('#helpWindow_hdr .helpWindow_title').hide();
			jQuery('#helpWindow').animate({width:'239px'},animSpeed, function () {
				window.frameWidth = '239';
				repositionFrame();
			});
			jQuery('#helpWindow_hdr').animate({width:'211px'},animSpeed);
			jQuery('#helpWindow_ftr').animate({width:'0px'},animSpeed);
			jQuery('#helpWindow_content').animate({width:'233px'},animSpeed);
			jQuery('#helpWindow_vwCtrlExtsn').animate({width:'0px'},animSpeed);
			jQuery('#helpWindow_mainFrame').animate({width:'233px'},animSpeed);
			jQuery('#helpWindow_ftrExtsn').animate({width:'211px'},animSpeed);
			jQuery('#helpWindow_vwCtrl').animate({width:'232px'},animSpeed);
			break;
		default:
			break;
	}
	
	var className = getViewMode();
	var leftVal = ($('#helpWindow')) ? $('#helpWindow').css('left') : '';
	var topVal = ($('#helpWindow')) ? $('#helpWindow').css('top') : '';
	var cookieInfo = rnEncodeCookieInfo(className, leftVal, topVal, '#', '#', 'true', lang);
	rnCreateCookie('helpWindowConfig', cookieInfo, 1);
	return true;
}

function repositionFrame () {
	// move the modal window back into view if expanding it movies it out of view
	var windowWidth = jQuery(window).width();
	var framePos = jQuery('#helpWindow').offset();
	var frameMargin = parseInt(jQuery('#helpWindow').css('margin-left'));
	var outOfView = (parseInt(window.frameWidth) + parseInt(framePos.left)) - parseInt(windowWidth);
	//console.log('Window Width: '+windowWidth+', Frame Width: '+window.frameWidth+', Frame Position: '+framePos.left);
	// console.log('outOfView Value: '+outOfView);
	if (outOfView > 0) {jQuery('#helpWindow').animate({'left':'-='+outOfView},500);
		var className = getViewMode();
		var leftVal = ($('#helpWindow')) ? $('#helpWindow').css('left') : '';
		var topVal = ($('#helpWindow')) ? $('#helpWindow').css('top') : '';
		var cookieInfo = rnEncodeCookieInfo(className, leftVal, topVal, '#', '#', 'true', lang);
		rnCreateCookie('helpWindowConfig', cookieInfo, 1);
	}
}

function getViewMode () {
	var classes = (jQuery('#helpWindow').attr('class')).split(' ');
	var className;
	for ( var i=0, len=classes.length; i<len; ++i ){
		if (classes[i].indexOf('selected_option_')!=-1) {
			className = classes[i];
			break;
		}
	}
	return className;
}

/* NOT SURE IF THESE ALREADY EXIST SOMEWHERE ELSE... */

	// Cookie Structure;
	// viewMode|leftPos|topPos|mainPage|subPage|visible

/* 
This function sanitize some values and create a pipe separated
value list ready to be used in for cookie 
*/
function rnEncodeCookieInfo(viewMode, leftPos, topPos, mainPage, subPage, visible, language) {
	var encStr = '';
	encStr += viewMode + '|';
	
	if (leftPos == 'auto' || leftPos == '' || leftPos == '0px') {
		leftPos = '50%';
	}
	encStr += leftPos + '|';
	
	if (topPos == 'auto' || topPos == '' || topPos== '0px') {
		topPos= '50%';
	}
	encStr += topPos + '|';
	
	encStr += mainPage + '|';
	
	encStr += subPage + '|';
	
	encStr += visible + '|';
	
	encStr += language;	
	
	return encStr;
}	

function rnCreateCookie(name,value,days) {
	if (days) {
		var date = new Date();
		// expire time forced to 15 minutes
		date.setTime(date.getTime()+(15*60*1000));
		//date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	//document.cookie = name+"="+value+expires+"; domain="+document.domain+"; path=/";
	document.cookie = name+"="+value+"; domain="+document.domain+"; path=/";
}

function rnReadCookie(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 rnEraseCookie(name) {rnCreateCookie(name,"",-1);}

/*****************************************************/