function setActives(){
	var url = document.location.toString();
	var url_parts = url.split( '?' );
	if(url_parts[1]){
		var querystring = url_parts[1].split( '&' );
		for(var i=0;i<querystring.length;i++){
			var key = querystring[i].replace('=','');
			// alert( key );
			var link = o(key);
			if(link){
				// link.focus();
			}
		}
	}
}

function setExternalLinks(){
	var links = getElementsByClassName('external');
	for(var i=0;i<links.length;i++){
		setExternalLink( links[i] );
	}
}

function setExternalLink( obj ){
	obj.setAttribute('target','_blank');
}
	
function innitAjax(){
	var links = o( 'c' ).getElementsByTagName( 'a' );
	for( var i = 0 ; i < links.length ; i++ ){
		var link = links[i];
		link.onclick = function(){ 
			callBack( setCountryClick, this.href + '&view=xml' );
			deactivate( 'c', this );
			clearOut( 'contact' );
			this.blur();
			return false; 
		};
	} 
}

function clearOut( id ){
	var parent = o( id ).getElementsByTagName( 'div' )[0];
	parent.innerHTML = '';
}

function deactivate( parent_id, obj ){
	var links = o( parent_id ).getElementsByTagName( 'a' );
	for( var i = 0 ; i < links.length ; i++ ){
		if( obj.getAttribute( "id" ) == links[ i ].getAttribute( "id" ) ){
			obj.parentNode.className = 'on';
		}else{
			links[ i ].parentNode.className = '';
		}
	}
}

function getQueryString(){
	var querystring = {};
	var url = document.location.toString();
	var url_parts = url.split( '' );

}

function setAddressFormat( country_code, parent, zip, city, state ){
	country_code = country_code.toUpperCase();
	var address_format_codes = {
		'AU' : 1,	//Australia
		'ID' : 1,	//Indonesia
	        'GB' : 3,   //United Kingdom
        	'UK' : 3,   //United Kingdom
	        'ENGLAND' : 3,  //...and some new additions
        	'WALES' : 3,
	        'NORTHERN IRELAND' : 3,
        	'SCOTLAND' : 3,
	        'JERSEY' : 3,
        	'GUERNSEY' : 3,
		'IE' : 4,	//Ireland
		'KR' : 5,	//South Korea
		'BH' : 6,	//Bharain
		'ET' : 6,	//Ethiopia
		'CA' : 7,	//Canadia
		'US' : 7	//United States
	};
	
	switch( address_format_codes[ country_code ] ){
		case 1:
			var holder = co( 'span' );
			holder.appendChild( co( 'span','','locality inline', city ) );
			holder.appendChild( document.createTextNode( ' ' ) );
			holder.appendChild( co( 'span','','postal-code inline', zip ) );
			parent.appendChild( holder );
			break;
		case 3:
			parent.appendChild( co( 'span','','locality', city ) );
			parent.appendChild( co( 'span','','postal-code', zip ) );
			break;
		case 4:
			parent.appendChild( co( 'span','','postal-code', zip ) );
			parent.appendChild( co( 'span','','locality', city ) );			
			break;
		case 5:
			var holder = co( 'span' );
			holder.appendChild( co( 'span','','postal-code inline', zip ) );
			holder.appendChild( document.createTextNode( ', ' ) );
			holder.appendChild( co( 'span','','locality inline', city ) );			
			parent.appendChild( holder );
			break;
		case 6:
			parent.appendChild( co( 'span','','locality', city ) );
			break;
		case 7:
			var holder = co( 'span' );
			holder.appendChild( co( 'span','','locality inline', city ) );
			holder.appendChild( document.createTextNode( ', ' ) );
			holder.appendChild( co( 'span','','state-name inline', state ) );
			holder.appendChild( document.createTextNode( ' ' ) );
			holder.appendChild( co( 'span','','postal-code inline', zip ) );
			parent.appendChild( holder );
			break;
		default:
			var holder = co( 'span' );
			if( zip ){
				holder.appendChild( co( 'span','','postal-code inline', zip ) );
				holder.appendChild( document.createTextNode( ', ' ) );
			}
			holder.appendChild( co( 'span','','locality inline', city ) );
			parent.appendChild( holder );
			break;
	}
}


function setCountryClick( xml ){
	// check here for state exceptions !!
	var exception = xml.getElementsByTagName( 'exception' );
	var exceptions = {};
	for( var i = 0 ; i < exception.length ; i++ ){
		exceptions[ exception[ i ].getAttribute( 'id' ) ] = 1;
	}
	var country = xml.getElementsByTagName( 'country' )[0].getAttribute( 'id' );
	var parent = o( 'contactsBox' );
	var holder = o( 's' );
	if( exceptions[ country ] ){
		if( ! holder ){
			holder = co( 'div','s','subBox' );
				var title = co( 'h2','','','State/Province' );
				var list = co( 'div','','listBox' );
				holder.appendChild( title );
				holder.appendChild( list );
			parent.appendChild( holder );
			// prepend BOX
			prependNode( holder, o( 'i' ) );
			// set widths of other boxes
			setMaxiWidth( 'on' );
		}
		setStates( xml );
		clearOut( 'i' );
	}else{
		if( holder ){
			removeItem( holder );
		}
		setMaxiWidth( 'off' );
		// set other boxes width
		setCategories( xml );
	}
}

function setMaxiWidth( param ){
	var country = o( 'c' );
	var city = o( 'i' );
	var contacts = o ( 'contact' );
	if( param == 'on' ){
		country.className += ' maxi';
		city.className += ' maxi';
		contacts.className += ' maxi';
	}else{
		country.className = country.className.toString().replace( ' maxi', '' );
		city.className = city.className.toString().replace( ' maxi', '' );
		contacts.className = contacts.className.toString().replace( ' maxi', '' );
	}
}

function setStates( xml ){
	var items = xml.getElementsByTagName( 'item' );
	var country = xml.getElementsByTagName( 'country' )[0].getAttribute( 'id' );
	var state = xml.getElementsByTagName( 'state' )[0].getAttribute( 'id' );
	clearOut( 'contact' );
	var parent = o( 's' ).getElementsByTagName( 'div' )[0];
	parent.innerHTML = 'Loading...';
	
	if( items.length ){
		// tag, id, cls, txt
		var list = co( 'ul' );
		for( var i = 0 ; i < items.length ; i++ ){
			var id = items[i].getAttribute( 'id' );
			var name = items[i].getAttribute( 'name' );
			var item = document.createElement( 'li' );
			var link = document.createElement( 'a' );
			link.appendChild( document.createTextNode( name ) );
			link.setAttribute( 'id', 'i'+ id );
			link.setAttribute( 'href', '?c='+ country +'&s='+ id +'&i='+ 0 );
			link.onclick = function(){
				callBack( setCategories, this.href +'&view=xml' );
				deactivate( 's', this );
				this.blur();
				return false;
			}
			item.appendChild( link );
			list.appendChild( item );
		}
		parent.innerHTML = '';
		parent.appendChild( list );
	}
}

function setCategories( xml ){
	var items = xml.getElementsByTagName( 'item' );
	var country = xml.getElementsByTagName( 'country' )[0].getAttribute( 'id' );
	var state = xml.getElementsByTagName( 'state' )[0].getAttribute( 'id' );
	clearOut( 'contact' );
	var parent = o( 'i' ).getElementsByTagName( 'div' )[0];
	parent.innerHTML = 'Loading...';
	
	if( items.length ){
		// tag, id, cls, txt
		var list = co( 'ul' );
		for( var i = 0 ; i < items.length ; i++ ){
			var id = items[i].getAttribute( 'id' );
			var name = items[i].getAttribute( 'name' );
			var item = document.createElement( 'li' );
			var link = document.createElement( 'a' );
			link.appendChild( document.createTextNode( name ) );
			link.setAttribute( 'id', 'i'+ id );
			link.setAttribute( 'href', '?c='+ country +'&s='+ state +'&i='+ id );
			link.onclick = function(){
				callBack( setContacts, this.href +'&view=xml' );
				deactivate( 'i', this );
				this.blur();
				return false;
			}
			item.appendChild( link );
			list.appendChild( item );
		}
		parent.innerHTML = '';
		parent.appendChild( list );
	}
}


function Contact(){
}

Contact.prototype.setContact = function( xml_obj ){
	var root = xml_obj.getElementsByTagName( '*' );
	for( var i = 0 ; i < root.length ; i++ ){
		var node = root[ i ];
		if( node.tagName && node.childNodes && node.childNodes.length ){
			// fax hack
			if( node.getAttribute( 'tel.type') == 'FAX' ){
				// this.clearProperty( 'fax' );
				this.setProperty( 'fax', node.firstChild.nodeValue );
			}else{
				// this.clearProperty( node.tagName );
				this.setProperty( node.tagName, node.firstChild.nodeValue );
			}
		}
	}
}
	
Contact.prototype.setProperty = function( key, value ){
	this[ key ] = value;
}
	
Contact.prototype.clearProperty = function( key ){
	this[ key ] = null;
}
	
Contact.prototype.getProperty = function( key, value ){
	return this[ key ];
}

function setContacts( xml ){
	var parent = o( 'contact' ).getElementsByTagName( 'div' )[0];
	parent.innerHTML = 'Loading...';
	var holder = xml.getElementsByTagName( 'contacts' );
	if( holder ){
		holder = holder[0];
		var items = holder.getElementsByTagName( 'vCard' );
		// tag, id, cls, txt
		var list = co( 'dl' );		
		if( items.length ){
			for( var i = 0 ; i < items.length ; i++ ){
				var tmp_xml = items[ i ];
				var vcard = co( 'dd','','vcard','' );
				var contact = new Contact;
				contact.setContact( tmp_xml );
				vcard.appendChild( co( 'div', '', 'fn org', contact.orgname ) );				
				var address_holder = co( 'div' );
					var address = co( 'address','','adr' );
					var street = [];
					var streets = co( 'span','','street-address' );
					if( contact.street1 ){ street.push( contact.street1 ); }
					if( contact.street2 ){ street.push( contact.street2 ); }
					if( contact.street3 ){ street.push( contact.street3 ); }
					for( var s = 0 ; s < street.length ; s++ ){
						streets.appendChild( co( 'span', '', '', street[ s ] ) );
					}
					address.appendChild( streets );
					setAddressFormat( contact.country_code, address, contact.pcode, contact.locality, contact.state );
					address.appendChild( co( 'span','','street-address', contact.country ) );
					address_holder.appendChild( address );
				vcard.appendChild( address_holder );
				if( contact.tel || contact.fax ){
					var fcontact_holder = co( 'div','','details' );
						if( contact.tel ){
							fcontact_holder.appendChild( co( 'span','','tel', 'Tel: '+ contact.tel ) );
						}
						if( contact.fax ){
							fcontact_holder.appendChild( co( 'span','','fax', 'Fax: '+ contact.fax ) );
						}
					vcard.appendChild( fcontact_holder );
				}
				if( contact.email || contact.web ){
					var econtact_holder = co( 'div','','details' );
						if( contact.email ){
							var email = co( 'span','','' );
							var email_link = co( 'a','','email', contact.email );
							email_link.setAttribute( 'href', 'mailto:'+ contact.email );
							email.appendChild( email_link );
							econtact_holder.appendChild( email );
						}
						if( contact.web ){
							var website = co( 'span','','','' );
							var website_link = co( 'a','','external url fn', contact.web );
							website_link.setAttribute( 'href', contact.web );
							setExternalLink( website_link );
							website.appendChild( website_link );
							econtact_holder.appendChild( website );
						}
					vcard.appendChild( econtact_holder );
				}
				list.appendChild( vcard );
			}
		}else{
			var item = document.createElement( 'dd' );
			item.appendChild( document.createTextNode( 'No contacts found' ) );
			list.appendChild( item );
		}
		
		parent.innerHTML = '';
		parent.appendChild( list );
	}	
}

function init(){
	setExternalLinks();
	if( document.createElement ){
		innitAjax();
	}else{
		setActives();	
	}
}

addEvent( window, "load", init );

