/* 
	Project Name: Ciriaco Sforza
	Description: Portfolio for Ciriaco Sforza
	Author: Innovatics GmbH
	Author URI: http://www.innovatics.de/
	Version: 1.0
*/

/* Scripts */
document.write('<script type="text/javascript" src="/js/cufon-yui.js"></script>');
document.write('<script type="text/javascript" src="/js/aller.font.js"></script>');
document.write('<script type="text/javascript" src="/js/aller-display.font.js"></script>');
document.write('<script type="text/javascript" src="/js/jquery.validate.min.js"></script>');
document.write('<script type="text/javascript" src="/js/jquery.form.js"></script>');
document.write('<script type="text/javascript" src="/js/jquery.scrollTo-1.4.2-min.js"></script>');
document.write('<script type="text/javascript" src="/js/jalert/jquery.alerts.js"></script>');    

/* Stylesheets */
document.write('<link rel="stylesheet" type="text/css" href="/js/jalert/jquery.alerts.css" />');

/* Functions */
function toggleContent(id) {
	for(i = 1; i < 4; i++)
	{
		if(i == id)
		{
			if($('#toggle' + id).css('display') == 'none')
				$('#toggle' + id).slideDown('slow', function() {
					$.scrollTo($('#toggleGoal' + id), 800, {axis: 'y'});
				});
		}
		else
			$('#toggle' + i).slideUp('slow');
	}
}

/* Document Ready */
$(document).ready(function() {
	// Cufon
	Cufon.replace('.aller', {fontFamily: 'Aller'});
	Cufon.replace('.aller-display', {fontFamily: 'Aller Display'});
	
	// Externe Links automatische in neuem Fenster öffnen
	$('a[href^=http]').click( function() {
		window.open(this.href);
		return false;
	});
	
	// Dropdown Navigation
	$("ul#navigation li").hover(function() {
		$(this).find('div.subnavigation').show();
		$(this).find('a.dropdown').addClass('active');	
	}, function() {
		$(this).find('div.subnavigation').hide();
		
		if(document.URL.search('Interaktiv') != -1 && $(this).attr('class') != 'interaktiv') 
		   	$(this).find('a.dropdown').removeClass('active');
		else if(document.URL.search('Vita') != -1 && $(this).attr('class') != 'vita') 
		   	$(this).find('a.dropdown').removeClass('active');
		else if(document.URL.search('Karriere') != -1 && $(this).attr('class') != 'karriere') 
		   	$(this).find('a.dropdown').removeClass('active');
		else if (document.URL.search('Interaktiv') == -1 && document.URL.search('Vita') == -1 && document.URL.search('Karriere') == -1)
			$(this).find('a.dropdown').removeClass('active');		
	});
	
	// formelements initial leeren
	$('form input[type=text], form textarea').each(function() {
		if ($('form').attr('id') != 'form-edit-news')
		{
			$(this).val('');
		}
		else
		{
			if ($(this).val() != '')
			{
				$(this).parent('p').children('label').hide();
			}
		}
	});
	
	// form input and textarea onfocus/onblur effect
	$('form input[type=text], form textarea').live('focus', function() {
		$('label.overlay[for=' + $(this).attr('id') + ']').hide();
	});
	
	$('form input[type=text], form textarea').live('blur', function() {
		if($(this).val() == '')
			$('label.overlay[for=' + $(this).attr('id') + ']').show();				
	});
	
	// contact form validation
	$("#form-contact").validate({
 		submitHandler: function() {
			// send contact form
			$.post('/pages/send_mail', $('#form-contact').serializeArray(), function(data) {
				if(data != 'success')
				{
					jAlert(data, 'Hinweis', function() {
						if($('#name').val().length == 0)
							$('#name').focus();
						else if($('#email').val().length == 0)
							$('#email').focus();
						else if($('#subject').val().length == 0)
							$('#subject').focus();
						else if($('#message').val().length == 0)
							$('#message').focus();	
					});
				}
				else
					jAlert('Vielen Dank für Ihr Interesse! Ich werde mich so schnell wie möglich mit Ihnen in Verbindung setzen.', 'Hinweis', function() {
						window.location.reload();
					});
			});
		},
		messages: {
			name: 	 "Bitte tragen Sie Ihren Namen ein.",
			email: 	 "Bitte tragen Sie Ihre E-Mail Adresse ein.",
			subject: "Bitte tragen Sie einen Betreff ein.",
			message: "Bitte tragen Sie Ihre Nachricht ein."	
		}
	});
	
	// guestbook form validation
	$('#form-write-entry').validate({
 		submitHandler: function() {
			// send guestbook form
			$.post('/Interaktiv/Gaestebuch/write_entry', $('#form-write-entry').serializeArray(), function(data) {
				if(data != 'success')
				{
					jAlert(data, 'Hinweis', function() {
						if($('#name').val().length == 0)
							$('#name').focus();
						else if($('#subject').val().length == 0)
							$('#subject').focus();
						else if($('#message').val().length == 0)
							$('#message').focus();	
					});
				}
				else
					window.location.reload();
			});
		},
		messages: {
			name: 	 "Bitte tragen Sie Ihren Namen ein.",
			subject: "Bitte tragen Sie einen Betreff ein.",
			message: "Bitte tragen Sie Ihre Nachricht ein."
		}
	});
	
	// accept/decline guestbook entry
	$('img.accept-entry').live('click', function() {
		var accept = $(this);
		$.post('/Interaktiv/Gaestebuch/accept_entry', {entry_id: accept.attr('id')}, function(data) {
			if(data == 'success')
			{
				accept.parents('.options').hide();
				accept.parents('.guestbook-top').removeClass('red-top');
				$('#accept-entry-' + accept.attr('id')).show();
			}
			else
				jAlert(data, 'Achtung!');	
		});
	});
	
	$('img.decline-entry').live('click', function() {
		var decline = $(this);
		$.post('/Interaktiv/Gaestebuch/decline_entry', {entry_id: decline.attr('id')}, function(data) {
			if(data == 'success')
				decline.parents('.guestbook').replaceWith('<div class="replaced w583 mb45">Eintrag wurde erfolgreich abgelehnt und entfernt.</div>');
			else
				jAlert(data, 'Achtung!');
		});	
	});
	
	// write news
	if($('#form-write-news').is('form'))
	{
		var validator = $('#form-write-news').validate({
			messages: {
				title:  "Bitte tragen Sie einen Titel ein.",
				author: "Bitte tragen Sie einen Autor ein.",
				body:   "Bitte tragen Sie einen Inhalt ein."
			}
	  	});
  	}
	
	$('#form-write-news').ajaxForm({
		beforeSubmit: function() {
			validator.form();
			  
			if(validator.numberOfInvalids() > 0)
		  	{
				validator.showErrors();
		  		return false;
  			}
		},
		success: function(data) {
			if(data != 'success')
			{
				jAlert(data, 'Hinweis', function() {
					if($('#title').val().length == 0)
						$('#title').focus();
					if($('#author').val().length == 0)
						$('#author').focus();
					else if($('#body').val().length == 0)
						$('#body').focus();
				});
			}
			else
				window.location.reload();
		}
	});

	// edit news
	if($('#form-edit-news').is('form'))
	{
		var editValidator = $('#form-edit-news').validate({
			messages: {
				'edit-title':  "Bitte tragen Sie einen Titel ein.",
				'edit-author': "Bitte tragen Sie einen Autor ein.",
				'edit-body':   "Bitte tragen Sie einen Inhalt ein."
			}
	  	});
  	}

	$('#form-edit-news').ajaxForm({
		beforeSubmit: function() {
			editValidator.form();

			if(editValidator.numberOfInvalids() > 0)
		  	{
				editValidator.showErrors();
		  		return false;
  			}
		},
		success: function(data) {
			if(data != 'success')
			{
				jAlert(data, 'Hinweis', function() {
					if($('#edit-title').val().length == 0)
						$('#edit-title').focus();
					if($('#edit-author').val().length == 0)
						$('#edit-author').focus();
					else if($('#edit-body').val().length == 0)
						$('#edit-body').focus();
				});
			}
			else
				window.location.reload();
		}
	});
  	
  	// delete news
  	$('span.delete-news').live('click', function() {
		var deleteNews = $(this);
		$.post('/News/delete', {news_id: deleteNews.attr('id')}, function(data) {
			if(data == 'success')
				deleteNews.parents('.news-container').replaceWith('<div class="replaced w583 mb45">News wurde erfolgreich entfernt.</div>');
			else
				jAlert(data, 'Achtung!');
		});	
	});
});
