// JavaScript Document
function submenu_effect(){
	if($chk('sub_menu')){
		$$('#sub_menu a').each(function(el) {  
			var fx = new Fx.Morph(el,{ duration:300, link:'cancel' });
			el.addEvents({
				'mouseenter': function() { fx.start({ 'padding-left': 15 }); },
				'mouseleave': function() { fx.start({ 'padding-left': 4 }); }
			});
		}); 	
	}
}

function element_effect(){
	
	$$('.element').each(function(el) {  
		el.addEvents({
				'mouseenter': function() { if(!el.hasClass('no_bg')){el.tween('border-color', '#fec408');} },
				'mouseleave': function() { if(!el.hasClass('no_bg')){el.tween('border-color', '#000000');} }
		});
	}); 	
	
}

function drawing_slideshow(){
	var ss = new Asset.javascript("/slideshow_basic/js/slideshow.js", {id: 'slideshow'});
	var request = new Request.JSON({
		url: build_action_url('ajax_get_latest_drawings', current_lang + '/index.htm'),
		onComplete: function(rJSON, response) {					
			var json = $H(JSON.decode(response, true));
			if(json["status"] == 1){				
				(function(){
					$('drawing_container').getElement('img').fade('out').retrieve('tween').chain(function(){						
						window.slides = Slides.start($('drawing_container'), json["images"], {showFor: 3500, transitionFor: 800, rel:"drawing", init_mb:false, link:true, autorotate:true});												
					});															
				}).delay(500);				
			} else {
				$('drawing_container').adopt(new Element('span').addClass('italic').set('html', json["msg"]));
			}
		}
	}).post();	
}

function partner_slideshow(){
	var request = new Request.JSON({
		url: build_action_url('ajax_get_partners', current_lang + '/index.htm'),
		onComplete: function(rJSON, response) {					
			var json = $H(JSON.decode(response, true));
			if(json["status"] == 1){				
				(function(){
					$('partner_container').getElement('img').fade('out').retrieve('tween').chain(function(){						
						window.slides = Slides.start($('partner_container'), json["partners"], {showFor: 3500, transitionFor: 800, rel:"partner", init_mb:false, link:true, autorotate:true});												
					});															
				}).delay(3000);				
			} else {
				$('partner_container').adopt(new Element('span').addClass('italic').set('html', json["msg"]));
			}
		}
	}).post();	
}

function tour_moof_effect(){
	var mbc = new Asset.css("/milkbox/css/milkbox.css", {id: 'milkbox_css'});
	var mbj = new Asset.javascript("/milkbox/js/milkbox.js", {id: 'milkbox_js'});
	var ml = $('movie_lnk');
	ml.adopt(new Element('img', {src: '/movie_start_over.jpg', width:200, height:150, alt: ml.getElement('img').get('alt')}).setStyles({'opacity': 0, 'z-index': 2}));
	ml.addEvent('mouseenter', function(){
		ml.getLast().fade('in');								   
	});
	ml.addEvent('mouseleave', function(){
		ml.getLast().fade('out');								   
	});
}

var current_quote = 0;
var quote_timer;

function testimonial_slide(){
	var request = new Request.JSON({
		url: build_action_url('ajax_get_random_testimonials', current_lang + '/index.htm'),
		onComplete: function(rJSON, response) {					
			var json = $H(JSON.decode(response, true));
			if(json["status"] == 1){				
				(function(){							  
					var start = $('testimonial').getElement('img[class=start]');
					var end = $('testimonial').getElement('img[class=end]');
					var l = $('testimonial');
					json["quotes"].each(function(q){
						var lnk = new Element('a', {'href':q["url"], 'html':q["quote"]});
						l.adopt(new Element('li').setStyle('opacity', 0).adopt(start.clone(), lnk, end.clone()));												 												 
					 });						  						  
					quote_timer = quote.delay(3000);					
				}).delay(1000);				
			} else {
				$('testimonial').adopt(new Element('span').addClass('italic').set('html', json["msg"]));
			}
		}
	}).post();		
}

function quote(){
	var previous = $('testimonial').getElements('li')[current_quote];
	previous.fade('out').retrieve('tween').chain(function(){
		current_quote++;
		if(current_quote >= $('testimonial').getElements('li').length){ current_quote = 0; }	
		$('testimonial').getElements('li')[current_quote].fade('in').retrieve('tween').chain(function(){			
			quote_timer = quote.delay(3000);
		});
	});
	
}

function gallery_slideshow(){	
	var request = new Request.JSON({
		url: build_action_url('ajax_get_random_imgs', current_lang + '/index.htm'),
		onComplete: function(rJSON, response) {					
			var json = $H(JSON.decode(response, true));
			if(json["status"] == 1){				
				(function(){
					$('gallery_container').getElement('img').fade('out').retrieve('tween').chain(function(){						
						window.slides = Slides.start($('gallery_container'), json["images"], {showFor: 2000, transitionFor: 1000, rel:"gallery", init_mb:false, link:true, autorotate:true});												
					});															
				}).delay(1000);				
			} else {
				$('gallery_container').adopt(new Element('span').addClass('italic').set('html', json["msg"]));
			}
		}
	}).post();	
}

function build_action_url(action, file){
	var url = window.location.href.toURI();
	var data = url.getData();
	data["action"] = action;
	url.set('file', file);
	url.setData(data, true);
	return url.toString();
}

window.addEvent('domready', function(){
	if($chk($('intro_overlay'))){ 
		$('intro_overlay').setStyle('visibility', 'visible');
		resize_overlay();
		position_container();
		window.addEvent('resize', function(){resize_overlay(); position_container();});
	
		$('intro_top').getElement('a[class=close]').addEvent('click', function(){$('intro_overlay').fade('out')});	
	}
	
	submenu_effect();
	$('nav').MooDropMenu(); 
	var ss = new Asset.javascript("/slideshow_basic/js/slideshow.js", {id: 'slideshow'});
	if($chk($('drawing_container'))){ drawing_slideshow(); }
	if($chk($('partner_container'))){ partner_slideshow(); }
	if($chk($('movie_lnk'))){ tour_moof_effect(); }
	if($chk($('testimonial'))){ testimonial_slide(); }
	if($chk($('gallery_container'))){ gallery_slideshow(); }
//	element_effect();
});

function resize_overlay(){
	var scrollSize = $(window).getScrollSize().y;
	var scrollTop = $(window).getScroll().y;
	$('intro_overlay').setStyles({ 'height':scrollSize+scrollTop,'top':-scrollTop });
}

function position_container(){
	var top = $(window).getSize().y/2;
	$('intro_container').setStyles({'top':top});
}