$(document).ready(function() {
	init();
	hover();
	printLink();
});

var interval;
var profiles;
var position = 0;

function hover() {
	if (! document.getElementById('startseite') ) return;
	
	$('.teaserBox').each(function(i) {
		$(this).click(function() {
			window.location.href = $(this).find('h2 > a').attr('href');
		});
		
		$(this).hover(
			function() {
				$(this).addClass('over');
			},
			
			function() {
				$(this).removeClass('over');
			}
		);
	});
}

function printLink() {
	if (! document.getElementById('printThis') ) return;
	$('#printThis').replaceWith('<a href="#" class="printLink" onclick="window.print(); return false;">Drucken</a>');
}

function init () {
	var searchterm = 'Volltextsuche'
	$('#searchterm').val(searchterm);
	$('#searchterm').focus(function(){
		if( $(this).val() == searchterm ) $(this).val('');
	});
	$('#searchterm').blur(function(){
		if( $(this).val() == '' ) $(this).val(searchterm);
	});
	
	if (! document.getElementById('headerimageContent') ) return;
	profiles = createProfiles();
	
	insertProfiles(profiles);
	playProfiles(profiles);
	
	$('#stopanimation').click(stopPlaying);
	$('#startanimation').click(startPlaying);
}

// a profile Class
function Profile (identifier, image, alttext, next, heading, text, cite) {
	this.identifier = identifier;
	this.image = image;
	this.alttext = alttext;
	this.next = next;
	this.heading = heading;
	this.text = text;
	this.cite = cite;
	
	this.nextProfile = function() {
		var thisProf = this.next;
		var nextProf = this.next + 1;
		
		$('#' + this.identifier + 'link').removeClass('active');
		$('#profiltext').fadeOut('slow');
		$('#' + this.identifier).fadeOut('slow', function(){
			$('#profiltext h2').html(profiles[thisProf].heading);
			$('#profiltext blockquote p').html(profiles[thisProf].text);
			$('#profiltext cite').html(profiles[thisProf].cite);
			$('#profile' + nextProf).fadeIn('slow');
			$('#profiltext').fadeIn('slow');
			$('#profile' + nextProf + 'link').addClass('active');
		});
	};
}

function createProfiles() {
	var p1 = new Profile('profile1', 'img/woman01.jpg', 'Rosveisa Ramirez-Steiner', 1, 'Zu ruhig im Büro', '&bdquo;Sechs Stunden alleine <br/> werden es heute sicher noch werden.&rdquo;', 'Rosveisa Ramirez-Steiner, Unternehmensberaterin');
	var p2 = new Profile('profile2', 'img/man01.jpg', 'Benjamin Kuplinger', 2, 'Urlaubsvertretung<br/>gesucht', '&bdquo;3 Wochen Segeltörn! Und trotzdem <br /> durchgehende Betreuung für meine Kunden.&rdquo;', 'Benjamin Kuplinger, Systemprogrammierer');
	var p3 = new Profile('profile3', 'img/woman02.jpg', 'Kathrin Melzinger', 3, 'Service zum Quadrat', '&bdquo;One Stop Shop: Ein breites<br/>Angebotsportfolio aus einer Hand!&rdquo;', 'Kathrin Melzinger, Spezialistin für Arbeitszeitgestaltung');
	var p4 = new Profile('profile4', 'img/woman04.jpg', 'Bridget Okowani', 4, 'Stark am Markt', '&bdquo;Gemeinsame KundInnenevents mit<br/>PartnerInnen bringen Spaß und Aufträge!&rdquo;', 'Bridget Okowani,<br/>Webdesignerin');
	var p5 = new Profile('profile5', 'img/man02.jpg', 'Lorenz Zeilinger', 5, 'Interessante<br/>Ausschreibung!', '&bdquo;...aber leider nur ein Teil davon!<br/> Schade! oder Kooperation?&rdquo;', 'Lorenz Zeilinger, Anwendungsentwickler');
	var p6 = new Profile('profile6', 'img/woman03.jpg', 'Susanne Kimberger', 6, 'Spezialist/in gesucht', '&bdquo;Man sollte sich auf die eigenen Stärken <br/> konzentrieren!&rdquo;', 'Susanne Kimberger, Systemadministratorin');
	var p7 = new Profile('profile7', 'img/man04.jpg', 'Kenan Yildirim', 7, 'Riesenauftrag<br/>was nun?', '&bdquo;Zusammen mit meinem Partner<br/>schaff&rsquo; ich das!&rdquo;', 'Kenan Yildirim, Unternehmensberater');
	var p8 = new Profile('profile8', 'img/man03.jpg', 'Boris Rakovic', 0, 'Stress lass nach!', '&bdquo;Der eine Großauftrag erledigt,<br/>zwei weitere stehen ins Haus...&rdquo;', 'Boris Rakovic, IT Consultant');
	
	return new Array(p1, p2, p3, p4, p5, p6, p7, p8);
}

function insertProfiles (profiles) {
	for (var i in profiles) {
		if ( profiles[i].identifier == 'profile1' ) continue;
		$('#headerimageContent li:last')
			.clone()
			.appendTo('#headerimageContent')
			.attr('id', profiles[i].identifier)
			.css('display', 'none')
			.children('img').attr({'src': profiles[i].image, 'alt': profiles[i].alttext})
		;
	}
}

function playProfiles(profiles) {
	var currentProfile = profiles[position];
	
	interval = setInterval(function(){
		currentProfile.nextProfile();
		position = currentProfile.next;
		currentProfile = profiles[position];
	}, 4000);
}

function stopPlaying(event) {
	event.preventDefault();
	clearInterval(interval);
	$('#startanimation').removeClass('none');
	$('#stopanimation').addClass('none');
}

function startPlaying(event) {
	event.preventDefault();
	playProfiles(profiles);
	$('#startanimation').addClass('none');
	$('#stopanimation').removeClass('none');
}