var iaz = {}; // globales Variablen-Objekt

jQuery(document).ready(function() {
	iaz.iconurl = "http://labs.google.com/ridefinder/images/replace";
	iaz.foehr_lat = '54.718223';
	iaz.foehr_lng = '8.495554';
	iaz.map_rendered = false;
	iaz.map;
	
	var initSelect = jQuery('#subcategory_select option[selected=selected]').attr('value');
	
	iaz.allSubs = jQuery('#subcategory_select option').clone();

	// premium galleries
	jQuery('.tx-smsfoehraz-pi1 .switch_prev').live('click',function(){slideGallery('prev',jQuery(this).parent().attr('id').replace('curr_',''));});
	jQuery('.tx-smsfoehraz-pi1 .switch_next').live('click',function(){slideGallery('next',jQuery(this).parent().attr('id').replace('curr_',''));});

	// Suche
	iaz.ignoreSearch = jQuery('#sword').val();	// bei Suche ignorieren
	jQuery('#reset_search_link').click(function(){resetSearch();});
	jQuery('#maincategory_select').change(function(){
		updateSubcatSelect(jQuery(this).val());
		tx_smsfoehraz_pi1_updatePage(buildSearchString(1));
	});
	jQuery('#subcategory_select').change(function(){tx_smsfoehraz_pi1_updatePage(buildSearchString(1));});
	jQuery('#search_submit').click(function(){updateSearchInfo();tx_smsfoehraz_pi1_updatePage(buildSearchString(1));});
	jQuery('#sword').focus(function(){if (jQuery(this).val()===iaz.ignoreSearch) jQuery(this).val('');});
	jQuery('#sword').blur(function(){if (jQuery(this).val() == '') {jQuery(this).val(iaz.ignoreSearch);}});

	// Pager
	jQuery('.paginator a').live('click',function(e){
		e.preventDefault();
		if (jQuery(this).hasClass('cur')) {
			return;
		}
		tx_smsfoehraz_pi1_updatePage(buildSearchString(jQuery(this).attr('rel')));
		
		
	});

	// link-to-map
	jQuery('.maplink').live('click',function(){showMapWithMarker(this);});
	jQuery('.map_container .map_close_hint').live('click',function(){jQuery(this).parent().hide();});

	// special handling for "Kurabgabe"
	if(window.location.href.indexOf('keyword=Kurabgabe') !== -1){
		tx_smsfoehraz_pi1_updatePage('1@@6@@78@@');
		jQuery('#maincategory_select option[value=6]').attr('selected','selected');
		updateSubcatSelect('6');
		jQuery('#subcategory_select option[value=78]').attr('selected','selected');
	} else {
		updateSubcatSelect(jQuery('#maincategory_select option:selected').val());
		jQuery('#entry_list').show();
		//jQuery('#pager').show();
	}
	jQuery('#subcategory_select [value=' + initSelect + ']').attr('selected','selected');
});

function responseCallback(){
	 $('html').animate({scrollTop : 350});
}

function updateSearchInfo(){
	var sI = jQuery('#searchInfo').empty();
	var val = jQuery('#sword').val();
	if (val.length && val != iaz.ignoreSearch) {
		sI.html('Folgende Anbieter wurden zu Ihrer Suchanfrage nach "<b>' + val + '</b>" gefunden:');
	}	
};

// GoogleMap rendern
function showMapWithMarker(entry){
	jQuery('.map_container').show();

	if (iaz.map_rendered) {
		iaz.map.clearOverlays();
		iaz.map.setCenter(new GLatLng(iaz.foehr_lat,iaz.foehr_lng), 12);
	} else {
		if (jQuery('#main').length){
			jQuery('#main').append(jQuery('.map_container').remove());
		}
		if (GBrowserIsCompatible()) {
			iaz.map = new GMap2(document.getElementById('map'));
			iaz.map.setCenter(new GLatLng(iaz.foehr_lat,iaz.foehr_lng), 12);
			iaz.map.enableScrollWheelZoom();
			iaz.map.disableDoubleClickZoom();
			iaz.map.addControl(new GLargeMapControl());
			iaz.map.addControl(new GMenuMapTypeControl());
			//iaz.map.disableDragging();
		} else {
			return alert('Ihr Browser kann keine Karten von google anzeigen.');
		}
		iaz.map_rendered = true;
	}

	var id = entry.id.replace('maplink_','');
	var lat = jQuery('#lat_' + id).val();
	var lng = jQuery('#lng_' + id).val();
	var title = jQuery('#entry_' + id + ' .special_headline:first').text();
	var shortText = jQuery('#entry_' + id + ' .shortText:first').text();

	if (lat && lng) {
		var icon = new GIcon();
		icon.image = smsfoehraz_googleMarker[jQuery('#map_cat_' + id).val()];
		icon.shadow = iaz.iconurl.replace('replace','mm_20_shadow.png');
		icon.iconSize = new GSize(12, 20);
		icon.shadowSize = new GSize(22, 20);
		icon.iconAnchor = new GPoint(6, 20);
		icon.infoWindowAnchor = new GPoint(5, 1);
		var marker = new GMarker(new GLatLng(lat,lng),{icon:icon,title:''});
		iaz.map.addOverlay(marker);
		GEvent.addListener(marker,'click',function() {
			marker.openInfoWindowHtml('<div class="map_bubble"><div class="title">' + title + '</div><div class="short">' + shortText + '</div>');
		});
		marker.openInfoWindowHtml('<div class="map_bubble"><div class="title">' + title + '</div><div class="short">' + shortText + '</div>');
	}
}

// image-gallery at premium-entries
function slideGallery(direction,id){
	var entry = parseInt(id.split(':')[0]);
	var current = parseInt(id.split(':')[1]);
	var count = parseInt(jQuery('#entry_' + entry + ' .img_wrapper img').length);
	var next_img_to_show;
	var info;
	if (direction=='prev') {
		next_img_to_show = (current-1);
		info = current;
		if (next_img_to_show < 0) {
			next_img_to_show = (count-1);
			info = count;
		}
	}
	if (direction=='next') {
		next_img_to_show = (parseInt(current)+1);
		info = (parseInt(current)+2);
		if (next_img_to_show >= count) {
			next_img_to_show = 0;
			info = 1;
		}
	}
	jQuery('.tx-smsfoehraz-pi1 img[id=image_' + entry + ':' + current + ']').hide();
	jQuery('.tx-smsfoehraz-pi1 img[id=image_' + entry + ':' + next_img_to_show + ']').show();
	jQuery('.tx-smsfoehraz-pi1 [id=curr_' + id + ']').attr('id','curr_' + entry + ':' + next_img_to_show);
	jQuery('.tx-smsfoehraz-pi1 [id=entry_' + entry + '] .switch_info').text('Bild ' + info + ' von ' + count);
}

// empties search-fields and reloads data
function resetSearch(){
	jQuery('#maincategory_select option:first').attr('selected',true);
	updateSubcatSelect('0');
	jQuery('#sword').val(iaz.ignoreSearch);
	tx_smsfoehraz_pi1_updatePage(buildSearchString(1));
}

// Suchstring zusammenbauen
function buildSearchString(page){	
	var maincat = parseInt(jQuery('#maincategory_select option:selected').attr('value'));
	//var subcat = parseInt(jQuery('#subcategory_select option[selected=selected]').attr('value'));
	var subcat = parseInt(jQuery('#subcategory_select option:selected').attr('value'));
	var sword = ((jQuery('#sword').val() === iaz.ignoreSearch) ? '' : jQuery('#sword').val());
	return page + '@@' + maincat + '@@' + subcat + '@@' + sword;
}

// updates subcategory-select, depends on maincategory-select

function updateSubcatSelect(mc){
	var sel = jQuery('#subcategory_select');
	sel.children().remove();

	sel.append(iaz.allSubs.filter(':first').attr('selected',true));
	if (mc == '0'){
		// = alle Kategorien auswaehlen		
		sel.attr('disabled',true);
	} else {
		// = nur passende Subkategorien anzeigen
		sel.append(iaz.allSubs.filter('[rel=' + mc + ']'));
		sel.attr('disabled',false);
	}
}
