var allplan =
{
	isIE : false,
	IEversion : 0,
	
	handleReady : function ()
	{
		allplan.isIE = navigator.appName.toLowerCase().indexOf('internet explorer') >= 0 ? true : false;
		
		if (allplan.isIE)
		{
			var v = navigator.appVersion.toLowerCase();
			v = v.substr( v.indexOf('msie ')+5 );
			v = v.substr( 0, v.indexOf(';') );
			allplan.IEversion = v-0;
		}
		
		allplan.search.init();
		allplan.login.init();
		allplan.langchooser.init();
		allplan.newsticker.init();
		allplan.inpform.init();
		allplan.tables.init();
		allplan.mailtos.init();
		allplan.forms.init();
		allplan.videos.init();
		allplan.tabs.init();
	}
};



// functionality for site search field
// 
allplan.search =
{
	sitelabel : '',
	forumlabel : '',
	
	init : function ()
	{
		this.initSearchField('site');
		this.initSearchField('forum');
	},
	
	initSearchField : function (part)
	{
		var fld = jQuery('#frm_'+part+'search_input');
		if (!fld || !fld.length)
			return;
		
		this[part+'label'] = fld.val() ? fld.val() : '';
		
		fld.bind('focus', this.handleFocus);
		fld.bind('blur', this.handleBlur);
		
		document.getElementById('frm_'+part+'search').onsubmit = function () { return allplan.search.handleSubmit(part); };
	},
	
	handleFocus : function (e)
	{
		var part = jQuery(this).attr('id');
		part = part.substring( 4, part.indexOf('search_input') );
		
		if (this.value == allplan.search[part+'label']) this.value = '';
		jQuery('#'+part+'search').removeClass();
		jQuery('#'+part+'search').addClass('active');
	},
	
	handleBlur : function (e)
	{
		var part = jQuery(this).attr('id');
		part = part.substring( 4, part.indexOf('search_input') );
	
		if (this.value == '') this.value = allplan.search[part+'label'];
		jQuery('#'+part+'search').removeClass();
	},
	
	handleSubmit : function (part)
	{
		var val = jQuery('#frm_'+part+'search_input').attr('value');
		return (  val == ''  ||  val == allplan.search[part+'label']  ) ? false : true;
	}
};



// functionality for topbar login
// 
allplan.login =
{
	labelemail : '',
	labelpwd : '',
	
	init : function ()
	{
		var fldemail = jQuery('#frm_login_email');
		var fldpwd = jQuery('#frm_login_password');
		
		if (!fldemail || !fldpwd || !fldemail.length || !fldpwd.length)
			return;
		
		this.labelemail = fldemail.val() ? fldemail.val() : '';
		this.labelpwd = fldpwd.val() ? fldpwd.val() : '';
		
		fldemail.bind('focus', this.handleEmailFocus);
		fldemail.bind('blur', this.handleEmailBlur);
		
		fldpwd.bind('focus', this.handlePwdFocus);
		fldpwd.bind('blur', this.handlePwdBlur);
		
		
		fldemail.keydown(function(e){
		        if (e.keyCode == 13) {
		            jQuery(this).parents('form').submit();
		            return false;
		        }
		    });
		fldpwd.keydown(function(e){
		        if (e.keyCode == 13) {
		            jQuery(this).parents('form').submit();
		            return false;
		        }
		    });
		
		
		if (allplan.isIE)
			return;
		
		fldpwd.get(0).type = 'text';
	},
	
	handleEmailFocus : function (e)
	{
		if (this.value == allplan.login.labelemail) this.value = '';
	},
	
	handleEmailBlur : function (e)
	{
		if (this.value == '') this.value = allplan.login.labelemail;
	},
	
	handlePwdFocus : function (e)
	{
		if (this.value == allplan.login.labelpwd) this.value = '';
		if (allplan.isIE)
			return;
		jQuery('#frm_login_password').get(0).type = 'password';
	},
	
	handlePwdBlur : function (e)
	{
		if (this.value == '')
		{
			this.value = allplan.login.labelpwd;
			if (allplan.isIE)
				return;
			jQuery('#frm_login_password').get(0).type = 'text';
		}
	},
	
	handleSubmit : function ()
	{
		return ( jQuery('#frm_sitesearch_input').attr('value') == '' ) ? false : true;
	}
};



// functionality for language chooser
//
allplan.langchooser =
{
	status : 0,
	
	init : function ()
	{
		jQuery('#langchooser').css('display','none');
		jQuery('#currentlang').bind('click', this.handlePulldown);
	},
	
	handlePulldown : function ()
	{
		switch(allplan.langchooser.status)
		{
			case 0 :
				jQuery('#currentlang').addClass('active');
				jQuery('#langchooser').css('display','block');
				allplan.langchooser.status = 1;
				break;
			
			case 1 :
				jQuery('#currentlang').removeClass();
				jQuery('#langchooser').css('display','none');
				allplan.langchooser.status = 0;
				break;
		}
	}
};


// functionality for news ticker
//
allplan.newsticker =
{
	act : 0,
	total : 0,
	timerID : null,
	list : null,
	
	init : function ()
	{
		if (!jQuery('#newsticker').get(0))
			return;
		
		jQuery('#newsticker').addClass('active');
		
		this.list = jQuery('#newsticker > ul').slice(0,1);
		this.total = jQuery('#newsticker > ul > li').length;
		
		var p=this;
		this.timerID = setInterval(function() { p.tween() }, 3000);
	},
	
	tween : function ()
	{
		this.act++;
		if (this.act>this.total)
		{
			this.act = 0;
			this.list.css('marginTop', '26px');
		}
		var targ = this.act * -26;
		this.list.animate( {marginTop:targ+'px'}, 500 );
	}
};


// functionality for forms
//
allplan.inpform =
{
	init : function ()
	{
		jQuery('.formsection .txt').bind('focus', this.handleFldFocus);
		jQuery('.formsection .txt').bind('blur', this.handleFldBlur);
		
		jQuery('.formsection select').bind('focus', this.handleFldFocus);
		jQuery('.formsection select').bind('blur', this.handleFldBlur);
		
		jQuery('.btnbar input').bind('mouseover', this.handleBtnOver);
		jQuery('.btnbar input').bind('mouseout', this.handleBtnOut);
	},
	
	handleFldFocus : function (e)
	{
		jQuery(this).parent().parent().addClass( 'active' );
	},

	handleFldBlur : function (e)
	{
		jQuery(this).parent().parent().removeClass( 'active' );
	},

	handleBtnOver : function (e)
	{
		jQuery(this).addClass( 'over' );
	},

	handleBtnOut : function (e)
	{
		jQuery(this).removeClass( 'over' );
	}
};


// functionality for tables
//
allplan.tables =
{
	activeFAQtr : null,
	
	init : function ()
	{
		this.initArrowList();
		this.initFAQTable();
	},
	
	initArrowList : function ()
	{
		jQuery('table.arrowlist tr').bind('mouseover', this.handleTROver);
		jQuery('table.arrowlist tr').bind('mouseout', this.handleTROut);
		
		jQuery('table.arrowlist a').each( function (i) {
				var href = jQuery(this).attr('href');
				jQuery(this).parent().parent().parent().bind('click', function() { var b=document.getElementsByTagName('base'); var base = b.length ? b[0].href : ''; window.location.href = base+href;  } )
			} );
	},
	
	initFAQTable : function ()
	{
		var tables = jQuery('table.faq');
		if (!tables.length)
			return;
		
		jQuery('table.faq .answer').css( {'display' : 'none'} );
		jQuery('table.faq tr').bind( 'click', allplan.tables.showFAQ ).find('td:last').after('<td class="arr"><span>&gt;</span></td>');
	},
	
	showFAQ : function ()
	{
		var answers = jQuery(this).find('.answer');
		if (!answers.length)
			return;
		
		if (allplan.tables.activeFAQtr == jQuery(this).attr('id'))
		{
			jQuery('#'+allplan.tables.activeFAQtr).removeClass('active');
			answers.eq(0).slideUp(500);
			allplan.tables.activeFAQtf = null;
		} else {
			if (allplan.tables.activeFAQtr)
			{
				jQuery('#'+allplan.tables.activeFAQtr).removeClass('active');
				jQuery('#'+allplan.tables.activeFAQtr).find('.answer').eq(0).slideUp(500);
			}
			answers.eq(0).slideDown(500);
			allplan.tables.activeFAQtr = jQuery(this).attr('id');
		}
	},
	
	handleTROver : function (e)
	{
		if (jQuery(this).attr('id') == allplan.tables.activeFAQtr) return;
		jQuery(this).addClass('active');
	},

	handleTROut : function (e)
	{
		if (jQuery(this).attr('id') == allplan.tables.activeFAQtr) return;
		jQuery(this).removeClass('active');
	}
};


allplan.mailtos =
{
	
	init : function ()
	{
		jQuery('span.mto').each( function () { allplan.mailtos.parse( this ) } );
	},
	
	parse : function ( span )
	{
		var spans = jQuery(span).find('span');
		if (spans.length != 3)
			return;
		
		var local = spans.eq(0).text(), domain = spans.eq(1).text(), tld = spans.eq(2).text();
		var a = new Array(109,97,105,108,116,111,58), e = local+"&#"+"64;"+domain+"."+tld;
		
		jQuery(span).empty().append( "<a href='&#"+a.join(';&#')+";"+e+"'>"+e+"</a>" );
	}
};


allplan.forms =
{
	init : function ()
	{
		this.initSchoolSelect();
		
		this.initSelectOther('hearabout');
		this.initSelectOther('school');
	},
	
	initSchoolSelect : function ()
	{
		if (!jQuery('#frm_selectschool').length)
			return;
		
		this.clearSelect('sel_schooltype', lang.chooseCountry);
		this.clearSelect('sel_school', lang.chooseCountry);
		
		jQuery('#sel_country').bind( 'change', function (e) { allplan.forms.handleSchoolCountrySelect() } );
		jQuery('#sel_schooltype').bind( 'change', function (e) { allplan.forms.handleSchoolTypeSelect() } );
		jQuery('#sel_school').bind( 'change', function (e) { allplan.forms.handleSchoolSelect() } );
		jQuery('#sel_school_other').bind( 'blur', function (e) { allplan.forms.handleSchoolOtherBlur() } );
	},
	
	handleSchoolCountrySelect : function ()
	{
		var countryid = jQuery('#sel_country option:selected').val();
		
		// no country selected
		if (!countryid)
		{
			this.clearSelect('sel_schooltype', lang.chooseCountry);
			this.clearSelect('sel_school', lang.chooseCountry);
			jQuery('#sel_school_other').attr('disabled','disabled');
			return;
		}
		
		
		this.clearSelect('sel_school', lang.chooseSchoolType);
		this.populateSelect( 'sel_schooltype', '<option value="">--</option><option value="1">'+lang.university+'</option><option value="2">'+lang.techCollege+'</option><option value="3">'+lang.vocationalSchool+'</option><option value="4">'+lang.other+'</option>' );
	},
	
	handleSchoolTypeSelect : function ()
	{
		var typeid = jQuery('#sel_schooltype option:selected').val();
		if (!typeid)
		{
			this.clearSelect('sel_school', lang.chooseSchoolType);
			jQuery('#sel_school_other').attr('disabled','disabled');
			return;
		}
		
		this.getSchools();
	},
	
	handleSchoolSelect : function ()
	{
		var schoolid = jQuery('#sel_schooltype option:selected').val();
		if (schoolid)
		{
			jQuery('#sel_school_other').val('');
			return;
		}
	},
	
	handleSchoolOtherBlur : function ()
	{
		var schoolother = jQuery('#sel_school_other').val();
		if (schoolother)
		{
			jQuery('#sel_school option').attr('selected','');
			jQuery('#sel_school option:first').attr('selected','selected');
			return;
		}
	},
	
	initSelectOther : function (part)
	{
		var sel = jQuery('#sel_'+part);
		var oth = jQuery('#other_'+part);
		if (!sel.length || !oth.length)
			return;
		
		sel.bind( 'change', function (e) { var v=jQuery(this).val(); if (v!=='0') oth.hide(); else oth.show().get(0).focus(); } );
		if (sel.val()!=='0') oth.hide(); else oth.show();
	},
	
	// DATA METHODS
	/*
	getSchools : function ()
	{
		//  do ajax request here
		this.populateSelect( 'sel_school', '<option value="0">--</option><option value="1">School A</option><option value="2">School B</option>' );
		jQuery('#sel_school_other').attr('disabled','');
		allplan.forms.initSelectOther('school');
	},
	*/
	
	
	// DATA METHODS
	getSchools : function ()
	{
		var schooltype_id = jQuery('#sel_schooltype option:selected').val();
		var country_id = jQuery('#sel_country option:selected').val();
		var base_url = jQuery('#base-url').attr('value'); 

		jQuery('#sel_school').load(
			base_url+'?eID=acferegister-async&country_id=' + country_id + '&schooltype_id='+schooltype_id,
			null,
			function () { allplan.forms.initSelectOther('school') }
		);
		jQuery('#sel_school').attr('disabled', '');         
		jQuery('#sel_school_other').attr('disabled','');
	},
	
	
	
	clearSelect : function (id,msg)
	{
		jQuery('#'+id).attr('disabled', 'disabled');
		jQuery('#'+id).html( '<option value="">[ ' + msg + ' ]</option>' );
	},
	
	populateSelect : function (id,html)
	{
		jQuery('#'+id).attr('disabled', '');
		jQuery('#'+id).html(html);
	}
};


allplan.videos =
{
	
	init : function ()
	{
		if (jQuery('#videocontainer').length == 0)
			return;
		
		// get file to load
		var file = jQuery('#videocontainer > a').eq(0).attr('href');
		
		// get related videos label
		var label = jQuery('#relatedlabel').eq(0).text();
		
		swfobject.embedSWF(
			Config.get('player_path'),
			"videocontainer",
			"598",
			"429",
			"9.0.0",
			null,
			{file:file, assetspath:Config.get('player_assets_path'), relatedlabel:label, volume:1}, 
			{allowfullscreen:true, allowscriptaccess:'always'},
			{id:'videoplayer', name:'videoplayer'}
		);
	},
	
	
	
	getRelated : function ()
	{
		// get related videos
		var related = new Array();
		if( jQuery('#relatedvideos a').length )
		{
			jQuery('#relatedvideos a').each( function (i) { related.push( {img:jQuery(this).attr('name'), href:jQuery(this).attr('href'), title:jQuery(this).text() } ) } );
		}
		return related;
	}
};


allplan.tabs =
{
	/**
	 * handles highlighting of active tab when typo3 just can't do it.
	 */
	init : function ()
	{
		var loc = window.location.href;
		var part = loc.substr(loc.lastIndexOf('/')+1);
		
		this.setTabs( part, 'forumtabs', ['list_prefix','list_unans'] );
		this.setTabs( part, 'messagingtabs', ['message_write','send','archive','inbox'] );
	},
	
	
	
	/**
	 * @param part string part of url following last slash
	 * @param tablist string css id of tablist containing tabs
	 * @param tabs array contains if string XXX occurs as substring of 'part' parameter, tab with id 'tab_XXX' within tablist is activated
	 */
	setTabs : function (part, tablist, tabs)
	{
		var id = null;
		for (var i=0,l=tabs.length;i<l;i++)
		{
			if (part.indexOf(tabs[i]) > -1)
			{
				id = tabs[i];
				i=l;
			}
		}
		if (id)
		{
			jQuery('#'+tablist+' li').removeClass('active');
			jQuery('#tab_'+id).eq(0).addClass('active');
			console.log(id);
		}
	}
};


// access point for flash ExternalInterface
getRelatedVideos = function ()
{
	var videoplayer = (navigator.appName.indexOf("Microsoft") != -1) ? window['videoplayer']  :  document['videoplayer'];
	videoplayer.setRelated( allplan.videos.getRelated() );
};



jQuery(document).ready( allplan.handleReady );