var pid;
var objAjax;
var options = {};
var handleHelper = false;
var scrollbar = false;



function gotoPage(id)
{
	pid = id;
	$("#content").fadeOut(800, callback);  
}

function formSubmit(id)
{
	if(checkForm(id))
	{
		var zeit = new Date();
		var ms = zeit.getMilliseconds();
		
		var dataArr = "page="+document.forms[id].elements["page"].value;
		var counter = document.forms[id].elements.length;
		
		for(var i=0; i<counter; i++)
		{
			if(document.forms[id].elements[i].type != "hidden")
			{
				dataArr += "&"+document.forms[id].elements[i].name+"="+document.forms[id].elements[i].value
			}
		}
		objAjax = $.ajax({url: '/scripts/ajax/submit.php?time='+ms, 
        async: false, dataType: 'html', 
		data: dataArr,
		type: "POST",
        success: function(html) {             
			gotoPage(html)
        }, error: function(http, message, exc) { 
            cont = "hihi"
			$("#content").html(cont);
		}});	
		
	}
	else
	{
		return false;
	}
}


function callback()
{
	var zeit = new Date();
	var ms = zeit.getMilliseconds();

	objAjax = $.ajax({url: '/scripts/ajax/goto.php?page='+pid+'&time='+ms, 
        async: false, dataType: 'html', 
        success: function(html) { 
            cont = html; 
	    $("#content").html(cont);	   
	    $("#content").fadeIn(800, showCallback);
        }, error: function(http, message, exc) { 
            cont = "hihi"
			$("#content").html(cont);
    }});
}

function showCallback()
{
	iniSlider();
	getClicks();
}
function getClicks()
{
	$("a").each(function() {
		if($(this).attr("rel") != "")
		{		
			$(this).unbind("click");			
			$(this).click(function () {
				gotoPage($(this).attr("rel"));
			});
		}
		if($(this).attr("rev") != "")
		{	
			var clickVal = $(this).attr("rev");
			clickVal = clickVal.split(/#/);
			$(this).click(function () {
				setTimeout(clickVal[0]+"('"+clickVal[1]+"')", 0);
			});							
		}
	});
}

$(document).ready(function() {
	getClicks();
	iniSlider();
	$("#konniBild").click(function () {
	$("#wrapper2").css("display", "none");
	});
});


function iniSlider()
{				
		if(scrollbar)
			scrollbar.slider("destroy");
		
		if(handleHelper)			
			handleHelper.remove();				
		
		

		//scrollpane parts
		var scrollPane = $(document).find('.slideBox');
		var scrollContent = $(document).find('.mainContent');
		
		if( scrollContent.height() < scrollPane.height())
		{
			$(".scrollbarWrap").css("display", "none");
		}
		else
		{
			$(".scrollbarWrap").css("display", "block");
		}
		
		//build slider
		scrollbar = $(document).find(".scrollbar").slider({
			slide:function(e, ui){
				if( scrollContent.height() > scrollPane.height() ){ scrollContent.css('bottom', Math.round( ui.value / 100 * (  scrollPane.height()- scrollContent.height() )) + 'px'); 	
					}
				else { scrollContent.css('bottom', 0); }
			}
		, orientation: 'vertical', min: -100, max: 0});
		
		//append icon to handle
		
		handleHelper = scrollbar.find('.ui-slider-handle')		
			
			.append('<span class="ui-icon ui-icon-grip-dotted-vertical"></span>')
			.wrap('<div class="ui-handle-helper-parent"></div>').parent();			
		
		//change overflow to hidden now that slider handles the scrolling
		scrollPane.css('overflow','hidden');		
				
		//reset slider value based on scroll content position
		function resetValue(){
			var remainder = scrollContent.height() - scrollPane.height();
			var leftVal = scrollContent.css('bottom') == 'auto' ? 0 : parseInt(scrollContent.css('bottom'));
			var percentage = Math.round(remainder / leftVal * 100);
			scrollbar.slider("value", (percentage/100));
		}
		//if the slider is 100% and window gets larger, reveal content
		function reflowContent(){
				var showing = scrollContent.height() + parseInt( scrollContent.css('bottom') );
				var gap = scrollPane.height() - showing;
				if(gap > 0){
					scrollContent.css('bottom', parseInt( scrollContent.css('bottom') ) + gap);
				}
		}
		
		//change handle position on window resize
		$(window)
		.resize(function(){
				resetValue();				
		});		
		
		
}
