var hdl_scrollerTimeout = '';
var int_mouseWheelEvent = 0;
var int_personalPage = 1;
var int_personalPages = 2;

jQuery.noConflict();

// Bind dom functions
jQuery(document).ready(function () {
	// Bind editEase plugin
	jQuery("#login").editease();
	
	// 1. Fade in / Fade out for #persons ul li	
	// Remove normal hover effect for each '#persons ul li' element. 
	jQuery('#persons ul li').each(function() {
		jQuery(this).css('opacity', '0.75');
		jQuery(this).css('-moz-opacity', '0.75');
		jQuery(this).css('filter', 'alpha(opacity=100)');
	});
	
	// Add mouseover event (fade in)
	jQuery('#persons ul li').mouseenter(function() {
			jQuery(this).stop();
			jQuery(this).animate({
				opacity:"1",
				filter:"alpha(opacity=100)"
			}, 250);
	}).mouseleave(function() {
			jQuery(this).stop();
			jQuery(this).animate({
				opacity:"0.75",
				filter:"alpha(opacity=100)"
			}, 1000);
	});
	
	jQuery('#persons p a').mouseenter(function() {
	    jQuery(this).stop();
			jQuery(this).css({
				opacity:"1",
				filter:"alpha(opacity=100)"
			});
  }).mouseleave(function() {
    jQuery(this).stop();
    jQuery(this).css({
      opacity:"0.5",
      filter:"alpha(opacity=100)"
    });
	})
	.click(function() {
	  if (jQuery(this).parent().hasClass("more")) int_personalPage++;
	  else int_personalPage--;
	  
	  if (int_personalPage === 1) jQuery("#persons p.less").hide();
    else jQuery("#persons p.less").show();

    if (int_personalPage === int_personalPages) jQuery("#persons p.more").hide();
    else jQuery("#persons p.more").show();
    
    jQuery("#persons li").hide();
    jQuery("#persons li.page_" + int_personalPage).show();
	});
	
	
	
	if (jQuery("#maincontent div.viewport"))
	{
		// Add the draggable behaviour to main content text element.
		jQuery("#maincontent div.viewport").draggable({ 
			axis: "y",
			cancel:"a",
			start:function(event, ui) {
				jQuery("#maincontent div.scroller").stop();
				jQuery("#maincontent div.scroller").animate({
					opacity:"0.45" /* opacity:"0.25" */
				}, 300);
			},
			drag:function(event, ui) {

				// Set bounding box
				if (ui.position.top < ((jQuery("#maincontent div.viewport").height() - jQuery("#maincontent div.scrollarea").height() + 20) * -1))
				{
					ui.position.top = (jQuery("#maincontent div.viewport").height() - jQuery("#maincontent div.scrollarea").height() + 20) * -1;
				}
				if (ui.position.top > 0)
				{
					ui.position.top = 0;
				}
				
				// Set scrollbar offset
				jQuery("#maincontent div.scroller").css("top", parseInt(jQuery("#maincontent div.scrollarea").height() * ((ui.position.top * -1) / (jQuery("#maincontent div.viewport").height() + 20))));
			},
			stop:function(event, ui) {
				jQuery("#maincontent div.scroller").animate({
					opacity:"0.45" /* opacity:"0" */
				}, 1000);
			}
		});
	
		jQuery("#maincontent div.scroller").draggable({ 
			axis: "y",
			cancel:"a",
			start:function(event, ui) {
				jQuery("#maincontent div.scroller").stop();
				jQuery("#maincontent div.scroller").animate({
					opacity:"0.45" /* opacity:"0.25" */
				}, 300);
			},
			drag:function(event, ui) {
				// Set bounding box
				if (ui.position.top > ((jQuery("#maincontent div.scroller").height() - jQuery("#maincontent div.scrollarea").height())) * -1)
				{
					ui.position.top = (jQuery("#maincontent div.scroller").height() - jQuery("#maincontent div.scrollarea").height()) * -1;
				}
				if (ui.position.top < 0)
				{
					ui.position.top = 0;
				}

				//Set viewport offset
				jQuery("#maincontent div.viewport").css("top", (((jQuery("#maincontent div.viewport").height() + 20) - jQuery("#maincontent div.scrollarea").height()) * (ui.position.top / (jQuery("#maincontent div.scrollarea").height() - jQuery("#maincontent div.scroller").height()))) * -1);
			},
			stop:function(event, ui) {
				jQuery("#maincontent div.scroller").animate({
					opacity:"0.45" /* opacity:"0" */
				}, 1000);
			}
		});
		
		// Set virtuell scrollbar height
		jQuery("#maincontent div.scroller").height(parseInt(jQuery("#maincontent div.scrollarea").height() * (jQuery("#maincontent div.scrollarea").height() / jQuery("#maincontent div.viewport").height())) + "px");  
		
		// Fade out virtuell scrollbar on start
		jQuery("#maincontent div.scroller").animate({
			opacity:"0.45" /* opacity:"0" */
		}, 6000);
		
		jQuery('#maincontent div.viewport').mousewheel(function(event, delta) {
			int_mouseWheelEvent += 1;
			
			if (int_mouseWheelEvent == 1)
			{
				// Fade in virtuell scrollbar on start
				jQuery("#maincontent div.scroller").stop();
				jQuery("#maincontent div.scroller").animate({
						opacity:"0.45" /* opacity:"0.25" */
				}, 300);
			}
			else if (int_mouseWheelEvent > 1)
			{
				window.clearTimeout(hdl_scrollerTimeout);
				hdl_scrollerTimeout = '';
			}
			
			int_scrollWidthMuliplicator = 7;
			
			// Calculate new top position
			int_top = parseInt(jQuery(this).css('top')) + Math.round(int_scrollWidthMuliplicator * delta);
			if (isNaN(int_top)) int_top = 0;
			
			// Consider bounding box
			if (int_top < ((jQuery("#maincontent div.viewport").height() - jQuery("#maincontent div.scrollarea").height() + 20) * -1))
			{
				int_top = (jQuery("#maincontent div.viewport").height() - jQuery("#maincontent div.scrollarea").height() + 20) * -1;
			}
			if (int_top > 0)
			{
				int_top = 0;
			}
			
			// Set new top value
			jQuery(this).css('top', int_top + 'px')
			
			// Set scrollbar offset
			jQuery("#maincontent div.scroller").css("top", parseInt(jQuery("#maincontent div.scrollarea").height() * ((int_top * -1) / (jQuery("#maincontent div.viewport").height() + 20))));
			
			// Fade out scroller
			hdl_scrollerTimeout = window.setTimeout('fadeOutScroller()', 300);
			
			return false;
		});
	}
	
	// Set Interval function to check if a Google maps schould be displayed
	hdl_interval = window.setInterval("checkForMaps()", 500);
});


function fadeOutScroller() {
	if (hdl_scrollerTimeout != '')
	{
		window.clearTimeout(hdl_scrollerTimeout);
		
		// Fade out virtuell scrollbar on start
		jQuery("#maincontent div.scroller").animate({
			opacity:"0.45" /* opacity:"0" */
		}, 3000);
		
		int_mouseWheelEvent = 0;
		hdl_scrollerTimeout = '';
	}
}

// Animate elements
jQuery(window).load(function() {
		/*
	jQuery('#sun').animate({
		opacity:"1",
		filter:"alpha(opacity=100)"	
	}, 1000, animateSun());*/
});

var str_mode = "down";
function animateSun() {
	jQuery('#sun').stop();
	
	if (str_mode == "down")
	{
		str_opacity = "0.75";
		str_filter = "alpha(opacity=75)";
		str_mode = "up";
	}
	else {
		str_opacity = "1";
		str_filter = "alpha(opacity=100)";
		str_mode = "down";
	}
		
	flt_random = Math.random() * 5000;
	
	window.setTimeout(function() {
		jQuery('#sun').animate({
			opacity:str_opacity,
			filer:str_filter	
		}, Math.random() * 8000, animateSun())
	}, parseInt(flt_random));
}

function checkForMaps()
{
	// Integrate Google maps
	//if (document.getElementById("map") && GBrowserIsCompatible() )
	if (document.getElementById("streetmap") )
	{
		// Clear google maps interval check
		window.clearInterval(hdl_interval);
		
		// Create Google Maps view.
		map = new GMap2(document.getElementById("streetmap"));
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl(), new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(6, 6)));
		map.setMapType(G_HYBRID_MAP);
		// map.addControl(new GOverviewMapControl(new GSize(200,160)));
		map.setCenter(obj_mapStartPoint, int_mapStartZoomLevel);
		
		// Create own marker icon.
		markerIcon = new GIcon();
		markerIcon.image = "img/marker.png";
		markerIcon.shadow = "img/marker_shadow.png";
		markerIcon.iconSize = new GSize(19, 16);
		markerIcon.shadowSize = new GSize(19, 16);
		markerIcon.iconAnchor = new GPoint(7, 16);
			
		markerOptions = {icon:markerIcon};
	
		// Set Info Marker to center position and bind 'onclick' function.
		obj_infoMarker = new GMarker(obj_mapStartPoint, markerOptions)
		GEvent.addListener(obj_infoMarker, "click", function() {
				
			// Transfer Adress field HTML into Info-Window.
			map.openInfoWindowHtml(obj_mapStartPoint, jQuery("#popup .adress").html());
		});
		
		// Add marker to map
		map.addOverlay(obj_infoMarker);
		
		// Create pesonal scroller, if needed
		createPersonalScroller();
	}
}

function switchMap(str_pPerson, obj_pMapStartPoint, int_pMapStartZoomLevel) {
	jQuery("#persons li." + str_pPerson +" a").click();
	hdl_interval = window.setInterval("checkForMaps()", 5000);
}


function createPersonalScroller() {
  if (jQuery("#popup div.personal").height() > jQuery("#TB_ajaxContent").height() - jQuery("#popup div.personal").position().top - parseInt(jQuery("#popup div.personal").css("margin-top")))
  {
    jQuery("#popup div.personal").height(jQuery("#TB_ajaxContent").height() - jQuery("#popup div.personal").position().top - parseInt(jQuery("#popup div.personal").css("margin-top")));
    jQuery("#popup div.personal").wrapInner('<div class="scrollarea" />');

    jQuery('<div/>', {
      "class": "scroller",
      style: "top:" + (jQuery("#popup div.personal").position().top + parseInt(jQuery("#popup div.personal").css("margin-top"))) + "px"
    }).insertAfter("#popup div.personal");
    
    
		// Add the draggable
		jQuery("#popup div.scrollarea").draggable({ 
			axis: "y",
			cancel:"a",
			drag:function(event, ui) {

				// Set bounding box
				if (ui.position.top < ((jQuery("#popup div.scrollarea").height() - jQuery("#popup div.personal").height()) * -1))
				{
					ui.position.top = (jQuery("#popup div.scrollarea").height() - jQuery("#popup div.personal").height()) * -1;
				}
				if (ui.position.top > 0)
				{
					ui.position.top = 0;
				}
				
				// Set scrollbar offset
				jQuery("#popup div.scroller").css("top", jQuery("#popup div.scroller").data("startTopOffset") + parseInt(jQuery("#popup div.scrollarea").height() * ((ui.position.top * -1) / (jQuery("#popup div.personal").height()))));
			}
		});
	
		jQuery("#popup div.scroller").draggable({ 
			axis: "y",
			cancel:"a",
			drag:function(event, ui) {
				// Set bounding box
				if (ui.position.top > (jQuery("#popup div.scroller").data("startTopOffset") + ((jQuery("#popup div.scroller").height() - jQuery("#popup div.personal").height())) * -1))
				{
					ui.position.top = jQuery("#popup div.scroller").data("startTopOffset") + ((jQuery("#popup div.scroller").height() - jQuery("#popup div.personal").height()) * -1);
				}
				if (ui.position.top < jQuery("#popup div.scroller").data("startTopOffset"))
				{
					ui.position.top = jQuery("#popup div.scroller").data("startTopOffset");
				}

				//Set viewport offset
				jQuery("#popup div.scrollarea").css("top", jQuery("#popup div.scroller").data("startTopOffset") + (((jQuery("#popup div.scrollarea").height()) - jQuery("#popup div.personal").height()) * (ui.position.top / (jQuery("#popup div.personal").height() - jQuery("#popup div.scroller").height()))) * -1);
			}
		});
		
		// Set virtuell scrollbar height
		jQuery("#popup div.scroller").height(parseInt(jQuery("#popup div.personal").height() * (jQuery("#popup div.personal").height() / jQuery("#popup div.scrollarea").height())) + "px");  
		jQuery("#popup div.scroller").data("startTopOffset", jQuery("#popup div.personal").position().top + parseInt(jQuery("#popup div.personal").css("margin-top")));
		
		jQuery('#popup div.scrollarea').mousewheel(function(event, delta) {
      window.clearTimeout(hdl_scrollerTimeout);
      hdl_scrollerTimeout = '';
    
			int_scrollWidthMuliplicator = 7;
			
			// Calculate new top position
			int_top = parseInt(jQuery(this).css('top')) + Math.round(int_scrollWidthMuliplicator * delta);
			if (isNaN(int_top)) int_top = 0;
			
			// Consider bounding box
			if (int_top < ((jQuery("#popup div.scrollarea").height() - jQuery("#popup div.personal").height()) * -1))
			{
				int_top = (jQuery("#popup div.scrollarea").height() - jQuery("#popup div.personal").height()) * -1;
			}
			if (int_top > 0)
			{
				int_top = 0;
			}
			
			// Set new top value
			jQuery(this).css('top', int_top + 'px')
			
			// Set scrollbar offset
			jQuery("#popup div.scroller").css("top", jQuery("#popup div.scroller").data("startTopOffset") + parseInt(jQuery("#popup div.personal").height() * ((int_top * -1) / (jQuery("#popup div.scrollarea").height() + 20))));
			
			return false;
		});
  }
}

