/* DOM ready *********************************************************************************************** */

	$(document).ready(function() {
		if($('#team').length==1) //Team
			initTeam();
		else if($('#select_options').length==1){ //Immobilien

			$('#select_options input[type="radio"]').click(function(){
				updatePropertyList(false,0);						
			});
			$('#select_options input[type="text"]').bind('keyup blur',function(){
				updatePropertyList(false,0);						
			}).bind('focus',function(){
				$(this).parent().children('input[type="radio"]')[0].checked = true;						
			});
			updatePropertyList(false,0);
		}
		else if($('#gmap').length==1) //Kontakt
			initGMap();

		smoothScroll();
	});



/* Immobilien ************************************************************************************************ */
	function showDetails(id){
		trace('@func showDetails -> '+id);
		var target;
		var obj = $('#property_list_item_'+id);
		var loading = $('#property_list_item_'+id+' '+'.property_list_thumb span img');
		loading.css({'opacity':0.3});
		
		if(document.getElementById('property_details_'+id)){
			obj.hide();
			loading.css({'opacity':1});
			
			var el = $('#property_details_'+id);
			el.hide();
			el.fadeIn();
		}
		else{
			($('#admin').length!=1) ? target = "ajax/ajax.immobilien.php" : target = "ajax/ajax.immobilien.admin.php";
			$.ajax({
				type: "POST",
				url: target,
				data: "id="+id,
				success: function(msg){
					obj.after(msg);
					obj.hide();
					loading.css({'opacity':1});
					if($('#admin').get(0)){
						setTimeout("uploadMaster('"+id+"')",1000);
					}
					
					var el = $('#property_details_'+id);
					el.hide();
					el.fadeIn();
					smoothScroll();
				}
			});
		}
	}
	
	function uploadMaster(id){
		$('#upload_master_container_'+id).uploadify({
			'uploader': 	'js/jquery.uploadify/uploadify.swf',
			'script': 		'js/jquery.uploadify/uploadify.php',
			'folder': 		'file_upload/'+id,
			'cancelImg' : 	'js/jquery.uploadify/cancel.png',
			'multi' : 		true,
			'buttonText' : 'Bilder Upload',
			//'buttonImg' : 'js/jquery.uploadify/button_upload.gif',
			onComplete : function(event_obj,queue_id,file_object,response,type){
				ajaxUpdateImages(id);
				//$('#admin_info_'+id).html('SUCCESS');
			},
			onError : function(event_obj,queue_id,file_object,error_object){
				//$('#admin_info_'+id).html('ERROR');
			},
			onProgress : function(event_obj,queue_id,file_object,data){
				//$('#admin_info_'+id).html('PROGRESS: speed:'+data['speed']+'kb/s');
			},
			onOpen : function(event_obj,queue_id,file_object){
				//$('#admin_info_'+id).html('OPEN');
			}

		});	
	}
	
	function uploadFiles(id){
		//$('#admin_info_'+id).html('Hochladen');
		var obj = $('#upload_master_container_'+id);
		obj.uploadifyUpload();
	}
	
	function closeDetails(id){
		var obj = $('#property_details_'+id);
		var li = $('#property_list_item_'+id);
		obj.hide();
		li.fadeIn();
	}
	
	function showImage(id,pos,src){
		var img = $('#property_details_image_'+id);
		var parentTag = img.parent();
		var a = $('#property_details_'+id+' .property_details_image_list a');
		
		//Teaserbild um Klasse 'active' ergänzen
			a.each(function(i){
				$(this).removeClass('active');
			});
			$(a[pos]).addClass('active');
		
		//Höhe des Parent Div erhalten, bis neues Bild geladen ist
			var h = img.attr('height')+2;
			parentTag.css('height',h);
			img.hide();
			img.attr("src",src);
			img.load(function () {
				parentTag.css('height','auto');
				img.fadeIn();
			});
	}
	
	
	
/* Update Immobilien Liste *********************************************************************************** */
	function updatePropertyList(re,limit){
		trace('@func showDetails -> '+re+' / '+limit);

		var cat = $('#select_options_category input:checked').val();
		var us = $('#select_options_usage input:checked').val();
		var so = $('#select_options_sort input:checked').val();
		var space_from = $('#space_from').val();
		var space_to = $('#space_to').val();
		var h = $('.property_hits');
		
		h.addClass('loading');
		h.html('');
		
		var obj = $('#property_list');
		$.ajax({
			type: "POST",
			url: "ajax/ajax.immobilien.list.php",
			data: "cat="+cat+"&us="+us+"&so="+so+'&sf='+space_from+'&st='+space_to+'&limit='+limit,
			success: function(content){
				obj.html(content);
				smoothScroll();
				if(re)reloadProject();
			}
		});	
	}
	
	function reloadProject(){
		$.ajax({
			type: "POST",
			url: "ajax/ajax.immobilien.lastid.php",
			success: function(id){
				showDetails(id);
			}
		});
	}



/* Print Ansicht ********************************************************************************************** */
	
	function printPreview(id){
		if(!document.getElementById('print_preview')){
			//console.log('Insert Print Preview');
			$('#center').append('<div id="print_preview"><div class="load">Druckvorschau wird erstellt...</div></div>');	}
		else  {
			$('#print_preview').html('<div class="load">Druckvorschau wird erstellt...</div>');
			//console.log("Nein");}
		}
		
		$('#immobilien').addClass('print');
		$('#header').css('display','none');
		$('#content').css('display','none');
		$('#seo_keywords').css('display','none');
		
		$.ajax({
			type: "POST",
			url: "ajax/ajax.immobilien.print.php",
			data: "id="+id,
			success: function(msg){
				$('#print_preview').html(msg);
			}
		});
		
	}
	
	function closePrintPreview(){
		$('#immobilien').removeClass('print');
		$('#header').css('display','block');
		$('#content').css('display','block');
		$('#seo_keywords').css('display','block');
		$('#print_preview').html('');
	}


/* Contact ************************************************************************************************** */
	function initGMap(){
		if (GBrowserIsCompatible()) {
			var address = 'Futterstraße 18 , 99084 Erfurt (antaris Immobilien GmbH)';
			var point = new GLatLng(50.97924153197689,  11.03415995836258);
			//javascript:void(prompt('',gApplication.getMap().getCenter()));
			var geocoder = new GClientGeocoder();
			geocoder.getLatLng(address, function(point){
				trace(point);
				//point = point;
				//center = point;
			});
			
			var address_str = $('#gmap').html();
			var map = new GMap2(document.getElementById("gmap"));
			map.setCenter(point, 13);
			map.addControl(new GSmallMapControl());
			
			var marker = new GMarker(point);
			map.addOverlay(marker);
			GEvent.addListener(marker, "click", function() {
			  marker.openInfoWindowHtml(address_str);
			 });
			marker.openInfoWindowHtml(address_str);
		  }	
	}


/* Team **************************************************************************************************** */

	function initTeam(){
		var team = $('#team_links a');
		var slogan = $('.team_slogan');
		var details = $('.team_details');
		var active=-1;
		
		details.each(function(i){
			$(this).css({'opacity' : 0, 'display' : 'none'});
			$(slogan[i]).css({'opacity' : 0, 'display' : 'none'});
		});

		team.each(function(i){			
			$(this).bind('mouseenter', function(e){
				for(var k=0;k<team.length;k++){
					$(team[k]).stop();
					if(active==k){
						$(team[k]).animate({opacity:1}, 300);
					}
					else if(k!=i){
						$(team[k]).animate({opacity:0.3}, 300);
					}
					else{
						$(team[k]).animate({opacity:1}, 300);
					}
				}	  	
			});
			
			$(this).bind('mouseleave', function(e){
				if(active===-1){
					for(var k=0;k<team.length;k++){
						$(team[k]).stop();
						$(team[k]).animate({opacity:1}, 500);
					}
				}
				else{
					for(var k=0;k<team.length;k++){
						$(team[k]).stop();
						if(k!=active)$(team[k]).animate({opacity:0.3}, 300);
					}	
				}
			});
			
			$(this).bind('click', function(e){
				if(i!=active){
					
					$(slogan[active]).css({'opacity' : 0, 'display' : 'none'});
					$(team[active]).css({'opacity' : 0.3});
					$(details[active]).css({'opacity' : 0, 'display' : 'none'});
					
					$(slogan[i]).css({'display' : 'block'});
					$(slogan[i]).animate({opacity:0.8}, 500);
					$(details[i]).css({'display' : 'block'});
					$(details[i]).animate({opacity:1}, 500);
	
					active=i;
				}
			});
			
		});
	}
	
	
	
/* Smooth Scroll *********************************************************************************************** */
	function smoothScroll(){
		$(function(){

			$('a[href*=#]').click(function() {
			
			if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') 
				&& location.hostname == this.hostname) {
				
					var $target = $(this.hash);
					
					$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
					
					if ($target.length) {
					
						var targetOffset = $target.offset().top;
						
						$('html,body').animate({scrollTop: targetOffset}, 1000);
						return false;	
					}	
				}
			});
		});
	}


/* Trace *********************************************************************************************** */

	function trace(msg){
		try{
			console.log(msg);
		} catch(e){}	
	}
