
var userAgentString = navigator.userAgent;

function loadEPG() {
    if(isChrome()) {
        writeChromeMessage();
    } else if(isSafari2()) {
        writeSafari2Message();
    } else {
    	writeEpgIframe();
    }
}

moduleManager.registerModuleLoadOnLoad(loadEPG);

function writeSafari2Message() {
   	var skyEpgDiv = $('skyEPG');
   	skyEpgDiv.innerHTML = '<div class="browserMessageWrapper"><p class="fontStyle07">We have detected that you are using an old version of Safari. ' + 
   		'You need Safari 3 or Firefox to view our latest version of the TV Listings. Please click on one of the links below:</p>' +
		'<ul>' + 
			'<li class="fontStyle07"><a style="color:#000000;" href="http://www.apple.com/safari" target=_new id="linkToSafari">I want to upgrade Safari to see the latest version of the TV Listings</a></li>' +
			'<li class="fontStyle07"><a style="color:#000000;" href="http://www.mozilla.com" target=_new id="linkToFirefox">I want to download Firefox (Mac) instead to see the latest version of the TV Listings</a></li>' + 
			'<li class="fontStyle07"><a style="color:#000000;" href="http://www.sky.com/portal/site/skycom/tvguide/tvlistingsalternate" target=_new id="linkToOldEPG">Take me to the old version of the TV Listings in Safari 2</a></li>' +
		'</ul></div>';
}

function writeChromeMessage() {
   	var skyEpgDiv = $('skyEPG');
   	skyEpgDiv.innerHTML = '<div class="browserMessageWrapper"><p class="fontStyle07">We have detected that you are using Chrome. You need Internet Explorer, Safari 3 or Firefox to view the latest version of the TV Listings.' +
		'<ul>' + 
			'<li class="fontStyle07"><a style="color:#000000;" href="http://www.sky.com/portal/site/skycom/tvguide/tvlistingsalternate" target=_new id="linkToOldEPGChrome">If you would like to proceed using Chrome please click here and you will be taken to an older version of our TV listings</a></li>' +
		'</ul></div>';
}

function writeEpgIframe() {    
    if(ENVIRONMENT_MODE != 'test' && ENVIRONMENT_MODE != 'seleniumTv' ){
        document.domain = location.host.match(/\w+\.com/);
    }

    var skyEpgDiv = $('skyEPG');
   	skyEpgDiv.innerHTML = '<iframe src="' + EPG_IFRAME_URL + location.search + '" width="100%" height="'+(navigator.appName.match(/(Microsoft Internet Explorer)/gi)?598:594)+'" scrolling="no" frameborder="0" marginwidth="0" marginheight="0"></iframe>'; 
}

function isSafari2() {
	if (userAgentString.indexOf('Safari') > -1 && !(userAgentString.indexOf('Version') > -1)) {
		return true;
	} 
	return false;
}

function isChrome() {
	return /chrome/.test( userAgentString.toLowerCase() );
}
