Element.Events.hashchange = {
    onAdd: function(){
        var hash = self.location.hash;

        var hashchange = function(){
            if (hash == self.location.hash) return;
            else hash = self.location.hash;

            var value = (hash.indexOf('#') == 0 ? hash.substr(1) : hash);
            window.fireEvent('hashchange', value);
            document.fireEvent('hashchange', value);
        };

        if ("onhashchange" in window){
            window.onhashchange = hashchange;
        } else {
            hashchange.periodical(50);
        }
    }
};


window.addEvent('domready', function() {
	
	$$('input.search_query').each(function(item){
		
		form = item.getParent('form')
		
		item.addEvent('focus', function(e){
			if(item.value == 'Search Bookstore'){
				item.value = ''
				form.addClass('active')
			}
		});
		
		item.addEvent('blur', function(e){
			if(item.value == ''){
				item.value = 'Search Bookstore'
				form.removeClass('active')
			}
		});
		//just change the class
		form.addEvent('submit', function(e){
			form.removeClass('active')
		});
	});



	$$('.payment-button').each(function(item){
		item.addEvent('click', function(e){
			item.addEvent('click', function(e2){
				item.setProperty('disabled', 'disabled');
				e2.stop();
			});
		});
	});

	if($('search-field')){
		$('search-field').store('default', $('search-field').value);		
		$('search-field').addEvents({
			'focus':function(){
				if(this.value == this.retrieve('default', '')){ this.value = ''; }
				this.addClass('focused');
				this.select();
			},
			'blur':function(){
				if(!(this.value.length)){ this.value = this.retrieve('default', ''); }
				this.removeClass('focused');
			}
		});
	}
	
	
	$$('.tabgroup').each(function(group){
		
		tabs			= group.getElements('.tabs a');
		contents 	= group.getElements('div.tab');

		tabs.each(function(tab, indx){
		
			if(tab.hasClass('current')){
				window.store('default_hash', tab.getProperty('id'));
			}			
				
			tab.addEvent('click', function(e){
				if(!tab.hasClass('disabled')){
					tabs.removeClass('current');
					contents.removeClass('current');
					tab.addClass('current');
					if(contents[indx]){
						contents[indx].addClass('current');
					}
				} else {
					e.stop();
				}			
			});
			
			if(tab.getProperty('href') == window.location.hash){
				tab.fireEvent('click');
			}
		});
	});
	
	var country = 'US'
	
	if(!Cookie.read('country')){
		Request.JSON({url: "/ajax/geo/country/", onSuccess: function(response){
			country = response.country
		}}).get();
	} else {
		country = Cookie.read('country')
	}
	
	//country = 'CA';
	change_currency(country);

	$('currency-select').addEvent('change', function(e){
		Cookie.write('country', $('currency-select').value);
		change_currency($('currency-select').value);
	});
	
	


	/*
	$$('.title-in-list').each(function(title){
		title.addEvent('click', function(e){
			window.location = '/bookstore/title'+title.getProperty('id')+'/';
		});
	});
	*/
	
	if(window.retrieve('default_hash')){
		window.addEvent('hashchange', function(e){			
			if($('hash-'+e)){
				$('hash-'+e).fireEvent('click');
			} else {
				$(window.retrieve('default_hash')).fireEvent('click');
			}
		});
	}
	
	
});

function change_currency(country){
	if(country == 'CA'){
		$$('.usd').setStyle('display', 'none');
		$$('.cad').setStyle('display', 'inline');
		$('currency-select').value = 'CA'		
	} else {
		$$('.usd').setStyle('display', 'inline');
		$$('.cad').setStyle('display', 'none');
		$('currency-select').value = 'US'
	}

}



