	Ext.ns('WB');
	//requires Ext 2 built w/ core, core-components, core-utilities, core-xtemplate, core-date, data-core

	/**
	*	Download file
	*/
	function download(url) {
		window.location.href = url;
	}
	
	/**
	*	Visit url
	*/
	function visit(url) {
		//window.location.href = url;
		window.open(url,'DigitalCopy','width=1000,height=600,left=200,top=100,scrollbars=1');
	}
	
	var	countries		= [];
		
	//split altClickText into array at carat
	altClickText	=	altClickText.split('^');
	
	WB.Country	= Ext.extend(Ext.Component, {
	
		 //static
		 countries:				[]
	
		,bdclick:					null
		,dvdclick:				null
		,download:				null
		,visitSupport:		null
		
		,lang:						null
		,country:					null
		,privacyURL:			null
		,privacyText:			''
		,termsURL:				null
		,termsText:				''
		,rightsReserved:	''
		,flagURL:					null
		,name:						null
		
		,initComponent:		function() {

			this.privacyURL			=	(this.privacyURL && this.privacyURL != "")			?	this.privacyURL			:	'/support/#privacy-content&country=' + this.country;
			
			this.termsURL				=	(this.termsURL && this.termsURL != "")					?	this.termsURL				:	'/support/#terms-content&country=' + this.country;
			
			this.flagURL				=	'/support/images/common/flags/' + this.country + '_flag.png';
			
			this._addCountry(this);
			
			WB.Country.superclass.initComponent.call(this);
		}
		
		,getHelpLink:			function(sku) {
			try {
				return '/support/?lang=' + this.lang + '#home-content&discid='+ sku + '&country=' + this.country;
			} catch(e) {
				if(console && console.error) console.error(e);
			}
		}
		
		,_addCountry:				function(countryInstance) {
			WB.Country.prototype.countries.push(countryInstance);
		}
	
	});

	
	//var country					= lang.replace(/gb/i,'uk');
	var country					= lang.split('-');
	var countrylang = lang;
	var lang						= new Array();
	for(var i=0; i<country.length; i++) {
		var c = (/gb/i.test(country[i]))?'uk':country[i];
		lang[i] = countryData[c]['defaultLanguage'];
	}
	
	var flagURLTemplate	= new Ext.Template('/support/images/common/flags/{lang}_flag.png');
	
	var baseTemplate		= '';
	
	var templates				= {
			'main': new Ext.XTemplate('\
				<div id="description-main">\
					<div id="tout">\
						<img src="{tout}" />\
					</div>\
					<div id="description">\
						<h1 id="header-text">\
							<tpl if="values.downloads.length &lt;= 1">{header_1sku}</tpl>\
							<tpl if="values.downloads.length &gt; 1">{header_2sku}</tpl>\
						</h1>\
						<h2 id="subheader-text">\
							<tpl if="values.downloads.length &lt;= 1">{subheader_1sku}</tpl>\
							<tpl if="values.downloads.length &gt; 1">{subheader_2sku}</tpl>\
						</h2>\
						<div id="instructions-text" class="instructions">\
							<tpl if="values.downloads.length &lt;= 1">{instructions_1sku}</tpl>\
							<tpl if="values.downloads.length &gt; 1">{instructions_2sku}</tpl>\
								<tpl if="values.expires.length &gt; 21 ">\
									<div id="expiredate">{expires}</div>\
								</tpl>\
							<div id="disclaimer">{disclaimer}</div>\
						</div>\
					</div>\
				</div>\
			<tpl if="values.expired != 1">\
				<tpl if="values.destination != \'\' ">\
					<div id="destinationsite">\
					 {destinationSite} \
					</div>\
				</tpl>\
			</tpl>\
			<tpl if="values.expired != 1">\
				<div id="download-main">\
					<div id="all-formats">\
						<tpl if="values.downloads.length &gt; 1">\
							<table border="0"><tr>\
							<td style="position: relative;">\
								<div class="bdclick-text">{bdclick}</div><br />\
						</tpl>\
						\
						<tpl if="countries.length &gt; 1">\
							<div id="country-support0" class="country-support-wrapper">\
								<div class="country-support">\
									<div style="position:absolute;top:0px;right:0px;"><a style="color: #ffffff" href="javascript:" onclick="Ext.get(\'country-support0\').hide();">[X]</a></div>\
									<tpl for="countries">\
										<a href="javascript:" onclick="window.open(\'{[parent.countries[xindex-1].getHelpLink(parent.downloads[0].sku)]}\');" class="button">\
											<span class="button-left">\
												<img src="/images/new/body_button_left.gif" width="42" height="19" />\
											</span>\
											<span class="button-right">\
												<img src="/images/new/body_button_right.gif" width="41" height="19" />\
											</span>\
											<span class="button-text">{name}</span>\
										</a>\
										<br />\
									</tpl>\
								</div>\
							</div>\
						</tpl>\
						<a href="javascript:download(\'{[values.downloads[0].filepath]}\');" onclick="download(\'{[values.downloads[0].filepath]}\');return false;" class="button">\
							<span class="button-left">\
								<img src="/images/new/body_button_left.gif" width="42" height="19" />\
							</span>\
							<span class="button-right">\
								<img src="/images/new/body_button_right.gif" width="41" height="19" />\
							</span>\
							<span class="button-text">{download}</span>\
						</a>\
						<br />\
						<tpl if="countries.length == 1">\
							<a class="button" href="javascript:" onclick="window.open(\'{helpLink1_sku1}\');">\
						</tpl>\
						<tpl if="countries.length &gt; 1">\
							<a class="button" href="javascript:" onclick="Ext.fly(\'country-support0\').show();">\
						</tpl>\
							<span class="button-left">\
								<img src="/images/new/body_button_left.gif" width="42" height="19" />\
							</span>\
							<span class="button-right">\
								<img src="/images/new/body_button_right.gif" width="41" height="19" />\
							</span>\
							<span class="button-text">{visitSupport}</span>\
						</a>\
						\
						\ <!-- Show this if there are two downloads --> \
						\
						<tpl if="values.downloads.length &gt; 1">\
								<td style="position: relative;">\
									<tpl if="countries.length &gt; 1">\
										<div id="country-support1" class="country-support-wrapper">\
											<div class="country-support">\
												<div style="position:absolute;top:0px;right:0px;"><a style="color: #ffffff" href="javascript:" onclick="Ext.get(\'country-support1\').hide();">[X]</a></div>\
												<tpl for="countries">\
													<a href="javascript:" onclick="window.open(\'{[parent.countries[xindex-1].getHelpLink(parent.downloads[1].sku)]}\');" class="button">\
														<span class="button-left">\
															<img src="/images/new/body_button_left.gif" width="42" height="19" />\
														</span>\
														<span class="button-right">\
															<img src="/images/new/body_button_right.gif" width="41" height="19" />\
														</span>\
														<span class="button-text">{name}</span>\
													</a>\
													<br />\
												</tpl>\
											</div>\
										</div>\
									</tpl>\
									<div class="dvdclick-text">{dvdclick}</div><br />\
									<a href="javascript:download(\'{[values.downloads[1].filepath]}\');" onclick="download(\'{[values.downloads[1].filepath]}\');return false;" class="button">\
										<span class="button-left">\
											<img src="/images/new/body_button_left.gif" width="42" height="19" />\
										</span>\
										<span class="button-right">\
											<img src="/images/new/body_button_right.gif" width="41" height="19" />\
										</span>\
										<span class="button-text">{download}</span>\
									</a>\
									<br />\
									<tpl if="countries.length == 1">\
										<a class="button" href="javascript:" onclick="window.open(\'{helpLink1_sku2}\');">\
									</tpl>\
									<tpl if="countries.length &gt; 1">\
										<a class="button" href="javascript:" onclick="Ext.fly(\'country-support1\').show();">\
									</tpl>\
										<span class="button-left">\
											<img src="/images/new/body_button_left.gif" width="42" height="19" />\
										</span>\
										<span class="button-right">\
											<img src="/images/new/body_button_right.gif" width="41" height="19" />\
										</span>\
										<span class="button-text">{visitSupport}</span>\
									</a>\
								</td>\
							</tr></table>\
						</tpl>\
					</div>\
				</tpl>\
				</div>\
				')
			
				,'copyright':									new Ext.Template('&trade; &amp; &copy; {year} Warner Bros. Entertainment, Inc. {rightsReserved}')
				
				,'privacy-terms':							new Ext.XTemplate('\
																				<tpl if="values.countries.length == 1">\
																					<tpl for="countries">\
																						<div class="onecountry">\
																							<a href="{privacyURL}" target="_blank">{privacyText}<\/a>\
																							<a href="{termsURL}">{termsText}</a>\
																						</div>\
																					</tpl>\
																				</tpl>\
																				<tpl if="values.countries.length &gt; 1">\
																					<div class="twocountries">\
																						<div>\
																							{[values.countries[0].privacyText]}\
																							<tpl for="countries">\
																								<a href="{privacyURL}" target="_blank"><img src="{flagURL}" border="0" alt="{country}" /></a>\
																							</tpl>\
																						<\/div>\
																						<div>\
																							{[values.countries[0].termsText]}\
																							<tpl for="countries">\
																								<a href="{termsURL}"><img src="{flagURL}" border="0" alt="{country}" /></a>\
																							</tpl>\
																						<\/div>\
																					<\/div>\
																				</tpl>\
																			')
	}
		
	
	var changeCountryInfo = function(country) {
		Ext.select("td.items>div").setVisibilityMode(Ext.Element.DISPLAY).hide();
		Ext.select("table#multi td.selected").removeClass("selected");
		Ext.fly('country-info-'+country).show();
		Ext.fly('country-click-'+country).parent().addClass('selected');
	}
		
	if(downloadable.length > 1 && format != 'bd-dvd') downloadable = [downloadable[0]];
	
	var ecommerce		= (price == 0 || price == "")?false:true;
	
	var requestURL	= '';
	if(country[0]) {
		var c = (/gb/i.test(country[0]))?'uk':country[0];
		requestURL = "/lang/" + c + ".json";
	}

	var ecommerceNext = function() {
		Ext.fly('ecommerce').hide();
		Ext.fly('content').show();
	}

	Ext.onReady(function() {
		//set opacity of background, or hide it
		try {
			Ext.fly('content-bg').setOpacity(0.65);
		} catch(e) {
			if(console && console.error) console.error(e);	
		}
		if(hideWhiteBlock) Ext.fly('content-bg').hide();
		
/*		//test for expiration date
		var today = new Date();
		expirationDate = Date.parseDate(expirationDate,"n/j/Y");
		if(parseInt(expirationDate.format("U")) < parseInt(today.format("U"))) {
			Ext.fly('non-pc').remove();
			Ext.fly('content').remove();
			Ext.fly('ecommerce').remove();
			Ext.fly('expired').setStyle({
				marginTop:	'-' + (Ext.fly('expired').getHeight()/2) + 'px'
			});
			Ext.fly('content-wrapper').setStyle({
				visibility:	'visible'
			});
			return;
		}
*/		
		//test for non-windows
		//if(/override/i.test(location.hash))
			//alert("overriede is in the hash");
		
		if(/Windows/i.test(navigator.userAgent) || /override/i.test(location.hash)) {
			Ext.fly('non-pc').remove();
			Ext.fly('expired').remove();
		} else {
			
			Ext.fly('content').remove();
			Ext.fly('ecommerce').remove();
			Ext.fly('non-pc').setStyle({
				marginTop:	'-' + (Ext.fly('non-pc').getHeight()/2) + 'px'
			});
			Ext.fly('content-wrapper').setStyle({
				visibility:	'visible'
			});
			return;
		}
		
		//declaration of var sites for Destination site url data
		var destinationsiteURL;
		
		
		var currentURL = document.location.href.split(".");
		var currentDomain= currentURL[0].charAt(7);
		var trackDestinationSiteDataURL="";
		//current domain should yield either 'w'=live;'p'=preview/preview2;'d'=dev 
		switch(currentDomain)
		{
		case 'd':	trackDestinationSiteDataURL = 'http://dev.wbdigitalcopy.com/lang/destinationsites.json';						
			break;
		case 'p':	trackDestinationSiteDataURL = 'http://preview.wbdigitalcopy.com/lang/destinationsites.json';						
			break;
		case 'w':	trackDestinationSiteDataURL = 'http://www.wbdigitalcopy.com/lang/destinationsites.json';						
			break;
			
		default:
		}//end switch
		
		Ext.Ajax.request({
			// url:			'../lang/destinationsites.json'
			url:			trackDestinationSiteDataURL			
			,asynchronous: false
			,method:		'get'
			,success:		function(responseObj) {
			
			var sites = Ext.util.JSON.decode(responseObj.responseText);
			sites = sites.destinationsites;
			destinationsiteURL = js_traverse(sites);
			loadContent();
		}
		});
		
		function js_traverse(o) {
			var str;
			var type = typeof o 
		    if (type == "object") {
		        for (var key in o)
		        {
			            
		        	if(key.indexOf('_') == 2)
			            {	
		        			//str =key.replace('_','-');
		        			  str =key.replace(/_/g, "-");
			            }else{str = key}
			        	//if(console && console.info){ console.info("country lang= "+countrylang +" str"+ str)}
			        	if(str == countrylang)
			        	{
			        		//alert("found " + str + " that title" + o[key]);
			        		return o[key];
			        	}
			        	
			        	js_traverse(o[key])
		        }
		    }
		   
			return "";
		}
		function datecountryformat(m,d,y, country){
			dstr = '';
			
			     if(country == 'us'){dstr= m +'/'+d+'/'+y;}//United States
			else if(country == 'ca'){dstr= m +'/'+d+'/'+y;}//Canada
			else if(country == 'mx'){dstr= d +'/'+m+'/'+y;}//Mexico 
			else if(country == 'br'){dstr= d +'/'+m+'/'+y;}//Brazil 
			else if(country == 'gb'){dstr= d +'/'+m+'/'+y;}//United Kingdom
			else if(country == 'ie'){dstr= d +'/'+m+'/'+y;}//Ireland
			else if(country == 'de'){dstr= d +'/'+m+'/'+y;}//Germany 
			else if(country == 'at'){dstr= d +'/'+m+'/'+y;}//Austria 
			else if(country == 'be'){dstr= d +'/'+m+'/'+y;}//Netherlands-Belgium
			else if(country == 'nl'){dstr= d +'/'+m+'/'+y;}//Netherlands-Holland 
			else if(country == 'it'){dstr= d +'/'+m+'/'+y;}//Italy
			else if(country == 'es'){dstr= d +'/'+m+'/'+y;}//Spain
			else if(country == 'dk'){dstr= d +'/'+m+'/'+y;}//Denmark 
			else if(country == 'no'){dstr= d +'/'+m+'/'+y;}//Norway
			else if(country == 'se'){dstr= d +'/'+m+'/'+y;}//Sweden 
			else if(country == 'fi'){dstr= d +'/'+m+'/'+y;}//Finland 
			else if(country == 'ch'){dstr= d +'/'+m+'/'+y;}//Switzerland
			else if(country == 'au'){dstr= d +'/'+m+'/'+y;}//Austrailia
			else if(country == 'nz'){dstr= d +'/'+m+'/'+y;}//New Zealand
			else if(country == 'jp'){dstr= y +'/'+m+'/'+d;}//Japan
			else{dstr= m +'/'+d+'/'+y;}
			return dstr;
		}

		if(requestURL == "") return;
		
		function loadContent(){
		Ext.Ajax.request({
			
			 url:				requestURL
			,method:		'get'
			,asynchronous: false
			,success:		function(responseObj) {
			
				try {
					
					if(ecommerce) {
						Ext.fly('content').hide();
					} else {
						Ext.fly('ecommerce').remove();
					}
					//format the expiration date to coinciding country
					
					if(expirationDate != ''){
					var formattedexpirationDate = '';
					formatdate = expirationDate.split('/');
					//Month
					fmonth = formatdate[0];
					//Day
					fday = formatdate[1];
					//Year
					fyear = formatdate[2];
					formattedexpirationDate = datecountryformat(fmonth,fday,fyear,countrylang);
					}
					//grab the correct data and throw it in
								
					var interpolationData = {
						 Title: title.replace(/(')/,"\\$1")
						,TITLE: title.replace(/(')/,"\\$1").toUpperCase()
						,DESTINATIONSITE : destinationsiteURL
						,price:	price
						,ecommerceNext: 'javascript:ecommerceNext();'
						,format: format.toUpperCase()
						,EXPIREDATE : formattedexpirationDate
						
						
					};
					var text = responseObj.responseText;
					
					//prior to parsing the string into an object, interpolate it!
					try {
						var tt		=	new Ext.Template(text);
						var re		=	tt.re.toString();
						re				=	'\\#' + re.slice(1, re.length-2);
						tt.re			=	new RegExp(re, 'g');
					} catch(e) {
						if(console && console.error) console.error(e);
					}
					text = tt.apply(interpolationData);
					//if(console && console.info) console.info("done Interpolating DATA");
					
					
					text = Ext.util.JSON.decode(text);
					text = text.text;

					var d			= new Date();
					text.year = d.format('Y');
					
					text.country1					= '';
					text.country2					= '';
					text.tout							= tout;
					//text.tout							= 'https://dcm.warnerbros.com/images/client/17Again/large.jpg';
										
					var countryHelpLinks = '';
					
					for(var i=0; i<country.length; i++) {
						
						//get country name to be uk when gb for lookups in countryData
						var _country	=	(/gb/i.test(country[i]))	?	'uk'	:	country[i];
						
					
						var c = new WB.Country({
						
							 lang:						lang[i]
							,country:					country[i]
							,name:						countryData[_country].country_native
							,privacyURL:			countryData[_country].privacyURL
							,privacyText:			countryData[_country].privacyText
							,termsURL:				countryData[_country].termsURL
							,termsText:				countryData[_country].termsText
							,rightsReserved:	countryData[_country].rightsReserved
						
						});
						//alert(countryData[_country].country_native);
						var e = (i>0)?(i+1):'';
						
						//loop downloadables
						for(var j=0; j<downloadable.length; j++) {
							var je = j+1;
							text['helpLink' + (i+1) + '_sku' + je]	= (country[i])?'/support/?lang='+lang[i]+'#home-content&discid='+downloadable[j]['sku']+'&country='+country[i]:'';
							text['helpLink' + (i+2) + '_sku' + je]	= (country[(i+1)])?'/support/?lang='+lang[(i+1)]+'#home-content&discid='+downloadable[j]['sku']+'&country='+country[(i+1)]:'';
							text['download' + je + '_path']					= downloadable[j]['filepath'];
							text['download' + je + '_sku']					= downloadable[j]['sku'];
						}
						
						//at this point, country can be replaced by uk if gb
						if(/gb/i.test(country[i])) country[i] = 'uk';
						
						if(countryData[country[i]]) {
							text['country' + (i+1)]			= countryData[country[i]].country_native;
							text['privacyURL' + e]			= (countryData[country[i]].privacyURL && countryData[country[i]].privacyURL != "")?countryData[country[i]].privacyURL:'/support/#privacy-content&country='+country[i];
							text['privacyText' + e]			= countryData[country[i]].privacyText;
							text['termsURL' + e]				= (countryData[country[i]].termsURL && countryData[country[i]].termsURL != "")?countryData[country[i]].termsURL:'/support/#terms-content&country='+country[i];
							text['termsText' + e]				= countryData[country[i]].termsText;
							text['rightsReserved' + e]	= countryData[country[i]].rightsReserved;
							text['flagURL' + e]					= flagURLTemplate.apply({lang: country[i]});							
						}
					}
					
//IF EXPIRED
					text['expired'] = 0;
					
					if(!Ext.isEmpty(expirationDate)) {
						
						//replace instructions_1sku and instructions_2sku with expiration text
						var today = new Date();
						expirationDate = Date.parseDate(expirationDate,"n/j/Y");
						
						
						if(/override/i.test(location.hash) != true)
						if(expirationDate != undefined && parseInt(expirationDate.format("U")) < parseInt(today.format("U"))) {
							text['instructions_1sku'] = expirationMsg;
							text['instructions_2sku'] = expirationMsg;
							text['subheader_1sku'] = '';
							text['subheader_2sku'] = '';
							text['disclaimer'] = '';
							text['expired'] = 1;
						}
						
						
					}
					
					if(Ext.get('ecommerce')) {
						text.header_1sku = text.header_ec;
						text.subheader_1sku = text.subheader_ec;
						Ext.fly('ecommerce').update( text.instructions_ec );
					}
					var template = templates['main'];
					Ext.fly('footer-content').update( templates['privacy-terms'].apply({countries: WB.Country.prototype.countries}) );
					Ext.fly('copyright').update( templates['copyright'].apply(text) );

					if(!Ext.isEmpty(altClickText[0])) {
						Ext.apply(text, {
							bdclick:	altClickText[0]
						});
					}
					
					if(!Ext.isEmpty(altClickText[1])) {
						Ext.apply(text, {
							dvdclick:	altClickText[1]
						});
					}

					//if(WB.Country.prototype.countries.length > 2) {
					
					Ext.apply(text, {
							 countries:	WB.Country.prototype.countries
							,downloads:	downloadable
							,destination : destinationsiteURL
							//,expires : expirationDate
						});
					//	Ext.fly('content').update( templates['multi'].apply(text) );
					//} else {
						Ext.fly('content').update( template.apply(text) );
					//	if(console && console.info) console.info("done writing to main:Template");
					//}
						

					//increase the overall outer object height if it doesn't fit the inner height
					var contentHeight = Ext.fly('content').getHeight();
					var wrapperHeight = Ext.fly('content-wrapper').getHeight();
					var newHeight			= (contentHeight > wrapperHeight)?contentHeight:wrapperHeight;

						Ext.fly('content-wrapper').setStyle({
							height:	newHeight + 'px'
						});

						//need to subtract top and bottom padding
						Ext.fly('content-bg').setStyle({
							height: (newHeight-40) + 'px'
						});

						Ext.fly('content-shadow-right').setStyle({
							height:	(newHeight-5) + 'px'
						});

					if(ecommerce && Ext.get('ecommerce')) {
						var ecommerceHeight = Ext.fly('ecommerce').getHeight();
						Ext.fly('ecommerce').setStyle({
							marginTop:	'-' + ecommerceHeight/2 + 'px'
						});
					}
					
					//hide country support
					Ext.select('.country-support-wrapper').hide();
					
					//SHOW THE MAIN BLOCK
					Ext.fly('content-wrapper').setStyle({
						visibility:	'visible'
					});
					
					if(destinationsiteURL != '' && destinationsiteURL != undefined)
					{
						
						Ext.onReady(function() {
							
							//remove useMap
							if(document.images['logomap'])
							{
							document.images['logomap'].useMap = '';
							}
							var logoDiv = Ext.get('logo');
						    logoDiv.addClass('pointer');
						   
						    var tmpurl = destinationsiteURL.split('#');
						    
						    logoDiv.on('click', function() {
						    	visit(tmpurl[0]);
						    });
						   
						});
					}				
					
					

				} catch(e) {
//					if(console != undefined) {
//						if(console.error) console.error(e);
//					} else {
for(var err in e) {
	alert(err + ': ' + e[err]);
}
		//				alert(e.msg);
//					}
				}
			} //eo Ajax success
		}); //eo Ext.Ajax.Request
	}
	}); //eo Ext.onReady
