$(document).ready(function(){
	// IE Checkbox fix 
    if ($('html').hasClass('ie')) {
        $('#radiostay input[type="radio"]:checked + label').addClass('checked');
    }
	$('#radiostay input[type="radio"] + label').click(function() {
		if ($('html').hasClass('ie')) {
			if ($(this).prev().is(':checked')) {
                return;
            } else {
                $('#radiostay input[type="radio"] + label').removeClass('checked');
                $(this).prev().attr('checked', 'checked');
                $(this).addClass('checked');
            }
		}
	});

	// Show the tooltip when we over the dog size button	
	$("#radiosize").delegate("label.ui-button","mouseover mouseout", function(event) {
		if (event.type == 'mouseover') {
			var tooltip = '#tooltip-' + $(this).attr("for");
			var pos = $('.ui-button[for="' + $(this).attr("for") + '"]').offset();
			var tooltipleft = pos.left;
			var tooltiptop = pos.top - 67 - $(window).scrollTop();
			$(tooltip).css({"position" : "fixed" , "top" : tooltiptop , "left" : tooltipleft}).show();
		}
		if (event.type == 'mouseout') {
			var tooltip = '#tooltip-' + $(this).attr("for");
			$(tooltip).hide();
		}
	});

	
	// jQueryUI: dog size buttons + calendar (from/to date)
	$(function() {
		$("#radiosize").buttonset();
        //sets min date is today, so no dates in past.
        //on change checks from_datepicker is in future compared to to_datepicker
        var fromdatepicker = $('input[name="fromdate"]').datepicker({
            minDate: 0,
            onSelect: startDateSelectHandler
        });
        var todatepicker = $('input[name="todate"]').datepicker({
            minDate: 0,
            onSelect: endDateSelectHandler
        });
	});

    //JS to modify CSS for getting the front page to work with different browsers/devices.
    var theWindow = $(window);
    var bg = $("#background .dog img");
    var aspectRatio = bg.width() / bg.height();
    
    // Style background based on screen resolution and orientation
    if (typeof window.orientation === 'undefined'){
        // Attach Event Lististeners if window.orientation property is NOT available.
        // This is for regular displays.
        bg.attr('style','left:0;');
        theWindow.resize(function() {
            resizeBg();
        }).trigger("resize");
    } else {
        // Attach Event Lististeners only if window.orientation property is available.
        // This is for hand held devices.
        window.addEventListener('load', setOrientation, false);
        window.addEventListener('orientationchange', setOrientation, false);
    }

    // Function which detects Orientation for hand held devices and applies
    // style for background.
    function setOrientation() {
        var orient = Math.abs(window.orientation) === 90 ? 'landscape' : 'portrait';
        if ( orient === 'portrait'){
            bg.attr('style','right:-340px;min-height: 1230px;');
        } else {
            bg.attr('style','right:-250px;min-height: 980px;');
        }
    }

    // Function which detects height/width of image with screen and applies
    // style for background
    function resizeBg() {
        if ( (theWindow.width() / theWindow.height()) < aspectRatio ) {
            bg.attr('style','height:100%');
        } else {
            bg.attr('style','width:100%');
        }
    }
});


//// Alternative for background image + dogname rotation (slideshow)
//// Source: http://www.alohatechsupport.net/webdesignmaui/maui-web-site-design/easy_jquery_auto_image_rotator.html
//function theRotator() {
//	$('#background .dog').css({opacity: 0.0});
//	$('#background .dog:first').css({opacity: 1.0});
//	setInterval('rotate()', 6000);
//}
//
//function rotate() {
//	var current = ($('#background .dog.show')?  $('#background .dog.show') : $('#background .dog:first'));
//    if ( current.length == 0 ) current = $('#background .dog:first');
//	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('#background .dog:first') :current.next()) : $('#background .dog:first'));
//
//	//Un-comment the 3 lines below to get the images in random order
//	//var sibs = current.siblings();
//        //var rndNum = Math.floor(Math.random() * sibs.length );
//        //var next = $( sibs[ rndNum ] );
//
//	next.css({opacity: 0.0})
//	.addClass('show')
//	.animate({opacity: 1.0}, 1000);
//	current.animate({opacity: 0.0}, 1000)
//	.removeClass('show');
//};
//
//$(document).ready(function() {
//    // dont rotate if we only have one image
//    if ($("#background>div.dog").length > 1) {
//        theRotator();
//        $('#background').fadeIn(1000);
//        $('#background .dog').fadeIn(1000); // tweek for IE
//    }
//});

