// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
var SilgaController =Class.create();
SilgaController.prototype = {
	
	activeSection: 'home',
	resizingScreen: false,
	slideshow: '',
	
	initialize: function() {
		this.load();
	},
	
	load:function() {
		Event.observe(window, 'resize', this.resize);
	},
	
	resize: function(){
		pageWidth = getPageWidth();
		if (silgaController.resizingScreen){
			screenWidth = parseFloat($('screen').style.width.replace('px',''));
			screeWidthDelta = (screenWidth-733)/1.5;
			try {
			$('screen').style.marginLeft = (((pageWidth-screenWidth)/2)-screeWidthDelta)+'px';
			}
			catch(exception) {
				
			}
		}
		else{
			$('screen').style.marginLeft = 'auto';
			$('screen').style.marginRight = 'auto';
		}
	},
	
	productsLoader: function() {
		if (!silgaController.resizingScreen){
			
			silgaController.resizingScreen = true;
			$('content').innerHTML = "";
			
			$('screen').style.border = "none";
			$('screen').style.background = "white";
			
			Element.addClassName($('content'), 'floating');

			
			wCur = $('screen').offsetWidth;
			xScale = ((794) / wCur) * 100;
			wDiff = wCur - 794;
			
			new Effect.Scale($('screen'), xScale, {scaleContent: false,
																			scaleY: false,
																			delay: 0.2,
																			duration: 0.4,
																			afterUpdate: function(){silgaController.resize()},
																			afterFinish:function(){
																				new Effect.SlideDown($('sidebar'));
																			}
																			});
		}
	},
	
	closeProducts: function() {
		if (silgaController.resizingScreen){

			silgaController.resizingScreen = false;
			
			Element.removeClassName($('content'), 'floating');
			Element.addClassName($('content'), 'normal');
			
			$('screen').style.border = '1px #cccccc solid';
			$('screen').style.background = "url(/images/greyPixel.png) repeat";
			
			
			wCur = $('screen').offsetWidth;
			xScale = ((733) / wCur) * 100;
			wDiff = wCur - 733;
			
			new Effect.SlideUp($('sidebar'), {afterFinish: function(){
					new Effect.Scale($('screen'), xScale, {scaleContent: false,
																scaleY: false,
																delay: 0.2,
																duration: 0.4,
																afterUpdate: function(){silgaController.resize()}});
				}});
		}
	},
	
	sectionSwitcher: function() {
		
		if (silgaController.slideshow != '')
			silgaController.slideshow.stop();
			
		new Effect.Fade('content', {duration: 0.5, afterFinish: function(){
			new Ajax.Updater('content', '/home/change_section?section='+silgaController.activeSection, {asynchronous:true,
																										evalScripts:true,
																										onComplete: function() {
																											new Effect.Appear('content', {duration: 0.5});
																										}});
		}});
	},
	
	doSlideshow: function (element, target) { 
		silgaController.slideshow = new Ajax.PeriodicalUpdater(element, target, {asynchronous:true, frequency:5});
	}
}

var silgaController = new SilgaController();