$(document).ready(function() {
						   
    initCalendarNavigation();
	createSlideshow();
	createLightboxes();

	/* Pause/resume on hover */
	$("#slideImage").hover(
		function () {
			slidePause();
		}, 
		function () {
		 slideResume();
		}
	);
	
})


function initCalendarNavigation() {
    $('.monthnav').click(function() {
		$('#calendars').fadeOut(10, function() {
        	$('#availabilities').html('<div id="calsLoading"></div>');
			$('#calsLoading').fadeIn(100);
		});
        var date = $(this).attr('name').split('-');
        var year = parseInt(date[1]);
        var month = parseInt(date[2], 10);
        $('#availabilities').load(
            liveSite+'index.php?option=com_rental&task=get_calendars',
            {pid: date[0], year: year, month: month},
            function() {
            	var prevmonth = month-1==0 ? 12 : month-1;
                var prevyear = month-1==0 ? year-1 : year;
                var nextmonth = month+1==13 ? 1 : month+1;
                var nextyear = month+1==13 ? year+1 : year;
                $('.prev_month').attr('name', date[0]+'-'+prevyear+'-'+prevmonth);
                $('.next_month').attr('name', date[0]+'-'+nextyear+'-'+nextmonth);
                $('.prev_year').attr('href', $('.prev_year').attr('href').replace(/year=(.\d+)&/i, 'year='+(year-1)+'&'));
                $('.next_year').attr('href', $('.next_year').attr('href').replace(/year=(.\d+)&/i, 'year='+(year+1)+'&'));
                /*
                $('#periodSelect > option').each(function() {
                    if ($(this).attr('value')) {}
                    attr('selected', 'selected');
                });
                */
				$('#calendars').fadeIn(100);
            }
        );
    });
    $('#periodSelect').bind('change', function() {
		$('#calendars').fadeOut(10, function() {
        	$('#availabilities').html('<div id="calsLoading"></div>');
			$('#calsLoading').fadeIn(100);
		});
        var date = $(this).val().split('-');
        var year = parseInt(date[1], 10);
        var month = parseInt(date[2], 10);
        $('#availabilities').load(
            liveSite+'index.php?option=com_rental&task=get_calendars',
            {pid: date[0], year: year, month: month},
            function() {
            	var prevmonth = month-1==0 ? 12 : month-1;
                var prevyear = month-1==0 ? year-1 : year;
                var nextmonth = month+1==13 ? 1 : month+1;
                var nextyear = month+1==13 ? year+1 : year;
                $('.prev_month').attr('name', date[0]+'-'+prevyear+'-'+prevmonth);
                $('.next_month').attr('name', date[0]+'-'+nextyear+'-'+nextmonth);
                $('.prev_year').attr('href', $('.prev_year').attr('href').replace(/year=(.\d+)&/i, 'year='+(year-1)+'&'));
                $('.next_year').attr('href', $('.next_year').attr('href').replace(/year=(.\d+)&/i, 'year='+(year+1)+'&'));
                $('#calendars').fadeIn(600);
            }
        );
    });
}



function slidePause() {

	$('#slideImage').cycle('pause');

	$('#slideStatus').fadeTo(200,0.75);

}



function slideResume() {

	$('#slideImage').cycle('resume');

	$('#slideStatus').fadeTo(200,0.0);

}





/* Create Slideshow */

function createSlideshow() {

	$('#slideImage').cycle({

			prev:   '#slidePrev', 

			next:   '#slideNext',

			speed: 400

	});

	/* Unhide the images after creating slideshow */

	$('#slideImage img').css('display', 'block');

}

		

/* Create Lightboxes */

function createLightboxes() {

	$('#slideImage a').lightBox({

		imageLoading: liveSite+'templates/casasol/images/lightbox/loading.gif',

		imageBtnClose: liveSite+'templates/casasol/images/lightbox/close.gif',

		imageBtnPrev: liveSite+'templates/casasol/images/lightbox/prev.gif',

		imageBtnNext: liveSite+'templates/casasol/images/lightbox/next.gif'

	});

}



