﻿function emptyfield(my_el, my_value) {
	my_el = $(my_el);
	if (my_el) {
		this.value = my_value;
		my_el.onfocus = function(){
			this.value = (this.value == my_value) ? '' : this.value;
		}
		my_el.onblur = function(){
			this.value = (this.value.match(/\S/)) ? this.value : my_value;
		}
	}
}


function rebuildBrandLocationTabs()
{
	var locations = $$('div.locations li.active');
	var brands = $$('div.brands li.active');
	
	
	// Tab "location" entfernen
	if (locations && locations.length > 0 && $('tab-71'))
	{
		var li = $('tab-71').getParent('li');
		if (li) 
			li.destroy();
	}
	
	// Tab "brand" entfernen
	if (brands && brands.length > 0 && $('tab-77'))
	{
		var li = $('tab-77').getParent('li');
		if (li)
			li.destroy();
	}
	
	// Tab "overview entfernen
	if (( locations.length == 0 || brands.length == 0 ) && $('tab-78'))
	{
		var li = $('tab-78').getParent('li')
		if (li)
			li.destroy();
	}
	
	// erstes tab bekommt class first
	$$('ul.tabs li:first-child').addClass('first');
	
}

function terminvereinbarung () {
	var check = $('terminvereinbarung');
	if (check) {
		var form = $('tx-powermail-pi1_fieldset_1');
		var id1 = $('uid1');
		var id2 = $('uid2');
		var handleSelects = function()
		{
			if (id1.value == "Marke")
			{
				id2.grab( new Element('option', {
				    'id': 'choose',
				    'html': 'Standort (Zunächst Marke wählen)'
				}), 'top');
				id2.selectedIndex = 0;
				id2.set('disabled', true);
			}
			else
			{
				id2.set('disabled', false);
				if ($('choose'))
				{
					$('choose').destroy();
				}
			}
		}
		if (form && id1 && id2) 
		{
			handleSelects();
			id1.addEvent('change', handleSelects);
		}
	}
}



function removeDuplicateTeamMembers()
{
	var members = $$( 'div.vcard' );
	var duplicates = [];
	var found = false;

	members.each( function( member )
	{
		var css_class = member.get( 'class' );
		var vcard_uid = 'vcard-' + css_class.replace( /.*vcard-(\d+).*/, '$1' ).trim();
		if ( !duplicates.contains( '.' + vcard_uid ) )
		{
			member.removeClass( vcard_uid );
			duplicates.include( '.' + vcard_uid );
		}
	} );

	duplicates.each( function( duplicate )
	{
		var elements = $$( duplicate );
		if ( elements && elements.length )
		{
			elements.destroy();
			found = true;
		}
	} );

	if ( found )
	{
		$$( 'div.csc-header h3' ).destroy();
	}

}


// TODO: evtl. boxen höhe anpassen, erste box margin weg etc...
function teaserboxes()
{
	var teaser = $$("#sub .content .download-box");
}

function toplink() {
	var toplink = $('tpl');
	if (toplink) {
		toplink.addEvent('click', function(){
		    scrollTo(0,0);
		    return false;
		});
	}
}

function init()
{
	terminvereinbarung();
	rebuildBrandLocationTabs();
	removeDuplicateTeamMembers();
	//emptyfield("quicksearchtoken","Suchbegriff eingeben");
	toplink();
}

window.addEvent('domready', init);


