var basehref; // set with jQuery $('base').attr('href');

var id = false;

var ShBoxOptions = {
		language:				'en',
		counterType:			'skip',//'default',//'skip',
		overlayOpacity:	   		'0.6',
		overlayColor:			'#000',
		//handleUnsupported:  	'remove',
		//onFinish:				galleryElemDesc,
		initialHeight:			'450',//'160',
		initialWidth:			'550',//'320',
		resizeDuration:			'0.2',//'0.55',
		fadeDuration:			'0.15',//'0.35',
		animSequence:			'sync',//'wh',
		handleOversize:			'resize',//'drag',
		continuous:				true,
		flvPlayer:				'gui/media/mediaplayer2.swf'
};
// Shadowbox start
Shadowbox.init(ShBoxOptions);

// init & start
$(function() {

	// set base url var
	basehref = $('base').attr('href');
	
	// searchfield
	initsearchfield();
	
	// google map
	if(id){
		
		if (GBrowserIsCompatible()) {
			
			var thiselement = document.getElementById(id);
			
			var info = $(thiselement).html();
			var map = new GMap2(thiselement);
			var point = new GLatLng(coordlat,coordlng);
			
			if( $("body.homepage").length ){
				map.addControl(new GSmallMapControl());
				map.setCenter(point,11);
			} else {
				map.addControl(new GLargeMapControl());
				map.addControl(new GMapTypeControl());
				map.setCenter(point,13);
				map.openInfoWindow(map.getCenter(),info);
			}
			
			map.addOverlay(createMarker(point,info));
			
		}
	}

	
	
});

/*
// functions
*/

// Creates a marker at the given point
function createMarker(point,info) {
	var marker = new GMarker(point);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(info);
	});
	return marker;
}


// search
function initsearchfield(){
	
	$('#searchform form')
  	.each(function(){
  	
		label = $('td.searchlabel label',$(this)).hide().text();
		
		var txt = label.replace(/^\s\s*/,'').replace(/\s\s*$/,'');
		var theform = $(this);
		var theinput = $('td input.search',$(this));
		var val = $(theinput).val();
		
		if(val == ''){
			$(theinput).val(txt);
		}
		
		$(theinput)
		.mouseover(function(){
			val = $(theinput).val();
			if(val == txt){
				$(theinput)
				.animate({ 
					"opacity":0.0
				},'fast','',function(){
					$(theinput).val('').animate({
						"opacity":1.0
					},1);
				});
			}
		})
		.mouseout(function(){
			val = $(theinput).val();
			if(val == ''){
				$(theinput).stop().val(txt);
			}
		})
		.focus(function(){
			$(theinput).unbind("mouseout");
		})
		.blur(function(){
			val = $(theinput).val();
			if(val == ''){
				$(theinput).val(txt);
			}
			
			$(theinput).mouseout(function(){
				val = $(theinput).val();
				if(val == ''){
					$(theinput).val(txt);
				}
			});
		});
		
	});
	
}

// searchfield show / hide
var searchlabel = '';
function initsearchfield_NEWNEEDSWORK(){
	
	$('td input.search',
		
		$('#searchform')
		.mouseover(function(){
			
			// hide label
			var theinput = $('td input.search',$(this));
			var val = $(theinput).val();
			
			$(theinput).stop();
			 
			if(val == ''){
				
				$(theinput)
				.animate({ 
					"opacity":'1.0'
				},'fast','',function(){
					$(this).focus();
				});
			}
			
		})
		.mouseout(function(){
			
			// showlabel
			var theinput = $('td input.search',$(this));
			field_showlabel(theinput);
			
		})
		.blur(function(){
			
			// showlabel
			var theinput = $('td input.search',$(this));
			field_showlabel(theinput);
			
		})
		
	).each(function(){
		
		var labeltxt =  $('label',
			$(this)
			.parent('td')
			.prev('td')
			.hide()
		)
		.text();
		
		searchlabel = labeltxt.replace(/^\s\s*/,'').replace(/\s\s*$/,'');
		
		// showlabel
		field_showlabel(this);
		
	});
}

// searchfield showlabel
function field_showlabel(theinput){
	
	var val = $(theinput).val();
	
	$(theinput)
	.stop();
	 
	if(val == ''){
		
		$(theinput)
		.animate({ 
			"opacity":'1.0'
		},'fast','',function(){
			$(this)
			.blur()
			.val(label)
			;
		});
	}
}
