/**
 * Kitzbuehel Tourenportal
 * init loader
 */

var loader = new Loader();
loader.setScriptPath("/static/js/");
loader.addFile("lib/prototype.js");

loader.addFile("lib/animator.js");
loader.addFile("lib/swfobject.js");
loader.addFile("lib/trimpath-template.js");
loader.addFile("lib/input-file-style.js");

//loader.addClass("at.elements.lightbox");

loader.addFile("src/scriptaculous.js");
loader.addFile("src/effects.js");
loader.addFile("lightbox.js");

loader.addClass("at.elements.tooltip");
loader.addClass("at.elements.textreplace.image");

loader.addClass("at.kitzbuehel.navigation.switchPicture");
loader.addClass("at.kitzbuehel.navigation.hover");
//loader.addClass("at.kitzbuehel.lightbox");
loader.addClass("at.kitzbuehel.slider.fade");
loader.addClass("at.kitzbuehel.voting.tour");
loader.addClass("at.kitzbuehel.controller.double");
loader.addClass("at.kitzbuehel.textarea");

debug = true;

if(debug) {
	loader.load();
}
else {
	loader.loadProxy("/" + language + "/intern/script-proxy");
}

// IE DEBUG HACK

if(typeof console == "undefined") {
	var console = {
		log : function () {return false;}
	}
}

var lightbox = null;

function DocumentLoad () {
	/**
	 * Main Menu
	 */
	if ($('menu')  !== null) {
		new at.kitzbuehel.navigation.hover([ $$('#menu ul li')]);
		new at.kitzbuehel.navigation.switchPicture([ $$('#menu ul li.main ')]);
	}
	if ($('download') !== null ){
		new at.kitzbuehel.navigation.hover([ $$('#download li')]);
	}
	if ($('console') !== null ){
		new at.kitzbuehel.navigation.switchPicture([ $$('#console .sports a ')]);
	}

	/**
	 * Lightbox
	 */

	try {
		initLightbox();
		// lightbox = new at.kitzbuehel.lightbox();
	}
	catch (e) {
	}
	/**
	 * Slide Effect in Tour Details
	 */
	try {
		tourSlider = new at.kitzbuehel.slider.fade( $$('#tourslide div.box'));
	}
	catch (e) {}
	/*
	 * Sliding Effect in Reviews
	 */
	try {
		reviewSlider = new at.kitzbuehel.slider.fade( $$('#reviews div.review'));
	}
	catch (e) {}
	/*
	 * Voting
	 */
	try {
		reviewSlider = new at.kitzbuehel.voting.tour( $$('#tourslide input.vote'));
	}
	catch (e) {}
	/**
	 * Search Sliding Controller
	 */
	try {
		var doubles = document.getElementsByClassName('controlldouble');
		if(doubles.length > 0) {
			for(j=0; j<doubles.length; j++ ) {
				new at.kitzbuehel.controller.double({
					element: doubles[j]
				});
			}
		}
	}
	catch (e) {}

	if ($("vote_form") !== null ) {

		// disable form
		$("vote_form").onsubmit = function () {return false;}

		// ad event handler
		$("vote_form").observe("submit",function () {
			$("vote_form").request({
				onComplete: function(transport) {
					var response = transport.responseText;
					alert (response);
				}
			});
		});
	}
	/**
	 * Dynamic textareas
	 */
	try {
		var textareas = document.getElementsByTagName('textarea');

		if(textareas.length > 0) {
			for(j=0; j<textareas.length; j++ ) {
				new at.kitzbuehel.textarea({
					element: textareas[j]
				});
			}
		}
	}
	catch (e) {}
	/**
	 * Tooltip
	 */
	var template = '<div class="mytooltip">${text}</div>';

	tooltip_container = $$('.tooltip');
	if(tooltip_container.length > 0) {
		for(i=0; i<tooltip_container.length; i++) {
			try {
				var data = {
					text: tooltip_container[i].getElementsBySelector(".tooltiptext")[0].innerHTML
				};
				new at.elements.tooltip(tooltip_container[i].getElementsBySelector(".name")[0],{
					offsetY: 0,
					offsetX: 15,
					template: template,
					data: data
				});
			}
			catch (e) {}
		}
	}
	/**
	 * Image Replacement (for Mac)
	 */
	var imageReplacer = new at.elements.textreplace.image("/cache/headlines/",{appendFilename: true});

	imageReplacer.add({
		elements: $$('#portal #search h1.img'),
		options: {
		font: 'TheSans-B7Bold.ttf',
		width: '175',
		height: '',
		color: 'f50000',
		size: '12',
		positionx: 0,
		positiony: 12,
		replaceElement: {
			tagName: "h1",
			className: "imgdone"
		},
		modifyValue : function (vari) {
			return vari.toUpperCase();
		}
	}});
	imageReplacer.add({
		elements: $$('#portal #toptours h1.img'),
		options: {
		font: 'TheSans-B7Bold.ttf',
		width: '335',
		height: '',
		color: 'f50000',
		size: '12',
		positionx: 0,
		positiony: 12,
		replaceElement: {
			tagName: "h1",
			className: "imgdone"
		},
		modifyValue : function (vari) {
			return vari.toUpperCase();
		}
	}});
	imageReplacer.add({
		elements: $$('#portal #news h2'),
		options: {
		font: 'TheSans-B7Bold.ttf',
		width: '175',
		height: '',
		color: 'CCCCCC',
		size: '10',
		positionx: 0,
		positiony: 10,
		replaceElement: {
			tagName: "h2",
			className: "imgdone"
		},
		modifyValue : function (vari) {
			return vari.toUpperCase();
		}
	}});
	imageReplacer.add({
		elements: $$('#portal #weather h2'),
		options: {
		font: 'TheSans-B7Bold.ttf',
		width: '175',
		height: '',
		color: 'CCCCCC',
		size: '10',
		positionx: 0,
		positiony: 10,
		replaceElement: {
			tagName: "h2",
			className: "imgdone"
		},
		modifyValue : function (vari) {
			return vari.toUpperCase();
		}
	}});

	imageReplacer.add({
		elements: $$('#content.userscreen h1.img'),
		options: {
		font: 'TheSans-B7Bold.ttf',
		width: '520',
		height: '',
		color: 'f50000',
		size: '12',
		positionx: 0,
		positiony: 12,
		replaceElement: {
			tagName: "h1",
			className: "imgdone"
		},
		modifyValue : function (vari) {
			return vari.toUpperCase();
		}
	}});

	imageReplacer.add({
		elements: $$('#content h1.img'),
		options: {
		font: 'TheSans-B7Bold.ttf',
		width: '325',
		height: '',
		color: 'f50000',
		size: '12',
		positionx: 0,
		positiony: 12,
		replaceElement: {
			tagName: "h1",
			className: "imgdone"
		},
		modifyValue : function (vari) {
			return vari.toUpperCase();
		}
	}});

	imageReplacer.start();

	/**
	 * File inputs Replace
	 */
	SI.Files.stylizeAll();

}

function DocumentUnLoad () {
	if ($('map') != null) {
		GUnload();
	}
}

function gotoLink (htmlElement) {
	if(htmlElement.href) {
		location.href = htmlElement.href;
	}
	else {
		if(htmlElement.childNodes) {
			if(htmlElement.getElementsByTagName("a")) {
				location.href = htmlElement.getElementsByTagName("a")[0].href;
			}
		}
	}
}

function popupopen(url) {
	mypopup = window.open(url, "popup", "width=570,height=600,left=100,top=200,scrollbars=yes,status=no,toolbar=no,menubar=no,resizeable=no");
	mypopup.focus();
}

function popupclose() {
	window.close();
}

window.onunload = DocumentUnLoad;
