﻿$(document).ready(function() {
						   
	$("img.email").each(function(){
					 
		var attr_ = $(this).attr("src");
		var d = attr_.lastIndexOf('.');
		var mail =  attr_.substring(attr_.lastIndexOf('/') + 1, d < 0 ? attr_.length : d);
		mail = mail.replace('_','.');
		
		// Also consider email-image with light-font, replace buzzword in name.
		mail = mail.replace('_light','');
		
		$(this).wrap('<a href="mailto:'+ mail +'@inova-ch.com" />');
	});					   
	
	$("img.email-block").each(function(){
					 
		var attr_ = $(this).attr("src");
		var d = attr_.lastIndexOf('.');
		var mail =  attr_.substring(attr_.lastIndexOf('/') + 1, d < 0 ? attr_.length : d);
		mail = mail.replace('_','.');
		
		// Also consider email-image with light-font, replace buzzword in name.
		mail = mail.replace('_light','');
		
		//$(this).wrap('<span class="hidden">.</span><a href="mailto:'+ mail +'@inova-ch.com" />');
		$(this).wrap('<a href="mailto:'+ mail +'@inova-ch.com" class="maillink" />');
		//$(this).wrap('<span class="hidden">.</span><a href="mailto:'+ mail +'@inova-ch.com" />');
	});	
	
	/* Make Slider sliding */					   
	
	if($("div.scrollable").size() > 0) {
	
		$("div.scrollable").scrollable({
			size:1,
			onStart: function() {
				var nextPage_ = $("a.nextPage.browse.right");
				if(this.getPageIndex() >= 4) {
					nextPage_.hide();
				}
				else {
					if(nextPage_.css("display") == "none") {
						nextPage_.show();
					}
				}
			},
			onSeek:  function() {
				var active_ = $("div.textbox.active");
				showBox(this.getIndex());
			},
			api: true
		}).seekTo(2);
	
	}
	
	function showBox(firstEle_) {
		
		
		$("div.image a").mouseover(function(){
				var id_ = $(this).attr("id");	
				$("div.textbox").hide().removeClass("active");
				$("div.textbox[rel=" + id_ + "]").show().addClass("active");
				
				var current_ = $("div.items div.image a").index($(this));

				var curarrow_ = current_-firstEle_;
				
				//console.log(curarrow_);
				$("div.textarrow div").hide();
				$("div.textarrow div").eq(curarrow_).show();
			});	
		
	}
	
	$("div.textbox").each(function(){
		var ta = $(this);

			// two rows of text
		if(ta.height() > 30 && ta.height() < 40) {
			ta.addClass("double-row");
		}
			// three rows of text
		else if(ta.height() > 50) {
			ta.addClass("triple-row");
		}
		
	});
	
	
	$("body").mouseover(function(e){
		var node = null;
	
		if(e.srcElement) {
			node = e.srcElement;	
		}
		else if(e.target){
			node = e.target;
		}
	
		if($(node).parents("div.scrollable").size() !== 1) {
				$("div.textbox").hide().removeClass("active");	
				$("div.textarrow div").hide();
		}
	});
	
	(function(){

		function getRequestParam(key) {
		  var requestParams = decodeURI(window.location.search), i, temp, list;
		  
		  if(requestParams !== null){
		      requestParams = requestParams.substr(1);
		      list = requestParams.split("&");
		      for(i=0; i < list.length; i++){
		          temp = list[i].split("=");  
		          if(key === temp[0]) {
		              return temp[1];
		          }
		      }
		  }
		  return null;
		}
		
		function getLanguage() {
			var language = "en";
			
			if(window.location.href.match(/\/deu\//)) {
				language = "de";
			}
			
			return language;
		}
		
		var language = getLanguage(), category = "Unspezifizierte_Seite_" + language, pagetype = getRequestParam("option");
		if($("div.productslider").size() === 1) {
			category = "Homepage_";
		}
		else if(pagetype && pagetype === "reports") {
			category = "Projektberichte_" + language;
		}
		else if(pagetype && pagetype === "com_inova_media_releases") {
			category = "Presse_und_News_" + language;
		}
		
	  $("td.content-area a").each(function(){
	  	var link = $(this), trackingName, onclick = link.attr("onclick");
	  	
	  	if(link.attr("href")) {
	  		trackingName = link.attr("href").match(/[^\/]*\.pdf$/i);
	  	}
	  	
	  	if(trackingName) {
	  		link.click(function(){
	  			_gaq.push(['_trackEvent', category, trackingName[0], 'clicked']);
	  		});
	  	}
	  	else if(onclick) {
	  		onclick = onclick.toString();
	  		if(onclick.match(/print=print/)) {
		  		link.click(function(){
		  			_gaq.push(['_trackEvent', 'PrintIcon', 'clicked']);
		  		});
		  	}
		  	else if(onclick.match(/emailfriend/)) {
		  		link.click(function(){
		  			_gaq.push(['_trackEvent', 'EmailToFriend', 'clicked']);
		  		});
		  	}	  	
	  	}
	  });
	
	})();
	
	
	
	/**
	 * Attendee form for seminars
	 */
	 
	// Show / hide invoice informations
	$('fieldset.invoice_information').hide();
	$('input#attend-invoice_address').click(function(){
		if($(this).attr('checked')) {
			$('fieldset.invoice_information').fadeIn();
		} else {
			$('fieldset.invoice_information').fadeOut();
		}
	});
	
	
	$('div#entryform form').validate({
		rules: {
			'attend[select]':  {
				required: true
			},
			'attend[lastname]':  {
				required: true
			},
			'attend[firstname]':  {
				required: true
			},
			'attend[function]':  {
				required: true
			},
			'attend[company]':  {
				required: true
			},
			'attend[street]':  {
				required: true
			},
			'attend[zipcity]': {
				required: true
			},
			'attend[mail]': {
				required: true,
				email: true
			},
			
			// Only if invoice is checked
			'attend[invoice][invoice_company]':  {
				required: '#attend-invoice_address:checked'
			},
			'attend[invoice][company_street]':  {
				required: '#attend-invoice_address:checked'
			},
			'attend[invoice][company_zipcity]':  {
				required: '#attend-invoice_address:checked'
			}
		},
		messages: {
			'attend[select]': 'Bitte treffen Sie eine Auswahl.',
			'attend[lastname]': 'Bitte geben Sie Ihren Nachnamen an.',
			'attend[firstname]': 'Bitte geben Sie Ihren Vornamen an.',
			'attend[function]': 'Bitte geben Sie Ihre Funktion an.',
			'attend[company]': 'Bitte geben Sie die Firma an.',
			'attend[street]': 'Bitte geben Sie eine Strasse an.',
			'attend[zipcity]': 'Bitte geben Sie eine PLZ und den Ort an.',
			'attend[mail]': {
				required: 'Bitte geben Sie Ihre E-Mail Adresse an.',
				email: 'Bitte geben Sie eine g&uuml;ltige E-Mail Adresse an.'
			},
			'attend[invoice][invoice_company]': 'Bitte geben Sie die Firma an.',
			'attend[invoice][company_street]': 'Bitte geben Sie eine Strasse an.',
			'attend[invoice][company_zipcity]': 'Bitte geben Sie eine PLZ und den Ort an.'
		},
		highlight: function(element, errorClass) {
			$(element).addClass('errorField');
		},
		unhighlight: function(element, errorClass) {
			$(element).removeClass('errorField');
		},
		errorElement: 'p',
		errorPlacement: function(error, element) {
			if(element.attr('name') == 'attend[accept]')
				error.insertAfter('#accept-label');
			else if(element.attr('name') == 'attend[select]')
				error.insertAfter('div.attend-no');
			else
				error.insertAfter(element);
		}
	});
	$("p.error").fadeIn(500);
});
