//functions to be executed on page load
$(function () {

    //Open all links with rel="external" in new window
    //	$(function(){
    //		$('a').filter(function(){
    //			return (!this.target && (this.href.indexOf(window.location.hostname) == -1 || this.href.match(/\.pdf$/i)));
    //		}).attr('target', '_blank');
    //	});


    //Clear value in search input box
    swapValues = [];
    $("#T_EMAIL").each(function (i) {
        swapValues[i] = $(this).val();
        $(this).focus(function () {
            if ($(this).val() == swapValues[i]) {
                $(this).val("");
            }
        }).blur(function () {
            if ($.trim($(this).val()) == "") {
                $(this).val(swapValues[i]);
            }
        });
    });

    $('#print').click(function () {
        window.print();
        return false;
    });

    if ($("#send-friend").length > 0) {
        var currURL = encodeURI(document.location.href);
        $("#send-friend").attr("href", "http://www.beech-hill.com/email_to_friend.aspx?url=" + currURL);
        $("#send-friend").fancybox({
            'width': 340,
            'height': 460,
            'scrolling': 'no',
            'autoScale': false,
            'transitionIn': 'none',
            'transitionOut': 'none',
            'type': 'iframe'
        });
        $("#send-friend").click(function () {
            $("#fancybox-wrap").addClass("mail-box");
        });
    }


});

//functions to be executed inline
var mylib =
{
	DatePicker :
	{
		init : function()
		{
			
			
			
			var months = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
			var months2 = "Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec";
                
                // initialise the "Select date" link
                $('#date-pick')
                    .datePicker(
                        // associate the link with a date picker
                        {
                            createButton:false,
                            endDate:'31/12/2020'
                        }
                    ).bind(
                        // when the link is clicked display the date picker
                        'click',
                        function()
                        {
                            updateSelects($(this).dpGetSelected()[0]);
                            $(this).dpDisplay();
                            return false;
                        }
                    ).bind(
                        // when a date is selected update the SELECTs
                        'dateSelected',
                        function(e, selectedDate, $td, state)
                        {
                            updateSelects(selectedDate);
                        }
                    ).bind(
                        'dpClosed',
                        function(e, selected)
                        {
                            updateSelects(selected[0]);
                        }
                    );
                    
                var updateSelects = function (selectedDate)
                {
                    var selectedDate = new Date(selectedDate);
                    $('#BookingForm1_CheckInDayDropDownList option[value=' + selectedDate.getDate() + ']').attr('selected', 'selected');
                    $('#BookingForm1_CheckInMonthYearDropDownList option[value=' + (months[selectedDate.getMonth()]) + '-' + (selectedDate.getFullYear()) + ']').attr('selected', 'selected');
                }
                // listen for when the selects are changed and update the picker
                $('#BookingForm1_CheckInDayDropDownList, #BookingForm1_CheckInMonthYearDropDownList')
                    .bind(
                        'change',
                        function()
                        {
                            var d = new Date(
                                        $('#BookingForm1_CheckInMonthYearDropDownList').val().split("-")[1],
                                        months2.indexOf($('#BookingForm1_CheckInMonthYearDropDownList').val().split("-")[0]),
                                        $('#BookingForm1_CheckInDayDropDownList').val()
                                    );
                            $('#date-pick').dpSetSelected(d.asString());
							
                        }
                    );
                
                // default the position of the selects to today
                var today = new Date();
                updateSelects(today.getTime());
                
                // and update the datePicker to reflect it...
                $('#BookingForm1_CheckInDayDropDownList').trigger('change');
				
				
		}
	},
	MailingForm:
	{
		init: function(){
			$("#mailing-form").fancybox({
				'width'				: 452,
				'height'			: 315,
				'scrolling'			: 'no',
				'autoScale'			: false,
				'transitionIn'		: 'none',
				'transitionOut'		: 'none',
				'type'				: 'iframe'
			});
			$("#mailing-form").click(function(){
				$("#fancybox-wrap").addClass("mail-box");
			});
		}
	},
	weather:
	{
		init: function(){
			$.simpleWeather({
				location: 'londonderry, united kingdom',
				unit: 'c',
				success: function(weather) {
					html = "<div><strong>Today's Weather</strong><br />";
					html += '<p>'+weather.currently+'<span>&nbsp;</span>'+weather.temp+'&deg;'+weather.units.temp+'</p></div>';
					html += '<img style="float:right;" width="34px" src="/images/weather/'+weather.code+'d.png">';
					
					$("#weather").html(html);
				},
				error: function(error) {
					$("#weather").html('<p>'+error+'</p>');
				}
			});
		}
	}
}
