/**
 * We use the initCallback callback
 * to assign functionality to the controls
 */
function mycarousel_initCallback(carousel) {
	/*jQuery('.jcarousel-control a').bind('click', function() {
		carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
		return false;
	});

	jQuery('.jcarousel-scroll select').bind('change', function() {
		carousel.options.scroll = jQuery.jcarousel.intval(this.options[this.selectedIndex].value);
		return false;
	});*/

	$('#carousel_down').bind('click', function() {
		carousel.next();
		return false;
	});

	$('#carousel_up').bind('click', function() {
		carousel.prev();
		return false;
	});
	
	

};


function mycarousel_buttonNextCallback(carousel, button, enabled) {
  //  alert('Next button is now ' + (enabled ? 'enabled' : 'disabled'));
	if(enabled) $('#carousel_down').show();
	else		$('#carousel_down').hide();
};

/**
 * This is the callback function which receives notification
 * about the state of the prev button.
 */
function mycarousel_buttonPrevCallback(carousel, button, enabled) {
   // alert('Prev button is now ' + (enabled ? 'enabled' : 'disabled'));
	if(enabled)		$('#carousel_up').show();
	else			$('#carousel_up').hide();
};


var tabs = ['photos','arrange_viewing','send_friend','mini_map'];

function show_div(div_to_show){
	//alert('s ' + div_to_show);
	$.each(tabs, function( intIndex, objValue ){
		if(objValue!=div_to_show){
			$('#' + objValue + ':visible').hide();
			$('#' + objValue + '_link').parent().removeClass('active');
		}
		else{
			$('#' + objValue + ':hidden').fadeIn('normal');
			$('#' + objValue + '_link').parent().addClass('active');
			
		}
	});


}



$(document).ready(function(){
	
	$('#particulars').hide();
	
	// Show enlarged version of image on thumbnail click
	$('#thumbnails-container ul li img').each(function(){
		$(this).click(function() {
			//console.log($(this));
			$('#main_image img').hide();
			$('#main_image img').attr('src', $(this).attr('src').replace('thumb', 'large'));
			$('#main_image img').fadeIn('normal');
		});
	});
	
	$('.full-details').click(function(){
		if($('#particulars').is(':hidden')){
			$('a.full-details span').addClass('open');
		}
		else{
			$('a.full-details span').removeClass('open');
		}
			
		$('#particulars').toggle('slide', { easing: 'easeOutQuint', direction: 'up' }, 1000);
	});
	
	
	$('#carousel').jcarousel({
		initCallback: mycarousel_initCallback,
		//itemFirstInCallback:  mycarousel_itemFirstInCallback,
	
		buttonNextCallback:   mycarousel_buttonNextCallback,
        buttonPrevCallback:   mycarousel_buttonPrevCallback,

		vertical: true,
		scroll: 3
		// This tells jCarousel NOT to autobuild prev/next buttons
		//buttonNextHTML: null,
		//buttonPrevHTML: null
		
	});
	
	// Tab switcher
	$.each(tabs, function( intIndex, objValue ){
		//console.log($('#'+objValue));
		$('#' + objValue + '_link').click(function(){
			//console.log($(this));
			show_div(objValue);						   
		});
	});
	
	var hash_id = document.location.toString().split('#')[1];
	//if(typeof hash_id == 'undefined') show_div('photos');
	if(hash_id=='arrange_viewing'){
		show_div('arrange_viewing');
	}
	else if(hash_id=='send_friend'){
		show_div('send_friend');
	}
	
	auto_fill($('input[name=name]'), 'Your name *');
	auto_fill($('input[name=email]'), 'Your e-mail address *');
	auto_fill($('input[name=phone]'), 'Your phone number *');
	auto_fill($('textarea[name=message]'), 'Optional message');
	
	auto_fill($('input[name=friend_name]'), 'Friend\'s name *');
	auto_fill($('input[name=friend_email]'), 'Friend\'s e-mail address *');
	
	
});