var stopAuto = false;
jQuery(document).ready(function() {
	 try{
		jQuery('#mycarousel').jcarousel({
	    	initCallback: mycarousel_initCallback,
	        scroll: 1,
			wrap: 'circular',
			buttonNextHTML: null,
			animation: 'slow',
			buttonPrevHTML: null,
			auto: 5,
			itemVisibleInCallback: updateControl, // update control for next button
			itemLoadCallback: stopCarousel // check whether carousel is step
	    });
	    
	    jQuery('.carousel').jcarousel({
	    	scroll: 1
	    });
	 }catch(e){}
	 
	$("a.lightbox").fancybox({
		'titlePosition'  : 'inside'
	});
});


// stop the carousel
function stopCarousel(carousel){
	if(stopAuto){
		carousel.stopAuto();
	}
	
}

// update control
function updateControl(carousel, li, currentItem){
	
	//set active button	
	activeNumber = currentItem%4
	if(activeNumber%4 == 0){
		activeNumber = 4;
	}
	switchImage(activeNumber);	
}

function mycarousel_initCallback(carousel) {
    jQuery('.jcarousel-control a').bind('click', function() {	
    	stopCarouselNow(carousel);
    	// scroll to item
    	carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
    	switchImage(jQuery(this).text());
    	//stopCarouselNow(carousel);
    	stopAuto = true;
    	return false;
    });
};

function stopCarouselNow(carousel){
	// stop carousel
	carousel.stopAuto();  
	//carousel.animation = 0;
	return false;	
}

function switchImage(activeNumber){
	//alert(activeNumber);		
	$('.jcarousel-control a').removeClass('active');
	$('#control'+activeNumber).addClass('active');
}



var mapType = 'select';
var activeHouseChoiceId = '';
function fillHouseChoice(value,hiddenValue) {
	$('#'+activeHouseChoiceId).val(value);
	$('#'+activeHouseChoiceId+'Hidden').val(hiddenValue);
	hideMap();
	
}

function showMapForHouseChoice(id){
	// set active house choice id
	activeHouseChoiceId = id;
	// Maptype
	mapType = 'select';
	// open map
	showMap();	
}

function getSelectedHousId() {
	return $('#'+activeHouseChoiceId).val();
}

function showMapForKavel() {
	mapType = 'view';
	showMap();
}

function getMapType() {
	return mapType;
}

function getAllSelectedHouseIds(){
	var items=new Array();	
	
	if(activeHouseChoiceId != 'formPreferredType1'){
		items[0]= $('#formPreferredType1Hidden').val();
	}
	if(activeHouseChoiceId != 'formPreferredType2'){
		items[1] = $('#formPreferredType2Hidden').val();
	}
	if(activeHouseChoiceId != 'formPreferredType3'){
		items[2] = $('#formPreferredType3Hidden').val();
	}
	return items;	
}

var activePreference = 'buy';
function switchBuyRent(preference){
	if(preference != activePreference){
		if(preference == 'buy'){			
			$('#buyHouseOptions').show();
			$('#loanHouseOptions').hide();
			
			$('.loanShow').hide();
		}else{
			$('#buyHouseOptions').hide();
			$('#loanHouseOptions').show();
			$('.loanShow').show();
		}
		activePreference = preference;
	}
}
