/*
 * jQuery NavDropDown v1.0.0 
 *
 * Copyright (c) 2008 Taranets Aleksey
 * www: markup-javascript.com
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 */

jQuery.fn.NavDropDown=function(j){
	var j=jQuery.extend({
		duration:500,
		hoverElement:'li',
		dropHolder:'div',
		hoverClass:'hover',
		showEffect:'slide'}
	,j);
	return this.each(function(){var f=jQuery(this);
		var g=jQuery(j.hoverElement+':has('+j.dropHolder+')',f);
		var h=j.duration;
		g.each(function(i,a){
			a=jQuery(a);
			var b=$(j.dropHolder+' :first',a);
			var c=$(j.dropHolder,a);
			a.h=b.outerHeight();
			var d={};
			var e={};
			if(j.showEffect=='slide'){
				c.css({'height':0,'overflow':'hidden'});
				b.css({'marginTop':-a.h,'overflow':'hidden'});
				d.SE={height:a.h};
				d.ME={marginTop:0};
				e.SE={height:0};
				e.ME={marginTop:-a.h}}
				if(j.showEffect=='fade'){
					c.css({'opacity':0});
					d.SE={opacity:1};
					e.SE={opacity:0};
					d.ME={};
					e.ME={}
				}
				if(j.showEffect=='slide&fade'){
					c.css({'height':0,'overflow':'hidden','opacity':0});
					b.css({'marginTop':-a.h,'overflow':'hidden'});
					d.SE={height:a.h,opacity:1};
					d.ME={marginTop:0};
					e.SE={height:0,opacity:0};
					e.ME={marginTop:-a.h}
				}
				a.hoverEl=false;
				a.hover(function(){
					if(this.timer)clearTimeout(this.timer);
					a.hoverEl=true;
					$(this).addClass(j.hoverClass);
					c.animate(d.SE,{queue:false,duration:h, complete:function(){
						if ($.browser.msie && $.browser.version=="6.0") {
							$("#sidebar").find('select').css({'visibility':'hidden'});
						}
					}});
					b.animate(d.ME,{queue:false,duration:h})
				},function(){
					this.timer=setTimeout(function(){
						a.hoverEl=false;
						b.animate(e.ME,{queue:false,duration:h});
						c.animate(e.SE,{queue:false,duration:h,complete:function(){
							if(!a.hoverEl)$(a).removeClass(j.hoverClass)
							if ($.browser.msie && $.browser.version=="6.0") {
								$("#sidebar").find('select').css({'visibility':'visible'});
							}

						}})
					},100)}
				)
		})
	})
};
$(document).ready(function(){
	$('ul#MenuLeft').NavDropDown();
	$('div#menu2 > ul').NavDropDown();
});

//set external links
$(document).ready(function() {
    $('a[rel = external]').attr('target', 'blank');
});

//correct, margin when video is disabled via WMS
/*$(document).ready(function() {
    $('#BannerContent-NoFlash').parent().attr('id', 'BannerHolder-NoFlash');
});*/

//set first link of sub menu
$(document).ready(function() {
    $('#sub-menu ul li:first').attr('class', 'first');
});
//set last link of sub menu
$(document).ready(function() {
    $('#sub-menu ul li:last').attr('class', 'last');
});

//redundant - used in first version of booking form usercontrol
$(document).ready(function() {
    $('#ctl00_SideBar_bookingForm_availcheck').click(function() {
        if ($('#ctl00_SideBar_bookingForm_SITEID')[0].selectedIndex == "0") {
            alert("Please choose a property.");
            return false;
        }
    });
});

//script for date picker
$(document).ready(function() {
    var date = new Date();
    startYr = date.getFullYear();
    endYr = date.getFullYear() + 1;
    $(function() {
        // initialise the "Select date" link
        $('#date-pick').datePicker(
        // associate the link with a date picker
			            {
			            createButton: false,
			            startDate: '01/01/' + startYr,
			            endDate: '31/12/' + endYr
			        }
		                ).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);
            $('#ctl00_SideBar_bookingForm_fd option[value=' + selectedDate.getDate() + ']').attr('selected', 'selected');
            $('#ctl00_SideBar_bookingForm_fm option[value=' + (selectedDate.getMonth() + 1) + ']').attr('selected', 'selected');
            $('#ctl00_SideBar_bookingForm_fy option[value=' + (selectedDate.getFullYear()) + ']').attr('selected', 'selected');
        }
        // listen for when the selects are changed and update the picker
        $('#ctl00_SideBar_bookingForm_fd, #ctl00_SideBar_bookingForm_fm, #ctl00_SideBar_bookingForm_fy')
		                .bind(
			                'change',
			                function() {
			                    var d = new Date(
							                $('#ctl00_SideBar_bookingForm_fy').val(),
							                $('#ctl00_SideBar_bookingForm_fm').val() - 1,
							                $('#ctl00_SideBar_bookingForm_fd').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...
        $('#ctl00_SideBar_bookingForm_fd').trigger('change');
    });
});