// JavaScript Document
window.addEvent('domready',function() {
/* add smooth scrolling for internal links */
	new SmoothScroll({ duration:700 }, window);
	
/* scrollspy instance */
	var ss = new ScrollSpy({
		min: 150,
		onEnter: function(position,enters) {
			$('gototop').setStyle('display', 'block');
			$('gototop').fade('in');
		},
		onLeave: function(position,leaves) {
			$('gototop').fade('out');
		},
		onTick: function(position,state,enters,leaves) {
		},
		container: window
	});	
	
/* cases */
	var c = new Case();
	
/* fac imgs*/
	$('fac_imgs').getElements('img').each(function(child) {
		var siblings = child.getParent().getParent().getParent().getElements('img').erase(child);
		child.addEvents({
			mouseenter: function() { siblings.tween('opacity',0.5); },
			mouseleave: function() { siblings.tween('opacity',1); }
		});
	});

});


var Case = new Class({

	Implements: [Events, Options],
	
	options:{},

	initialize: function(){
	// init
		this.list = $('cases');
		this.loader = new Element('img', {'id':'case_loader', 'src':'/ajax-loader.gif'}).setStyle('visibility', 'hidden').inject(this.list.getPrevious());		
		this.current_case_id = 0;
	// init events
		this.init_events();
	},
	
	init_events: function(){
		var clist = this.list.getElements('li');
		clist.each(function(el, i){
			var cid = el.get("id").replace("case", "");							
			el.getElement('a').addEvent('click', function(e){
				e.stop();
				this.show_case(cid);
			}.bind(this));
		}.bind(this));
	},
	
	show_case: function(cid){
		this.list.fade('out').retrieve('tween').chain(function(){this.loader.fade('in'); this.current_case_id = cid; this.get_detail();}.bind(this));		
	},
	
	show_cases: function(){
		this.list.fade('out').retrieve('tween').chain(function(){this.loader.fade('in'); this.current_case_id = 0; this.get_cases();}.bind(this));				
	},
		
	get_detail: function(){
		var request = new Request.JSON({
			url: this.build_action_url('ajax_get_detail'),
			data: {
				cid: this.current_case_id
			},
			onComplete: function(rJSON, response) {					
				var json = $H(JSON.decode(response, true));				
				if(json["status"]==true){					
					this.loader.fade('out').retrieve('tween').chain(function(){
						this.list.set('html', json["case"]).fade('in'); 
						var myFx = new Fx.Scroll(window).toElement('cases'); 
						milkbox.reloadGalleries();
						this.list.getElement('p[class=b2cases]').getElement('a').addEvent('click', function(e){
							e.stop();
							this.show_cases();
						}.bind(this));
					}.bind(this));					
				} else {
					this.list.grab(new Element('span', {'class':'notification'}).set('html', json["msg"])).fade('in');	
					this.loader.fade('out');					
				}
			}.bind(this)
		}).post();			
	},
	
	get_cases: function(){		
		var request = new Request.JSON({
			url: this.build_action_url('ajax_get_cases'),			
			onComplete: function(rJSON, response) {					
				var json = $H(JSON.decode(response, true));				
				if(json["status"] == true){
					this.loader.fade('out').retrieve('tween').chain(function(){		
						var myFx = new Fx.Scroll(window).toElement('cases'); 
						this.list.set('html', json["cases"]).fade('in');
						this.init_events();
					}.bind(this));
				} else {
					this.header.grab(new Element('span', {'class':'notification'}).set('html', json["msg"]));										
				}
			}.bind(this)
		}).post();			
		
	},
		
	build_action_url: function(action){
		var url = window.location.href.toURI();
		var data = url.getData();
		data["action"] = action;
		data["fragment"] = "";
		url.setData(data, true);
		return url.toString();
	}

});
