/**
 * @author Jeffrey Brayne
 * @copyright 2009
 * This file may not be reproduced without explicit permission
 * Application.js was developed custom for Andrew Kovacs by Jeffrey Brayne and Braynepower Media
 */

 
 var Application = Class.create();
 
 Object.extend(Application.prototype, {
 	
	version: 0.1,
	
	initialize: function(){
		this.g = {};
		this.kkeys = [];
		this.konami = "38,38,40,40,37,39,37,39,66,65";
		this.hotlink = "gallery";
		this.g = {};
		this.paused = false;
		this.ss = new SlideShow('listOfImages', { slideDuration: 3, crossFade: true, events: {init: 'gallery:loaded', play: 'gallery:play'}});
		document.observe('SlideShow_listOfImages:transitioned', function(ss){
			var title = ss.memo.coming.title;
			$('imagelabel').innerHTML = title;
		})
	},
			
	go: function(e){
		var id = e.target.id;

		if(app.hotlink !== "") $(app.hotlink).toggleClassName('hot');
		
		$(app.hotlink + "_div").fade({
			duration: 0.3,
			afterFinish: function(){
				/*if(id !== 'thankyou'){
					$(id).toggleClassName('hot')
					app.hotlink = id;
				}*/
				
				$(id).toggleClassName('hot')
				app.hotlink = id;
				$(id + "_div").appear({
					duration: 0.3
				});
			}
		});
		
	},
				
	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
		});
	},
		
	run: function(gallery){
		app.setMenuLinks();
		document.fire('gallery:loaded');
		document.fire('gallery:play');
		$('controller').observe('click', function(){
			if(!app.paused){
				app.ss.pause();
				$('controller').update('&lt;play&gt;');
				app.paused = true;
			}else{
				app.ss.play();
				$('controller').update('&lt;pause&gt;');
				app.paused = false;
			}
		});
		/* konami code easter egg */
		document.observe('keydown', function(e) {
			app.kkeys.push( e.keyCode );
			if (app.kkeys.toString().indexOf( app.konami ) >= 0 ){
				document.stopObserving('keydown',arguments.callee);
				app.go('disclaimer');
			}
		});
	},
	
	setMenuLinks: function(){
		$$('#menu li').each(function(g){
			$(g).observe('click', app.go)
		});
	},
	
	setLinks: function(cls, funktion){
		$$(cls).each(function(link){
			link.observe('click', function(){
				funktion(this, false);
			});
		})
	},
	
	submitForm: function(form){
		if(app.validate(form) && !app.submitted){
			var params = {};
			params.params = $(form).serialize(true);
			params.url = 'lib/php/submit.php';
			params.onSuccess = function(res){
				app.submitted = true;
				$('contact').update('thank you');
				$('contact').id = 'thankyou';
				$(app.hotlink + "_div").fade({
					duration: 0.3,
					afterFinish: function(){
						/*if(id !== 'thankyou'){
							$(id).toggleClassName('hot')
							app.hotlink = id;
						}*/
						
						//$(id).toggleClassName('hot')
						//app.hotlink = id;
						app.hotlink = "thankyou";
						$("thankyou_div").appear({
							duration: 0.3
						});
					}
				});			
			}
			app.request(params);
		}
	},
 			
	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
		});
	},
	
	validate: function(form){
		var required = $$("#" + form + ' .required');
		var validate = $$("#" + form + ' .validate');
		var msg = [];
		var me = this;
		required.each(function(field){
			if($(field).value==''){
				msg.push($(field).name.replace('_', ' ').toUpperCase() + " is Required");
			}else if($(field).readAttribute('datatype')){
				var err = me.validateForm($(field).readAttribute('datatype'), $(field).value);
				if(err) msg.push(err);
			}
		});
		validate.each(function(field){
			if($(field).readAttribute('datatype') && $(field).value!=''){
				var err = me.validateForm($(field).readAttribute('datatype'), $(field).value);
				if(err) msg.push(err);
			}
		});
		if (msg.length > 0) {
			alert(msg.join('\n'));
            return false;
		}else{
            return true;
        }
	},
	
	validateForm: function(datatype, value){
		var error = "";
		switch(datatype){
			case 'alnum_space':
				var filter = /[0-9a-zA-Z\ \.]/;
				if(!(filter.test(value))){
					error = "You must use only characters or numbers";
				}
				break;
			case 'alnum':
				var filter = /[0-9a-zA-Z]/;
				if(!(filter.test(value))){
					error = "You must use only characters or numbers";
				}
				break;
			case 'alpha_space':
				var filter = /[a-zA-Z\ ]/;
				if(!(filter.test(value))){
					error = "You must use only characters or spaces";
				}
				break;
			case 'alpha':
				var filter = /[a-zA-Z]/;
				if(!(filter.test(value))){
					error = "You must use only characters";
				}
				break;
			case 'digits':
				var filter = /[0-9]/;
				if(!(filter.test(value))){
					error = "You must use only numbers";
				}
				break;
			case 'email':
				var emailFilter= /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;   ///^.+@.+\..{2,3,4,6}$/;
				if (!(emailFilter.test(value))) {
					error = "Please enter a valid email address.";
				}
				var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
				if (value.match(illegalChars)) {
					error = "The email address contains illegal characters.";
				}
				break;
			case 'phone':
				var stripped = value.replace(/[\(\)\.\-\ ]/g, '');
				//strip out acceptable non-numeric characters
				if (isNaN(parseInt(stripped))) {
					error = "The phone number contains illegal characters.";
				}
				if (!(stripped.length == 10)) {
					error = "The phone number is the wrong length. No spaces or special characters.";
				}
				break;
            case 'phone_3':
                if(isNaN(value)){
                    error = "The phone number must be digits only";
                }
                if(value.length < 3){
                    error = "You must have at least three digits for this part of the phone number";
                }
                break;
             case 'phone_4':
                if(isNaN(value)){
                    error = "The phone number must be digits only";
                }
                if(value.length < 4){
                    error = "You must have at least three digits for this part of the phone number";
                }
                break;
			case 'dropdown':
			    if (value == '') {
			       error = "You didn't choose an option from the drop-down list.";
			    }
				break;
			case 'date':
				var re = /(^\d{4}\-\d{1,2}\-\d{1,2}$)/;
				if(!(re.test(value))) {
					error = "Please enter a valid date: YYYY-MM-DD";
				}
				break;
			case 'zip':
				var zipExp  = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
				if (!(zipExp.test(value))) {
					error = "Please enter a valid zip code.";
				}
				break;
			default:
				error = false;
		}

		return error;
	}
		
 });
