jQuery(document).ready(function() {
	if(jQuery.getAnchor() != null) {
		anchorParts = jQuery.getAnchor().replace('#', '').split('_');

		switch(anchorParts.length) {
			case 3:
				partners.showState(anchorParts[2]);
			case 2:
				partners.showCountry(anchorParts[1]);
			case 1:
				partners.showContinent(anchorParts[0]);
				break;
		}
	}
	
	// click on "show map" link
	jQuery('#showMap').click(function() {
		// when user clicks on "showMap":
		// 1. hide list of countries
		jQuery('#countryList .continent').hide();
		// 2. hide "showMap" link & not listed countries
		jQuery('#showMap').hide();
		jQuery('#localWebsite').hide();
		jQuery('#otherCountries').hide();
		jQuery('#stateList .country').hide();
		jQuery('#partnerList .country').hide();
		// 3. show map
		jQuery('#ImageMap').show();
	});

	// click on map continent
	jQuery('#Map area').click(function() {
		continentID   = jQuery(this).attr('alt');
		
		return partners.showContinent(continentID);
	});

	jQuery('#countryList .country > a').click(function() {
		listId = jQuery(this).parent().attr('id').split('_')[1];
		
		return partners.showCountry(listId);
	});

	jQuery('#stateList .state > a').click(function() {
		listId = jQuery(this).parent().attr('id').split('_')[1];
		
		return partners.showState(listId);
	});
});

var partners = {
	showContinent: function(continentID) {
		continentName = jQuery('area[alt="' + continentID + '"]').attr('title');
		
		// when user clicks on map area:
		// 1. hide map
		jQuery('#ImageMap').hide();
		// 2. show "showMap" link & not listed countries
		jQuery('#showMap').show();
		jQuery('#otherCountries').show();
		// 3. show list of countries
		jQuery('#countryList #continent_' + continentID + ' h2').text(continentName);
		jQuery('#countryList #continent_' + continentID).slideDown(1000);
		
		return true;
	},
	showCountry: function(listId) {
		if(jQuery('#countryList li#country_' + listId + ' a').attr('rel') == 'companies') {
			// when user clicks on country that has no states:
			// 1. show list of localized Nicelabel sites
			jQuery('#localWebsite').show();
			// 2. show all states as there are none
			jQuery('#partnerList .state').show();
			// 3. hide all countries that were opened before
			jQuery('#partnerList .country').hide();
			// 4. slide down the country that user clicked on
			jQuery('#partnerList #partners_country_' + listId).slideDown(1000);
			// 5. scroll down to countries
			jQuery('html, body').animate({
				scrollTop: jQuery('#partnerList #partners_country_' + listId + ' h2').offset().top
			}, 'slow');
			
			return true;
		} else {
			// when user clicks on country that has states:
			// 1. show list of localized Nicelabel sites
			jQuery('#localWebsite').show();
			// 2. hide all countries that were opened before
			jQuery('#stateList .country').hide();
			// 3. hide country list
			jQuery('#partnerList .country').hide();
			// 4. slide down the country that user clicked on
			jQuery('#stateList #states_country_' + listId).slideDown(1000);
			
			return true;
		}
	},
	showState: function(listId) {
		// when user clicks on state:
		// 1. show list of localized Nicelabel sites
		jQuery('#localWebsite').show();
		// 2. hide all states that may be opened as we need to show only one
		jQuery('#partnerList .state').hide();
		// 3. hide all countries that may be opened as we need to show only one
		jQuery('#partnerList .country').hide();
		// 4. show only state that user clicked on
		jQuery('#partnerList #partners_state_' + listId).show();
		// 5. slide down country container containing states
		jQuery('#partnerList #partners_state_' + listId).parent().slideDown(1000);
		// 6. scroll down to countries
		jQuery('html, body').animate({
			scrollTop: jQuery('#partnerList #partners_state_' + listId).parent().find('h2:first').offset().top
		}, 'slow');
		
		return true;
	}
};

jQuery.extend({
	getUrlVars: function() {
		var vars = [], hash;
		var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
		for(var i = 0; i < hashes.length; i++) {
			hash = hashes[i].split('=');
			vars.push(hash[0]);
			vars[hash[0]] = hash[1];
		}
		return vars;
	},
	getAnchor: function() {
		anchor = window.location.href.split('#')[1];
		
		return typeof anchor != 'undefined' ? '#' + anchor : null;
	},
	getUrlVar: function(name){
		return jQuery.getUrlVars()[name];
	}
});
