//ratings engine
var ratings = new Class({

	Implements: [Options],

	// set default options
	options: {
		url: '/_ajax_ratings.asp'
	},

	// do initialize
	initialize: function(tabElement, options) {
		// input custom options
		this.setOptions(options);

		//if no rate items then exit
		if (!$$('.rate-item')) return;

		var ajax = new Request({
			'url': this.options.url,
			'method': 'get'
		});

		$$('.rate-item').each(function(el) {
			var rated = false;
			var ratings = el.getElement('.ratings');

			if (!ratings) return;

			ratings.set({
				'styles': {
					'opacity': 0,
					'display': 'block'
				}
			});

			el.addEvents({
				'mouseenter': function(e) {
					if (rated) return;

					ratings.morph({'opacity': 1});
				},
				'mouseleave': function(e) {
					if (!rated) ratings.morph({'opacity': 0});
				}
			});

			ratings.getElements('a').each(function(_el) {
				_el.addEvent('click', function(e) {
					e.stop();

					rated = true;

					el.getElement('.ratings').set('html', 'Rated!').morph({'opacity': 1});

					ajax.send('IID=' + this.get('ref') + '&Rating=' + this.get('title'))
				});
			});

		}.bind(this));
	}
});

//iportalx portal engine
var iPortalX = new Class({

	Implements: [Events, Options],

	// set default options
	options: {},

	// do initialize
	initialize: function(options) {
		// input custom options
		this.setOptions(options);

		//will hold shoutbox messages
		this.messages = [];

		//add event to load up the stuff needed after HTML is ready
		window.addEvent('domready', function() {
			//add zip code
			if ($('txtZipCode') && Cookie.read('zipCode'))
				$('txtZipCode').set('value', Cookie.read('zipCode'));

			//load weather
			this.getWeather();

			//enable shoutbox
			this.shoutbox('shoutbox');
		}.bind(this));
	},
	shoutbox: function( box ) {
		//make sure its an element
		box = $(box);

		if (!box) return;

		form = $(box.get('id') + '-form');

		var scroller = new Fx.Scroll(box);

		//no shoutbox so exit
		if (!box || !form) return;

		//loading message
		box.set('html', 'Loading Shoutbox Messages... Please wait.');

		var ajaxComplete = function() {
			//clear loading message for the first time
			if (box.get('html') == 'Loading Shoutbox Messages... Please wait.')
			box.set('html', '');

			//loop through all messages
			this.messages.each(function(item) {
				//get shout item
				var shout = item[0];

				if ($('shoutbox-shout-' + shout.id) && shout.deleted == true) {
					$('shoutbox-shout-' + shout.id).dispose();
				}

				//if shout already exists or deleted remove and exit
				if ($('shoutbox-shout-' + shout.id) || shout.deleted) {
					return;
				};

				//add shout message
				var newShout = new Element('div', {
					'id': 'shoutbox-shout-' + shout.id,
					'class': 'smText',
					'html': '<b>' + shout.author + ':</b> ' + shout.message
				}).setStyles({'padding-bottom': 4, 'opacity': 0}).inject('shoutbox').morph({'opacity': 1});

				var options = new Element('div', {'class': 'smText'}).adopt(
					new Element('span', {
						'html': ' ' + shout.time,
						'styles': {
							'font-size': 9
						}
					})
				).inject(newShout, 'bottom');

				if (shout.admin) {
					new Element('img', {
						'alt': 'Delete Shout?',
						'title': 'Delete Shout?',
						'src': imagePath + 'delete_icon.png',
						'align': 'absmiddle',
						'events': {
							'click': function() {
								shoutboxAjax.cancel();

								new Request({
									'url': form.get('action') + '?DID=' + shout.id,
									'method': 'get',
									'evalResponse': true,
									'evalScripts': true,
									'onComplete': ajaxComplete
								}).send();
							}
						},
						'styles': {
							'cursor': 'pointer'
						}
					}).inject(options, 'top');

					new Element('img', {
						'alt': 'IP Ban',
						'title': 'IP Ban?',
						'src': imagePath + 'ban.png',
						'align': 'absmiddle',
						'events': {
							'click': function() {
								openWin(extraForumPath + 'pop_up_IP_blocking.asp?IP=' + shout.ip,'BanIP','toolbar=0,location=0,status=0,menubar=0,scrollbars=1,resizable=1,width=425,height=425');
							}
						},
						'styles': {
							'cursor': 'pointer'
						}
					}).inject(options, 'top');
				};

				//scroll to new shout
				scroller.cancel().toElement(newShout);

				//get new messages in 3 seconds
				(function() {
					//scroll to new shout
					scroller.cancel().toElement(newShout);
				}).delay(3000);
			});

			//get new messages in 3 seconds
			(function() {
				shoutboxAjax.send();
			}).delay(3000);

			if ($('txtShoutBtn').disabled) {
				$('txtShoutBtn').set('disabled',false);
				$('txtShout').set({'disabled':false,'value': ''});
			};
		}.bind(this);

		//create form for submitting
		form.set({
			//set send
			'send': {
				'evalScripts': true,
				'evalResponse': true,
				'onComplete': ajaxComplete
			},
			//events
			'events': {
				//add submit event
				'submit': function(E) {
					//stop events
					E.stop();

					shoutboxAjax.cancel();

					//send form
					this.send();

					$('txtShoutBtn').set('disabled',true);
					$('txtShout').set('disabled',true);
				}
			}
		});

		var shoutboxAjax = new Request({
			'url': form.get('action'),
			'method': 'get',
			'evalResponse': true,
			'evalScripts': true,
			'onComplete': ajaxComplete
		});

		//get items
		shoutboxAjax.send();
	},

	moduleToggle: function( tableID ) {
		var module = $(tableID);
		var moduleImage = $(tableID + '_image');

		module.get('slide').toggle();

		(function() {
			Cookie.write(tableID, module.get('slide').open);
		}).delay(550);

		//change the image if there is one
		if (moduleImage) {
			moduleImage.set('src', (module.get('slide').open ? moduleImage.src.replace('min','max') : moduleImage.src.replace('max','min')));
		};
	},

	getWeather: function() {
		if (!$('txtZipCode')) return;

		zipCode = $('txtZipCode').get('value').toInt();

		$('weatherBox').set('html', 'Loading Weather... please wait.');

		//save choice
		if (zipCode) Cookie.write('zipCode', zipCode);

		new Request({
			'method': 'get',
			'evalScripts': true,
			'evalResponse': true,
			'url': extraForumPath + 'functions/mods/weather/get_weather.asp'
		}).send('Path=' + extraForumPath + 'functions/mods/weather/&zipCode=' + zipCode);
	}

});

var portalEngine = new iPortalX();
//add event to load up the stuff needed after HTML is ready
window.addEvent('domready', function() {
	//comment out to remvoe ratings
	new ratings();

	//setup image resizer for forum posts
	new imageResizer('.forum-post img', {
		max: 400	//change to whatever max size you want
	});
});
		
		


//image resizer engine
var imageResizer = new Class({
	Implements: [Options],

	// set default options
	options: {
		max: 600
	},

	// do initialize
	initialize: function(el, options) {
		// input custom options
		this.setOptions(options);

		this.el = $$(el);

		//get window sizes
		this.options.windowWidth = window.getSize().x - 100;
		this.options.windowHeight = window.getSize().y - 100;

		this.el.each(function(el) {	
			//check check when loaded
			el.addEvent('load', function(e) {
				this.resize(el);
			}.bind(this));

			//run resize incase its done loading before the load event can be called
			this.resize(el);
		}.bind(this));
	},

	resize: function(el) {
		if (el.width > this.options.max || el.height > this.options.max) {
			var ratio = Math.min( this.options.max / el.width, this.options.max / el.height );
			
			el.width = ratio * el.width;
			el.height = ratio * el.height;
		} else {
			return;
		};

		new Element('a', {
			'href': '/_image_resize.asp?Height=' + this.options.windowHeight + '&Width=' + this.options.windowWidth + '&Path=' + unescape(el.src),
			'rel': 'lightbox'
		}).wraps(el).slimbox();

		el.setStyles({
			'display': 'block',
			'padding': 4,
			'background': '#FFF',
			'border': '2px solid #000'
		});
	}
});


//Function to jump to another forum
function ForumJump(URL) {
	if (URL.options[URL.selectedIndex].value != "") self.location.href = URL.options[URL.selectedIndex].value;
	return true;
}

//Function to open pop up window
function openWin(theURL,winName,features) {
	//if (theURL == '') {
		window.open(theURL,winName,features);
	/*
	} else {
		var ops = features.split(',');
		var hash = new Hash();

		for (var i=0; i < ops.length; i++) {
			var row = ops[i].split("=");
			hash.include(row[0], row[1]);
		}
		
		if (winName=='profile') {
			winName = 'Member Profile';
			
			hash.set('width', 550);
			hash.set('height', 700);
		}

		light = new LightFace.IFrame($merge({
			url: theURL, 
			title: winName
		}, hash)).addButton('Close', function() { 
			light.close(); 
		},true).open();
	}
	*/
}

//Function to open preview post window
function OpenPreviewWindow(targetPage, formName){
	var now = new Date();

	//Open the window first
   	openWin('','preview','toolbar=0,location=0,status=0,menubar=0,scrollbars=1,resizable=1,width=680,height=400')

   	//Now submit form to the new window
   	formName.action = targetPage + "?ID=" + now.getTime();
	formName.target = "preview";
	formName.submit();
}

//Function to check avatar image
function AvatarImage(objImage, newSize) {
	imgWidth = objImage.width;
	imgHeight = objImage.height;

	if (imgWidth > newSize) {
		objImage.width = newSize;
	}

	if (imgHeight > newSize) {
		objImage.height = newSize;
	}
}

function ChangeSkin(URL) {

   if (URL.options[URL.selectedIndex].value != "") self.location.href = URL.options[URL.selectedIndex].value;

   return true;

   }
//Function to check form is filled in correctly before submitting

function CheckQuickForm () {

	var errorMsg = "";

	//Check for a Username

	if (document.frmQuickLogin.name.value==""){

		errorMsg += "\n\tUsername \t- Enter your Forum Username";

	}

	//Check for a Password

	if (document.frmQuickLogin.password.value==""){

		errorMsg += "\n\tPassword \t- Enter your Forum Password";

	}

	//If there is aproblem with the form then display an error

	if (errorMsg != ""){

		msg = "_______________________________________________________________\n\n";

		msg += "The form has not been submitted because there are problem(s) with the form.\n";

		msg += "Please correct the problem(s) and re-submit the form.\n";

		msg += "_______________________________________________________________\n\n";

		msg += "The following field(s) need to be corrected: -\n";

		errorMsg += alert(msg + errorMsg + "\n\n");

		return false;

	}

	return true;

}

function userP( intProfileID ) {
	openWin('forum/pop_up_profile.asp?PF=' + intProfileID,'profile','toolbar=0,location=0,status=0,menubar=0,scrollbars=1,resizable=1,width=590,height=425');
}

