var color = 'grey';
var state_default = '../images/map/map_grey_0.gif';
var pics = new Array();

for (var i = 0; i <= 16; i++) {
	pics[i] = new Image();
	pics[i].src = '../images/map/map_grey_'+i+'.gif';
}

function on(state_id, color) {
	if(!color) {
		color = 'grey';
	}
	if(!state_id || state_id == '0' || state_default == '../images/map/map_blue_' + state_id + '.gif') {
		document.getElementById('image_map').src = state_default;
	}
	else {
		document.getElementById('image_map').src = '../images/map/map_' + color + '_' + state_id + '.gif';
	}
}

function Delete_Cookie( name, path, domain ) {
	if ( Get_Cookie( name ) ) document.cookie = name + "=" +
	( ( path ) ? ";path=" + path : "") +
	( ( domain ) ? ";domain=" + domain : "" ) +
	";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

function Set_Cookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );

	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
	( ( path ) ? ";path=" + path : "" ) +
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

function setLang(lang) {
	Delete_Cookie('__language');
	Set_Cookie('__language', lang, 30, '/', '', '');
	window.location.href = getBaseURL();
}

function a(b){
	var c = $('#slidernavi').find('.active').attr("id");
	c=c.substring(c.length-1,c.length);
	if($.browser.msie && $.browser.version.substr(0,1) <= 9) {
		$('.bg'+ c).css("filter", "alpha(opacity=0)"); 
		$('.bg'+ c).css({"visibility": "hidden"}); 
		$("li.active").removeClass("active");
		var c=b.position();
		$("#sliderArrow").animate({top:c.top+30},"normal","linear",function(){
			$(b).addClass("active")
		});
	} else if (!$.browser.msie) {
		$('.bg'+ c).animate({opacity: 0.0}, 1000, function() {
			$(this).css({visibility: "hidden"});
			$("li.active").removeClass("active");
			var c=b.position();
			$("#sliderArrow").animate({top:c.top+30},"normal","linear",function(){
				$(b).addClass("active")
			});
		});
	}
}
function strtolower (str) {
    return (str + '').toLowerCase();
}

function getBaseURL() {
    var url = location.href;
    var baseURL = url.substring(0, url.indexOf('/', 14));
    return baseURL + "/";
}

function getCurrentPage() {
	pathArray = window.location.pathname.split( '/' );
	newPathname = "";
	for ( i = 0; i < pathArray.length; i++ ) {
		if(i == (pathArray.length-1)) {
			newPathname += pathArray[i];
		}
	}
	return strtolower(newPathname);
}

$(document).ready(function(){
	//effect for the Browser to disable the dotted lines around a link
	$("a, input[type=submit], button").focus(function(){
	  $(this).blur();
	});

	$('a.new-window').click(function(){
		window.open(this.href);
		return false;
	});
	
	$("#slidernavi li").click(function(){
		if(!$(this).hasClass("active")) {
			a($(this));
			var b=$(this).attr("id");
			b=b.substring(b.length-1,b.length);
			$('.bg'+ b).css({opacity: 0.0, visibility: "visible"}).animate({opacity: 1.0}, 1000);
		}
	});
	
 	$(".sbMap").tooltip({ tipClass: 'map', effect: 'slide', offset: [10, -70], position: 'top right', delay: 500});
	$(".sbWeb").tooltip({ tipClass: 'web', effect: 'slide', offset: [10, -70], position: 'top right', delay: 500});

	$("#slidernavi li a").click(function(){
		if($(this).parent().hasClass("active")) {
			return true;
		} else {
			$(this).parent().click();
			return false;
		}
	});
	
	$('form#search').submit(function() {
	  	window.location.href=getBaseURL() + $(this).attr('action') + '?' + $('form#search :input[value]').serialize();
	  	return false;
	});
	
	
	if($('#covering').get(0) !== undefined && $('#covering').get(0).checked) {
		$('#labelcoveringletter').removeClass('none');
	}
	
	$('#covering').click(function() {
		if(!$(this).get(0).checked) {	
			$('#labelcoveringletter').removeClass('none');
		} else {
			$('#labelcoveringletter').addClass('none');
		}
	});
	
	$('#multichoice').click(function() {
		if(!$(this).get(0).checked) {
			Set_Cookie('__categorymenue', 'categorymenue_multi', 30, '/', '', '');
			$('#categorymenue_single').addClass('none');
			$('#categorymenue_multi').removeClass('none');
		} else {
			Set_Cookie('__categorymenue', 'categorymenue_single', 30, '/', '', '');
			$('#categorymenue_multi').addClass('none');
			$('#categorymenue_single').removeClass('none');
		}
	});
	
	$('#language_menue').click(function(e) {
		if($(this).hasClass('active')) {
			$(this).removeClass('active');
			$('.language_menue').css({'display': 'none'});
		} else {
			$(this).addClass('active');
			$('.language_menue').css({'display': 'block'});
		}
		return false;
	});
	
	$('.close').click(function() {
		$(this).parent().addClass('none');
		return false;
	});
	$('.minimize').click(function() {
		$(this).addClass('none').next('a.maximize').removeClass('none').parent().next('div.slide').slideUp('slow');
		return false;
	});
	$('.maximize').click(function() {
		$(this).addClass('none').prev('a.minimize').removeClass('none').parent().next('div.slide').slideDown('slow');
		return false;
	});
	
	$('.twitter[title], .facebook[title]').tipsy({gravity: 'nw', fade: true});
	$('.twitter2[title], .knowhow[title]').tipsy({gravity: 'n', fade: true});
	$('.seminar[title], .medienarkaden[title]').tipsy({gravity: 'no', fade: true});
	
	$('input[type="checkbox"], input[type="radio"]').checkbox({cls:'checkbox'});
	
	$("input[name=q]").autocomplete(getBaseURL() + "auto/search_term", {
		width: 395,
		selectFirst: false
	});
	$("input[name=where]").autocomplete(getBaseURL() + "auto/search_locate", {
		width: 395,
		selectFirst: false
	});
	$("input[name=zipcode]").autocomplete(getBaseURL() + "auto/zipcode", {
		width: 110,
		selectFirst: true
	});
});
