/**
 * 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;
	});*/
	
	//console.log(carousel);
	$('#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','print'];

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'));
			$('p#caption').hide().html($(this).attr('title')).fadeIn();
			$('#main_image img').fadeIn('normal');
		});
	});
	
	$('a.full-details').click(function(){
		if($('#particulars').is(':hidden')){
			$('a.full-details').html('Hide full particulars &uarr;');
		}
		else{
			$('a.full-details').html('Show full particulars &darr;');
		}
			
		$('#particulars').toggle('slide', { easing: 'easeOutQuint', direction: 'up' }, 1000);
	});
	
	$('#carousel').jcarousel({
							 
		initCallback: mycarousel_initCallback,
		
		buttonNextCallback:   mycarousel_buttonNextCallback,
        buttonPrevCallback:   mycarousel_buttonPrevCallback,

		vertical: true,
		scroll: 3
	});
	
	
	var print_sections = ['overview','photos','particulars','map'];
	var base_print_url = $('#preview').attr('href').replace('#overview','');
	$('.print_option input').change(function(){
		
		var selections = [];									  
		$.each(print_sections, function( intIndex, objValue ){
			if( $('#print_' + objValue).is(':checked') )  selections.push(objValue);
		});
		
		$('#preview').attr('href', base_print_url+'#'+selections);										  
	});
	
	// 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(hash_id=='arrange_viewing'){
		show_div('arrange_viewing');
	}
	else if(hash_id=='send_friend'){
		show_div('send_friend');
	}
	else if (hash_id=='viewing_sent' || hash_id=='enquiry_sent' ){
		$.modaldialog.success('Thank you - you\'ll hear from us soon.', {
		  timeout: 4
		});
	} 
	else if (hash_id=='send_friend_sent'){
		$.modaldialog.success('Thank you - your message has been sent', {
		  timeout: 4
		});
	}

	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 *');
	
	$('input[type=checkbox]').css('border','none');

});