//prevent errors in browsers that do not support console.
if (typeof window.loadFirebugConsole == "undefined") {
  var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml", "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
  window.console = {};
  for (var i = 0; i < names.length; ++i) {
    window.console[names[i]] = function(){};
  }
}

jQuery(function($) {
	$('body').addClass('js').removeClass('nojs');
	init_posts();
	
	$('.wpcf7 div.submit input').attr('value', ' ');
	
	//mouse over fürs logo, sofern der screen breit genug ist	
	$('#header #topTitle a').hover(function() {
		if($(document).width() >= 1050) {
			$(this).addClass('hover');
		}
	}, function() {
		$(this).removeClass('hover');
	});
	
	
	
	//hack um die aktive seite anzuzeigen
	active_page = $('#topNav ul').attr('class').split(' ');
	$.each(active_page, function(i) {
		if(active_page[i]) {
			$('#topNav li:has(a[title='+active_page[i]+'])').addClass('current_page_item');
		}
	});
	Cufon.refresh();
	
	//schöner für formulare
	$('form :input:visible').focus(function() {
		$(this).addClass('focus');
	}).blur(function() {
		$(this).removeClass('focus');
	});
	
	//labels im kontaktformular
	$('.wpcf7 form div.input').each(function() {
		var l = $(this).find('label').hide().text();
		if(!$(this).find(':input').val()) {
			$(this).find(':input').val(l);
		}
		$(this).find(':input').focus(function() {
			if($(this).val() == l) {
				$(this).val('');
			}
		}).blur(function() {
			if($(this).val() == '') {
				$(this).val(l);
			}
		});
		$(this)
			.closest('form').addClass('withjs').end()
			.find('br').remove();
	});
	
	//vorbereiten für die animiation vom header
	if($('body').is('.list')) {
		$('#content').wrap('<div class="contentWrap"></div>').parent();
		$(window).resize(function() {
			set_content_width();
		});
	}
	
	$('#topNav a').live('click', function(e) {
		$(this).closest('li').addClass('loading')
		   	.siblings('.current_page_item')
			.removeClass('current_page_item');
	});
	
	//nachladen der inhalte auf den übersichtsseiten
	$('body.list #topNav a').live('click', function(e) {
		e.preventDefault();
		
		f = new flip();
		f.init($(this), function() {
			this.$li
				.removeClass('loading')
				.siblings('.current_page_item')
					.removeClass('current_page_item')
				.end()
				.addClass('current_page_item');
			Cufon.refresh();
			
			//rückmeldung an den browser
			$(location).attr('hash', this.$a.text());
			//$('title').html($a.attr('title')+' &bull; Ape Unit GmbH');
			document.title = this.$a.attr('title')+' • Ape Unit GmbH';
		});
		f.run();
		
		var $a = $(this);
		window.setTimeout(function() {
			//boxen aktualisieren
			$.get($a.attr('href'), {onlyFooter: true}, function(r) {
				//vorhandene inhalte entfernen
				$('#posts .container_4 *').remove();
				//neue inhalte reinschreiben
				$('#posts .container_4').append(r);
				init_posts();
			});
		}, 500);
		
		/*$a = $(this);
		$li = $a.closest('li');
		h = '450px';
			
		//inhalt aktualisieren
		$.get($a.attr('href'), function(r) {
			$li.addClass('current_page_item').removeClass('loading');
			inlinestyle = $($(r).get(0)).text();
			
			//create new container and fill in the html code
			$('#content').after(
				$('#content')
					.clone()
					.html(r)
					.addClass('clone')
					.attr('style', inlinestyle)
			);
			
			//replace the font
			Cufon.refresh();
			
			//hide the boxes
			$('#posts .container_4 .post').animate({opacity: 0}, 500);
			
			//init the map
			if($('#openstreetmap').length == 1) {
				create_map();
			}
			
			//animate container
			window.setTimeout(function() {
				/*$('#content')
					.animate({top: '-350px'}, 500, function() {
						$(this).remove();
					});*
				$('#content').css({top: '-350px'});
				$('#content.clone').css({top: 0});
				/*$('#content.clone')
					.animate({top: '0px'}, 500, function() {
						$(this).removeClass('clone');
					});
				
				//rückmeldung an den browser
				$(location).attr('hash', $a.text());
				//$('title').html($a.attr('title')+' &bull; Ape Unit GmbH');
				document.title = $a.attr('title')+' • Ape Unit GmbH';
				
				//boxen aktualisieren
				$.get($a.attr('href'), {onlyFooter: true}, function(r) {
					//vorhandene inhalte entfernen
					$('#posts .container_4 *').remove();
					//neue inhalte reinschreiben
					$('#posts .container_4').append(r);
					init_posts();
				});
			}, 500);
		});*/
	});
	
	/*
	//geiler effekt für die vcard
	$('.topContact').click(function(e) {
		e.preventDefault();
		if($(this).is('.closed')) {
			if($('#topContactClone').length == 0) {
				//card clonen
				$('body').append(
					$('.topContact')
						.clone()
						.attr('id', 'topContactClone')
						.css('right', $('#content .container_4').offset().left)
						.find('>*').wrapAll('<div class="inner"></div>').end()
						.hide()
					);
				$('#topContactClone').slideDown();
				//clip initalisieren
				var clip = new ZeroClipboard.Client();
				clip.glue($('#topContactClone .downloads .copy span').get(0));
				clip.setText('Ape Unit GmbH'+"\r\n"+'Lausitzer Str. 23'+"\r\n"+'10999 Berlin'+"\r\n\r\n"+'030 306 490 10'+"\r\n"+'office@apeunit.com');
				clip.addEventListener('complete', function(client, text) {
	                alert("In Ihrer Zwischenablage befindet sich jetzt folgender Text:\n\n" + text + "\n\nWir freuen uns auf Ihre Nachricht. Bis gleich!");
	            });
			}
			$('#topContactClone').removeClass('closed').addClass('open');
		}
		else {
			$('#topContactClone').removeClass('open').addClass('closed');
		}
	});
	*/
	//geiler effekt braucht noch ein bisschen grafische überarbeitung. deshalb:
	$('.topContact').click(function() {
		window.location = 'mailto:office@apeunit.com';
	});
	
	//TODO: fkt below läuft gerade nicht
	$(document).click(function(event) {
		if(!$(event.target).closest('div').is('.topContact') && $('#topContactClone').is(':visible')) {
			$('#topContactClone').fadeOut(function() {
				$(this).remove();
			});
		}
	});
	
	//für die kontaktseite
	if($('#openstreetmap').length == 1) {
		create_map();
	}
	
	check_hash(false);
});

function create_map() {
	$.getScript('http://tile.cloudmade.com/wml/latest/web-maps-lite.js', function() {
		var cloudmap = new CM.Tiles.CloudMade.Web({key: 'c8ef25c9307d4ecfbd76a4e9f0c49f79', styleId: 18367});
		var map = new CM.Map('openstreetmap', cloudmap);
		var myMarker = new CM.Marker(new CM.LatLng(52.495405,13.426723), {
			title: 'Ape Unit, Lausitzer Str. 23, 10999 Berlin',
			icon: {
				image: base+'/images/yeah.png',
				iconSize: new CM.Size(46,41),
				iconAnchor: new CM.Point(40, 40)
			}
		});
		map.setCenter(new CM.LatLng(52.495405,13.426723), 14);
		map.addControl(new CM.LargeMapControl());
		map.addOverlay(myMarker);
		map.disableScrollWheelZoom();
		//myMarker.openInfoWindow('<strong>Hier sind Ihre Projekte gut aufgehoben:</strong><br />Ape Unit GmbH<br />Lausitzer Str. 23<br />10999 Berlin<br /><br />office@apunit.com<br />030 306 490 10');
	});
}

function set_content_width() {
	w = $(window).width();
	$('.contentWrap').css('width', w);
	$('#content').css('width', w);
		
}

function init_posts() {
	//cooles einfaden der posts unten
	$('#posts .post')
		.find('>.content')
			.css('opacity', '0.9')
			.hide()
			.end()
		.hover(function() {
			$('.content', this).fadeIn();
		}, function() {
			$('.content', this).fadeOut();
		})
		.css('opacity', 0)
		.fadeLine();
}

function check_hash(use_last_hash) {
  var lastHash = use_last_hash ? window.location.hash : '';
  check_hash_timeout = window.setTimeout(function() {
    if(lastHash != window.location.hash) {
			hash = window.location.hash.substr(1, window.location.hash.length);
			if(!$('#topNav a[title='+hash+']').closest('li').is('.current_page_item')) {
				$('#topNav a[title='+hash+']').click();
			}
    }
    check_hash(true);
  }, 500);
}