// JavaScript Document
function recommend(url){
	window.location.href = "mailto:?body="+url;
}
function printPage(){
	window.print();
}
//--------------------------------------------------
function getHash() {
    var index = window.location.href.indexOf('#');
    return index != -1 ? window.location.href.substr(index + 1) : '';
}
function getUrl() {
    var index = window.location.href.indexOf('#');
    return index != -1 ? window.location.href.substr(0, index) : window.location.href;
}
//--------------------------------------------------
//tab_navigation
function deactivateTabElemets(){
	//tabs aus
	var li = $$('#main .tab_navigation li');
	li.each(function(el){
		el.removeClass('active');
		//textbloecke aus
		$(el.getElement('a').getProperty('class')).setStyle('display','none');
		el.setStyle('cursor','pointer');
	});
}
function activateTabElemets(id){
	//tabs ein
	var it = $$('#main .tab_navigation a.'+id);
	if(it.length > 0){
		deactivateTabElemets();
		var li = $$('#main .tab_navigation a.'+id)[0].getParent();
		li.addClass('active');
		li.setStyle('cursor','auto');
		//textbloecke ein
		$(id).setStyle('display','block');
	}
}
function setupTabNavigation(){
	//find items with class 'tab_navigation_item'
	var items = $$('#main .tab_navigation_item');

	if(items.length > 0){
		//create Tabnavi
		var tab_navigation = new Element('div', {
		'class': 'tab_navigation'});

		var ul = new Element('ul');

		var li;
		var a;
		var div;
		var i = 0;

		items.each(function(el){
			//give id
			el.setProperty('id','tab_item'+i);

			li = new Element('li');

			//Text
			a = new Element('a');
			a.addClass(el.getProperty('id'));
			a.set('text', el.getElement('h2').get('text'));
			a.set('href', getUrl()+"#"+i);
			a.injectInside(li);

			//div
			div = new Element('div');
			div.injectInside(li);

			//hide titles
			el.getElement('.ctitle').addClass('hidden');

			li.injectInside(ul);
			i++;
		});
		ul.injectInside(tab_navigation);

		var nf = new Element('div', {
		'class': 'nf'});
		nf.injectInside(tab_navigation);

		//insert into dom
		tab_navigation.injectBefore(items[0]);

		var test = tab_navigation.getPrevious();
		if(test == null){
			//hide top border
			var cbox = $$('#main .cbox')[0];
			cbox.addClass('noborder');
		}else{
			tab_navigation.addClass('tab_content');
		}

		//default view
		deactivateTabElemets();

		if(getHash()){
			activateTabElemets('tab_item'+getHash());
		}else{
			activateTabElemets(tab_navigation.getElement('a').getProperty('class'));
		}
		lastHash = "";
		setInterval(intervalListener,50);
	}
}
//--------------------------------------------------
function intervalListener(){
	if(getHash()){
		if(getHash() != lastHash){
			lastHash = getHash();
			activateTabElemets('tab_item'+lastHash);
		}
	}
}
//--------------------------------------------------
//Flash visuals
function setupFlashVisuals(){
	var vis = $$('div.flash_visual div.ce_text');
	vis.each(function(el, index) {
		//flash_visual
		var vi = vis[index];
		var img = vi.getElements('img')[0];
		var container = vi.getElements('.image_container')[0];
		var p = container.getParent();
		var src = img.getProperty('src');
		var w = img.getProperty('width');
		var h = img.getProperty('height');
		var pAlias = window.location.href;
		pAlias = pAlias.substring(pAlias.lastIndexOf("/")+1, pAlias.lastIndexOf("."));
		//console.log(pAlias);

		var i = src.lastIndexOf("/");
		var path = src.substring(0, i+1);
		var alias = src.substring(i+1, src.lastIndexOf("."));
		container.setProperty("id", "flash_"+alias);
		p.setStyle('height', h.toInt());
		//console.log("",p.setStyle);

		//console.log("",index);
		UFO.launch("flash_"+alias, {
			id: alias+"Com",
			name: alias+"Com",
			swliveconnect: "true",
			movie: path+alias+".swf",
			width: w,
			height: h,
			allowfullscreen: "true",
			flashvars: "URL="+window.location.hostname+"&pAlias="+pAlias,
			majorversion: "9",
			build: "0"
		});
	});
}
//--------------------------------------------------
//Flash image
function setupFlashImages(){
	var imgs = $$('img.flash');
	imgs.each(function(el, index) {
		//flash_visual
		var img = imgs[index];
				
		var src = img.getProperty('src');
		var w = img.getProperty('width');
		var h = img.getProperty('height');

		var pAlias = window.location.href;
		pAlias = pAlias.substring(pAlias.lastIndexOf("/")+1, pAlias.lastIndexOf("."));

		var i = src.lastIndexOf("/");
		var path = src.substring(0, i+1);
		var alias = src.substring(i+1, src.lastIndexOf("."));
		
		var container = new Element('div');
		container.setProperty("id", "flash_"+alias);
		container.setStyles(img.getStyles('float', 'margin'));
		if(img.getParent().get('tag') == 'p'){
			container.injectBefore(img.getParent());
			container.grab(img);
		}else{
			container.wraps(img);
		}
		
		img.setStyles({float:'', margin:''});

		UFO.launch("flash_"+alias, {
			id: alias+"Com",
			name: alias+"Com",
			swliveconnect: "true",
			movie: path+alias+".swf",
			width: w,
			height: h,
			allowfullscreen: "true",
			flashvars: "URL="+window.location.hostname+"&pAlias="+pAlias,
			majorversion: "9",
			build: "0"
		});
	});
}
//--------------------------------------------------
//ajax loding of pages
function replaceWithContent(id){
	//var url = "http://localhost/design4real_08007_Cinterion/deploy/index.php/"+id+".html";
	var url = id+".html";
 	var el = new Element('content');
	//console.log("",el);

	new Ajax(url, {
		method: 'get',
		update: el,
		onComplete: function() {
			// when complete, we remove the spinner
			//console.log('ajax-loading');
			//el.setHTML(newHTML);
			//console.log(el.getElements("#main")[0]);
			$("main").replaceWith(el.getElements("#main")[0]);
			$("left").replaceWith(el.getElements("#left")[0]);
			$("footer").replaceWith(el.getElements("#footer")[0]);
			setupTabNavigation();
		}

	}).request();
}
//--------------------------------------------------
//search for adresses
function modifyAdressesForm(){
	var url = "tl_files/cinterion/php/adresses.php";
	if($$('#adress_form').length > 0){
		$$('#adress_form select')[0].addEvent('change', function(e) {
			/**
			 * Prevent the submit event
			 */
			new Event(e).stop();

			new Ajax(url, {
				method: 'get',
				update: $('adresses_content')
			}).request("country="+this.options[this.selectedIndex].value);
		});

		$$('#adress_form select')[0].selectedIndex = 0;
		$$('#adress_form input')[0].setStyle('display', 'none');
	}
}
//--------------------------------------------------
//contact finder
function modifyContactFinder(){
	if($$('#contact_finder').length > 0){
		$$('#contact_finder select')[0].addEvent('change', function(e) {
			/**
			 * Prevent the submit event
			 */
			new Event(e).stop();

			$$('#contact_finder')[0].submit();
		});

		$$('#contact_finder input')[0].setStyle('display', 'none');
	}
}
//--------------------------------------------------
//country selector in contact
function selectCountry(){

	var country = $$('select.country')[0];
	var selValue = country[country.selectedIndex].text;

	var countryDivs = $$('div.country').dispose();
	countryDivs.each(function(el) {
		el.dispose();
	});

	$$('.state').setStyles({
	   display: 'none'
	});

	$$('.province').setStyles({
	   display: 'none'
	});

	if(selValue == 'United States'){
		$$('.state').setStyles({
		   display: 'inline'
		});

		$$('select.province')[0].selectedIndex = 0;
	}else if(selValue == 'Canada'){
		$$('.province').setStyles({
		   display: 'inline'
		});
		$$('select.state')[0].selectedIndex = 0;
	}else{
		$$('select.state')[0].selectedIndex = 0;

		$$('select.province')[0].selectedIndex = 0;
	}
}
function testContactForm(e){
	/**
	 * Prevent the submit event
	 */
	new Event(e).stop();

	removeErrors();
	var test = true;

	var country = $$('select.country')[0];
	var selValue = country[country.selectedIndex].text;

	if (country.selectedIndex == 0) {
		addErrorDiv($$('select.country')[0], 'Country*');
		test = false;
	}

	if(selValue == 'United States'){
		if($$('select.state')[0].selectedIndex == 0){
			//div
			var p = new Element('p');
			p.addClass('error');
			p.addClass('country');
			p.set('text', 'Please fill in field "State*"!');

			//insert into dom
			p.injectBefore($$('select.state')[0]);
			test = false;
		}
	}
	if(selValue == 'Canada'){
		if ($$('select.province')[0].selectedIndex == 0) {
			//div
			var p = new Element('p');
			p.addClass('error');
			p.addClass('country');
			p.set('text', 'Please fill in field "Province*"!');

			//insert into dom
			p.injectBefore($$('select.province')[0]);
			test = false;
		}
	}

	if ($$('select.salutation')[0].selectedIndex == 0) {
		addErrorDiv($$('select.salutation')[0], 'Salutation*');
		test = false;
	}

	if ($$('input.first_name')[0].value == '') {
		addErrorDiv($$('input.first_name')[0], 'First Name*');
		test = false;
	}

	if ($$('input.last_name')[0].value == '') {
		addErrorDiv($$('input.last_name')[0], 'Last Name*');
		test = false;
	}


	if ($$('input.company')[0].value == '') {
		addErrorDiv($$('input.company')[0], 'Company*');
		test = false;
	}

	if ($$('input.email')[0].value == '') {
		addErrorDiv($$('input.email')[0], 'E-Mail*');
		test = false;
	}	else if (!/^[\w\-]+(\.[\w\-]+)*@[\w\-]+\.([\w\-]+\.)*[a-z]{2,}$/i.test($$('input.email')[0].value)) {
		var p = new Element('p');
		p.addClass('error');
		p.set('text', 'Please enter a valid e-mail address!');

		//insert into dom
		p.injectBefore($$('input.email')[0]);
		test = false;
	}
	if (!$$('div.agreement_save_personal_data input')[0].checked) {
		addErrorDiv($$('div.agreement_save_personal_data input')[0], 'Agreement to save personal data*');
		test = false;
	}
	if(test){
		this.submit();
	}

}
function addErrorDiv(el, name){
	//div
	p = new Element('p');
	p.addClass('error');
	p.set('text', 'Please fill in field "'+name+'"!');

	//insert into dom
	p.injectBefore(el);
}
function removeErrors(){
	var errors = $$('p.error').dispose();
	errors.each(function(el) {
		el.dispose();
	});
}
function modifyContactForm(){
	if($$('form.contact').length > 0){
		$$('select.country')[0].addEvents({'change':selectCountry},{'blur':selectCountry});
		$$('form.contact')[0].addEvents({
			'submit': testContactForm
		});
		selectCountry();
	}
}

//--------------------------------------------------
function modifyLinks(){
	var link = $$('a');
	var href;
	var str;
	var onclick;
	link.each(function(el, index) {
		href = el.getProperty('href');
		str = "";
		if(href){
			if(href.indexOf('tl_files/cinterion/downloads')!=-1){
				str = href.substring(href.indexOf('tl_files/cinterion/downloads'), href.lastIndexOf('.'));
			}else if(href.indexOf('tl_files/cinterion/marcompool/download')!=-1){
				str = href.substring(href.indexOf('tl_files/cinterion/marcompool/download'), href.lastIndexOf('.'));
			}else if(href.indexOf('http')!=-1 && href.indexOf('cinterion.com')==-1){
				str = 'external/'+ href.substring(href.indexOf('://')+3);
			}
			if(str != ""){
				onclick = el.getProperty('onclick');
				if(onclick){
					el.setProperty('onclick', ' pageTracker._trackPageview(\'' + str + '\'); '+onclick);
				}else{
					el.setProperty('onclick', 'pageTracker._trackPageview(\'' + str + '\'); ');
				}
			}
		}
	});
}
//--------------------------------------------------
//news_teaser
function modifyNewsTeaser(){
	var vi = $$('#news_teaser')[0];

	//news_teaser
	var pAlias = window.location.href;
	pAlias = pAlias.substring(pAlias.lastIndexOf("/")+1, pAlias.lastIndexOf("."));

	//console.log(pAlias);

	var path = "tl_files/cinterion/content/teaser/";

	//console.log("",index);
	UFO.launch("news_teaser", {
		id: "news_Com",
		name: "news_Com",
		swliveconnect: "false",
		movie: path+"news.swf",
		width: 306,
		height: 110,
		allowfullscreen: "false",
		flashvars: "URL="+window.location.hostname+"&pAlias="+pAlias,
		majorversion: "9",
		build: "0"
	});

}
//--------------------------------------------------
window.addEvent('domready', function() {
	setupTabNavigation();
	setupFlashVisuals();
	setupFlashImages();
	modifyAdressesForm();
	modifyContactFinder();
	modifyContactForm();
	modifyNewsTeaser();
});