var timerId;
var recaptchaPublicKey = '6LdntQYAAAAAADdejfLK4X5rojFsZu5YYom6aqtw';
var RecaptchaOptions = {
    theme: 'custom',
    callback: function() { 
      Recaptcha.old_finish_reload = Recaptcha.finish_reload;
      Recaptcha.finish_reload = function(a, b, c, d) {
        Recaptcha.old_finish_reload(a, b, c, d);
        RecaptchaResizeImage();
      }
      RecaptchaResizeImage();
    }
  };



$(window).resize(function () {
	if ($('#containerHome').length > 0) {
		resizeFlashContainer();
	}
});


$(document).ready(function () {
	
	if ($('#containerHome').length > 0) {
		timerId = setTimeout(resizeFlashContainer, 900);
		startFlashMovie();
		
	}
	
	if ($('#header-syntegration').length > 0) {
		startFlashHeader();
	}
	
	startHomeMenu();
	startSifr();
	startToggleCurriculum();
	startFlashLinkEntryPage();
	
	var conf =  {
		imageLoading:'_img/_common/lightbox-ico-loading.gif',
		imageBtnPrev:'_img/_common/lightbox-btn-prev.gif',
		imageBtnNext:'_img/_common/lightbox-btn-next.gif',
		imageBtnClose:'_img/_common/lightbox-btn-close.gif',
		imageBlank:'_img/_common/lightbox-blank.gif'
	}
	
	try {
		$('a.lightbox').lightBox(conf);
	
		$('a.lightbox-flash').lightBoxFlash(getFlashData('a.lightbox-flash'));
		$('a.lightbox-flash0').lightBoxFlash(getFlashData('a.lightbox-flash0'));
		$('a.lightbox-flash1').lightBoxFlash(getFlashData('a.lightbox-flash1'));
		$('a.lightbox-flash2').lightBoxFlash(getFlashData('a.lightbox-flash2'));
		$('a.lightbox-flash3').lightBoxFlash(getFlashData('a.lightbox-flash3'));

	}
	catch (e) { }
	
	bindTrackLinks(s);
	
});

function bindTrackLinks(s) {
	$('a').each(function () {
		var link = this;
		$(link).click(function () {
			trackLink(link, s);
			//return false;
		});
	});
}

function trackLink(link, s) {
	var pageFileName = getFileName();
	var pageTitle = $(document).attr('title');
	var headLine = $('h2.e9-headline > span').html() || '';
	var subHeadLine = $('h3.e19-subline > span').html() || '';
	var linkLabel = $(link).text().replace(/\n/g, '').replace(/\t/g, '');
	var linkHref = $(link).attr('href');
	var linkTitle = $(link).attr('title');
	var linkTarget = $(link).attr('target');
	var searchText = $('#suche').val();
	var channel = '';
	var prop1 = '';
	var prop2 = '';
	var prop3 = '';
	
	var pageName = pageFileName + ' ';
	pageName += (headLine || pageTitle);
	
	headLine = headLine.replace(/<br>/g, '');
	subHeadLine = subHeadLine.replace(/<br>/g, '');
	linkLabel = linkLabel.replace(/<br>/g, '');
	
	if (linkHref.search('flashData=') != -1) {
		var arrFlashData = eval('('+ linkHref.split('flashData=')[1] +')');
		linkHref = arrFlashData[0];
		linkLabel = pageFileName +': media: '+ linkHref;
	}
	
	if (!linkLabel) {
		linkLabel = $(link).attr('title');
	}
	
	if (linkHref.search('http') == 0) {
		var linkType = 'e';
	}
	else if (
			(linkHref.search('.pdf') == linkHref.length - 4) ||
			(linkHref.search('.swf') == linkHref.length - 4) ||
			(linkHref.search('.xls') == linkHref.length - 4) ||
			(linkHref.search('.doc') == linkHref.length - 4)
		
		) {
		var linkType = 'd';
		prop1 = linkHref;
		linkLabel = pageFileName +': '+ linkLabel +': '+ linkHref;
	}
	else {
		var linkType = 'o';
	}
	
	
	if ($(link).parents('#menu').length > 0) {
		channel = 'Toolbar';
		linkLabel = pageName +': Toolbar: '+ linkLabel;
	}

	var s = s_gi(s_account);
	s.linkTrackVars = "channel,prop1,prop2";
	s.pageName = pageName;
	s.channel = channel;
	//s.prop1 = linkHref;
	//s.prop2 = searchText;
	
	//showLogTracker(link,linkType,linkLabel);
	s.tl(link,linkType,linkLabel);

}

function getFileName() {
	var pageFileName = document.location.toString().split('/').pop().split('\.')[0];
	
	if (pageFileName == 'home' || $(document).attr('title') == 'Errors') {
		return '';
	}
	
	pageFileName = pageFileName.replace('search:', ': ');
	
	return pageFileName;
}

function showLogTracker(link,linkType,linkLabel) {
	console.log(s);
	console.log(s.pageName);
	console.log(s.channel);
	console.log(s.prop1);
	console.log(s.prop2);
	if (link) {
		console.log(linkType);
		console.log(linkLabel);
	}
}


function getFlashData(selector) {
	if ($(selector).length == 0) 
		return {}
	
	var strData = $(selector).attr('href');
	if (strData.search('flashData=') == -1) 
		return {}

	var arrData = strData.split('flashData=');
	var obj = eval('('+ arrData[1] +')');
	var conf =  {
		callBack: function () {
			loadFlashLayer(obj[0], obj[1], obj[2], obj[3], obj[4], obj[5], 1000);
		}
	}
	
	return conf;
}

function startHomeMenu() {
	/*
	$('.listLinks li a').click(function() {
		$(this).parent().find('.listLinks:first').slideToggle();
		return false;
	});
	*/
	
	$('.slidesBox dt a').click(function() {
		$(this).parent().next().slideToggle();
		$(this).toggleClass('open');
		return false;
	});
	
	$('li.withSubmenu').hover(function () {
		$(this).find('ul.submenu:first').show();
	}, function () {
		$(this).find('ul.submenu:first').hide();
	});
	
	
	
}

function startIntroMovie() {	
	var so = new SWFObject("_media/intro.swf", "main", "100%", "100%", "8", "");
	so.addVariable('homeURL', 'home.html');
	so.addVariable('suitID', suitID);
	so.addParam('wmode', 'transparent');
	so.write('intro');
}

function startFlashMovie() {
	
	if ($('#headerHome').length == 0)
		return false;
	
	if (hasFlash) {
		var so = new SWFObject("_media/main.swf", "main", "100%", "100%", "8", "");
		so.addParam('wmode', 'transparent');
		so.write('headerHome');
	}
	else {
		//$('#headerHome').html('<img src="_img/_common/ex_flash_main.jpg" alt="Teaser Home" /><span class="break"><!-- --></span>');
		$('#headerHome').html('<div class="no-flash-top center png"><a class="btn-get-flash" href="http://www.adobe.com/go/DE-H-M-A2" target="_blank" title="Get Adobe Flash Player"><span>Get Adobe Flash Player</span></a><a class="mms-no-flash" href="2295.html" title="MMS">MMS</a></div>');
	}
	
}

function startFlashHeader() {
	
	if ($('#header-syntegration').length == 0)
		return false;
	
	if (hasFlash) {
		var so = new SWFObject("_media/header_syntegration_malik_CS3_2.swf", "main", "100%", "363", "8", "");
		so.addParam('wmode', 'transparent');
		so.write('header-syntegration');
	}
}

function startSifr() {
	
	$('h2.e9-headline, h3.e19-subline, div.e16-quote cite').css('color', '#535353');
	
	if(typeof sIFR == "function"){
		sIFR.replaceElement(named({sSelector:"h2.e9-headline", sFlashSrc:"_fonts/frutiger_roman.swf", sColor:"#535353", sWmode:"transparent"}));
		sIFR.replaceElement(named({sSelector:"h3.e19-subline", sFlashSrc:"_fonts/frutiger_roman.swf", sColor:"#666666", sWmode:"transparent"}));
		sIFR.replaceElement(named({sSelector:"div.e16-quote cite", sFlashSrc:"_fonts/walbaumOSSSK_italic.swf", sColor:"#3f3f3f", sWmode:"transparent"}));
		sIFR.replaceElement(named({sSelector:".maintenance-text p", sFlashSrc: "_fonts/frutiger.swf", sColor:"#535353", sWmode:"transparent"}));
	};
}


function showRecaptcha(element, themeName) {
	Recaptcha.create(recaptchaPublicKey, element, RecaptchaOptions);
}

function RecaptchaResizeImage() {
	$('#recaptcha_image, #recaptcha_image > img').css({ width: 285});       
};


function startToggleCurriculum() {
	if ($('body.v10').length == 0) return false;
	$('#personList li').click(function () {
		toggleCurriculum(this);
	});
}

function toggleCurriculum(element) {
	$('#personList li').removeClass('selected');
	$('#curriculumList li').removeClass('selected');
	$(element).addClass('selected');
	$('#personList li').each(function (i) {
		if ($(this).hasClass('selected')) {
			$('#curriculumList li')[i].className = 'selected';
			return true;
		}
	});
}


var seminaryFilter = {
	
	start: function () {
		$('div.e32-selector-without-pagination').removeClass('hidden');
		this.setDefaults();
		$('#btSeminaryFilter').click();
	},
	
	setDefaults: function () {
		$('#filterCritery').val('durchfuhrungsort');
		$('#filterCritery').change();
		$('select.durchfuhrungsort').val('schweiz');
	},
	
	showAll: function () {
		$('#seminaryList li').removeClass('bg-0, bg-1');
		$('#seminaryList li').removeClass('hidden');
	},
	
	filter: function() {
		var crit1 = $('#filterCritery').val();
		var crit2 = $('select.'+crit1).val();
		
		if (!crit2) {
			this.clear();
		}
		else {
			$('#seminaryList li').removeClass('bg-0, bg-1');
			$('#seminaryList li').addClass('hidden');
			
			$('li.'+crit1).removeClass('hidden');
			$('li.'+crit2).removeClass('hidden');	
		}
		
		this.setBackground();
	},
	
	showFilter: function (element) {
		$("select[name='filterLocation']").addClass('hidden'); 
		$('select.'+element.value).removeClass('hidden');	
	},
	
	setBackground: function () {
		$('#seminaryList li:visible').each(function (i) {
			$(this).addClass('bg-'+ (i % 2));
		});
	},
	
	clear: function () {
		$('#filterCritery').val('empty');
		$('#filterCritery').change();
		$('select.empty').val('--');
		this.showAll();
		this.setBackground();
	}
	
}

function startFlashLinkEntryPage() {
	
	if ($('#flashLinkEntryPage').length == 0) {
		return false;
	}
	

	/* config */
	var movie = {
		ex_flash_link_entrypage: 'model28_teaser'
	}
	
	var image = $('#flashLinkEntryPage img')[0].src.split('/').pop();
	
	if (image.search('jpg') != -1) {
		var movieName = image.replace('.jpg', '');
	}
	
	if (!movie[movieName]) {
		return false;
	}
	
	
		
	var so = new SWFObject(BASE_URL+"_media/"+ movie[movieName] +".swf", "entrypage", "175", "158", "8", "");
	so.addVariable('homeURL', BASE_URL+'home.html');
	so.addParam("wmode", "transparent");
	so.write("flashLinkEntryPage");
}

function setWidthFlash(){
	return false;
	var widthFlash = document.body.offsetWidth;
	if (widthFlash < 1000) {
		widthFlash = 1000;
		document.getElementById("concept").style.width = widthFlash + "px";
		document.getElementById("menu").style.width = widthFlash + "px";
	}
	else{
		document.getElementById("concept").style.width = widthFlash + "px";
		document.getElementById("menu").style.width = widthFlash + "px";
	}
}

function resizeFlashContainer() {
	var h = 367;
	var w = 850;
	var heightFlash = parseInt((document.body.offsetWidth * h) / 1280);
	var widthFlash = parseInt((heightFlash * w) / h);
	
	if (heightFlash < h) {
		heightFlash = h; 
	}
	
	if (widthFlash < w) {
		widthFlash = w;
	}

	var top = -318;
	top = top - ((h - heightFlash) / 2)
	
	$('#headerHome').height(heightFlash);
	$('#headerHome').width(widthFlash);
	$('#headerHome').css('backgroundPosition', 'center '+ top +'px');
	$('#screen-hide').hide();
}

function lightBoxToTop(mTop) {
	mTop = (mTop) ? mTop : '0';
	function execLightBoxToTop() {
		$('#jquery-lightbox').css('top', mTop);
	}
	clearTimeout(timerId);
	timerId = setTimeout(execLightBoxToTop, 1);
}

function openHomeLayer() {
	var a = document.createElement('a');
	a.href = '_img/_common/GMM-Beschreibung.jpg';
	$(a).lightBox({
		imageLoading:'_img/_common/lightbox-ico-loading.gif',
		imageBtnPrev:'_img/_common/lightbox-btn-prev.gif',
		imageBtnNext:'_img/_common/lightbox-btn-next.gif',
		imageBtnClose:'_img/_common/lightbox-btn-close.gif',
		imageBlank:'_img/_common/lightbox-blank.gif'
	});
	$(a).click();
}

function loadFlashLayer(path, title, width, height, scale, bgcolor, time) {

	if (!time) time = 2000;
	if (!bgcolor) bgcolor = "";

	function loadFlash() {
		//$('#jquery-lightbox').unbind('click');
		var so = new SWFObject(path, title, width, height, "8", bgcolor);
		so.addParam("align", "middle");
		so.addParam("allowfullscreen", "true");
		so.addParam("allowscriptaccess", "always");
		so.addParam("quality", "high");
		if (scale) {
			so.addParam("scale", scale);
		}
		so.write("lightbox-container-image");
	}
	
	clearTimeout(timerId);
	timerId = setTimeout(loadFlash, time);
}