// convert rel="external" to blank-page-links
$(function(){$('a[href][rel*=external]').each(function(i){this.target = "_blank";});});


$(document).ready(function() {


	var largePath = '/images/immo/_project_large/';
	var thumbPath = '/images/immo/_project_thumb/';
	
	if($('#fiche #images #image_large').length > 0){
		var defaultImage = $('#fiche #images #image_large').css('background').between('_project_large/',')');
	}
	
	$('#fiche #images #thumbs img').hover(
		function() {
			var hoverImage = $(this).attr('src').replace(thumbPath, largePath);
			$('#fiche #images #image_large').css('backgroundColor','#939393');
			$('#fiche #images #image_large').css('backgroundPosition','center center');
			$('#fiche #images #image_large').css('backgroundImage','url('+hoverImage+')');
		}
	);

	
	var googlemapScript = $('#googlemap').html();
	
	if(googlemapScript != null){
		var mapID = googlemapScript.between('showmap_','(){');
  
	}
	
	
	if(mapID != ''){
		$('#fiche #googlemapClose').bind('click', function(){
			$('#googlemapContainer').css('display','none');	
		});
		
		$('#actions #showMap').bind('click', function(){
      
			$('#googlemapContainer').slideDown();	
			eval('showmap_'+mapID+'()');
		});
	}
	
	//slideshow
	$('#fiche #startSlideshow').bind('click', function(){
		$('#slideshow').fadeIn('slow');
		startSlideShow(0);
	});
	
	$('#images #thumbs img').bind('click', function(){
		$('#slideshow').fadeIn('slow');
		startSlideShow($(this).index());
	});
	

	$('#fiche #playmodus').bind('click', function() {
		var playmodus = $('#playmodus').html();
		if(playmodus == 'pauze'){
			$('#slideshowPics').cycle('pause');
			$('#playmodus').html('start');
		}else{
			$('#slideshowPics').cycle('resume');
			$('#playmodus').html('pauze');
		}
	});
		
	
	$('#closeSlideshow').bind('click', function() {
		$('#slideshowPics').cycle('pause');
		$('#slideshow').fadeOut('slow');
		
		  $('#fullContainer').animate({
   			marginTop: "0"
   		}, 'slow');
	});


  	$('#cancel-sendtofriend').bind('click', function() {
		$('#sendtofriend').hide();
	});

	$('#show-sendtofriend').bind('click', function() {
		$('.captcha').attr('src', '/code.php?'+Math.random());
		$('#sendtofriend').show();
    $('#fullContainer').css('margin-top','65px');
	});

	$('#sendtofriendFormClose').bind('click', function() {
		closeSendtofriendInfo();
	});


	$('#cancel-moreInfo').bind('click', function() {
		$('#moreInfo').hide();
	});

	$('#show-moreInfo').bind('click', function() {
		$('.captcha').attr('src', '/code.php?'+Math.random());
		$('#moreInfo').show();
	});
	
	$('#moreInfoFormClose').bind('click', function() {
		closeMoreinfo();
	});
	
	$('#visitFormClose').bind('click', function() {
			closeVisitInfo();
	});

	$('#cancel-visitInfo').bind('click', function() {
		$('#visitInfo').hide();
	});
	
	$('#show-visit').bind('click', function() {
		$('.captcha').attr('src', '/code.php?'+Math.random());
		$('#visitInfo').show();
	});
	
	$('#show-sv').bind('click', function() {
		$('#sv').show();
	});
	//SEND MAIL FORMS

  	// ---- SEND TO FRIEND---
	$('#submitSendtofriend').bind('click', function() {
		 var options = {
        target:        '#msg3',   // target element(s) to be updated with server response
        beforeSubmit:  checkFieldsSendtofriend,
        success:       showResponseSendtofriend
        }

		  $('#sendtofriendForm').ajaxSubmit(options);
			return false;
	});

	$('#closeSendtofriend').bind('click', function() {
		closeSendtofriendInfo();
	});




	// ---- MORE INFO ----
	$('#submitMoreInfo').bind('click', function() {
		 var options = { 
        target:        '#msg1',   // target element(s) to be updated with server response 
        beforeSubmit:  checkFields,
        success:       showResponse
        }
        
	$('#moreInfoForm').ajaxSubmit(options); 
			return false; 
	});
	
	
	$('#closeMoreInfo').bind('click', function() {
		closeMoreinfo();
	});


	
	// ---- ASK INFO ----
	$('#submitVisit').bind('click', function() {
		 var options = { 
        target:        '#msg2',   // target element(s) to be updated with server response 
        beforeSubmit:  checkFieldsVisit,
        success:       showResponseVisit
        }
        
		  $('#visitForm').ajaxSubmit(options); 
			return false; 
	});
	
	$('#closeVisitInfo').bind('click', function() {
		closeVisitInfo();
	});

	$('#closeSV').bind('click', function() {
		$('#sv').hide();
	});
	
});


function closeMoreinfo() {
		$('#moreInfo').hide();
		$('#moreInfoForm').show();
		$('#msg1').html('');
		$('#moreInfoFormClose').hide();
		
		$('#moreInfoForm').each (function(){
  		this.reset();
		});
}

function closeVisitInfo() {
		$('#visitInfo').hide();
		$('#visitForm').show();
		$('#msg2').html('');
		$('#visitFormClose').hide();
		
		$('#visitForm').each (function(){
  		this.reset();
		});
}


function closeSendtofriendInfo() {
    $('#fullContainer').css('margin-top','0px');
  	$('#sendtofriend').hide();
		$('#sendtofriendForm').show();
		$('#msg3').html('');
		$('#sendtofriendFormClose').hide();

		$('#sendtofriendForm').each (function(){
  		this.reset();
		});

}

function checkFields (formData, jqForm, options) {	
    for (var i=0; i < formData.length; i++) { 
        if (!formData[i].value) { 
            $('#msg1').html('gelieve alle velden correct in te vullen'); 
            return false; 
        } 
    } 
}

function showResponse (responseText, statusText) {
	if(responseText == 'captcha niet correct') {
		
	}	else if(statusText == 'success'){
		 $('#moreInfoForm').hide();
		 $('#moreInfoFormClose').show();
	}

}


function checkFieldsVisit (formData, jqForm, options) {

	
    for (var i=0; i < formData.length; i++) { 
        if (!formData[i].value) { 
            $('#msg2').html('gelieve alle velden in te vullen'); 
            return false; 
        } 
    } 
}

function checkFieldsSendtofriend(formData, jqForm, options) {


    for (var i=0; i < formData.length; i++) {
        if (!formData[i].value) {
            $('#msg3').html('gelieve alle velden in te vullen');
            return false;
        }
    }
}


function showResponseVisit (responseText, statusText) {
	if(responseText == 'captcha niet correct') {
		
	}	else if(statusText == 'success'){
		 $('#visitForm').hide();
		 $('#visitFormClose').show();
	}
}

function showResponseSendtofriend (responseText, statusText) {
	if(responseText == 'captcha niet correct') {

	}	else if(statusText == 'success'){
		 $('#sendtofriendForm').hide();
		 $('#sendtofriendFormClose').show();
	}
}





//slideshow controls
function startSlideShow(startslide) {
	$('#slideshowPics').cycle({
			fx:     'fade',
			speed:   400,
			timeout: 4000,
			next:   '#nextPic',
			prev:   '#prevPic',
			startingSlide: startslide
   });
   
   $('#fullContainer').animate({
   	marginTop: "230px"
   }, 'slow');
 }
    


/**
 * Usage var you = 'hello you guys'.between('hello ',' guys');
 * you = 'you';
 */

String.prototype.between = function(prefix, suffix) {
s = this;
var i = s.indexOf(prefix);
if (i >= 0) {
s = s.substring(i + prefix.length);
}

else {
return '';
}

if (suffix) {
i = s.indexOf(suffix);
if (i >= 0) {
s = s.substring(0, i);
}

else {
return '';
}

}
return s;

}
