/*====================================================================
  Kyoto Shisaku Net
====================================================================*/

// Onload events
[
	// Image Rollover
	function(){
		$$('a').each( function(anchor){
			anchor.select('img.rollover').each( function( e ){
				var original = e.src;
				var hover    = original.replace( /.([0-9a-zA-Z]+)$/, '.hover.$1' );
				( new Image()).src = hover;
				Event.observe( anchor, 'mouseover', function(){ e.src = hover;    });
				Event.observe( anchor, 'mouseout',  function(){ e.src = original; });
			});
		});	
	},

	// Smooth Scroll
	function(){
		$$( 'a[href^=#]' ).each( function( e ){
			e.observe( 'click', function( event ){
				new Effect.ScrollTo( this.hash.substr( 1 ) || document.body, {
					duration: .5,
					transition: function( pos ){ return (-Math.cos(((-Math.cos(Math.sqrt( pos )*Math.PI)/2)+.5)*Math.PI)/2)+.5; }
				});
				Event.stop( event );
			}.bindAsEventListener( e ));
		});
	},

	// Popup
	function(){
		$$( 'a[rel^=popup]' ).each( function( e ){
			var option;
			if( e.rel.length > 7 && e.rel.substr( 5, 1 ) == '[' ) option = e.rel.substr( 6, e.rel.indexOf(']') - 6 );
			e.observe( 'click', function( event ){
				option ? window.open( this.href, null, option ) : window.open( this.href );
				Event.stop( event );
			}.bindAsEventListener( e ));
		});
	},

	// Breadcrumb
	function(){
		var parent = $('Breadcrumb');
		if(!parent) return;
		var nodes = $A(parent.childNodes);
		while(nodes.length){
			var node = nodes.shift();
			if(node.nodeType !== 3) continue;
			var wrapper = new Element('span').addClassName('glue');
			parent.replaceChild(wrapper, node);
			wrapper.appendChild(node);
		}
	},

	// Counter
	(function(){
		var ready = false;
		var json;
		var setValue = function(){
			var counter   = $('OrdersCount');
			var timestamp = $('OrdersTimestamp');
			if(!counter || !timestamp) return;
			var set = function(n){
				counter.update();
				var string = Math.ceil(json.count*n) +'';
				//Zero padding: json.count = ('00'+json.count).slice(-3);
				for(var i=0, len=string.length; i<len; i++){
					var char = string.charAt(i);
					counter.insert(new Element('span').addClassName('counter-'+char).update(char));
				}
			};
			set(1);
			timestamp.update('('+ json.modified.timestamp.replace(/-/g, '.') +')');
		};
		new Ajax.Request('/common/js/orderCount.json', {
			onSuccess: function(request){
				json = eval('('+ request.responseText +')');
				if(ready) setValue();
				else ready = true;
			}
		});
		return function(){
			if(ready) setValue();
			else ready = true;
		};
	})()
].each( function(e){
	Event.observe( window, 'load', e );
});


// Google Analytics
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-24653351-1']);
_gaq.push(['_trackPageview']);

(function() {
	var domain = location.hostname;
	if(domain != 'www.kyoto-shisaku.com' && domain != 'kyoto-shisaku.com') return;
	var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
