// init loader
var loader = new Loader();
loader.setScriptPath("/static/js/");

loader.addClass("at.thermenland.navigation.hover");
loader.addClass("at.thermenland.emotion.checkboxmenu");
loader.addClass("at.thermenland.emotion.boxslider");

loader.load();

window.setTimeout(function () {
    var externalLinks = $$("a");

    for(var i = 0; i < externalLinks.length; i++) {
       if (externalLinks[i].getAttribute("href")) {
           if(externalLinks[i].getAttribute("href").indexOf("http") >= 0) {
               if(externalLinks[i].getAttribute("href").indexOf("thermenland.at") < 0) {
                  var hostname = parseUri(externalLinks[i].getAttribute("href")).host;
                  externalLinks[i].setAttribute("trackKey",hostname);
                  externalLinks[i].onclick = externalLinkClick;
               }
           }
       }
    }
},800);

function externalLinkClick () {
    var trackKey = this.getAttribute("trackKey");
    _gaq.push(['_trackPageview','/external/' + trackKey]);
}

function parseUri (str) {
    var o   = parseUri.options,
    m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
    uri = {},
    i   = 14;

    while (i--) uri[o.key[i]] = m[i] || "";

    uri[o.q.name] = {};
    uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
       if ($1) uri[o.q.name][$1] = $2;
    });

    return uri;
}

parseUri.options = {
    strictMode: false,
    key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
    q:   {
                   name:   "queryKey",
                   parser: /(?:^|&)([^&=]*)=?([^&]*)/g
    },
    parser: {
                   strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
                   loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
    }
};

function onDocumentLoad () {
	
	try {
		new at.thermenland.main_navigation.hover( [$$('#main_nav li')], [$$('#main_nav li ul li')], [$$('#main_nav li ul li ul li')] );	
	} catch (e) {}

	// navigation
	try {
		new at.thermenland.navigation.hover( [ $$('#tabmenu li') ] );
	} catch (e) {}
	
	// tabbing menu - checkboxes
	try {
		new at.thermenland.emotion.checkboxmenu( $$('#tabmenu ul li'), true );
		new at.thermenland.emotion.checkboxmenu( $$('#results_filter label'), false );
	} catch (e) {}
	
	// portal teaser slider
	try {
		if($('portal_slider') != null) {
			portal_slider = new at.thermenland.portalslider.teaserslider($('portal_slider'));
		}
	} catch (e) {}
	
	try {
		if($('quicksearch') != null) {
			new at.thermenland.portalslider.quicksearch($('quicksearch'));
		}
	} catch (e) {}
	
	try {
		if($('sidebar_qs') != null) {
			new at.thermenland.contentslider.quicksearch($('sidebar_qs'));
		}
	} catch (e) {}
	
	try {
		if($('quicksearch') != null || $('sidebar_qs') != null) {
			new at.elements.calendar.init({
				button : 'duration_a',
				output : "input",
				dayInput: 'aDay',
				monthInput: 'aMonth',
				yearInput: 'aYear',
				updateFieldOnStartup : true
			});
			new at.elements.calendar.init({
				button : 'duration_b',
				output : "input",
				dayInput: 'bDay',
				monthInput: 'bMonth',
				yearInput: 'bYear',
				updateFieldOnStartup : true
			});
		}
	} catch (e) {}


    try {
   		if($('customized') != null) {
   			new at.thermenland.popup.popup2($$('#customized'), $('ttipp'));
   		}
   	} catch (e) {}





	try {
		if($('site_specials') != null) {
			new at.thermenland.popup.popup($$('#site_specials .specials_button'), $('package_box'));
		}
	} catch (e) {}

	try {
		if($('site_languages') != null) {
			new at.thermenland.popup.popup($$('#site_languages .languages_button'), $('languages_box'));
		}
	} catch (e) {}
	
	try {
		var faq_lists = $$('.faq_list');
		if(faq_lists != null) {
			new at.thermenland.contentslider.boxes();
		}
	} catch (e) {}
	
	try {
		if($('tabbox') != null) {
			new at.thermenland.contentslider.tabs($('tabbox'));
		}
	} catch (e) {}

	try {
		if($('package_form') != null) {
			new at.elements.calendar.init({
				button : 'duration_from',
				output : "input",
				dayInput: 'fd',
				monthInput: 'fm',
				yearInput: 'fy',
				updateFieldOnStartup : true
			});
			new at.elements.calendar.init({
				button : 'duration_to',
				output : "input",
				dayInput: 'td',
				monthInput: 'tm',
				yearInput: 'ty',
				updateFieldOnStartup : true
			});
		}
	} catch (e) {}

	try {
		new at.thermenland.emotion.boxslider( $$('#content .contentcenter .left .box'), 33, 506, 473);
	} catch (e) {}
	
	try {
		if($('background') != null) {
			window.setTimeout(function () {
				resizeFlashBackground();
			},500);
			window.onresize = resizeFlashBackground;
		}
	} catch (e) {}

	try {
		initLightbox();
	} catch (e) {}

}

function showSelectBoxes(){
	var selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
}

function hideSelectBoxes(){
	var selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
	}
}

function GetWindowSize (w) {
    w = window;
    var width = w.innerWidth || (w.document.documentElement.clientWidth || w.document.body.clientWidth);
    var height = w.innerHeight || (w.document.documentElement.clientHeight || w.document.body.clientHeight);
    return [width, height]
}

function resizeFlashBackground () {
	
	var size = GetWindowSize();
	$("backgroundFlash").setAttribute("width",size[0]);
	$("backgroundFlash").setAttribute("height",size[1]);
}


function gotoLink (htmlElement) {
	
	var destinationURL = "";
	var target = "";
	
	if(htmlElement.href) {
		destinationURL = htmlElement.href;
		target = htmlElement.getAttribute("target");
	}
	else {
		if(htmlElement.childNodes) {
			if(htmlElement.getElementsByTagName("a")) {
				destinationURL = htmlElement.getElementsByTagName("a")[0].href;
				target = htmlElement.getElementsByTagName("a")[0].getAttribute("target");
			}
		}
	}
	
	if(destinationURL.length > 0) {
		if(target == "_blank") {
			window.open(destinationURL);
		}
		else {
			location.href = destinationURL;
		}
	}
	return false;
}

