$(document).ready(function () {
							
	$('.onpageopt_out').hover(
		function(){$(this).stop().animate({opacity : 0.5})},
		function(){$(this).stop().animate({opacity : 0.3})}
	);
	
	$('li','#clients').bind(function(){
							 $(this).animate(function(){
													  $(this).addId('act');
													  });
							 
							 });
	
	
	slideMember = function(num, li)
	{
		//console.log(num, 'slideMember');
		$('#member-full')
			.stop()
			.animate({
				marginLeft: '-' + (num * 920) + 'px'
			}, 1000);
		
		$('#member-switch .indic').remove();
		$('#member-switch').removeClass();
		// aktiv Klassen löschen
		$('#team li').removeClass('active');
		// die jeweilige "active" setzen
		li.addClass('active');
		
		//console.log($('#' + actT).position());
				
		var idx_start = li.position().left,
			idx_end = li.position().left,
			pos_start = li.position().left,
			el = '<div class="indic active"></div>',
			range = Math.abs((idx_start - idx_end));
		
		// append one indic per item
		$(el)
			.appendTo('#member-switch')
			.css({
				left: pos_start - 10
			});
			
		// == range
		if (range > 0) {
			$('.indic')
				.animate({
					width: '+=' + (97 +  range * 1.25) * range
				});
		}

	};

	$('#team li').click(function() {
		document.location.hash = '#' + $(this).attr('id').replace('member-', '');
		slideMember($(this).attr('num'), $(this));
	});
	
	var url = document.location.href,
		anchor = url.indexOf('#') + 1;
	
	if (anchor > 0)
	{
		$('#member-' + url.substr(anchor)).trigger('click');
	}
	else if ($('ul#team').length > 0)
	{
		var li = $('ul#team li:first');
		$('<div class="indic active" style="left: ' + (li.position().left - 10) + 'px"></div>').appendTo('#member-switch');
	}
	$('#wochenMember li').click(function() {
		slideMember($(this).attr('num'), $('#member-' + $(this).attr('list_id')));
	});
							
	/*
	**RECENT-WORKS
	*/
	$('#work-full').cycle({
        timeout:  30,
        pager:   '#workNav',
        pagerAnchorBuilder: pagerFactory
    });
	$('#work-full li').css('visibility','visible');
	
	function pagerFactory(idx, slide) {
        var s = '';
        return '<li'+s+' onClick="$(\'#work-full\').cycle(\'pause\');"><a href="#">'+(idx+1)+'</a></li>';
    };
	
	$('#wochenMember').cycle({
        timeout: 12000
    });
	$('#wochenMember li').css('visibility', 'visible');
	
	$.ajaxSetup({ type: 'post' });
	/*
	** MENU
	*/
	var menu = {
		objs		: $('ul.menu a'),
		opac		: 1,
		speed		: 150,
		out_faster	: 0.75
	};
	$.each(menu.objs, function() {
		var html = '<span class="hover"></span>' + $(this).html();
		$(this).addClass($(this).html());
		if ($(this).attr('title'))
		html += '<span class="caption">' + $(this).attr('title') + '</span>';
		$(this).html(html);
		
		
		
	});
	
	menu.objs.find('span').fadeTo(10, 0);
	menu.objs.hover(function () {
		$(this).find('span')
			.stop(true, true)
			.fadeTo(menu.speed, menu.opac);
		if($(this).attr('class') == 'Leistungen'){
			$('#submenu').fadeIn();
		}
		
	}, function () {
		$(this).find('span')
			.stop(true, true)
			.fadeTo(menu.speed * menu.out_faster, 0);  
		if($(this).attr('class') == 'Leistungen'){
			$('#submenu').fadeOut();
		}
	});
	
	$('#submenu').hover(function () {
		$('#submenu').stop(true,true).show();
	}, function () {
		$('#submenu').fadeOut();
	});
	
	
	/*
	** PORTFOLIO CIRCLES
	*/
	
	$('#kreise .inner').hide();
	
	
	$('#kreise .item').hover(function () {
		$(this).addClass('hov').find('.inner').stop(true, true).fadeIn();
	}, function () {
		$(this).removeClass('hov').find('.inner').stop(true, true).fadeOut();
	});
	
	/*
	** PARTNER
	*/
	var partner = {
		el			: $('#partner a'),
		opac		: 0.5,
		speed		: 200
		
	};

	partner.el.fadeTo(10, partner.opac);
	partner.el.hover(function () {
		$(this)
			.stop(true, true)
			.fadeTo(partner.speed, 1)
			.addClass($(this).attr('mc')+'_4c');
	}, function () {
		$(this)
			.stop(true, true)
			.fadeTo(partner.speed, partner.opac)
			.removeClass()
			.addClass($(this).attr('mc'));
			
	});
	
	
	/*
	** TOOLS
	*/
	var tools = {
		el			: $('#tools a'),
		opac		: 0.5,
		speed		: 200
		
	};

	tools.el.fadeTo(10, tools.opac);
	tools.el.hover(function () {
		$(this)
			.stop(true, true)
			.fadeTo(partner.speed, 1)
			.addClass($(this).attr('mc')+'_4c');
	}, function () {
		$(this)
			.stop(true, true)
			.fadeTo(tools.speed, tools.opac)
			.removeClass()
			.addClass($(this).attr('mc'));
			
	});
	
	
	/*
	** FOOT MORE
	*/
	// nur anwenden wenn > als IE7
	//
	if ($.browser.msie && $.browser.version < 8) {
		//alert($.browser.version);
	}
	else {
		var footMore = {
			handler			: $('#foot-more .foot-more-top'),
			obj				: $('#foot-more'),
			close_h			: 45,
			open_h			: 500,
			speed			: 750,
			close_faster	: 0.75, // > 1 closes slower
			ease			: 'easeOutSine'
		};
		function openFooter(footMore) {
			_gaq.push(['_trackEvent', 'interaktion', 'click', 'sie wollen mehr oeffnen']);
			footMore.obj.animate({
				height: footMore.open_h
			}, footMore.speed, footMore.ease);
			$('html, body').animate({
				scrollTop: $('html').scrollTop() + footMore.open_h
			}, footMore.speed, footMore.ease);
		}
		
		function closeFooter(footMore) {
			_gaq.push(['_trackEvent', 'interaktion', 'click', 'sie wollen mehr schliessen']);
			footMore.obj.animate({
				height: footMore.close_h
			}, footMore.speed * footMore.close_faster, footMore.ease);
			$('html, body').animate({
				scrollTop: $('html').scrollTop() + footMore.close_h
			}, footMore.speed * footMore.close_faster, footMore.ease);
		}
		
		footMore.obj.css({
			height: footMore.close_h,
			overflow: 'hidden'
		});
		
		footMore.handler.toggle(function () {
			openFooter(footMore);
		}, function () {
			closeFooter(footMore);
		});
	
	}
	
	/*
	** FORM CLEARING
	*/
	var forms = {
		//els : $('#foot-more form, #comment-form, #c2a form, .page-template-template-jobs-php form, .page-template-template-contact-php form')
		els: $('form').not('#formsearch')
	};
	forms.els.find('input, textarea').each(function () {
		if ($(this).is('input') && $(this).attr('type') === 'text' && $(this).val() !== '') {
			$(this).prev('label').hide();
		}
		if ($(this).is('textarea') && $(this).val() !== '') {
			$(this).prev('label').hide();
		}
	});
	
	forms.els.find('input, textarea').focus(function () {
		$(this).prev('label').hide();
		$(this).blur(function () {
			if ($(this).val() === '') {
				$(this).prev('label').show();
			}
		});
	});
	
	forms.els.find('label').focus(function () {
		$(this).next('input').blur(function () {
			if ($(this).val() === '') {
				$(this).prev('label').show();
			}
		});
	});
	
	
	
	/*
	** MAP
	*/
	
	function initialize() {
		var myLatlng = new google.maps.LatLng(52.52877, 13.41602);
		var myOptions = {
			zoom: 11,
			center: myLatlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		var map = new google.maps.Map(document.getElementById("map"), myOptions);
		new google.maps.Marker({
			position: myLatlng, 
			map: map,
			title: 'OVAN Marketing GmbH',
			icon: template_root + '/img/ico-marker.png'
		});
	}
	
	/*
	** CATS
	*/
	var cats = {
		el			: $('#categories, #monthly').find('a'),
		speed		: 200,
		padd		: 5
	};
	cats.el.hover(function () {
		$(this).stop(true, true).animate({
			paddingLeft:	'+=' + cats.padd
		}, cats.speed);
	}, function () {
		$(this).stop(true, true).animate({
			paddingLeft:	'-=' + cats.padd
		}, cats.speed);

	});
	
	
	/*
	** TEAM
	*/
	/*if ($.browser.msie && $.browser.version < 8) {
		
		$('#team li').each(function () {
			var text = $(this).text().toLowerCase();
			var text_neu = text.substr(0, text.length -1)
			$(this).attr('id',text_neu);
		});
	}
	else {
		$('#team li').each(function () {
			$(this).attr('id', $(this).text());
		});
	}*/
	$('#resorts li').click(function (e) {
		
		var matchClass = $(this).text().toLowerCase(),
			matched = [],
			matched = $('#team li.active'),
			teamItems = $('#team li'),
			activeIndexes = [],
			first = -1;

		// reset navigation
		$('#resorts li').removeClass('act');
		// addclass navigation
		$(this).addClass('act');

		
		// alle indics löschen
		$('#member-switch .indic').remove();
		// aktiv Klassen löschen
		$('#team li').removeClass('active');
		// die jeweilige "active" setzen
		$('#team li.'+matchClass).addClass('active');
		$('#member-switch').attr('class', matchClass);
		
		// build objs
		for (var i = 0; i < teamItems.length; i++) {
			if (first < 0 && teamItems.eq(i).hasClass('active')) {
				first = i;
			} else if (first >= 0 && !teamItems.eq(i).hasClass('active')) {
				activeIndexes.push({first:first, last:i-1});
				first = -1;
			}
			if (i == teamItems.length - 1 && first >= 0) {
				activeIndexes.push({first:first, last:i});
				first = -1;
			}
		}
		
		
		for (var i = 0; i < activeIndexes.length; i++) {
			
			var idx_start = activeIndexes[i].first,
				idx_end = activeIndexes[i].last,
				pos_start = $('#team li').eq(idx_start).position().left,
				el = '<div class="indic"></div>',
				range = Math.abs((idx_start - idx_end));
			
			// append one indic per item
			$(el)
				.appendTo('#member-switch')
				.css({
					left: pos_start - 10
				});
				
			// == range
			if (range > 0) {

				$('.indic')
					.eq(i)
					.animate({
						width: '+=' + (95 +  range * 1.25) * range
					});

			}
		}
		
		
		
		
	});
	

	
	/*
	** LEISTUNGEN
	*/
	// SEO
	
	// WORKING

	var els = $('#infografik').find('div'),
		els_arr = [];
	
	els.hide().each(function () {
		
		els_arr.push($(this));
	});
	
	/*
	** ANCHOR JUMPING
	*/
	function jump(ev) {
		var conf = {
			speed	: 1500,
			ease	: 'easeOutQuad'
		};
		//prevent the "normal" behaviour which would be a "hard" jump
		
		//Get the target
		var target = $(this).attr('href');
		if (target.substr(0, 1) == '#') {
			ev.preventDefault();
			//perform animated scrolling
			$('html, body').animate({
				//get top-position of target-element and set it as scroll target
				scrollTop: parseInt($(target).offset().top, 10)
				//scrolldelay: 2 seconds
			}, conf.speed, conf.ease, function () {
				//attach the hash (#jumptarget) to the pageurl
				location.hash = target;
			});
		}
	}
	// BIND
	$('a[href*=#]').bind('click', jump);
	//return false;
	
	/*
	 * CONTACT FORM
	 */
	
	

	
	
	$('#contactform').validate({
		rules: {
			contact_name: {
				required: true
			},
			contact_email: {
				required: true,
				email: true
			},
			contact_message: {
				required: true
			}
		},
		messages: {
			contact_name: 'Bitte geben Sie einen Namen ein',
			contact_email: {
				required: 'Bitte geben Sie Ihre E-Mail-Adresse ein',
				email: 'Die E-Mail muss das Format name@domain.com haben'
			},
			contact_message: 'Bitte geben Sie eine Nachricht ein'
		},
		submitHandler: function(form) {
			
			var checkArray = new Array();
			$("input[name='reason[]']:checked").each(function(i) {
					//console.log($(this).val());
					checkArray.push($(this).val());
			});
			//console.log(checkArray);return;
			$.ajax({
				url: template_root + '/ajax.php',
				data: {
					type: 'submitContactForm',
					contact_name: $('#contact_name').val(),
					contact_email: $('#contact_email').val(),
					contact_phone: $('#contact_phone').val(),
					contact_website: $('#contact_website').val(),
					contact_message: $('#contact_message').val(),
					contact_reason: checkArray.join(',')
				},
				success: function(r) {
					if (r == 'true') {
						resetForm('#contact_form');
						tb_remove();
						alertWindow("<div id='right'><p>Mail wurde versandt!</p></div", 250, 100);
					}
					else {
						tb_remove();
						alertWindow("<div id='fail'><p>Ein Fehler ist aufgetreten!</p></div>", 280, 100);
					}
				}
			});
		} 
	});
	
	/*
	 * FOOTER CONTACT FORM
	 * http://docs.jquery.com/Plugins/Validation/validate#options	
	 */
	$('#footer_contact_form').validate({
		rules: {
			footer_contact_name: {
				required: true
			},
			footer_contact_email: {
				required: true,
				email: true
			},
			footer_contact_message: {
				required: true
			}
		},
		messages: {
			footer_contact_name: 'Bitte geben Sie einen Namen ein',
			footer_contact_email: {
				required: 'Bitte geben Sie Ihre E-Mail-Adresse ein',
				email: 'Die E-Mail muss das Format name@domain.com haben'
			},
			footer_contact_message: 'Bitte geben Sie eine Nachricht ein'
		},
		submitHandler: function(form) {
			$.ajax({
				url: template_root + '/ajax.php',
				data: {
					type: 'submitFooterContactForm',
					contact_name: $('#footer_contact_name').val(),
					contact_email: $('#footer_contact_email').val(),
					contact_message: $('#footer_contact_message').val()
				},
				success: function(r) {
					if (r == 'true')
						alert('Ihre Nachricht wurde erfolgreich verschickt');						
					else
						alert('Es ist ein Fehler aufgetreten.');
				}
			});
		} 
	});
	
	/*
	 * JOBS UPLOAD FORM
	 * 	 
	 */
	
	// class definition - http://www.phpied.com/3-ways-to-define-a-javascript-class/ 
	function UploadedFile(params)
	{
		this.uid = params.uid;
		this.tmp_name = params.tmp_name;
		this.name = params.name;
	};
	
	var jobs_uploaded_files = [];
	
	function formatSize(size)
	{
		var units = ['Byte', 'Kilobyte', 'Megabyte'];
	
		for (var i = 0; size > 1024; i++)
		{
			size = size / 1024;
		}
		
		return size.toFixed(2).replace('.', ',') + ' ' + units[i];
	}

	$('#jobsuploadform').submit(function() {
	    
		$('#jobsuploadform .container').addClass('loading');
	    
		var frameName = "upload" + new Date().getTime(),
			uploadIFrame = $('<iframe style="display:none" name="' + frameName + '"></iframe>');

		uploadIFrame.load(function()
		{			
			var o = $.evalJSON($(this).contents().find('body').html());
			
			setTimeout(function() {								

				$('#jobsuploadform .container').removeClass('loading');
				uploadIFrame.remove();
				for (var i = 0, l = o.length; i < l; i++)
				{
					if (o[i].error == 0 && o[i].size > 0) {
						jobs_uploaded_files.push(new UploadedFile({
							uid: o[i].uid,
							name: o[i].name + '.' + o[i].extension,
						 	tmp_name: o[i].tmp_name
						}));
					 	$('<div title="' + o[i].name + '" uid="' + o[i].uid + '" class="uploaded_file' + o[i].className + '"><b>' + o[i].name + '</b><br /><code>' + o[i].extension.toUpperCase() + '-Datei</code><br />' + formatSize(o[i].size) + '</div>').appendTo('#jobs_uploaded_files').fadeIn();
					 }
				}				
				
				
			}, 500);
		});
		
		$('body:first').append(uploadIFrame);
		$(this).attr('target', frameName);
	});
	
	$('.uploaded_file').live('click', function() {
		
		var uid = $(this).attr('uid'),
			tmp = jobs_uploaded_files;
		
		jobs_uploaded_files = [];
		for (var i = 0, l = tmp.length; i < l; i++)
		{
			if (tmp[i].uid != uid)
				jobs_uploaded_files.push(tmp[i]);
		}
		
		$(this).fadeOut();
	});

	
	function alertWindow(msg, width, height)
	{
    if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
  			$("body","html").css({height: "100%", width: "100%"});
  			$("html").css("overflow","hidden");
  			if (document.getElementById("TB_HideSelect") === null) {
  				$("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div><div id='TB_window'></div>");
  				$("#TB_overlay").click(tb_remove);
  			}
		}else{
			if(document.getElementById("TB_overlay") === null){
				$("body").append("<div id='TB_overlay'></div><div id='TB_window'></div>");
				$("#TB_overlay").click(tb_remove);
			}
		}
  		
		if(tb_detectMacXFF()){
			$("#TB_overlay").addClass("TB_overlayMacFFBGHack");//use png overlay so hide flash
		}else{
			$("#TB_overlay").addClass("TB_overlayBG");//use background and opacity
		}
		$("body").append("<div id='TB_window' style='display:block; margin-left:-"+width/2+"px; margin-top: -"+height/2+"px; width: "+width+"px; height: "+height+"px; text-align: center'>"+msg+"</div>");
		
 		};
	
	function resetForm(form)
	{
		$(':input', $(form)).each(function(i, item)
		{
			switch(item.tagName.toLowerCase())
			{
				case 'input':
					switch(item.type.toLowerCase())
					{
						case 'hidden':
						case 'text':
							item.value = '';
							break;
						
						case 'radio':
						case 'checkbox':
							item.checked = '';
							break;
					}
					break;
				
				case 'select':
					item.selectedIndex = 0;
					break;
					
				case 'textarea':
					item.value = '';
					break;
			}
		});
		jobs_uploaded_files = [];
		$('#jobs_uploaded_files').html('');
	};
	
	$('#jobform').validate({
		rules: {
			jobs_name: {
				required: true
			},
			jobs_email: {
				required: true,
				email: true
			},
			jobs_message: {
				required: true
			},
			jobs_website: {
				url: true
			}
		},
		messages: {
			jobs_name: 'Bitte geben Sie einen Namen ein',
			jobs_email: {
				required: 'Bitte geben Sie Ihre E-Mail-Adresse ein',
				email: 'Die E-Mail muss das Format name@domain.com haben'
			},
			jobs_message: 'Bitte geben Sie eine Nachricht ein',
			jobs_website: {
				url: 'Die URL muss das Format http://www.domain.com/ haben'
			}
		},
		submitHandler: function(form) {
			$.ajax({
				url: template_root + '/ajax.php',
				data: {
					type: 'submitJobsContactForm',
					jobs_name: $('#jobs_name').val(),
					jobs_email: $('#jobs_email').val(),
					jobs_phone: $('#jobs_phone').val(),
					jobs_website: $('#jobs_website').val(),
					jobs_message: $('#jobs_message').val(),
					jobs_files: $.toJSON(jobs_uploaded_files) 
				},
				success: function(r) {
					if (r == 'true') {
					  resetForm('#jobform');
					  tb_remove();
						alertWindow("<div id='right'><p>Mail wurde versandt!</p></div", 250, 100);
					}
					else {
					  tb_remove();
					  
						alertWindow("<div id='fail'><p>Ein Fehler ist aufgetreten!</p></div>", 280, 100);
					}
				}
			});
			 alertWindow("<div style='margin-top: 25px; height: 50px; width: 100px;' class='loading'></div><p>Mail wird versandt.</p>", 300, 150);
		}
	});
	//CHANGES HTTP
	$('#check_url').change(function() {
		var Aussage = $(this).val();
		var Ergebnis = Aussage.search(/http.+/);
		if (Ergebnis == -1)
			$(this).val('http://'+$(this).val());
	});
	
	$('#websitecheck').validate({
		rules: {
			check_url: {
				required: true,
				url: true
			}
		},
		messages: {
			check_url: {
				required: '',
				//url: 'Die URL muss das Format http://www.domain.com/ haben'
				url:''
			}
		},
		submitHandler: function(form) {
			alertWindow("<div style='margin-top: 25px; height: 50px; width: 100px;' class='loading'></div><p>Bitte warten...</p>", 300, 150);
			$.ajax({
				url: template_root + '/ajax.php',
				data: {
					type: 'websiteCheck',
					url: $('#check_url').val()
				},
				success: function(r) {
					//$('#c2a_form').fadeOut('fast', function() {
						$('#c2a_result').html(r);
						$('#helpText').clone().appendTo('#c2a_result');
						//$('<h4>'+$('#helpText').html()+'</h4>').appendTo('#c2a_result');
						_gaq.push(['_trackEvent', 'interaktion', 'SEO-Tool', 'abgeschickt']);
						_gaq.push(['_trackEvent', 'SEO-Tool', 'Adresse', $('#check_url').val()]);
						
						tb_remove();
					//});
				}
			});
		}
	});
	
	$('a').each(function(){
		if ($(this).parent().parent().attr('id') == 'menu-hauptmenu') {
			$(this).bind('click', function(){
				_gaq.push(['_trackEvent', 'interner link header', 'click', $(this).attr('href')]);
			});
		}
		else if ($(this).parent().parent().attr('id') == 'bread') {
			$(this).bind('click', function(){
				_gaq.push(['_trackEvent', 'interner link breadcrumb', 'click', $(this).html()]);
			});
		}
		else if ($(this).parent().attr('id') == 'helpText') {
			$(this).bind('click', function(){
				_gaq.push(['_trackEvent', 'interner link content', 'click', $(this).html()]);
			});
		}
		else{
		   $(this).bind('click', function(){
				_gaq.push(['_trackEvent', 'externer link', 'click', $(this).html()]);
			});
		}
	});
	
	$('#start-test').click(function(){
		_gaq.push(['_trackEvent', 'interaktion', 'click', 'test starten']);
	});
	
	
	initialize();
	
	

});

