
var map;
Cufon.replace('h1')('h2')('a.nav')('a.subNav', {hover:true})('a.subNavActive');

$(document).ready(function()
{
	var i;

    // mitarbeiter detail js
    var list = $('div.mitarbeiterListe');
    if (list.length > 0) {
    	list.data('current', 0);
		var persons = $(list).find('div.mitarbeiter');
		var person;
		for (i=0; i<persons.length; i++) {
			person = persons[i];
			$(person).data('container', list);
			$(person).click(showPerson);
		}
	}
	
	// topnav
	var topNav = $('div.navItem');
	for (i=0; i<topNav.length; i++) {
		var item = topNav[i];
		$(item).hover(navOver, navOut);
	}	

	// gallery
    var immoGallery = $('div.detailThumb');
    if (immoGallery.length > 0) {
		// find images
		$(immoGallery).find('img:last').css('margin-bottom', '0px');
		$(immoGallery).data('current', 'image0');

		var images = $(immoGallery).find('img.thumb');		
		if (images.length < 2) {
			$('#prevImage').css('display', 'none');
			$('#nextImage').css('display', 'none');
		} else {
			$.each(images, function(i,image){
				$(image).click(thumbClick);
			});
			$('#prevImage').click(prevImage);
			$('#nextImage').click(nextImage);
		}

		// check scroller?
		var scroll = $(immoGallery).find('.detailThumbScroller');
		var overflow = $(immoGallery).find('.detailThumbOverFlow');
		if ($(scroll).height() > $(overflow).height()) {
			$('#galleryScrollUp').css('cursor', 'pointer');
			$('#galleryScrollDown').css('cursor', 'pointer');
			$('#galleryScrollUp').click(galleryScrollUp);
			$('#galleryScrollDown').click(galleryScrollDown);
		} else {
			$('#galleryScrollUp').hide(0);
			$('#galleryScrollDown').hide(0);
		}
	}

    // mitarbeiter detail js
//    var list = $('#immoList');
//    if (list.length > 0) {
//    	$(list).find('input.favorite').click(favoriteClicked);
//	}
	$('input.favorite').click(favoriteClicked);


	// lightbox?
	$('a.lightbox').lightBox();

	// cufon
    Cufon.now();

	// suchform?
    var search = $('div.searchFormWrapper');
    if (search.length > 0) {

		// register tab switchers
		if (isHomeSearch) {
			$('#homeTeaserImage')
				.animate({opacity:1.0}, 3000)
				.fadeOut(2500);
			$('#homeImmoForms')
				.animate({opacity:1.0}, 1000)
				.animate({top:0}, 500);

			$('#wohnen-RENT').data('wohnenGewerbe','wohnen');
			$('#wohnen-RENT').data('deal','RENT');
			$('#wohnen-SALE').data('wohnenGewerbe','wohnen');
			$('#wohnen-SALE').data('deal','SALE');
			$('#gewerbe-RENT').data('wohnenGewerbe','gewerbe');
			$('#gewerbe-RENT').data('deal','RENT');
			$('#gewerbe-SALE').data('wohnenGewerbe','gewerbe');
			$('#gewerbe-SALE').data('deal','SALE');

			$('.dealTab').click(dealTabClick);
			$('#wohnenTab').click(homeTabClick);
			$('#gewerbeTab').click(homeTabClick);

			// hack for IE
			//$('#homeImmoForms').css({ zIndex:'1', position:'relative' });
			//$('#homeImmoForms div').css({ zIndex:'1', position:'relative' });
			
		} else {
			$('#wohnenTab').click(immoTabClick);
			$('#gewerbeTab').click(immoTabClick);
		}
		// load json data for hit display
		loadCurrentHits();

		// register onchange handlers
		$(search).find('select').change(loadCurrentHits);

		// da map!!!
		if (GBrowserIsCompatible()) {
			map = new GMap2(document.getElementById("immoMap"));
			map.setCenter(new GLatLng(46.993836,7.518082), 9);
			map.addControl(new GSmallMapControl());
			map.setMapType(G_HYBRID_MAP);
		}
	}

	// detailzeug?
    var detailMap = $('div#detailMap');
    if (detailMap.length > 0) {
		// da map!!!
		if (GBrowserIsCompatible()) {
			map = new GMap2(document.getElementById("detailMap"));
			map.setCenter(new GLatLng(detailLat, detailLong), 17);
			// zoom
			map.addControl(new GSmallMapControl());
			//map.setMapType(G_HYBRID_MAP);
			// satellit/karte
			map.addControl(new GMapTypeControl(true));

			var markerOptions = getMarkerOptions('big');
			var point = new GLatLng(detailLat, detailLong);
			var marker = new GMarker(point, markerOptions);
			//marker.bindInfoWindowHtml(item.title+'<br/>'+item.address+'<br/><a href="">Details zeigen</a>');
			map.addOverlay(marker);
		}
	}
});

function submitForm() {
	// form stuff
	var options = {
		//target:        '#output1',   // target element(s) to be updated with server response
		beforeSubmit:  showRequest,  // pre-submit callback
		success:       showResponse  // post-submit callback
	};
	$('#immoContactForm').ajaxSubmit(options);
//	$('#immoContactForm').submit();
}

function showFillFields() {
	alert('Bitte mindestens Name und gültige E-Mail Adresse eingeben.');
}

function showRequest(formData, jqForm, options) {
	var form = jqForm[0];
    if (!form.firstlast.value || !form.email.value) {
        showFillFields();
        return false;
    } else {
		$('#immoContactForm').data('sendbutton', $('#immoContactSubmit').html());
		$('#immoContactSubmit').html('<img style="margin-right: 40px; margin-top: 2px" src="/fileadmin/templates/main/images/ajax-loader.gif" />');
		return true;
	}
}

function showResponse(response, status) {
	if (response == 'ok') {
		$('#immoContactForm').html('Vielen Dank für Ihre Nachricht.');
	} else {
		$('#immoContactSubmit').html($('#immoContactForm').data('sendbutton'));
		//showRequest(false, $('#immoContactForm'));
		showFillFields();
	}
}

function favoriteClicked() {
	var baseUrl = this.value;
	var action = this.checked ? 'addFav' : 'removeFav';
	var item = this.name;
	url = baseUrl+'?tx_pfister_pi2[action]='+action+'&tx_pfister_pi2[itemUid]='+item;
	$.get(url);
}

function immoTabClick() {
	if (this.id == 'gewerbeTab') {
		wohnenGewerbeFilter = 'gewerbe';
		$('#wohnenTab').removeClass('wohnenActive');
		$('#wohnenTab').addClass('wohnen');
		$('#gewerbeTab').removeClass('gewerbe');
		$('#gewerbeTab').addClass('gewerbeActive');
		$('#wohnen-'+dealFilter+'-Form').hide(0);
		$('#gewerbe-'+dealFilter+'-Form').show(0);
	} else {
		wohnenGewerbeFilter = 'wohnen';
		$('#wohnenTab').addClass('wohnenActive');
		$('#wohnenTab').removeClass('wohnen');
		$('#gewerbeTab').addClass('gewerbe');
		$('#gewerbeTab').removeClass('gewerbeActive');
		$('#gewerbe-'+dealFilter+'-Form').hide(0);
		$('#wohnen-'+dealFilter+'-Form').show(0);
	}
	Cufon.replace('h2');
	loadCurrentHits();
}

function homeTabClick() {
	if (this.id == 'gewerbeTab') {
		wohnenGewerbeFilter = 'gewerbe';
		dealFilter = 'RENT';
	} else {
		wohnenGewerbeFilter = 'wohnen';
		dealFilter = 'RENT';
	}
	updateTabClasses();
	loadCurrentHits();
}

function dealTabClick() {
	wohnenGewerbeFilter = $(this).data('wohnenGewerbe');
	dealFilter = $(this).data('deal');
	updateTabClasses();
	loadCurrentHits();
	return false;
}

function updateTabClasses() {
	if (wohnenGewerbeFilter == 'gewerbe') {
		$('#wohnenTab').removeClass('wohnenActive');
		$('#wohnenTab').addClass('wohnen');
		$('#gewerbeTab').removeClass('gewerbe');
		$('#gewerbeTab').addClass('gewerbeActive');

		$('#wohnen-RENT').removeClass('dealTabActive dealTabActiveInactive');
		$('#wohnen-SALE').removeClass('dealTabActive dealTabActiveInactive');
		$('#wohnen-RENT').addClass('dealTabInactive');
		$('#wohnen-SALE').addClass('dealTabInactive');
		$('#gewerbe-RENT').removeClass('dealTabInactive dealTabActiveInactive');
		$('#gewerbe-SALE').removeClass('dealTabInactive dealTabActiveInactive');
		if (dealFilter == 'SALE') {
			$('#gewerbe-RENT').addClass('dealTabActiveInactive');
			$('#gewerbe-SALE').addClass('dealTabActive');
			$('.immoForm').hide(0);
			$('#gewerbe-SALE-Form').show(0);
		} else {
			$('#gewerbe-RENT').addClass('dealTabActive');
			$('#gewerbe-SALE').addClass('dealTabActiveInactive');
			$('.immoForm').hide(0);
			$('#gewerbe-RENT-Form').show(0);
		}
	} else {
		$('#gewerbeTab').removeClass('gewerbeActive');
		$('#gewerbeTab').addClass('gewerbe');
		$('#wohnenTab').removeClass('wohnen');
		$('#wohnenTab').addClass('wohnenActive');

		$('#gewerbe-RENT').removeClass('dealTabActive dealTabActiveInactive');
		$('#gewerbe-SALE').removeClass('dealTabActive dealTabActiveInactive');
		$('#gewerbe-RENT').addClass('dealTabInactive');
		$('#gewerbe-SALE').addClass('dealTabInactive');
		$('#wohnen-RENT').removeClass('dealTabInactive dealTabActiveInactive');
		$('#wohnen-SALE').removeClass('dealTabInactive dealTabActiveInactive');
		if (dealFilter == 'SALE') {
			$('#wohnen-RENT').addClass('dealTabActiveInactive');
			$('#wohnen-SALE').addClass('dealTabActive');
			$('.immoForm').hide(0);
			$('#wohnen-SALE-Form').show(0);
		} else {
			$('#wohnen-RENT').addClass('dealTabActive');
			$('#wohnen-SALE').addClass('dealTabActiveInactive');
			$('.immoForm').hide(0);
			$('#wohnen-RENT-Form').show(0);
		}
	}
	Cufon.replace('h2');
}

function thumbClick() {
	showImage(this.id);
}
function showImage(which) {
	var immoGallery = $('div.detailThumb');
	$('#'+$(immoGallery).data('current')+'-detail').hide(0);
	$('#'+which+'-detail').fadeIn(330);
	$(immoGallery).data('current', which);	
}
function prevImage() {
	var immoGallery = $('div.detailThumb');
	var images = $(immoGallery).find('img.thumb');
	$.each(images, function(i,image){
		if (image.id == $(immoGallery).data('current')) {
			prevId = i > 0 ? images[i-1].id : images[images.length - 1].id;
		}
	});	
	showImage(prevId);	
}
function nextImage() {
	var immoGallery = $('div.detailThumb');
	var images = $(immoGallery).find('img.thumb');
	$.each(images, function(i,image){
		if (image.id == $(immoGallery).data('current')) {
			nextId = images[i+1] != null ? images[i+1].id : images[0].id;
		}
	});		
	showImage(nextId);
}

function galleryScrollUp() {
	var immoGallery = $('div.detailThumb');
	var scroll = $(immoGallery).find('.detailThumbScroller');
	var overflow = $(immoGallery).find('.detailThumbOverFlow');
	var currentOffset = $(scroll).position();
	var newOffset = currentOffset.top + $(overflow).height();
	newOffset = newOffset > 0 ? 0 : newOffset;
	$(scroll).animate( { top:newOffset+"px" }, { queue:false, duration:500 });			
}
function galleryScrollDown() {
	
	var immoGallery = $('div.detailThumb');
	var scroll = $(immoGallery).find('.detailThumbScroller');
	var overflow = $(immoGallery).find('.detailThumbOverFlow');
	var currentOffset = $(scroll).position();
	var newOffset = currentOffset.top - $(overflow).height();
	newOffset = $(scroll).height() - $(overflow).height() + 6 > -newOffset ? newOffset : $(overflow).height() - $(scroll).height();
	$(scroll).animate( { top:newOffset+"px" }, { queue:false, duration:500 });	
}

function loadCurrentHits() {
	
	$('#'+wohnenGewerbeFilter+'-'+dealFilter+'-Form .searchHitsId').html('<img src="/fileadmin/templates/main/images/ajax-loader.gif" />');
	if (map != null) {
		map.clearOverlays();
	}

	// filters
	var filtersURL = '';
	filtersURL += '&tx_pfister_pi2[f][wohnengewerbe]='+wohnenGewerbeFilter;
	filtersURL += '&tx_pfister_pi2[f][deal]='+dealFilter;
	var filters = $('#'+wohnenGewerbeFilter+'-'+dealFilter+'-Form select');
	$.each(filters, function(i,item){
		if (item.value != 'all' && item.value > 0) {
			filtersURL += '&'+item.name+'='+item.value;
		}
	});

	var url = "/index.php?id=10&tx_pfister_pi2[ajax]=true"+filtersURL;
	$.getJSON(url, updateSearchHits);
	/*
        function(data){
          $.each(data.items, function(i,item){
            $("<img/>").attr("src", item.media.m).appendTo("#images");
            if ( i == 3 ) return false;
          });
        });
		*/
}

function updateSearchHits(json) {
	// display xy Treffer
	$('#'+wohnenGewerbeFilter+'-'+dealFilter+'-Form .searchHitsId').html(json.count+' Treffer');
	// adapt link
	$('#'+wohnenGewerbeFilter+'-'+dealFilter+'-Form .showResults').attr('href', json.url);
	// update map
	var markerOptions = getMarkerOptions('small');

	if (json.items != null) {
		$.each(json.items, function(i,item){
			if (item.lat != null && item.lat > 0) {
				var point = new GLatLng(item.lat, item.long);
				var marker = new GMarker(point, markerOptions);
				marker.bindInfoWindowHtml(item.title+'<br/>'+item.address+'<br/><a href="'+item.link+'">Details zeigen</a>');
				map.addOverlay(marker);
			}
		});
	}
}

function getMarkerOptions (size) {
	var icon = new GIcon(G_DEFAULT_ICON);
	if (size == 'small') {
		icon.image = "http://pfister-immobilien.ch.pfister.nine.ch/fileadmin/templates/main/images/mapIconSmall.png";
		icon.iconSize = new GSize(49, 27);
		icon.shadow = "http://pfister-immobilien.ch.pfister.nine.ch/fileadmin/templates/main/images/mapIconSmallBg.png";
		icon.shadowSize = new GSize(49, 27);
	} else {
		icon.image = "http://pfister-immobilien.ch.pfister.nine.ch/fileadmin/templates/main/images/mapIconDef.png";
		icon.iconSize = new GSize(30, 30);
		icon.shadow = "http://pfister-immobilien.ch.pfister.nine.ch/fileadmin/templates/main/images/mapIconShadow.png";
		icon.shadowSize = new GSize(64, 30);
	}
	var markerOptions = { icon:icon };
	return markerOptions;
}

function showPerson() {
	var current = $(this).data('container').data('current');
	var myNumber = this.id;
	$('#'+current).parent().css('z-index','1');
	$('#detail-'+current).hide(0);
	$('#detail-'+myNumber).show(0);
	$('#'+myNumber).parent().css('z-index','9999');
	$(this).data('container').data('current', myNumber);
}
function hidePerson(id) {
	$('#detail-'+id).hide(0);
}


function navOver() {
	$(this).addClass('navActive');
	$(this).removeClass('nav');
}

function navOut() {
	if (!$(this).hasClass('isActive')) {
		$(this).addClass('nav');
		$(this).removeClass('navActive');
	}
}


