/* embed flash (IE activex fix) */

function embedFlash(file, flashvars, width, height)
{
	document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.adobe.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+ width +'" height="'+ height +'" id="' + file + '" align="middle">\n');
	document.write('<param name="allowScriptAccess" value="sameDomain" />\n');
	document.write('<param name="movie" value="'+ file +'" />\n');
	document.write('<param name="FlashVars" value="'+ flashvars +'" />\n');
	document.write('<param name="quality" value="high" />\n');
	document.write('<param name="wmode" value="transparent" />\n');
	document.write('<embed wmode="transparent" src="'+ file +'" FlashVars="'+ flashvars +'" quality="high" bgcolor="#000000" width="'+ width +'" height="'+ height +'" name="' + file + '" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />\n');
	document.write('</object>\n');
}

// mootools

window.addEvent('domready', function() {

	// tab tootltips

	var tooltips = new Tips($$('.tabTip'), {
		className: 'tabTip',
		initialize:function(){
			this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 250, wait: false}).set(0);
		},
		onShow: function(toolTip) {
			this.fx.start(1);
		},
		onHide: function(toolTip) {
			this.fx.start(0);
		}
	});

	// accordion

	new Accordion($$('div#accordionExample .stretchtoggle'), $$('div#accordionExample .stretcher'),
		{
		onActive: function(tog){
			//tog.setStyle('display', 'none');
		},
		show: function(tog){
			alwaysHide: true;
			//tog.setStyle('display', 'none');
		},
		onComplete: function(tog) {
			var tog = $(this.elements[this.previous]);
			if (tog.offsetHeight > 0) tog.setStyle('height', 'auto');
		}
	});

});

function loadAjaxForms()
{
	window.addEvent('domready', function() {

		logWrapper = new Array();

		$$('.ajaxLog').each(function(el, index) {
			logWrapper[index] = el;
		});

		// create logs
		$$('form.ajax').each(function(el, index) {
			logs = [];
			logs[index] = new Element('div', {
				'styles': {
					'overflow': 'auto'
				},
				'id': 'log' + index
			}).injectInside(logWrapper[index]);

			el.addEvent('submit', function(e) {

				new Event(e).stop();

				log = logWrapper[index].empty().addClass('ajax-loading');

				this.send({
					update: log,

					onComplete: function() {
						log.removeClass('ajax-loading');
					},
					onSuccess: function() {
						log.getNext().remove();
					},
					onFailure: function() {
						log.setHTML(this.transport.responseText);
						log.removeClass('ajax-loading');
					}
				});
			});

		});

	});
}

function submitGuide(type)
{
	window.addEvent('domready', function() {

		var myForm = $E('form.ajax');

		var log;

		if (type == 3) {
			// create rating hidden input
			ratingEl = new Element('input', {
				'type': 'hidden',
				'name': 'rating',
				'value': 1
			}).injectAfter($('sortKarakter'));

			if (!rating) {
				rating = 1;
			}

			var rating = ratingEl.value;
		}

		// send form
		function sendForm(rating)
		{

			log = $E('div#log').empty().addClass('ajax-loading');

			if (type == 3) {
				if (!rating) {
					rating = ratingEl.value;
				} else {
					ratingEl.setProperty('value', rating);
				}
			}

			myForm.send({
				update: log,
				onSuccess: function() {
					log.removeClass('ajax-loading');
				},
				onComplete: function() {
					log.removeClass('ajax-loading');
				},
				onFailure: function() {
					log.setHTML(this.transport.responseText);
					log.removeClass('ajax-loading');
				}
			});
			if (type == 3) {
				myElement.getChildren().each(function(el, index) {
					if (index < rating) {
						el.addClass('active');
					}
				});
				initState = rating;
				if (initState == '') initState = 1;
			}
		}

		// submit form
		myForm.addEvent('submit', function(e) {

			new Event(e).stop();

			sendForm(rating);

		});

		if (type == 3) {

			// setup rating images and add events
			myElement = $('sortKarakter');

			var num = myElement.getChildren().length;
			if (!initState) {
				var initState = 0;
			}

			myElement.getChildren().each(function(el, index) {
				if (el.hasClass('active')) {
					initState++;
				}

				el.addEvents({
					'click': function() {
						sendForm(index+1);
					},
					'mouseenter': function() {
						myElement.getChildren().each(function(subEl, subIndex) {
							if (subIndex <= index) {
								subEl.addClass('active');
							} else {
								subEl.removeClass('active');
							}
						});
					},
					'mouseleave': function() {
						el.removeClass('active');
					}
				});

				myElement.addEvent('mouseleave', function() {
					myElement.getChildren().each(function(el, index) {
						if (index < initState) {
							el.addClass('active');
						} else {
							el.removeClass('active');
						}
					});
				});
			});
		}

		// sort submit
		$ES('select', myForm).each(function(el, index) {
			el.addEvent('change', function() {
				sendForm();
			});
		});

		// facts submit
		$ES('input', myForm).each(function(el, index) {
			el.addEvent('click', function() {
				sendForm();
			});
		});

	});
}