window.addEvent('domready', function(){
	// smooth anchor scrolling
	new SmoothScroll(); 
	// style area
	if(document.id('aimStyleArea')){
		$$('#aimStyleArea a').each(function(element,index){
			element.addEvent('click',function(e){
	            e.stop();
				changeStyle(index+1);
			});
		});
	}
	// font-size switcher
	if(document.id('aimTools') && document.id('aimComponentWrap')) {
		var current_fs = 100;
		var content_fx = new Fx.Tween(document.id('aimComponentWrap'), { property: 'font-size', unit: '%', duration: 200 }).set(100);
		document.id('aimToolsInc').addEvent('click', function(e){ 
			e.stop(); 
			if(current_fs < 150) { 
				content_fx.start(current_fs + 10); 
				current_fs += 10; 
			} 
		});
		document.id('aimToolsReset').addEvent('click', function(e){ 
			e.stop(); 
			content_fx.start(100); 
			current_fs = 100; 
		});
		document.id('aimToolsDec').addEvent('click', function(e){ 
			e.stop(); 
			if(current_fs > 70) { 
				content_fx.start(current_fs - 10); 
				current_fs -= 10; 
			} 
		});
	}
	// login popup
	if(document.id('btnLogin') || document.id('btnRegister')) {
		var popup_overlay = document.id('aimPopupOverlay');
		popup_overlay.setStyle('display', 'block');
		var popup_overlay_fx = new Fx.Tween(popup_overlay, {property: 'opacity', duration:200}).set(0);
		var opened_popup = null;
		var popup_login = null;
		var popup_login_h = null;
		var popup_login_fx = null;
		var popup_register = null;
		var popup_register_h = null;
		var popup_register_fx = null;
		
		if(document.id('btnLogin')) {
			popup_login = document.id('aimPopupLogin');
			popup_login.setStyle('display', 'block');
			popup_login_h = popup_login.getElement('.aimPopupWrap').getSize().y + 8;
			popup_login_fx = new Fx.Morph(popup_login, {duration:200, transition: Fx.Transitions.Circ.easeInOut}).set({'opacity': 0, 'height': 0, 'margin-top':0}); 
			document.id('btnLogin').addEvent('click', function(e) {
				new Event(e).stop();
				popup_overlay_fx.start(0.85);
				popup_login_fx.start({'opacity':1, 'margin-top': -popup_login_h / 2, 'height': popup_login_h});
				opened_popup = 'login';
			});
		}
		
		if(document.id('btnRegister')) {
			popup_register = document.id('aimPopupRegister');
			popup_register.setStyle('display', 'block');
			popup_register_h = popup_register.getElement('.aimPopupWrap').getSize().y + 8;
			popup_register_fx = new Fx.Morph(popup_register, {duration:200, transition: Fx.Transitions.Circ.easeInOut}).set({'opacity': 0, 'height': 0, 'margin-top':0}); 
			document.id('btnRegister').addEvent('click', function(e) {
				new Event(e).stop();
				popup_overlay_fx.start(0.85);
				popup_register_fx.start({'opacity':1, 'margin-top': -popup_register_h / 2, 'height': popup_register_h});
				opened_popup = 'register';
			});
		}
		
		popup_overlay.addEvent('click', function() {
			if(opened_popup == 'login')	{
				popup_overlay_fx.start(0);
				popup_login_fx.start({
					'opacity' : 0,
					'height' : 0,
					'margin-top': 0
				});
			}
			
			if(opened_popup == 'register') {
				popup_overlay_fx.start(0);
				popup_register_fx.start({
					'opacity' : 0,
					'height' : 0,
					'margin-top': 0
				});
			}	
		});
	}
});
// function to set cookie
function setCookie(c_name, value, expire) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expire);
	document.cookie=c_name+ "=" +escape(value) + ((expire==null) ? "" : ";expires=" + exdate.toUTCString());
}
// Function to change styles
function changeStyle(style){
	var file1 = $aim_TMPL_URL+'/css/style'+style+'.css';
	var file2 = $aim_TMPL_URL+'/css/typography.style'+style+'.css';
	new Asset.css(file1);
	new Asset.css(file2);
	Cookie.write('aim1_style',style, { duration:365, path: '/' });
}
