function listenAudio(url) {
	var newwindow = window.open(url, 'name', 'height=30,width=300');
	if (window.focus) {newwindow.focus()}
	return false;
}


    var search_type = ['borough', 'type', 'boroughs'].associate(['locations', 'categories', 'counties']);
	var unselected = ['Select a borough/district', 'Select a sub-category', 'Select a borough/district'].associate(['locations', 'categories', 'counties']);

    var Contact = new Class({
	  cur_form: -1,
	  form: ['general_enquiry', 'tangles', 'franchise', 'bug_report', 'cashback', 'listing', 'event'],
      initialize: function(){
        return;
      },
	  toggle: function(n) {
        if (this.cur_form != -1) {
          $(this.form[this.cur_form]).setStyle('display', 'none');
	      $(this.form[this.cur_form] + '_btn').removeClass('close');
		  $(this.form[this.cur_form] + '_btn').addClass('open');
        }

        if (this.cur_form != n) {	  
	      this.cur_form = n;
          $(this.form[n]).setStyle('display', 'block');
	      $(this.form[n] + '_btn').removeClass('open');
	      $(this.form[n] + '_btn').addClass('close');
		  $('error_' + this.form[n]).setStyle('display', 'none');
		} else {
		  this.cur_form = -1;
		}
	  }
    });
	var contact = new Contact();
	
	function goto_category() {
	  document.location.href = $('guide_list').options[$('guide_list').selectedIndex].value;
	  return;
	}
	
	function disable_search(status) {
		var county = document.getElementById('county');
		var borough = document.getElementById('borough');
		var category = document.getElementById('category');
		var type = document.getElementById('type');
		
		var saved_search_preloader = document.getElementById('saved_search_preloader');
		
		if (status) {
			county.disabled = true;
			borough.disabled = true;
			category.disabled = true;
			type.disabled = true;
			saved_search_preloader.style.display = 'block';
		} else {
			county.disabled = false;
			borough.disabled = false;
			category.disabled = false;
			type.disabled = false;
			saved_search_preloader.style.display = 'none';
		}
	}
	
	function display_saved_search(step) {	
		if (!continue_step) {
			return;
		}
		
		switch (step) {
			case 1:
				var county = document.getElementById('county');
				if (county) {
					var i = getCookie('county[id]');
					var name = getCookie('county[name]');
					if (i > 0) {
						//disable_search(true);
						county_selectedIndex = 0;
						for (x = 0; x < county.options.length; x++) {
							if (county.options[x].value == i) {
								county_selectedIndex = x;
								//break;
							}
						}
						if (county_selectedIndex > 0) {
							county.options[county_selectedIndex].selected = true;
							update_search_list(county, 'locations', 'borough');
						}
					}
				}
			break;
			
			case 2:
				var borough = document.getElementById('borough');
				if (borough) {
					var i = getCookie('borough[id]');
					var name = getCookie('borough[name]');
					if (i > 0) {
						borough_selectedIndex = 0;
						for (x = 0; x < borough.options.length; x++) {
							if (borough.options[x].value == i) {
								borough_selectedIndex = x;
								break;
							}
						}
						if (borough_selectedIndex > 0) {
							borough.options[borough_selectedIndex].selected = true;
							display_saved_search(3);
						}
					}
				}
			break;
			
			case 3:
				var category = document.getElementById('category');
				if (category) {
					var i = getCookie('category[id]');
					var name = getCookie('category[name]');
					if (i > 0) {
						category_selectedIndex = 0;
						for (x = 0; x < category.options.length; x++) {
							if (category.options[x].value == i) {
								category_selectedIndex = x;
								break;
							}
						}
						if (category_selectedIndex > 0) {
							category.options[category_selectedIndex].selected = true;
							update_search_list(category, 'categories', 'type');
						}
					}
				}
			break;
			
			case 4:
				var type = document.getElementById('type');
				if (type) {
					var i = getCookie('type[id]');
					var name = getCookie('type[name]');
					if (i > 0) {
						type_selectedIndex = 0;
						for (x = 0; x < type.options.length; x++) {
							if (type.options[x].value == i) {
								type_selectedIndex = x;
								break;
							}
						}
						if (type_selectedIndex > 0) {
							type.options[type_selectedIndex].selected = true;
							//disable_search(false);
						}
					}
				}
			break;
			
			default:
			break;
		}
	}
	
	var current_step = 1;
	var continue_step = false;
	
	function update_search_list(obj, type, target_id) {
	  url = template_url + '/pp-svc.php';
	  id  = obj.options[obj.selectedIndex].value;
	  
	  target = (target_id == null) ? search_type[type] : target_id;
		
	  // clear options
	  var opts = $(target).getElements('option');
      opts.each(function(item, index, array) { item.dispose(); });
	  if (id == -1) {
		var opt = new Element('option', {value: -1, text: '<--------------'}).inject($(target), 'bottom');
	  } else {
		var opt = new Element('option', {value: -1, text: 'Please wait...updating list'}).inject($(target), 'top');
	    var rq = new Request(
														{
															method: 'get', 
															url: url, 
															onSuccess: function(text, xml) {
	      												$(target).getElements('option').each(function(item, index, array) { item.dispose(); });
          											var opt = new Element('option', { value: -1, text: unselected[type] }).inject($(target), 'bottom');
																
		  													text.split('|').each(function(item, index) {
		    													value = item.split(';');
																	v = (type == 'categories') ? value[0] : value[0];
            											var opt = new Element('option', { value: v, text: value[1] }).inject($(target), 'bottom');
		  													});
																//
																if ( (boroughs_nr > 1) && (target_id == 'borough_list') ) {
																	//$('include_list').
																	$(target).getElements('option').each(function(item, index, array) { item.dispose(); });
																	boroughs_nr = 1;
																}
															}
														} 
													);
		
			id = (type == 'counties') ? id.split('|')[0] : id;
	    rq.send('id=' + id + '&svc=' + type);
			
    }
	  return;
	}
	
	function continue_to_step() {
		if (continue_step) {
			current_step = current_step + 1;
			display_saved_search(current_step);
			if (current_step >= 4) {
				continue_step = false;
			}
		}
	}
	
	function update_multi_list(obj, type, target_id) {
	  url = template_url + '/pp-svc.php';
	  id  = obj.options[obj.selectedIndex].value;
	  
	  target = (target_id == null) ? search_type[type] : target_id;

	  // clear options
	  var opts = $(target).getElements('option');
      opts.each(function(item, index, array) { item.dispose(); });
	  
	  if (id == -1) {
		var opt = new Element('option', {value: -1, text: '<--------------'}).inject($(target), 'bottom');
	  } else {
		var opt = new Element('option', {value: -1, text: 'Please wait...updating list'}).inject($(target), 'top');
	    var rq = new Request({method: 'get', url: url, onSuccess: function(text, xml) { 
	      $(target).getElements('option').each(function(item, index, array) { item.dispose(); });
          //var opt = new Element('option', { value: -1, text: unselected[type] }).inject($(target), 'bottom');
		  text.split('|').each(function(item, index) {
		    value = item.split(';');
			v = (type == 'categories') ? value[1] : value[0];
            var opt = new Element('option', { value: v, text: value[1] }).inject($(target), 'bottom');
		  });		
		}} );
		
		id = (type == 'counties') ? id.split('|')[0] : id;
	    rq.send('id=' + id + '&svc=' + type);
      }
	  return;
	}
	
	function validateFdo(o) {
		var fdo_county = o.county;
		if (fdo_county.options[fdo_county.selectedIndex].value == -1){
			new Alert('You have not selected a county!');
			return false;
		}
		
	  var fdo_type = o.family_day_out_category;
	  
	  if (fdo_type.options[fdo_type.selectedIndex].value == -1) {
	    new Alert('You have not selected a family day out category!');
		return false;
	  }
	  return true;
	}
	function validateSearchDirectory(o){
		var fdo_county = o.county;
		if (fdo_county.options[fdo_county.selectedIndex].value == -1){
			new Alert('You have not selected a county!');
			return false;
		}
		var fdo_borough = o.borough;
		if (fdo_borough.options[fdo_borough.selectedIndex].value == -1){
			new Alert('You have not selected a borough!');
			return false;
		}
		var fdo_category = o.category;
		if (fdo_category.options[fdo_category.selectedIndex].value == -1){
			new Alert('You have not selected a category!');
			return false;
		}
		var fdo_type = o.type;
		if (fdo_type.options[fdo_type.selectedIndex].value == -1){
			new Alert('You have not selected a type!');
			return false;
		}
		return true;
	}
	var Overlay = new Class({
	  overlay: null, 
	  
	  initialize: function() {
		this.overlay = new Element('div', { id: 'modal_overlay'}).inject(document.body, 'bottom');
		return this.overlay;
	  },
	  
	  destroy: function() {
		if (this.overlay != null) {
	      this.overlay.dispose();
		  this.overlay = null;
		}
	  }
	});
	
	var Alert = new Class({
	  initialize: function(msg) {
	    this.overlay = new Overlay();
		this.horizon = new Element('div', { id: 'alert_horizon'}).inject(this.overlay, 'bottom');
		this.content = new Element('div', { id: 'alert_win'}).setStyle('opacity', '0').inject(this.horizon, 'bottom');
		this.dismiss = new Element('a', { id: 'alert_dismiss', href: '#'}).inject(this.content, 'bottom').addEvent('click', this.close.bind(this));
		this.top = new Element('div', { id: 'alert_top', text: 'Alert'}).inject(this.content, 'bottom');
		this.text = new Element('div', { id: 'alert_message', text: msg}).inject(this.content, 'bottom');
		this.bottom = new Element('div', { id: 'alert_close'}).inject(this.content, 'bottom');
		this.button = new Element('input', {id: 'alert_btn', value: 'Close', type: 'button'}).inject(this.bottom, 'bottom').addEvent('click', this.close.bind(this));
		this.content.set('tween', {duration: 750});
		this.content.fade('in');
	  },
	  
	  close: function() {
		this.content.fade('out');
        this.destroy();
		return false;
	  },
	  
	  destroy: function() {
		  if (this.overlay != null) this.overlay.dispose();
       }
	  
	});
	
function getCookie(c_name) {
	if (document.cookie.length > 0) {
  	var c_start = document.cookie.indexOf(c_name + "=");
  	if (c_start != -1) {
    	c_start = c_start + c_name.length + 1;
    	c_end = document.cookie.indexOf(";", c_start);
    	if (c_end==-1) {
				c_end=document.cookie.length;
			}
			var _return = unescape(document.cookie.substring(c_start,c_end));
    	return _return.replace('+', ' ');
    }
  }
	return "";
}

function strip_tags (str, allowed_tags) { 
    var key = '', allowed = false;
    var matches = [];
    var allowed_array = [];
    var allowed_tag = '';
    var i = 0;
    var k = '';
    var html = '';
 
    var replacer = function (search, replace, str) {
        return str.split(search).join(replace);
    };
 
    // Build allowes tags associative array
    if (allowed_tags) {
        allowed_array = allowed_tags.match(/([a-zA-Z0-9]+)/gi);
    }
 
    str += '';
 
    // Match tags
    matches = str.match(/(<\/?[\S][^>]*>)/gi);
 
    // Go through all HTML tags
    for (key in matches) {
        if (isNaN(key)) {
            // IE7 Hack
            continue;
        }
 
        // Save HTML tag
        html = matches[key].toString();
 
        // Is tag not in allowed list? Remove from str!
        allowed = false;
 
        // Go through all allowed tags
        for (k in allowed_array) {
            // Init
            allowed_tag = allowed_array[k];
            i = -1;
 
            if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+'>');}
            if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+' ');}
            if (i != 0) { i = html.toLowerCase().indexOf('</'+allowed_tag)   ;}
 
            // Determine
            if (i == 0) {
                allowed = true;
                break;
            }
        }
 
        if (!allowed) {
            str = replacer(html, "", str); // Custom replace. No regexing
        }
    }
 
    return str;
}

function countWords(obj) {
	var nr = 0;
	
	var content = obj.value;
	var cnt = '';
	
	cnt = content.replace( /<.[^<>]*?>/g, ' ' ).replace( /&nbsp;/gi, ' ' );
	cnt = content.replace( /[0-9.(),;:!?%#$¿'"_+=\\/-]*/g, '' );
	cnt.replace( /\S\s+/g, function(){ nr++; } );
	
	$('excerpt_words_count').innerHTML = nr;
}

Panels = {
	panels: new Array(),
	imgs: new Array(),
	incs: new Array(),
	time_out: null,
	
	loadArrays: function() {
		this.panels[0] = 'panel_top_left';
		this.panels[1] = 'panel_bottom_left';
		this.panels[2] = 'panel_top_right';
		this.panels[3] = 'panel_bottom_right';
		this.panels[4] = 'panel_middle_top';
		this.panels[5] = 'panel_middle_bottom';
		
		
		this.imgs[0] = 'img_top_left';
		this.imgs[1] = 'img_bottom_left';
		this.imgs[2] = 'img_top_right';
		this.imgs[3] = 'img_bottom_right';
		this.imgs[4] = 'img_middle_top';
		this.imgs[5] = 'img_middle_bottom';
		
		this.incs[0] = typeof(panels_info[0]) != 'undefined' ? panels_info[0].length : 0;
		this.incs[1] = typeof(panels_info[1]) != 'undefined' ? panels_info[1].length : 0;
		this.incs[2] = typeof(panels_info[2]) != 'undefined' ? panels_info[2].length : 0;
		this.incs[3] = typeof(panels_info[3]) != 'undefined' ? panels_info[3].length : 0;
		this.incs[4] = typeof(panels_info[4]) != 'undefined' ? panels_info[4].length : 0;
		this.incs[5] = typeof(panels_info[5]) != 'undefined' ? panels_info[5].length : 0;
	},
	
	initPanels: function(path) {
		if (this.time_out == null) {
			this.loadArrays();
		}
		this.time_out = setTimeout("Panels.loadPanels('" + path + "')", 10000);
	}	,
	
	loadPanels: function(path) {
		//alert(panels_info[0][0]['image']);
		
		for (var i = 0; i < this.panels.length; i++) {
			var panel_len = typeof(panels_info[i]) != 'undefined' ? panels_info[i].length : 0;
			
			if (this.incs[i] < 1) {
				//this.incs[i] = panels_info[i].length;
				this.incs[i] = panel_len;
			}
			
			if (panel_len > 1) {
				$(this.imgs[i]).src = path + panels_info[i][this.incs[i] - 1]['image'];
				var old_obj = this;
				$(this.panels[i]).onclick = function () {
					window.location = panels_info[i][old_obj.incs[i] - 1]['link'];
				}
				this.incs[i]--;
			}
		}
		
		this.initPanels(path);
	}
} // end class