Page = {
    init: function( oContainer, oSearch ) {
        this.oParams = this.getUrlParams();
        if ( !this.oParams.type )
            this.oParams.type = 'All';
				document.body.className=this.oParams.type.toLowerCase();
				document.title +=" "+this.oParams.type;
        new MMHelperText( oSearch, 'Address, Town or Postcode' );
        this.oResultsContainer = document.getElementById( 'MMResults' );
        this.oContainer = oContainer;
        this.oMap = new GMap2(this.oContainer);
		this.oMap.addControl(new GSmallMapControl());
		this.oMap.addControl(new GMapTypeControl());
        this.oMap.enableScrollWheelZoom();
        this.oMap.setCenter(new GLatLng(54.57278,-3.16278), 5);
        this.oDisam = new MMDisambiguation( oContainer, undefined, true );
        this.createLocationSearch( oSearch );
        if ( this.oParams['qs'] && this.oParams['qs'] != '' ) {
    		var qs = this.oParams['qs'].replace(/\53/g," ");
    		var qs = qs.replace(/\%20/g," ");
    		document.getElementById('MMSearchField').value=qs;
            this.LocationSearch.onFormSubmit();
            if ( oSearch.form && oSearch.form.onsubmit )
                oSearch.form.onsubmit();
        }
    },
    createLocationSearch: function( oElement ) {
        var oResultContainer = document.getElementById( 'MMResults' );
        var oOtherResultContainer = document.getElementById( 'MMResults2' );
        Callbacks.init( this.oMap, MarkerFields, HtmlFields, oResultContainer, this.getIcon );
  		OtherCallbacks.init( this.oMap, MarkerFields, HtmlFields, oOtherResultContainer, this.getIcon );
        var oStoreCallback = function() {
            Callbacks.storeSearch( arguments[0], arguments[1] );
        }
         var oOtherStoreCallback = function() {
            OtherCallbacks.OtherStoreSearch( arguments[0], arguments[1] );
        }
        var oDisamHandler = function() {
            Page.oDisam.handleGeocode( arguments[0], arguments[1], arguments[2], arguments[3] );
        }
        this.StoreSearch = new StoreSearch( this.oMap, undefined, 10, this.getFilters, undefined, oStoreCallback );
        this.OtherStoreSearch = new OtherStoreSearch( this.oMap, undefined, 10, this.getOtherFilters, undefined, oOtherStoreCallback );
		this.LocationSearch = new LocationSearch( oElement, 'GB', this.oMap, null, this.StoreSearch,this.OtherStoreSearch, null, oDisamHandler ,this.oParams.type);
        oElement.form.onsubmit = function() {
            var oContainer = document.getElementById( 'MMContainer' );
            oContainer.className = oContainer.className.replace( /\s?MMIntro\s?/g, '' );
  //          Page.oMap.resize( new MMPoint( 0, 0 ) );

            Page.oDisam.setContainerDimensions();
            this.onsubmit = function() {
                return false;
            }
            return this.onsubmit();
        }
    },
    getIcon: function() {
				var counter = arguments[1]+1;
				var marker_path = 'i/coop.marker.' + arguments[0].business.replace( ' ', '' ).toLowerCase() +counter+'.png';
		   		var baseIcon = new GIcon(G_DEFAULT_ICON);
				baseIcon.iconSize = new GSize(37, 44);
				baseIcon.iconAnchor = new GPoint(3, 10);
				baseIcon.infoWindowAnchor = new GPoint(9, 2);
		    	baseIcon.shadow = ""; 
		  		var oIcon = new GIcon(baseIcon);
		  		oIcon.image = marker_path;
		  		oIcon.group_name = 'StoreMarker';
		    	oIcon.cssClass = 'MMMarker MMStoreMarker';
		    	oIcon.iconSize = new GSize( 37, 44 ); 
	
				oIcon.iconAnchor = new GPoint(15, 40);
				oIcon.infoWindowAnchor = new GPoint(9, 2);        
		    return oIcon;
    },
    getUrlParams: function() {
        var urlparams = {};
        try {
            var params = window.location.search.split( '?' )[1].split( '&' );
            for ( var i = params.length-1; i >= 0; i-- ) {
                urlparams[params[i].split( '=' )[0]] = params[i].split( '=' )[1];
            }
        } catch(e) {}
        return urlparams;
    },
    getFilters: function( oSearch ) {
        oSearch.filters = [];
        if ( Page.oParams.type.toLowerCase() == 'all' && !Page.oParams.client_id ) {
            oSearch.filters.push( new MMSearchFilter( 'business', 'not', 'Corporate' ) );
						oSearch.filters.push( new MMSearchFilter( 'business', 'not', 'Private Funerals' ) );
            return oSearch;
        }
        if ( Page.oParams.client_id && Page.addClientIdFilter ) {
            oSearch.filters.push( new MMSearchFilter( 'client_id', 'eq', Page.oParams.client_id ) );
            Page.addClientIdFilter = false;
        } else
        	if (Page.oParams.type.toLowerCase() == 'funerals') {
        		oSearch.logic='OR';
    //        oSearch.filters.push( new MMSearchFilter( 'business', 'eq', 'Private Funerals') );   
            oSearch.filters.push( new MMSearchFilter( 'business', 'eq', 'Funerals' ) );        		
        	} else {
            oSearch.filters.push( new MMSearchFilter( 'business', 'eq', Page.oParams.type ) );
           }

        return oSearch;
    },
    getOtherFilters: function( oSearch ) {
        oSearch.filters = [];
        if ( Page.oParams.type.toLowerCase() == 'all' && !Page.oParams.client_id ) {
            oSearch.filters.push( new MMSearchFilter( 'business', 'not', 'Corporate' ) );
			oSearch.filters.push( new MMSearchFilter( 'business', 'not', 'Private Funerals' ) );
            return oSearch;
        }
        if ( Page.oParams.client_id && Page.addClientIdFilter ) {
            oSearch.filters.push( new MMSearchFilter( 'client_id', 'eq', Page.oParams.client_id ) );
            Page.addClientIdFilter = false;
        } else
        	if (Page.oParams.type.toLowerCase() == 'funerals') {
        		oSearch.filters.logic='OR';
           		oSearch.filters.push( new MMSearchFilter( 'business', 'eq', 'Private Funerals') );   
       //     oSearch.filters.push( new MMSearchFilter( 'business', 'eq', 'Funerals' ) );        		
        	} else {
            oSearch.filters.push( new MMSearchFilter( 'business', 'eq', Page.oParams.type ) );
           }
        return oSearch;
    }
}
function initialize() {
        var oContainer = document.getElementById( 'GGViewer' );
        var oSearch = document.getElementById( 'MMSearchField' );
        Page.init( oContainer, oSearch );
}
Array.prototype.indexOf = function( sSearch ) {
    for ( var i = 0; i < this.length; i++ ) {
        if ( this[i] == sSearch )
            return i;
    }
    return -1;
}
Number.prototype.isOdd = function() {
    return parseInt( this / 2 ) != this / 2;
}
String.prototype.trim = function() {
    return this.replace( /^\s+|\s+$/g, '' );
}
String.prototype.ucfirst = function() {
    return this.substring( 0, 1 ).toUpperCase() + this.substring( 1 ).toLowerCase();
}
String.prototype.lcfirst = function() {
    return this.substring( 0, 1 ).toLowerCase() + this.substring( 1 ).toUpperCase();
}
String.prototype.toCamelCase = function() {
    var aFrags = this.split( ' ' ), rFrags = [];
    for ( var i = 0; i < aFrags.length; i++ ) {
        if ( aFrags[i] != '' )
            rFrags.push( aFrags[i].ucfirst() );
    }
    return rFrags.join( ' ' );
}
function parseJson (doc) {			
			var jsonData = eval("(" + doc + ")");
		}     
function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	if(typeof(arr) == 'object') { //Array/Hashes/Objects 
		for(var item in arr) {
			var value = arr[item];
			
			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}