/**
 * @author braynej
 */
var Application = Class.create();
 
Object.extend(Application.prototype, {
 	
	version: 0.1,
	panestate: {},
	imgct: 0,
	slides: [
		{
			src: 'lib/img/slides/shutterwax.jpg', 
			title: 'Shutterwax', 
			url: "http://shutterwax.com", 
			desc: "Site designed, branded and coded for the original band Shutterwax"
		},
		{
			src: 'lib/img/slides/askoldbuk.jpg', 
			title: 'Askold Buk', 
			url: "http://askoldbuk.com", 
			desc: "Site designed, branded and coded for Askold Buk, guitarist and composer"
		},
		{
			src: 'lib/img/slides/jeffreybraynephotography.jpg', 
			title: 'Jeffrey Brayne Photography', 
			url: "http://jeffreybraynephotography.com",
			desc: "Site designed, branded and coded for Jeffrey Brayne Photography"
		},
		{
			src: 'lib/img/slides/blacktieboogie.jpg', 
			title: 'BlackTieBoogie.com', 
			url: "http://blacktieboogie.com",
			desc: "Site designed, branded and coded for the event band Black Tie Boogie"
		}
	],
	
	initialize: function(){
		this.imgct = 0;
	},
	
	closePane: function(e){
		var id = e.target.id;
		if($('pane').visible() && (id !== "werk" && id !== "kontact")){
			Effect.Fade('pane', {
				afterFinish: function(){
					document.stopObserving('click');
				}
			});
		}
	},
	
	gup: function( name ){
		var name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
		var regexS = "[\\?&]"+name+"=([^&#]*)";
		var regex = new RegExp( regexS );
		var results = regex.exec( window.location.href );
		if( results == null )
			return false;
		else
			return results[1];
	},
		
	request: function(params){

		new Ajax.Request(params.url, {
			method: params.method || 'POST',
			evalJS: params.evalJS || false,
			evalJSON: params.evalJSON || false,
			parameters: params.params || null,
			onLoading: params.onLoading || null,
			onSuccess: params.onSuccess || function(res){ window.status = res.status },
			onFailure: params.onFailure || function(){ }, //Controller.onFailure(); },
			onComplete: params.onComplete || null
		});
	},
	
	resetForm: function(form){
		Form.reset(form);
	},
	
	run: function(){
		this.slideshow(this.slides[this.imgct]);
	},

	slideshow: function(img){
		var a = new Element('a', {href: img.url, target: '_blank', title: img.title});
		var slide = new Element('img', {src: img.src, alt: img.title, title: img.title});
		a.update(slide);
		$('slide').update(a);
		$('slidetitle').down('h1').update(img.title);
		$('slidetitle').down('p').update(img.desc);
		$('slidetitle').down('a').update(img.url);
		$('slidetitle').down('a').href = img.url;
		new Effect.Move('slide', { 
			x: -300, 
			y: 0, 
			mode: 'relative', 
			duration: .3, 
			afterFinish: function(){
				new Effect.Move('slide', {
					x: -300,
					y: 0,
					mode: 'relative',
					duration: .1,
					delay: 5,
					afterFinish: function(){
						$('slide').setStyle({left: '600px'});
						app.imgct++;
						app.slideshowseed();
					}
				});
			}
				
		});
	},

	slideshowseed: function(){
		if(app.imgct >= app.slides.length) app.imgct = 0;
		var img = app.slides[app.imgct];
		app.slideshow(img);
	},
	
	stripslashes: function(str) {
        str=str.replace(/\\'/g,'\'');
        str=str.replace(/\\"/g,'"');
        str=str.replace(/\\\\/g,'\\');
        str=str.replace(/\\0/g,'\0');
        return str;
    },

	updater: function(params){
		/**
		 * @description generic interface into Ajax.Updater();
		 */
		var div = params.div || 'content';
		new Ajax.Updater(div, params.url, {
			method: params.method || 'POST',
			parameters: params.params || null,
			evalScripts: params.evalScripts || false,
			onLoading: params.onLoading || null,
			onSuccess: params.onSuccess || null,
			onFailure: params.onFailure || function(){},
			onComplete: params.onComplete || null
		});
	},

	last: ""
	
});