/**
 * jQuery.ScrollTo - Easy element scrolling using jQuery.
 * Copyright (c) 2007-2009 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
 * Dual licensed under MIT and GPL.
 * Date: 5/25/2009
 * @author Ariel Flesler
 * @version 1.4.2
 *
 * http://flesler.blogspot.com/2007/10/jqueryscrollto.html
 */
;(function(d){var k=d.scrollTo=function(a,i,e){d(window).scrollTo(a,i,e)};k.defaults={axis:'xy',duration:parseFloat(d.fn.jquery)>=1.3?0:1};k.window=function(a){return d(window)._scrollable()};d.fn._scrollable=function(){return this.map(function(){var a=this,i=!a.nodeName||d.inArray(a.nodeName.toLowerCase(),['iframe','#document','html','body'])!=-1;if(!i)return a;var e=(a.contentWindow||a).document||a.ownerDocument||a;return d.browser.safari||e.compatMode=='BackCompat'?e.body:e.documentElement})};d.fn.scrollTo=function(n,j,b){if(typeof j=='object'){b=j;j=0}if(typeof b=='function')b={onAfter:b};if(n=='max')n=9e9;b=d.extend({},k.defaults,b);j=j||b.speed||b.duration;b.queue=b.queue&&b.axis.length>1;if(b.queue)j/=2;b.offset=p(b.offset);b.over=p(b.over);return this._scrollable().each(function(){var q=this,r=d(q),f=n,s,g={},u=r.is('html,body');switch(typeof f){case'number':case'string':if(/^([+-]=)?\d+(\.\d+)?(px|%)?$/.test(f)){f=p(f);break}f=d(f,this);case'object':if(f.is||f.style)s=(f=d(f)).offset()}d.each(b.axis.split(''),function(a,i){var e=i=='x'?'Left':'Top',h=e.toLowerCase(),c='scroll'+e,l=q[c],m=k.max(q,i);if(s){g[c]=s[h]+(u?0:l-r.offset()[h]);if(b.margin){g[c]-=parseInt(f.css('margin'+e))||0;g[c]-=parseInt(f.css('border'+e+'Width'))||0}g[c]+=b.offset[h]||0;if(b.over[h])g[c]+=f[i=='x'?'width':'height']()*b.over[h]}else{var o=f[h];g[c]=o.slice&&o.slice(-1)=='%'?parseFloat(o)/100*m:o}if(/^\d+$/.test(g[c]))g[c]=g[c]<=0?0:Math.min(g[c],m);if(!a&&b.queue){if(l!=g[c])t(b.onAfterFirst);delete g[c]}});t(b.onAfter);function t(a){r.animate(g,j,b.easing,a&&function(){a.call(this,n,b)})}}).end()};k.max=function(a,i){var e=i=='x'?'Width':'Height',h='scroll'+e;if(!d(a).is('html,body'))return a[h]-d(a)[e.toLowerCase()]();var c='client'+e,l=a.ownerDocument.documentElement,m=a.ownerDocument.body;return Math.max(l[h],m[h])-Math.min(l[c],m[c])};function p(a){return typeof a=='object'?a:{top:a,left:a}}})(jQuery);

/* Artprofound Javascript Functions. Coded by Deniz */
function letsGO(target, speed, Soffset)
{
	currentColumn = target;
	$.scrollTo(target, (speed ? speed : 1600), {offset: (Soffset ? Soffset : -200)});
}

function home()
{
	$.scrollTo(
		homeFirstPOS, 
		1000,
		{axis: 'x'}
	);
}

function WebsiteInit()
{
	if(isTouch)
	{
		$('#login_register').remove();
		$('#main_logo').attr('src', imageurl+'main/logo_ipad.png');

		$('a.scroll').live('click', function(e)
		{
			var movePos = $(this.hash).offset();
			movePos = movePos.left;
			$.scrollTo(
				movePos, 
				1000,
				{axis: 'x'}
			);

			e.preventDefault();
			return false;
		});
	}
	else
	{
		$('a.scroll').live('click', function(e)
		{
			letsGO(this.hash);
			e.preventDefault();
			return false;
		});
	}

	$footerText = $('#footer #text');
	$stopAnimation = $('#footer #stopAnimation');
	$playAnimation = $('#footer #playAnimation');
	$footerText.hide().html(animatedText[runCount]).fadeIn('slow', function(){startAnimation();});
	$footerText.hover(function(){stopAnimation();},function(){startAnimation();});			
	$stopAnimation.click(function(e)
	{
		e.preventDefault();
		stopAnimation();
		$footerText.hide();
		$('#footer').animate({width: '30px'}, 'slow');
		$stopAnimation.hide();
		$playAnimation.show();
	});
	$playAnimation.click(function(e)
	{
		e.preventDefault();
		$('#footer').animate({width: '100%'}, 'slow', function(){$footerText.show();});
		startAnimation();
		$playAnimation.hide();
		$stopAnimation.show();
	});
	
	$('[rel="back"]').click(function(e){e.preventDefault();home();return false;});
	
	$('#clickable_contact').click(function(e){e.preventDefault();letsGO('#contact');return false;});
	$('#clickable_logo').click(function(e){e.preventDefault();window.location=fullurl;return false;});

	$('#move_left').click(function(e)
	{
		e.preventDefault();
		$.scrollTo( {top:'0', left:'-=500'}, 900);
		return false;
	});
	$('#move_right').click(function(e)
	{
		e.preventDefault();
		$.scrollTo( {top:'0', left:'+=500'}, 900);
		return false;
	});

	$('#move_bottom').click(function(e)
	{
		e.preventDefault();
		$.scrollTo( {top: '+=200px', left: '+=0'}, 900);
		return false;
	});
	$('#move_top').click(function(e)
	{
		e.preventDefault();
		$.scrollTo( {top: '-=200px', left: '+=0'}, 900);
		return false;
	});

	$('#move_home').click(function()
	{
		home();
		return false;
	});

	$('#gebruikersnaam,#wachtwoord').live('click', function(e)
	{
		e.preventDefault();
		$(this).val('');
		return false;
	});

	$('#gebruikersnaam,#wachtwoord').live('keyup', function(e)
	{
		if(e.keyCode == 13)
		{
			e.preventDefault();
			$gebruikersnaam = $('#gebruikersnaam').val();
			$wachtwoord = $('#wachtwoord').val();
			
			if($gebruikersnaam && $wachtwoord)
			{
				$form = 'Bezig met laden, een moment svp..<div style="display: none;"><form id="webmailform" name="form" action="http://webmail.artprofound.nl/roundcube/?lang=nl" method="post"><input name="_action" value="login" type="hidden" /><input name="_user" id="rcmloginuser" size="30" autocomplete="off" value="'+$gebruikersnaam+'" type="text" /><input name="_pass" id="rcmloginpwd" size="30" type="password" value="'+$wachtwoord+'" /><input type="hidden" name="_url" id="rcmloginurl" /><input type="hidden" name="_timezone" id="rcmlogintz" value="_default_" /><input type="hidden" name="_task" value="login" /></form></div>';
				$('#login_register').html($form);
				$('#webmailform').submit();
			}
		}
	});

	$('[rel=uitloggen]').live('click', function(e)
	{
		loginRegisterLoading();
		e.preventDefault();
		$.ajax
		({
			type: "POST",
			url: fullurl+"ajax_front.php",
			data: "action=uitloggen",
			success: function(sonuc)
			{
				$('#login_register').html(sonuc);
			}
		});
		return false;
	});

	$('#poll_form').submit(function(e)
	{
		$('.poll_server_response').html('<img src="'+styleurl+'images/misc/ajax-loader-small.gif" class="middle" border="0"> Bezig met laden, een moment svp..');
		e.preventDefault();
		var postData = $(this).serialize();
		
		$.ajax
		({
			type: "POST",
			url: fullurl+"ajax_front.php",
			data: postData,
			success: function(sonuc)
			{
				if(sonuc.match(/Faut/i))
				{
					$('.poll_server_response').html(sonuc);
				}
				else
				{
					$('#ap_poll').html(sonuc);
				}
			}
		});
	});
	
	$("#contactform").submit(function(e){$("input[type=submit]", this).attr("value", "Bezig met laden, een moment svp..");$("input[type=submit]", this).attr("disabled", "disabled");});
	
	$('a.ap_portfolio').colorbox(
	{
		slideshow: true, slideshowAuto: false, slideshowSpeed: 3500, slideshowStart: 'slideshow', slideshowStop: 'stop', scalePhotos: true, maxWidth: '95%', maxHeight: '90%', current: '', title: function(){return $(this).attr('title');}, 
		onOpen: function()
		{
			if(isTouch)
			{
				$('#scroller,.logo').hide();
			}
			$(document).bind('touchmove',function(e)
			{
				e.preventDefault();
			});
		},
		onClosed: function()
		{
			if(isTouch)
			{
				$('#scroller,.logo').show();
			}
			$(document).unbind('touchmove');
		}
	});

	$('#route_bergen').live('click', function(e)
	{
		e.preventDefault();
		$(this).colorbox({href: fullurl+'showroute.php?r=bergen', maxWidth: '80%', maxHeight: '80%', open: true,
			onOpen: function()
			{
				if(isTouch)
				{
					$('#scroller,.logo').hide();
				}
				$(document).bind('touchmove',function(e)
				{
					e.preventDefault();
				});
			},
			onClosed: function()
			{
				if(isTouch)
				{
					$('#scroller,.logo').show();
				}
				$(document).unbind('touchmove');
			}
		});
	});

	$('#youtube_video1').live('click', function(e)
	{
		e.preventDefault();
		$(this).colorbox({href: fullurl+'playvideo.php?v=1', maxWidth: '80%', maxHeight: '80%', open: true,
			onOpen: function()
			{
				if(isTouch)
				{
					$('#scroller,.logo').hide();
				}
				$(document).bind('touchmove',function(e)
				{
					e.preventDefault();
				});
			},
			onClosed: function()
			{
				if(isTouch)
				{
					$('#scroller,.logo').show();
				}
				$(document).unbind('touchmove');
			}
		});
	});

	$('#youtube_video2').live('click', function(e)
	{
		e.preventDefault();
		$(this).colorbox({href: fullurl+'playvideo.php?v=2', maxWidth: '80%', maxHeight: '80%', open: true,
			onOpen: function()
			{
				if(isTouch)
				{
					$('#scroller,.logo').hide();
				}
				$(document).bind('touchmove',function(e)
				{
					e.preventDefault();
				});
			},
			onClosed: function()
			{
				if(isTouch)
				{
					$('#scroller,.logo').show();
				}
				$(document).unbind('touchmove');
			}
		});
	});

	$('#route_amsterdam').live('click', function(e)
	{
		e.preventDefault();
		$(this).colorbox({href: fullurl+'showroute.php?r=amsterdam', maxWidth: '80%', maxHeight: '80%', open: true,
			onOpen: function()
			{
				if(isTouch)
				{
					$('#scroller,.logo').hide();
				}
				$(document).bind('touchmove',function(e)
				{
					e.preventDefault();
				});
			},
			onClosed: function()
			{
				if(isTouch)
				{
					$('#scroller,.logo').show();
				}
				$(document).unbind('touchmove');
			}
		});
	});

	//logVisitor();//Remove this to active again..
}

function loginRegisterLoading()
{
	$('#login_register').html('<img src="'+styleurl+'images/misc/ajax-loader-small.gif" class="middle" border="0"> Bezig met laden, een moment svp..');
}

function stopAnimation()
{
	clearInterval($animateText);
}

function startAnimation()
{
	$animateText = setInterval('animateText()', 9000);
}

function animateText()
{
	runCount++;

	if(runCount >= animatedTextCount)
	{
		runCount = 0;
	}

	$footerText.slideUp('slow', function()
	{
		$footerText.html(animatedText[runCount]);
		$footerText.slideDown();
	});
}

function logVisitor()
{
	$.ajax
	({
		type: 'POST',
		url: fullurl+'ajax_front.php',
		data: 'action=log_visitor&browser='+encodeURIComponent(Vbrowser)+'&width='+encodeURIComponent(Vwidth)+'&height='+encodeURIComponent(Vheight)
	});
}

var Vbrowser = navigator.userAgent;
var Vwidth = $(window).width();
var Vheight = $(window).height();

var logoOffset = $('.logo').offset();
var leftMargin = logoOffset.left + 305;
var menuPos = $('.scrollable_middle').offset();
var homeFirstPOS = parseInt(menuPos.left) - leftMargin;

$(document).ready(function()
{
	var $minpContentHeight = (Vheight - 94);//79: Top Logo height, 44: Footer height.

	$('.scrollable_left .pContent, .scrollable_right .pContent').each(function()
	{
		$this = $(this);
		if($this.attr('rel') != 'geenScrollbalk')
		{
			$this.css({'height':$minpContentHeight+'px', 'overflow':'auto'});
		}
	});

	WebsiteInit();
});

$(window).load(function()
{
	$('#loading').remove();

	window.scrollTo(0, 1);
	
	if(moveTo)
	{
		if(isTouch)
		{
			var movePos = $(moveTo).offset();
			movePos = movePos.left;
			$.scrollTo(
				movePos, 
				1000,
				{axis: 'x'}
			);
		}
		else
		{
			letsGO(moveTo);
		}
	}
	else
	{
		home();
	}
});
