//Google Analytics code -- must be first in this file
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-22929659-4']);
_gaq.push(['_trackPageview']);

(function() {
	var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

//menu items and their corresponding page filenames
var menu_item_dict = {
	'':'menu_index',	//corresponds to "www.dzzi.com", which is the same as index.shtml
	'index.shtml':'menu_index',
	'kurse.shtml':'menu_kurse',
	'informationen_ueber_eine_behandlung_mit_zahnimplantaten.shtml':'menu_info',
	'informationen_ueber_zahnimplantate_in_hamburg.shtml':'menu_implants',
	'die_behandlung_mit_zahnimplantaten_in_hamburg.shtml':'menu_behandlung',
	'klinik_fuer_zahnimplantate_in_hamburg.shtml':'menu_klinik',
	'beratung_zahnimplantate_hamburg.shtml':'menu_beratung',
	'fragen_zu_zahnimplantaten_dritte_zaehne.shtml':'menu_fragen',
	'zahnimplantate_in_hamburg_das_team.shtml':'menu_team',
	'zahnimplantate_niederlassungen.shtml':'menu_nieder',
	'buecher_ueber_zahnimplantate.shtml':'menu_buecher',
	'presseartikel_ueber_zahnimplantate_in_hamburg.shtml':'menu_press',
	'zahnimplantate-hamburg-kosten-zeit-heilung.shtml':'menu_press',
	'zahnimplantate-hamburg-roentgen-vorteil-implantation.shtml':'menu_press',
	'zahnimplantate-hamburg-behandlung-schmerzlos-erfolg.shtml':'menu_press',
	'zahnimplantate-hamburg-spezialklinik-zahnarzt-botenstoffe-vorteil.shtml':'menu_press',
	'zahnimplantate-hamburg-schoenheit-laecheln-operation-kosten.shtml':'menu_press',
	'zahnimplantate-hamburg-kosten-dauer-heilung-zahnarzt.shtml':'menu_press',
	'zahnimplantate-hamburg-vollnarkose-klinik-vollimplantation-zahnaerztliche-mitteilungen-1-6-1993.shtml':'menu_press',
	'zahnimplantate_hamburg_login.shtml':'menu_login',
	'links_zu_seiten_ueber_zahnimplantate.shtml':'menu_links',
	'bewerbung.shtml':'menu_bewerbung',
	'kontakt.shtml':'menu_kontakt'		//dictionary cannot end in ',' due to IE6
}

var press_article_dict = {
	'zahnimplantate-hamburg-kosten-zeit-heilung.shtml':'article_0',
	'zahnimplantate-hamburg-roentgen-vorteil-implantation.shtml':'article_1',
	'zahnimplantate-hamburg-behandlung-schmerzlos-erfolg.shtml':'article_2',
	'zahnimplantate-hamburg-spezialklinik-zahnarzt-botenstoffe-vorteil.shtml':'article_3',
	'zahnimplantate-hamburg-schoenheit-laecheln-operation-kosten.shtml':'article_4',
	'zahnimplantate-hamburg-kosten-dauer-heilung-zahnarzt.shtml':'article_5',
	'zahnimplantate-hamburg-vollnarkose-klinik-vollimplantation-zahnaerztliche-mitteilungen-1-6-1993.shtml':'article_6'
}
var press_article_dict_length = 7;	//IE6 can't use press_article_dict.length for some reason

var current_page;		//used in onload and toggleArticleSelectorText

var cookie_name = "visit20120219"; //to reset, change the name of the cookie
var maxTimesPopup = 1;	//used to how many times the popup should show









window.onload = function() {
		current_page = getCurrentFile();
		setCurrentMenuItem(current_page);
		
		var popupState = 0;						//see below switch/cases for details
		var popupUrl = 'https://andredzzi.wufoo.com/forms/z7p9s9/';		//link to popup window
		switch (popupState) {
			case 1:
				//testing popup only
				if (current_page == "test.shtml") {
					openPopupOnFirstTime(popupUrl);	
				}
				break;
			case 2:
				//popup live
				openPopupOnFirstTime(popupUrl);
				break;
			case 3:
				//testing overlay popup only
				if (current_page == "test.shtml") {
					openOverlayPopupOnFirstTime();	
				}
				break;
			case 4:
				//live overlay popup
				openOverlayPopupOnFirstTime();	
				break;
			default:
				//popup off
				break;
		}
		
		//if page is a press article page, set article selector, next, prev links
		var current_article_id = press_article_dict[current_page];
		if (current_article_id != null) {
			setCurrentArticleItem(current_article_id);
		}		
		
		//if page has a datepicker, set it up
		if (document.getElementById('datepicker') != null) {
			//creates calendar
			g_datePicker = new JsDatePick({
				useMode:1,							//embedded mode
				target:"datepicker",					//id of trigger element
				dateFormat:"%d-%M-%Y",	
				isStripped:true 				//removes border around popup
			});
			//trigger function when date is selected
			g_datePicker.setOnSelectedDelegate(function(){
					var obj = g_datePicker.getSelectedDay();
					document.getElementById('datum').value = obj.day + "-" + obj.month + "-" + obj.year;
					toggleDisplay('datepicker');
			});
		}
}

function getCurrentFile() {
	var url_split = document.URL.split('/');
	var current_page = url_split[url_split.length-1];
	//remove bookmarks in url
	var hash_index = current_page.indexOf("#");
	if (hash_index >= 0) 
		current_page = current_page.slice(0,hash_index);
		
	return current_page;
}

//if current page is a menu link, set the link to the 'current' style and change href
function setCurrentMenuItem(current_page) {		
	var menu_item_id = menu_item_dict[current_page];
	if (menu_item_id == null) return;
	
	//set current menu item
	var menu_item = document.getElementById(menu_item_id);
	if (menu_item != null) {
		menu_item.className = 'current';
		menu_item.href = '#';
	}
}

//set article selector, next, prev links for a press articles page
function setCurrentArticleItem(current_article_id) {		
	if (current_article_id == null) return;
	
	//set current article item
	var article_item = document.getElementById(current_article_id);
	if (article_item != null) {
		article_item.className = 'active';
		article_item.firstChild.href = '';	//set the link inside to nothing
		
		//set the text of the article selector link when not hovering
		var article_selector = document.getElementById('article_selector_link');
		if (article_selector != null)	{
			article_selector.innerHTML = article_item.firstChild.innerHTML;
		}
		
		//all article ids are in the form article_x, where x is a number
		var article_index = parseInt(current_article_id.slice(8));
		
		//set the prev article link
		var prev_article_link = document.getElementById('prev_article');
		var prev_article_index = article_index - 1;
		if (prev_article_index >= 0) {
			var prev_article_id = 'article_' + prev_article_index.toString();
			prev_article_link.href = document.getElementById(prev_article_id).firstChild.href;
			prev_article_link.title = document.getElementById(prev_article_id).firstChild.title;
		} else {
			prev_article_link.style.display = 'none';
			document.getElementById('prev_article_spacer').style.display = 'none';
		}
		
		//set the next article link
		var next_article_link = document.getElementById('next_article');
		var next_article_index = article_index + 1;
		if (next_article_index < press_article_dict_length) {
			var next_article_id = 'article_' + next_article_index.toString();
			next_article_link.href = document.getElementById(next_article_id).firstChild.href;
			next_article_link.title = document.getElementById(next_article_id).firstChild.title;
		} else {
			next_article_link.style.display = 'none';
			document.getElementById('next_article_spacer').style.display = 'none';
		}
	}
}
	
//toggles CSS display property between none and ''
function toggleDisplay(id) {
	var obj = document.getElementById(id);
	if (obj != null) {
		obj.style.display = (obj.style.display != 'none' ? 'none' : '' );
	}
}

//toggles the text of the article selector link, between the title of the current article and "Select an article" 
function toggleArticleSelectorText(none_selected_text) {
	var article_selector_link = document.getElementById('article_selector_link');
	if (article_selector_link != null) {
		article_selector_link.innerHTML = article_selector_link.innerHTML == none_selected_text ? 
			document.getElementById(press_article_dict[current_page]).firstChild.innerHTML : 
			none_selected_text;
	}	
}

function openPopupOnFirstTime(popup_url)
{
	if (numTimesVisited() < maxTimesPopup) 
	{
		// link to form is the url right after window.open()
		var newWin = window.open(popup_url,  null, 'height=900, width=680, toolbar=0, location=0, status=1, scrollbars=1, resizable=1'); 
	}
			
	incrementVisitedCookie();
}

function openOverlayPopupOnFirstTime() 
{
	
	if (numTimesVisited() < maxTimesPopup) 
	{
		$.colorbox({
			'inline':true,
			'href':'#wufoo-form iframe',
			'width':700,
			'innerHeight': getWufooHeight('#wufoo-form iframe')
		});
	}
		
	incrementVisitedCookie();
}

function getWufooHeight(form_href)
{
	var submit_offset = 220;
	var logo_offset = 100;
	return $(form_href).height() + submit_offset + logo_offset;		
}

function numTimesVisited()
{
	//to reset, go to line 52.
	var start_index = document.cookie.indexOf(cookie_name);
	if(start_index != -1)
	{	
		var end_index = start_index + document.cookie.substring(start_index).indexOf(';');
		start_index += cookie_name.length + 1;
		var visits_str;
		if (end_index > start_index)
		{
			visits_str = document.cookie.substring(start_index,end_index);
		}
		else
		{
			visits_str = document.cookie.substring(start_index);
		}
		var visits = parseInt(visits_str);
		if (!isNaN(visits)) return visits;
		// else return 0, see below
	}
	
	return 0;
}

function incrementVisitedCookie()
{
	var times_visited = numTimesVisited() + 1;
	var expires = new Date();
	expires.setDate(expires.getDate()+365);
	document.cookie = cookie_name+"="+times_visited+"; expires="+expires.toUTCString(); 
}
