/**
 * Main Function File for FGOLLE Website
 * @author Fabian Golle <f.golle@micronax.de>
 * @copyright Fabian Golle <f.golle@micronax.de>
 * @version 1.0
 */

window.onload = function(){
	slideshow = new slideshow;
	slideshow.initTimer();
	
	newsbar = new newsbar;
	newsbar.initTimer();
}

$.getScript('/internal/preloader', function() {
  //alert('Load was performed.');
});

function GetRandom( min, max ) {
	if( min > max ) {
		return( -1 );
	}
	if( min == max ) {
		return( min );
	}
	return( min + parseInt( Math.random() * ( max-min+1 ) ) );
}

// Submit Form Handler
function submitComment(id) {
	$("#loader").fadeIn("fast", function() {
		var rand = GetRandom(1,100);
		$.post("/blog/processcomment", {
			id: id,
			action: "add",
			name: $("#name").val(),
			website: $("#website").val(),
			email: $("#email").val(),
			password: $("#password").val(),
			text: $("#text").val(),
			address: $("#address").val(),
			rand: rand
		}, function(data){
			if (data == "1") {
				$("#message").html("Der von Ihnen eingegeben Text &uuml;berschreitet die maximale L&auml;nge von 8000 Zeichen");
				$("#message").fadeIn();
				$("#loader").fadeOut();
			} else if (data == "2") {
					$("#message").html("Bitte f&uuml;llen Sie alle Pflichtfelder aus");
					$("#message").fadeIn();
					$("#loader").fadeOut();
			} else {
				$("#comments").prepend(data);
				$("#commentform").slideUp("fast", function(){
					$("#comment_" + rand).fadeIn("slow");
					$("#name").val("");
					$("#website").val("");
					$("#text").val("");
					$("#email").val("");
					$("#loader").fadeOut();
				})
			}
		});
	});
}

function submitContactform(id) {
	$("#loader").fadeIn("fast", function() {
		var rand = GetRandom(1,100);
		$.post("/contact/process", {
			action: "send",
			name: $("#name").val(),
			email: $("#email").val(),
			password: $("#password").val(),
			text: $("#text").val(),
			address: $("#address").val(),
			rand: rand
		}, function(data){
			if (data == "1") {
				$("#message").html("Bitte f&uuml;llen Sie alle Pflichtfelder aus");
				$("#message").fadeIn();
				$("#loader").fadeOut();
			} else if (data == "2") {
				$("#message").html("Die eingegebene Email-Adresse scheint nicht g&uuml;ltig zu sein..");
				$("#message").fadeIn();
				$("#loader").fadeOut();
			} else {
				$("#messagea").html("Vielen Dank f&uuml;r Ihre Nachricht. Ich werde mich schnellstm&ouml;glich darum k&uuml;mmern! :)");
				$("#message").fadeOut();
				$("#messagea").fadeIn();
				$("#comment_" + rand).fadeIn("slow");
				$("#name").val("");
				$("#text").val("");
				$("#email").val("");
				$("#loader").fadeOut();
			}
		});
	});
}

function deleteComment(id) {
	if (confirm("M&ouml;chten Sie diesen Eintrag wirklich l&ouml;schen?")) {
		$.post("/blog/processcomment", {
			id: id,
			action: "delete"
		}, function(data){
			if (data == "1") {
				$("#comment_" + id).slideUp("slow", function(){
					$("#comment_" + id).remove();
				})
			}
			else alert(data);
		});
	}
}

/**
 * Slideshow on Front-Template
 */
function slideshow(){
	this.timer;
	this.initTimer = function() {
		timer = setTimeout("slideshow.slideshow_start()", 8000);
	};
	
	this.slideshow_start = function() {
		$('#ss_innercontent').fadeOut('fast', this.loadNewSlideshow);
	};
	
	this.loadNewSlideshow = function() {
		$('#ss_innercontent').load("/slideshow.php", '', slideshow.updateSlideshow)
	};
	
	this.updateSlideshow = function() {
		$('#ss_innercontent').fadeIn('normal', function(){
			timer = setTimeout("slideshow.slideshow_start()", 8000);
		});
	};
}

/**
 * Slideshow on Front-Template
 */
function newsbar(){
	this.timer;
	this.initTimer = function() {
		timer = setTimeout("newsbar.startUpdate()", 6000);
	};
	
	this.startUpdate = function() {
		$('#newsbar').fadeOut('fast', this.loadNewNewsItem);
	};
	
	this.loadNewNewsItem = function() {
		$('#newsbar').load("/internal/newsbar", '', newsbar.updateNewsBar)
	};
	
	this.updateNewsBar = function() {
		$('#newsbar').fadeIn('normal', function(){
			timer = setTimeout("newsbar.startUpdate()", 6000);
		});
	};
}

/**
 * Dropdownmenu JS
 */
$(function(){
    $('#hm_center ul li ul').each(function(){
        $(this).parent().eq(0).hoverIntent({
            timeout: 600,
			sensitivity:200,
            over: function(){
				/*$(this).animate({ 
			        fontWeight: "14px"
			    }, "fast");
				$('#hm_center ul li ul:eq(0)').css("font-size", "12px");*/
				//$('#hm_center ul li ul:eq(0)').fadeIn();
				
				$(this).children().fadeIn();
            },
            out: function(){
				/*$(this).animate({ 
			        fontSize: "12px"
			    }, "fast");*/
            	//$('#hm_center ul li ul:eq(0)').fadeOut(200);
				$(this).children().eq(1).fadeOut();
            }
        });
    });    
	$('#loginpanel').hoverIntent({
        timeout: 600,
		sensitivity:200,
        over: function(){
			$('#loginpanel').animate({ 
		        height: "39px"
		    },'slow' , "", function(){$('#loginpanel *').fadeIn()});
        },
        out: function(){
			$('#loginpanel').animate({ 
		        height: "14px"
		    },'slow' , "", function(){$('#loginpanel *').fadeOut()});
        	$('#loginpanel *').fadeOut(200);
        }
    });   
})