/* jQuery.ScrollTo - Easy element scrolling using jQuery. Copyright (c) 2007-2009 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com */
;(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);

/* Url helper */
var url_helper={qs:false,get:function(key){var qs=this.qs;if(qs===false){qs={};q=window.location+'';q=q.split('?');if(q[1]){$.each(q[1].split('&'),function(i,value){v=value.split('=');qs[v[0]]=v[1]});this.qs=qs}}if(qs[key])return qs[key];return false}};

var ssl_msg = false;

DD_roundies.addRule('.select-list-action', '11px');

$(document).ready(function(){
	
	$('.tabWrapper').each(function(){
		var t = $(this);
		t.find('.new-tab').hide();
		t.find('.new-tabs a').click(function(){
			var th = $(this);
			var index = t.find('.new-tabs a').index(this);
			t.find('.new-tab').hide().eq(index).show();
			t.find('.new-tabs .s').removeClass('s');
			th.parent().addClass('s');
			return false;
		});
		t.find('.new-tabs li.s a').click();
	});
	
	$('#go_to_photos_tab').click(function() {
		$('.tabWrapper a').eq(4).trigger('click');
		return false;
	});
	
	$('#.photo-box a').lightBox();
	
	$('#send-a-friend-form').submit(function() {
		var errors = [];
		
		if($('#saf_name').val().length < 1) {
			errors.push('U bent uw naam vergeten!');
		}
		if($('#saf_mail').val().length < 1) {
			errors.push('U bent uw e-mailadres vergeten!');
		}
		if($('#saf_fname').val().length < 1) {
			errors.push('U bent de naam van uw vriend vergeten in te vullen!');
		}
		if($('#saf_fmail').val().length < 1) {
			errors.push('Het e-mailadres van uw vriend is ongeldig!');
		}
		if($('#saf_msg').val().length < 1) {
			errors.push('Het bericht aan uw vriend is leeg!');
		}
		
		if(errors.length) {
			alert(errors.join("\n"));
		} else {
			$.post('http://www.italianresidence.nl/send_a_friend.php', $(this).serialize(), function() {
				//done sending
			});
			
			$('.saf_msg').fadeIn();
			
			setTimeout(function() {
				$('.send-a-friend').fadeOut();
			}, 2000);
		}
		
		return false;
	});
	
	$('.saf_close').click(function() {
		$('.send-a-friend').fadeOut();
		return false;
	});
	
	$('.saf_show').click(function() {
		saf_show();
		return false;
	});
	
	/////////////////
	
	$('#send_my_list_to_friends').click(function() {
		var sel = [];
		$('.my-selected-houses:checked').each(function() {
			sel.push($(this).val());
		});
		
		if(!sel.length) {
			alert('You need to select at least one house.');
			return false;
		}
		
		//sel = sel.join(',');
		sel = sel.join('+');
		
		var url = window.location + '&ff_add_houses=' + sel;
		
		if(!ssl_msg) {
			ssl_msg = $('#saf_msg').html();
		}
		
		$('#saf_msg').html(ssl_msg.replace('##url##', url));
		saf_show();
		
		return false;
	});
	if(url_helper.get('action') == 'doorsturen') $('#send_my_list_to_friends').trigger('click');
	
	$('#my_list_check_avail').click(function() {
		
		var sel = [];
		$('.my-selected-houses:checked').each(function() {
			sel.push($(this).val());
		});
		
		if(!sel.length) {
			alert('You need to select at least one house.');
			return false;
		}
		
		sel = sel.join(',');
		
		var ha = $('<div/>').css({
			background:		'#FFFFCC',
			height:			700,
			width:			960,
			position:		'absolute',
			top:			'50%',
			left:			'50%',
			marginLeft:		-480,
			marginTop:		-250,
			zIndex:			999,
			padding:		20
		}).appendTo(document.body).html('<br /><iframe src="check_avail_multiple.php?ids=' + sel + '" width="960" height="680"></iframe>');
		
		$('<a/>').data('hah', ha).html('X').css({
			position:	'absolute',
			right:		10,
			top:		10,
			fontSize:	'160%',
			border:		'3px solid #006600',
			color:		'#006600',
			padding:	4,
			background:	'#ffcc33',
			textDecoration:	'none'
		}).appendTo(ha).attr('href', '#').click(function() {
			
			$(this).data('hah').remove();
			
			return false;
		});
		
		//showAvail('check_avail_multiple.php?ids=' + sel);
		
		return false;
	});
	if(url_helper.get('action') == 'beschikbaarheid-aanvragen') $('#my_list_check_avail').trigger('click');
	
	$('#my_list_direct_reserv').click(function() {
		
		var sel = [];
		$('.my-selected-houses:checked').each(function() {
			sel.push($(this).val());
		});
		
		if(!sel.length) {
			alert('You need to select at least one house.');
			return false;
		}
		
		sel = sel.join(',');
		
		var ha = $('<div/>').css({
			background:		'#FFFFCC',
			height:			500,
			width:			960,
			position:		'absolute',
			top:			'50%',
			left:			'50%',
			marginLeft:		-480,
			marginTop:		-250,
			zIndex:			999,
			padding:		20
		}).appendTo(document.body).html('<br /><iframe src="reserveren_multiptle.php?ids=' + sel + '" width="960" height="480"></iframe>');
		
		$('<a/>').data('hah', ha).html('X').css({
			position:	'absolute',
			right:		10,
			top:		10,
			fontSize:	'160%',
			border:		'3px solid #006600',
			color:		'#006600',
			padding:	4,
			background:	'#ffcc33',
			textDecoration:	'none'
		}).appendTo(ha).attr('href', '#').click(function() {
			
			$(this).data('hah').remove();
			
			return false;
		});
		
		//showAvail('check_avail_multiple.php?ids=' + sel);
		
		return false;
	});
	if(url_helper.get('action') == 'direct-reserveren') $('#my_list_direct_reserv').trigger('click');
	
});

function saf_show() {
	$('.send-a-friend').fadeIn();
	//$.scrollTo('48%');
}
