/* navScripts----------------------------------------------------------------*/
/**--------------------------------------------------------------------------*/
/**--------------------------------------------------------------------------*/ 
// Jument Communicator
/**--------------------------------------------------------------------------*/ 
var Jument = {};
Jument.ajax = Class.create({
	initialize: function(params) {
		this.response_data = '';
		this.params = {};
		
		this.params = params;
		
		if(this.params.options == undefined) {
			this.params.options = {};
		}
		
		this.request();
	},
	
	request: function() {
		this.show_loader();
		
		this.params.options.method = this.params.options.method ? this.params.options.method : 'post';
		
		if(this.params.form) {
			if(this.params.options.parameters == undefined) {
				this.params.options.parameters = Form.serialize(this.params.form);
			} else {
				this.params.options.parameters = this.params.options.parameters.concat('&', Form.serialize(this.params.form));
			}
		}
		
		if(!this.params.url) alert('AJAX: Requires URL');
		if(this.params.debug) console.log('url: %o \npars: %o', this.params.url, pars);
		
		// prep the onComplete function. Add on the hide_loader function
		if(!this.params.options.onComplete) {
			var onComplete = function() { this.hide_loader(); }.bind(this);
		} else if(this.params.onComplete instanceof Array) {
			var funtions = "";
			for(i=0; i < this.params.options.onComplete.length; i++) {
				functions += "this.params.options.onComplete["+i+"]();";
			}
			var onComplete = function(){ eval(functions); this.hide_loader(); }.bind(this);
		} else {
			var func = this.params.options.onComplete;
			var onComplete = function() { func(); this.hide_loader(); }.bind(this);
		}
		
		this.params.options.onComplete = onComplete;
		
		if(this.params.div) {
			new Ajax.Updater(this.params.div, this.params.url, this.params.options);
		} else {
			this.params.options.onSuccess = this.params.options.onSuccess ? this.params.options.onSuccess : this.response;
			new Ajax.Request(this.params.url, this.params.options);
		}
	},
	
	response: function(r) {
		eval('this.response_data = '+r.responseText);

		if(this.response_data.debug) {
			console.log(this.response_data);
			console.log(this.response_data.javascript.replace(/;/, "\n"));
		}
	
		if(this.response_data.javascript) eval(this.response_data.javascript); 
	},
	
	show_loader: function() {
		var loader;
		if (loader = $(this.params.loader)) loader.show();
		document.body.style.cursor = "wait";
	},
	
	hide_loader: function() {
		var loader;
		if (loader = $(this.params.loader)) loader.hide();
		document.body.style.cursor = "default";
	}
});

Jument.submit_form = Class.create({
	initialize: function(url, frm, btn, xpars) {		
		this.submit_form(url, frm, btn, xpars);
	},
	submit_form: function(url, frm, btn, xpars) {
		var btn = $(btn);
		var original_btn_value = btn.value;
		btn.value = 'Please wait...';
		btn.disabled = true;
		
		var pars;
		if(xpars != undefined) pars += '&' + xpars;
		
		new Jument.ajax({
			url: url,
			form: frm,
			options: {
				parameters: pars,
				onFailure: function() { alert('An error occurred. Please try again.'); },
				onComplete: function() { btn.disabled = false; btn.value = original_btn_value; }
			}
		});
	}
});


// Library
DSP = function(){};

(function() {
	
    this.ns = function(fn){
        var ns = {};
        fn.apply(ns);
        return ns;
    };

    
    /***************************/
    
	
	changeSort = function(orderBy) {
		
		new Jument.ajax({
			div: 'demotivators_list', 
			url: 'postSort.php', 
			options: {
				parameters: {sort: orderBy}
			}
		}); 
		new Jument.ajax({
			div: 'nav_buttons',
			url: 'changeNav.php',
			options: {
				parameters: {sort: orderBy}
			}
		});
		$('demotivators_list').setAttribute('onmouseover','applyToolTip();');
	};

//	addMiniPosters = function() {
//		new Jument.ajax({
//			div:'mini_prints_select',
//			url:'loadMiniPrints.php'
//			
//		});
//	};

	setSize = function(style) {
		if (style==='Male'){
		$('Size').replace("<select id=Size name=Size><option value=\"S\">Small</option><option value=\"M\">Medium</option><option value=\"L\">Large</option><option value=\"XL\">X-Large</option><option value=\"XXL(+2.00)\">XXL: Add $2</option><option value=\"3XL(+2.00\">3XL: Add $2</option></select>");
		} else {
		$('Size').replace("<select id=Size name=Size><option value=\"S\">Small</option><option value=\"M\">Medium</option><option value=\"L\">Large</option><option value=\"XL\">X-Large</option></select>");
		}
	};

	wlSubmit = function() {

		$('wailingListMemo').update('<ul><li><img id="wlStatus" src="http://site.despair.com/images/buttons/THANKS.gif" style="border:0;"></li></ul>');	
	};

	wlSubmit_list = function() {
		$('wailingListMemo').update('<ul><li style="width:250px;"><img id="wlStatus" src="http://site.despair.com/images/buttons/THANKS.gif" style="border:0;"></li></ul>');
	};
		
	showMouseOver = function(id) {
		var divLoad = id+'_image';
		$(divLoad).replace("<div style=\"product_image\" id=\""+id+"_image\"><a onmouseout=\"hideMouseOver('"+id+"');\" href=\"\"><img src=\"http://site.despair.com/images/despairWear/mouseover/"+id+".gif\" width=\"180\" height=\"180\"/></a></div>");
	};
	
	hideMouseOver = function(id) {
		var divLoad = id+'_image';
		$(divLoad).replace("<div style=\"product_image\" id=\""+id+"_image\"><a onmouseover=\"showMouseOver('"+id+"');\" href=\"\"><img src=\"http://site.despair.com/images/despairWear/"+id+".gif\" width=\"180\" height=\"180\"/></a></div>");
	};
	
	swap = function(show, hide) {
		show.style.display = "";
		hide.style.display = "none";
	};
	
	
}).apply(DSP);


function applyToolTip() {
		new Tip('243_text', "Just because you're necessary doesn't mean you're important.", {title : 'Worth'});
		new Tip('232_text', "There are no stupid questions, but there are a LOT of inquisitive idiots.", {title : 'Cluelessness'});
		new Tip('228_text', "When you wish upon a falling star, your dreams can come true.  Unless it's really a meteor hurtling to the earth which will destroy all life.  Then you're pretty much hosed no matter what you wish for.  Unless it's death by meteorite.", {title : 'Wishes'});
		new Tip('226_text', "It's only a virtue if you're not a screwup.", {title : 'Consistency'});
		new Tip('225_text', "It could be that the purpose of your life is only to serve as a warning to others.", {title : 'Mistakes'});
		new Tip('224_text', "Not everyone gets to be an astronaut when they grow up.", {title : 'Potential'});
		new Tip('230_text', "Attitudes are contagious.  Mine might kill you.", {title : 'Burnout'});
		new Tip('255_text', "If you think the problems we create are bad, just wait until you see our solutions.", {title : 'Government'});
		new Tip('259_text', "Just because you've always done it that way doesn't mean it's not incredibly stupid.", {title : 'Tradition'});

		$('demotivators_list').removeAttribute('onmouseover');
			
	};

function wlSubmit_list() {
	$('wl_text').innerHTML = "Thanks for Subscribing!";
};

function showImage(type,id,object){
	object.className='selected';
	if(type=='image') {
		if(id >= imageArray.length) id = 0;
		else if(id < 0) id = imageArray.length-1;			
		imagePointer=id;
		id = imageArray[id];
	}
	var superAjax = new Ajax.Updater('mainImg',"/despairwear/updateImage.php",{method: 'post', parameters: "&type="+type+"&id="+id});	
}
