$(document).ready(function() {
	
	//
	//	Anchor links
	//
	$("#header a[href^='#'], .content a[href^='#']").click(function(){
		var href = $(this).attr("href");
		if (href != '#top') {
			if ($(href+' .image').size()) {
				$(href+' .image').click();
			} else {
				scrollTop(href);
			}
		}
		return;
	});
	
	//
	//	Header
	//
	
	$('#head_news .image ul').cycle({
		pagerEvent: 'mouseover',
		pager: '#head_news',
		pagerAnchorBuilder: function(idx, slide) { 
        	return '#head_news .list li:eq('+idx+')';
    	}
	});

	/*
	$('#header a').click(function(){
		var obj = $(this).attr('href');
		$(obj+' .image').click();
		return false;
	});
	*/

	$('#head_portfolio .image ul').cycle();
	
	//
	//	Portfolio & Blog shared
	//
	$('.item .image').hover(
		function(){
			var obj = $(this).closest('.item');
			$(obj).find('.overlay').stop(false,true).animate({opacity: 0},300);
		},
		function(){
			var obj = $(this).closest('.item');
			if (!$(obj).hasClass('active') && !$(obj).hasClass('animating')) {
				$(obj).find('.overlay').stop(false,true).animate({opacity: 0.7},300);
			}
		}
	);
	
	$('#blog .item .content').each(function(){
		if ($(this).find("li").size() > 1) {
			$(this).show();
			$(this).find("ul").jcarousel({
				scroll: 1,
				buttonNextHTML: '<div>volgende pagina</div>',
				buttonPrevHTML: '<div>vorige pagina</div>'
			});
			$(this).hide();
		} else {
			$(this).addClass("margin");
		}
	});

	//
	//	Portfolio
	//
	$('#head_portfolio .image li a').click(function(){
		var obj = $(this).attr('href');
		$(obj+' .image').click();
		return false;
	});

	
	$('#portfolio .item .image').click(function(){
		var obj = $(this).closest('.item');
		if (!$(obj).hasClass('active')) {
			scrollTop(obj);
			$(obj).addClass('animating');
			$(obj).find('.overlay').animate({opacity: 0});
			$(obj).find('.image').stop(true,true).delay(600).animate({height: 400},700);
			$(obj).find('.image .img').stop(true,true).delay(600).animate({height: 400,width: 500},700);
			$(obj).find('.content').delay(1700).slideDown(500);
			
			$(obj).find('.image .img').each(function(){
				$(this).css("background-image","url("+$(this).attr("rel")+")");
			});
			$(obj).find('.image .img img').delay(2000).animate({opacity: 0},1000);
			$(obj).delay(2000,function(){ $(this).addClass('active'); $(this).removeClass('animating'); });
		} else {
			//scrollTop(obj,-100);
			$(obj).find('.content').slideUp(500);
			$(obj).find('.image').stop(true,true).delay(500).animate({height: 100});
			$(obj).find('.image .images').stop(true,true).delay(500).animate({marginLeft: 0});
			$(obj).find('.image .img img').animate({opacity: 1},500);
			$(obj).find('.image .img').stop(true,true).delay(500).animate({height: 100,width: 125},500,'',function(){ $(this).css("background-image","none"); });
			$(obj).removeClass('active');
		}
	});

	$('#portfolio .item').mousemove(function(e){ 
		if ($(this).hasClass('active') && e.clientY <= 400) {
			var img = $(this).find('.image .images');
			var num = $(this).find('.image .images .img').size();
			var basepos = e.pageX - $(this).offset().left;
			
			var pos = (((num * 500) - 1000) * (basepos / 1000)) * -1;
			$(img).stop(true,true).animate({marginLeft: pos});
		}
	});
	
	$("#portfolio .top").click(function(){
		var obj = $(this).closest('.item');
		$(obj).find('.content').slideUp(500);
		$(obj).find('.image').stop(true,true).delay(500).animate({height: 100});
		$(obj).find('.image .images').stop(true,true).delay(500).animate({marginLeft: 0});
		$(obj).find('.image .img img').animate({opacity: 1},500);
		$(obj).find('.image .img').stop(true,true).delay(500).animate({height: 100,width: 125},500,'',function(){ $(this).css("background-image","none"); });
		$(obj).removeClass('active');
		$(obj).delay(1000,function(){
			scrollTop('#top');
		});
		return false;
	});
	
	//
	//	Blog
	//
	$('#blog .item .image').click(function(){
		var obj = $(this).closest('.item');
		if (!$(obj).hasClass('active')) {
			scrollTop(obj);
			$(obj).find('.content').delay(500).slideDown(1000);
			$(obj).find('.overlay').stop(true,true).delay(500).animate({opacity: 0},100);
			$(obj).find('.image').stop(true,true).delay(600).animate({height: 140},1000);
			$(obj).addClass('active');
		} else {
			$(obj).find('.content').slideUp(500);
			$(obj).find('.image').stop(true,true).animate({height: 100},500);
			$(obj).find('.overlay').stop(true,true).animate({opacity: 0.7},1500);
			$(obj).removeClass('active');
		}
	});

	$("#blog .top").click(function(){
		var obj = $(this).closest('.item');
		$(obj).find('.content').slideUp(500);
		$(obj).find('.image').stop(true,true).animate({height: 100},500);
		$(obj).find('.overlay').stop(true,true).animate({opacity: 0.7},1500);
		$(obj).removeClass('active');
		$(obj).delay(1000,function(){
			scrollTop('#top');
		});
		return false;
	});

	
	//
	//	Twitter
	//
	
	$("#twittertext").getTwitter({
		userName: "BrianValbergGo",
		numTweets: 10,
		loaderText: "Tjielp tjielp...",
		slideIn: false,
		showHeading: false,
		showProfileLink: false
	});
	
	//
	//	Contact
	//

	$("#contact .top").click(function(){
		scrollTop('#top');
		return false;
	});


	//
	//	Google Maps
	//
	if ($("#googlemaps").size()) { go_go_google_maps(); }
	
	function go_go_google_maps() {
		var latlng = new google.maps.LatLng(51.000524,5.886928);
		var myOptions = {
			zoom: 15,
			center: latlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
    	};
		var map = new google.maps.Map(document.getElementById("googlemaps"), myOptions);
	
		var marker = new google.maps.Marker({
			position: latlng,
			map: map,
			title:"Brian Valberg GO"
		});
		
		var infowindow = new google.maps.InfoWindow({
			position: latlng,
			content: '<strong>Brian Valberg GO</strong><br />Burgemeester Kampsstraat 28<br />6137 VS Sittard<br />046 4587232<br />'
		});
		infowindow.open(map);
	}

	$("#startcalculate").click(function(){
		var url = 'http://maps.google.nl/maps?f=d&source=s_d&saddr='+$("#gmaprstreet").val()+',+'+$("#gmaprcity").val()+'&daddr=Burg.+Kampsstraat+28,6137+VS,Sittard&hl=nl&geocode=FRyjCAMdWW9bACmNTcNUqr3ARzEPzre6WfU1aA;FZ40CgMdkNNZAClV6W50aLjARzEgs08h571cnA&mra=ls&sll=51.000478,5.886864&sspn=0.010992,0.027874&g=Burg.+Kampsstraat+28,6137+VS,Sittard&ie=UTF8&z=12&pw=2';
		window.open(url);
		return false;
	});

});

//
//	function scrollTop
//
function scrollTop(obj,extramargin) {
	if (!extramargin) { extramargin = 0; }
	var top = ($(obj).offset().top + extramargin);
	$('html, body').animate({scrollTop: top },700);
}


//
//	function delay
//
$.fn.delay = function(time, callback){
    jQuery.fx.step.delay = function(){};
    return this.animate({delay:1},time,callback);
}
