
var FO_hot = { movie:"whatsnew.swf", width:"930", height:"501", majorversion:"8", build:"0", bgcolor:"#FFFFFF", flashvars:"find_file=find.asp&submit_file=signup&submit_ext=asp&field0=email&field1=postcode" };
var FO_home = { movie:"home.swf", width:"930", height:"413", majorversion:"8", build:"0", bgcolor:"#FFFFFF", flashvars:"wn_file=hot_arrivals.asp&submit_file=signup&submit_ext=asp&field0=email&field1=postcode" };
var FO_main = { movie:"image_display.swf", width:"930", height:"413", majorversion:"8", build:"0", bgcolor:"#FFFFFF"  };
var FO_map = { movie:"store_locator.swf", width:"336", height:"365", majorversion:"7", build:"0", wmode:"transparent", flashvars:"search_file=find1&search_ext=asp&search_name=region" };
var FO_crvideo = { movie:"cr_video.swf", width:"320", height:"268", majorversion:"7", build:"0", bgcolor:"#cccccc" };


TKM_flash = {
	hot: 'main_hotarrivals',
	home: 'main_home_flash',
	main: 'main_flash',
	teaser: 'teaser_flash',
	map: 'widget_map',
	crvideo: 'cr_video',
	
	extra_vars: {},
	
	init: function(fo_obj, html_id, section_data){
		if (section_data) {
			// Specify some data for our swf
			// The random querystring it to reduce caching confilicts
			this.extra_vars.data_file = section_data+"_data.xml?"+ Math.random();
		}
		if (window.location.hash) {
			// For jumping to a slide in "whats new"
			this.extra_vars.id_str = window.location.hash.substr(1);
		}
		
		// Add the flashvars we just created, plus any others that are in extra_vars 
		this.addVars(fo_obj, this.extra_vars);
		
		// Do some Flash version checking and let the brill UFO do it's work
		this.showVersion(fo_obj, html_id);
	},

	addVars: function(fo_obj, vars_obj){
		var _fv = fo_obj.flashvars;
		var _pairs = "";
		var _count = 0;
		
		for (key in vars_obj) {
			_pairs += key + "=" + vars_obj[key] + "&";
			_count++;
		}
		if (_count != 0) {	
			if (_fv == null) {
				_pairs = _pairs.replace(/&$/,"");
				fo_obj.flashvars = _pairs;
			}
			else {
				fo_obj.flashvars = _pairs + _fv;
			}
		}
		return;
	},
	
	showVersion:function(fo_obj, html_id){
		UFO.getFlashVersion();
		if (UFO.hasFlashVersion(fo_obj.majorversion, fo_obj.build)) {
			UFO.create(fo_obj, html_id);	// Show latest and greatest
		}
		else {
			var _l = fo_obj.movie.length - 4;
			var _mov_trunk = fo_obj.movie.substr(0,_l);
			
			fo_obj.movie = _mov_trunk + "_f7.swf";
			fo_obj.majorversion = "7";

			UFO.create(fo_obj, html_id);	// Try and show degraded Flash 7
		}
	}	
}


Array.prototype.inArray = function (value) {
	var i;
	for (i=0; i < this.length; i++) {
		if (this[i] === value) {
			return true;
		}
	}
	return false;
};



function addEvent( obj, type, fn ) {
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
		EventCache.add(obj, type, fn);
	}
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
		EventCache.add(obj, type, fn);
	}
	else {
		obj["on"+type] = obj["e"+type+fn];
	}
}
	
var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler){
			listEvents.push(arguments);
		},
		flush: function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				item[0][item[1]] = null;
			};
		}
	};
}();


function stopDefaultAction(ev) {
	if (!ev) ev = window.event;
	(ev.stopPropagation) ? ev.stopPropagation() : ev.cancelBubble = true;
	(ev.preventDefault) ? ev.preventDefault() : ev.returnValue = false;
	return false;
}


function getElementsByClass(searchClass,node,tags) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tags == null )
		tags = '*';
	
	/* Ideally this would be an isArray function, though this will do as long as you pass an array or a single tag as a string */
	if (typeof(tags) == 'object' ){
		var els = new Array();
		for (var t = 0; t < tags.length; t++){
			/* This is a bit of a kludge, but since getElementsByTagName returns a nodeList, we cannot use array.concat() */
			var get_tag = node.getElementsByTagName(tags[t]);
			for (var g=0;g<get_tag.length;g++){
				els.push(get_tag[g]);
			}
		}
	}else{
		var els = node.getElementsByTagName(tags);
	}
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}



/* Fills input['text'] boxes with the content of their title attr. Disappears onFocus, then returns onBlur */
var InputTips = {
	init: function () {	
		if (!document.getElementsByTagName) return false;
		var inputs = document.getElementsByTagName("input");
		for (var i=0;i<inputs.length;i++) {
			if (inputs[i].getAttribute("type") == "text") {
				if (inputs[i].getAttribute("title") && inputs[i].getAttribute("title").length > 0) {
					inputs[i].value = inputs[i].getAttribute("title");
					InputTips.events(inputs[i]);
				}
			}
		}
	},
	events: function(obj) {
		addEvent(obj,'focus',InputTips.focus);
		addEvent(obj,'blur',InputTips.blur);
	},
	focus: function(ev) {
		if (!ev) ev = window.event;
		var eventElement = (window.event) ? window.event.srcElement : ev.target;
		if (eventElement.value == eventElement.getAttribute("title")) {
			eventElement.value = "";
		}
	},
	blur: function(ev) {
		if (!ev) ev = window.event;
		var eventElement = (window.event) ? window.event.srcElement : ev.target;
		if (eventElement.value.length < 1) {
			eventElement.value = eventElement.getAttribute("title");
		}
	}
};


var PopUp = {
	newWindow: null,
	close: function() {
		if (this.newWindow != null){
			if(!this.newWindow.closed)
				this.newWindow.close();
		}
	},
	show: function(url, type, strWidth, strHeight){
		this.close();
		if (type == "fullScreen"){
			strWidth = screen.availWidth - 10;
			strHeight = screen.availHeight - 160;
		}
		var tools="";
		if (type == "standard" || type == "fullScreen") tools = "resizable,toolbar=yes,location=yes,scrollbars=yes,menubar=yes,width="+strWidth+",height="+strHeight+",top=0,left=0";
		if (type == "console") tools = "resizable,toolbar=no,location=no,scrollbars=yes,width="+strWidth+",height="+strHeight+",left=0,top=0";
		this.newWindow = window.open(url, 'newWin', tools);
		this.newWindow.focus();
	}
};


addEvent(window,'unload',EventCache.flush);
addEvent(window,'load',InputTips.init);


	function submitLocator(caller){
		var town = caller.town.value;
		//var postcode = caller.postcode.value;
		var country;
		for (var i=0; i < caller.country.length; i++)
        {
           if (caller.country[i].checked){
                 country = caller.country[i].value;
           }
        }
		
		if ( town == "")
		{
			var alert_msg = "Please enter a town or a postcode.";
			window.alert(alert_msg);
		}
		else
		{
			var ccVal = new RegExp("^\\s*charing\\s+cross\\s*$")
			if (ccVal.test(town.toLowerCase())) {
				town += " rd";
			}
			caller.geolat.value = "";
			caller.geolong.value = "";
			eurGeocode(caller,town + " " + country);
			//eurGeocode(caller,town + " " + country);
		}			
		return false;				
	}
	function eurGeocode(frm,locToGeocode) { 
		if (GBrowserIsCompatible()) {
			var errmsg = "We apologise.  We're not able to find the town/postcode.  Please try again";
			var geocoder = new GClientGeocoder();
			geocoder.getLocations( locToGeocode, 
							function(response) {
								if ( !response || response.Status.code != 200 ) {
									alert(errmsg);
								} else {
									var place = response.Placemark[0];
									if (place.AddressDetails.Country.CountryNameCode == "GB") place.AddressDetails.Country.CountryNameCode="UK";
									if (place.AddressDetails.Country.CountryNameCode != locToGeocode.substr(locToGeocode.length - 2,2)) {
										alert(errmsg);
									}
									else {
										frm.geolat.value = place.Point.coordinates[1];
										frm.geolong.value = place.Point.coordinates[0];	
										frm.submit();
									}
								}
							});
		} 
		else {
			alert("We apologize. Your browser is not compatible with Google Maps.");
		}
	}
