//On DOM Ready events
$(document).ready(function() {
	var bid = 0;
		$('a.main').each(function(index) {
			$(this).mousedown(function(e){
				if($(this).attr('id')==null||$(this).attr('id')==undefined) return;
				if($(this).hasClass('noredir')==true) return;
				bid = $(this).attr('id').split('-')[2];
				if(bid==null||bid==undefined) return;
				if(typeof(srRadius) == 'undefined' || srRadius == null) { srRadius = '0' };
				$(this).attr('href', 'http://www.suche.ch/redir/?t='+ oldTags +'&l='+ srLocation +'&bId=' + bid +'&r=' + srRadius);
			});
		});

	if($('#homepage').length && navigator.appName == 'Microsoft Internet Explorer' && navigator.appVersion.indexOf('MSIE 7') < 0) {
		$('#homepage').css('display', '');
	}
	
	$('.tipper').each(function() {
	  var obj = $(this).attr('title');
	  obj = obj.split(':');
	  if (obj.length==1) {
		$(this).attr('title','');
		$(this).qtip({
        	content: obj[0]
		});
	  }
	  else if(obj.length==2) {
	  	if (obj[0]=='DOM') {
			$(this).attr('title','');
			$('img', this).qtip({
		        content: $('#' + obj[1]).html(),
				position: {
                  corner: {
                     tooltip: 'topMiddle',
					 target: 'bottomMiddle'
                  }
                },
				style: {
				  width: 400,
                  border: {
                     width: 3,
                     radius: 8,
					 color: '#666666'
                  },
                  padding: 10, 
                  textAlign: 'left',
                  tip: true, 
                  name: 'light' 
               }				
		    });
		}
		else if (obj[0]=='AJAX') {
			$(this).attr('title','');
			$('img', this).qtip({
		        content: { url: obj[1]}
		    });
			
		}
	  }
   });	
	
	/*var createTips = new TipsX3($$('.tipper'), {
		showDelay: 850,
		hideDelay: 250
	});
	*/
	
	$('#badge').click(function(e) {
		e.preventDefault();
		e.stopPropagation();
		showBoxVideo(this, this.href, 640, 520);
	});

	$('.startseite-video').click(function(e) {
		e.preventDefault();
		e.stopPropagation();
		showBoxVideo(this, this.href, 640, 520);
	});
	
	$('.portrait-video').click(function(e) {
		e.preventDefault();
		e.stopPropagation();
		showBoxVideo(this, this.href, 640, 380);
	});
	
	/*	
	Growl.Bezel = new Gr0wl.Bezel('/style/image/bezel.png');
	Growl.Smoke = new Gr0wl.Smoke('/style/image/smoke.png');
	*/
	if(oldTags == '') {
		hideMoreBoxes();
		$('#banners li.showSub').each(function() {
			$(this).bind('mouseenter', function() {
				showMoreBox(this);
			});
			$(this).bind('mouseleave', function() {
				hideMoreBoxes();
			});
		});
	};
	
	$('#banners li.showSub').each(function() {
		if($('div.more-info',this)) {
			$('div.more-info a').bind('click', function(e) {
			e.preventDefault();
				e.stopPropagation();
				showBox(this, $(this).attr('href'));
			});
		}
		if($('div.more-video',this)) {
			$('div.more-video a').bind('click', function(e) {
				e.preventDefault();
				e.stopPropagation();
				showBox(this, $(this).attr('href'));
			});
		}					
		$('a.noredir').bind('click', function(e) {
		e.preventDefault();
			e.stopPropagation();
			showBox(this, $(this).attr('href'));
		});
	});

	$('#favorite-video').bind('click', function(e) {
		e.preventDefault();
		e.stopPropagation();
		showBoxVideo(this, $(this).attr('href'), 640, 400);
		return false;
	});
		
	$('#favorite-sort').bind('click', function(e) {
		e.preventDefault();
		e.stopPropagation();
		showBox(this, $(this).attr('href'));
		return false;
	});

	$('#favorite-add').bind('click', function(e) {
		e.preventDefault();
		e.stopPropagation();
		showBoxFavorites(this, $(this).attr('href'));
		return false;
	});
	
	$('.favorite-add-banner').bind('click', function(e) {
		e.preventDefault();
		e.stopPropagation();
		showBoxFavorites(this, $(this).attr('href'));
		return false;
	});
	
	$('.favorite-add-video').bind('click', function(e) {
		e.preventDefault();
		e.stopPropagation();
		showBoxVideo(this, $(this).attr('href'), 640, 400);
		return false;
	});
			
	$('#favorite-email').bind('click', function(e) {
		e.preventDefault();
		e.stopPropagation();
		showBoxFavoritesEmail(this, $(this).attr('href'));
		return false;
	});

		
	if($('#flash-position').length) swfobject.embedSWF("/einstieg.swf", "flash-position", "189", "200", "7.0.0", "expressInstall.swf", false, {menu: false, wmode: 'transparent'});
	
	if(!multipleLocations) {
				
		$('#ort').autocomplete({
			source: '/zip-range.cfc?method=search',
			minLength: 2,
			autoFocus: false,
			select: function( event, ui ) {
				//$('#tagIndicator').css('display', 'none');
				//$('#ortIndicator').css('display', 'none');
				if($.trim(ui.item.value) != '') {
					sendForm(false, $.trim(ui.item.value));
				}
				//log( ui.item ?
				//	"Selected: " + ui.item.value + " aka " + ui.item.id :
				//	"Nothing selected, input was " + this.value );
			}
		});		
			
		
		// AutoComplete Original
		/*
		if (!window.webkit) {
			ortIndicator = new Element('div', {'class': 'autocompleter-loading', 'styles': {'display': 'none'}}).setHTML('').injectAfter(inpOrt);
			var ortCompleter = new Autocompleter.Ajax.Json(inpOrt, '/zip-range.cfc', {
					postVar: 'ort',
					delay: 5,
					minLength: 2,
					maxChoices: 15,
					inheritWidth: false,
					method: 'GET',
					postData: {
						method: 'search',
						ort: inpOrt.value
					},
					onRequest: function(el) {
						ortIndicator.setStyle('display', '');
					},
					onComplete: function(el) {
						ortIndicator.setStyle('display', 'none');
					},
					onBlur: function(el) {
						ortIndicator.setStyle('display', 'none');
					},
					onSelect: function(el) {
						if(tagIndicator) tagIndicator.setStyle('display', 'none');
						ortIndicator.setStyle('display', 'none');
						if($('tag').value.trim() != '') sendForm(); 
					}
				}
			);
		}
		*/
	}
	$('#tag').autocomplete({
			source: '/tag-suggest.cfc?method=suggest',
			minLength: 3,
			autoFocus: false,
			select: function( event, ui ) {
				//$('#tagIndicator').css('display', 'none');
				//$('#ortIndicator').css('display', 'none');
				if($.trim(ui.item.value) != '') {
					sendForm($.trim(ui.item.value), false); 
				}
				//log( ui.item ?
				//	"Selected: " + ui.item.value + " aka " + ui.item.id :
				//	"Nothing selected, input was " + this.value );
			}
		});		
	
	
	/*
	if (!window.webkit) {
		tagIndicator = new Element('div', {'class': 'autocompleter-loading', 'styles': {'display': 'none'}}).setHTML('').injectAfter(inpTag);
		var tagCompleter = new Autocompleter.Ajax.Json(inpTag, '/tag-suggest.cfc', {
				postVar: 'tag',
				delay: 50,
				minLength: 3,
				maxChoices: 10,
				inheritWidth: false,
				method: 'GET',
				postData: {
					method: 'suggest',
					tag: inpTag.value
				},
				onRequest: function(el) {
					tagIndicator.setStyle('display', '');
				},
				onComplete: function(el) {
					tagIndicator.setStyle('display', 'none');
				},
				onBlur: function(el) {
					tagIndicator.setStyle('display', 'none');
				},
				onSelect: function(el) {
					tagIndicator.setStyle('display', 'none');
					ortIndicator.setStyle('display', 'none');
					if($('tag').value.trim() != '') sendForm();
				}
			}
		);
	}
	*/
	
	$('#tag').bind('click', function() { 
		$('#tag').addClass('notempty');
	});	
	
	$('#ort').bind('click', function() { 
		$('#ort').addClass('notempty');
	});		
	
	$('#tag').bind('blur', function() { 
		if($('#tag').val() != '') { 
			$('#tag').addClass('notempty');
		}
		else {
			$('#tag').removeClass('notempty');
		}
	});
	
	$('#ort').bind('blur', function() { 
		if($('#ort').val() != '') {
			$('#ort').addClass('notempty');
		}
		else {
			$('#ort').removeClass('notempty');
		}			
	});	
	
	
	$('#tag').bind('keyup', function() { 
		if($('#tag').val() != '') { 
			$('#tagKill').css('display',''); 
			$('#tag').addClass('notempty');
		}
		else {
			$('#tagKill').css('display','none'); 
			$('#tag').removeClass('notempty');
		}
	});
	
	$('#ort').bind('keyup', function() { 
		if($('#ort').val() != '') {
			$('#ortKill').css('display',''); 
			$('#ort').addClass('notempty');
		}
		else {
			$('#ortKill').css('display','none'); 
			$('#ort').removeClass('notempty');
		}			
	});
		
	$('#tag').bind('keydown', function(e){
		if(e.keyCode == "13") {
			e.preventDefault();
			e.stopPropagation();
			if($.trim($('#tag').val()) != '') sendForm($.trim($('#tag').val()), false);
		}
	});
			
	$('#ort').bind('keydown', function(e){
		if(e.keyCode == "13") {
			e.preventDefault();
			e.stopPropagation();
			if($.trim($('#tag').val()) != '') sendForm();
		}
	});
	
	if($('#tag').val() == '') {
		$('#tag').focus();
		$('#tagKill').css('display','none'); 
	} else if($('#tag').val() != '') {
		$('#ort').focus();
		$('#ortKill').css('display','none'); 	
	} else {
		document.body.focus();
	}
	
	if($('#ort').val() == '') {
		$('#ortKill').css('display','none'); 
	}
	else {
		$('#ortKill').css('display',''); 
	}
});

