//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',
			'evalResponse': true,
			'evalScripts': true
		});

		$$('.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: {
		moduleOptions: true
	},

	// 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() {
			this.load();
		}.bind(this));
	},

	load: function() {
		//add zip code
		if ($('txtZipCode') && Cookie.read('zipCode'))
			$('txtZipCode').set('value', Cookie.read('zipCode'));

		//load weather
		this.getWeather();

		if (this.options.moduleOptions) {


			$$('.moduleTitle').each(function(el) {
				var table = el.getParent('table').getNext('table').set('slide', {});
				var li = el.getParent('li').set('slide', {});

				if (!li.hasClass('no-disable')) {
					new Element('img', {
						'styles': {
							'float': 'right'
						},
						'events': {
							'click': function(e) {
								e.stop();

								li.slide('toggle');

								Cookie.write(li.get('id'), 'true', {path: '/'});
							}
						},
						'src': imagePath + 'module-close.png'
					}).inject(el, 'before');
				};

				new Element('img', {
					'styles': {
						'float': 'right'
					},
					'events': {
						'click': function(e) {
							e.stop();

							table.slide('toggle');

							Cookie.write(li.get('id'), 'minimized', {path: '/'});
						}
					},
					'src': imagePath + 'module-min-arrow.png'
				}).inject(el, 'before');

				if (Cookie.read(li.get('id')) == 'minimized')
					table.slide('hide');
			});
		};

		//enable shoutbox
		this.shoutbox('shoutbox');
	},

	openPMWindow: function() {

		if (Cookie.read('pmHide'))
			return;

		var pmWindow = $('pmNotice').setStyles({
			'position': 'absolute',
			'z-index': 100,
			'top': -300,
			'left': -300,
			'display': 'block'
		}).set('morph');

		var info = pmWindow.getSize();
		var win = window.getSize();

		var pmOverlay = new Element('div', {
			'id': 'pmOverlay',
			'styles': {
				'position': 'absolute',
				'z-index': 2,
				'top': 0,
				'left': 0,
				'opacity': 0.5,
				'background': '#000000',
				'height': 3000,
				'width': win.x
			}
		}).inject(document.body);

		//removes PM
		function pmRemove() {
			$('pmNotice').morph({
				'opacity': 0
			});
			$('pmOverlay').morph({
				'opacity': 0
			});

			Cookie.write('pmHide', true);

			window.removeEvent('resize', pmResize);
			window.removeEvent('scroll', pmScroll);
		};

		//get the new browser size
		function pmResize() {
			if (!pmWindow) return;

			info = pmWindow.getSize();
			win = window.getSize();

			window.fireEvent('scroll');
		};

		//move PM notice into view
		function pmScroll() {
			if (!pmWindow) return;

			pmWindow.morph({
				'top': ((win.y / 2)-(info.y/2)) + window.getScroll().y,
				'left': ((win.x / 2)-(info.x/2))
				
			});
			//ben müdahale ettim
			//if (Cookie.read('pmHide'))
			//{
				///pmRemove();
				//}
				//else
				//{
				//Cookie.write('pmHide')	
				//}
			Cookie.write('pmHide', true);
		};

		$('pmClose').addEvent('click', function(E) {
			E.stop();

			//remove window
			pmRemove();
		});

		//events
		window.addEvent('resize', pmResize);
		window.addEvent('scroll', pmScroll);
		window.fireEvent('scroll');
	},
	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', 'Yükleniyor, Lütfen Bekleyiniz !');

		var ajaxComplete = function() {
			//clear loading message for the first time
			if (box.get('html') == 'Yükleniyor, Lütfen Bekleyiniz !')
			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': 'Sil?',
						'title': 'Sil?',
						'src': imagePath + 'delete.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);
	}

});

//load portal engine
portalEngine = new iPortalX();

//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) {	
			//alert(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.width = this.options.max;
		} else if (el.height > this.options.max) {
			el.height = this.options.max;
		} 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'
		});
	}
});


//setup some stuff once the html code 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
	});

	//setup accordions for news items
	if ($$('.accord-title')) new Accordion($$('.accord-title').setStyle('cursor','pointer'),$$('.accord-body'));
});

//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) {
  	window.open(theURL,winName,features);
}

//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;
	}
}
