$(document).ready(function(){
	$("body").addClass("Active");
	ActivateTabs();
	/*DatePicker();*/
	FadeImages();
	ClickablePanels();
	ClickableOffers();
	RotatingHeader();
	SpecialOffers();
	RotatingBody();
	ImageGallery();
	BingMap();
	Twitter();
	BookingSystem();	
});



/* ----- Clickable Panels ----- */

function ClickablePanels(){
	$('.Panel1, .Panel2, .Panel3, .Panel4, .Panel5, .Panel6, .Panel7, .Panel, .HomePanel .MainAreaWrap .Enhance2ContentElement1, .HomePanel .MainAreaWrap .Enhance2ContentElement2, .HomePanel .MainAreaWrap .Enhance2ContentElement3, .HomePanel .MainAreaWrap .Enhance2ContentElement4, .HomePanel .MainAreaWrap .Enhance2ContentElement5, .HomePanel .MainAreaWrap .Enhance2ContentElement6, .HomePanel .MainAreaWrap .Enhance2ContentElement7, .HomePanel .MainAreaWrap .Enhance2ContentElement8').each(function(){
		MakeClickable($(this));
	});
}

function MakeClickable(element){
	var count = 0;
	element.find('h3 a').each(function(){
		count++;
	});
	if(count == 1){
		var panelurl = element.find('h3 a').attr('href');
		if(panelurl){
			element.css({ 'cursor' : 'pointer' });
			element.attr("onclick", "window.location = '" + panelurl + "'");
			/*element.click(function(){
				window.location = panelurl;		
			});*/
		}
	}
}



/* ----- Clickable Offers ----- */

function ClickableOffers(){
	$('.SpecialOffers table td, .SpecialTest table td').each(function(){
		MakeOfferClickable($(this));
	});
}

function MakeOfferClickable(element){
	var count = 0;
	element.find('a').each(function(){
		count++;
	});
	if(count == 1){
		var panelurl = element.find('a').attr('href');
		if(panelurl){
			element.css({ 'cursor' : 'pointer' });
			element.attr("onclick", "window.location = '" + panelurl + "'");
			/*element.click(function(){
				window.location = panelurl;
			});*/
		}
	}
}



/* ----- Fading Header ----- */

function FadeImages(){
	if($('.PortfolioPanel').length == 0){
		$('.Heading').cycle({ 
			fx: 'fade', 
			speed: 2500,
			pause: true,
			slideExpr: 'img'
		 });
	}
}



/* ----- Rotating Header ----- */

function RotatingHeader(){
	if($('.PortfolioPanel').length > 0){
		/* Begin Rotating Header for home page */
		//$('.HomePanel .Heading > .Enhance2ContentElement1 > .EnhanceContentElementWrap > div').wrapInner('<div class="PortfolioPanel"></div>');
		$('.PortfolioPanel').append('<div id="PortfolioNav"></div>');
		$('#PortfolioNav').append('<a id="Prev" href="#">&nbsp;</a>');
		$('#PortfolioNav').append('<div id="PortfolioNavContent"></div>');
		$('#PortfolioNav').append('<a id="Next" href="#">&nbsp;</a>');
		//var count = 1;
		$('.PortfolioPanel p').each(function(){
			if($(this).find('img').length > 0){
				var image = $(this);
				var slide = image;
				var header = $(this).next('.PortfolioBody');
				slide = slide.add(header);
				slide.wrapAll('<div class="Slide"></div>');
			}
		});
		
		$('.PortfolioPanel').cycle({ 
			fx: 'scrollHorz', 
			speed: 5000,
			timeout: 14000,
			pause: true,
			slideExpr: '.Slide',
			next: '#Next', 
			prev: '#Prev',
			pager: '#PortfolioNavContent'
		 });
		/* End Rotating Header for home page */
	}
}


/* ----- Rotating Body ----- */

function RotatingBody() {
	if($('.Home1').length > 0 || $('.SpecialOffers, .SpecialTest').length > 0){

		var arVersion = navigator.appVersion.split("MSIE")
		var version = parseFloat(arVersion[1])
		if ((version >= 5.5) && (version < 7.0) && (document.body.filters)) 
		{
		}else{
			$('.Panel1, .Panel2, .Panel3, .Panel4, .Panel5, .Panel6, .Panel7, .Panel, .HomePanel .MainAreaWrap .Enhance2ContentElement1, .HomePanel .MainAreaWrap .Enhance2ContentElement2, .HomePanel .MainAreaWrap .Enhance2ContentElement3, .HomePanel .MainAreaWrap .Enhance2ContentElement4, .HomePanel .MainAreaWrap .Enhance2ContentElement5, .HomePanel .MainAreaWrap .Enhance2ContentElement6, .HomePanel .MainAreaWrap .Enhance2ContentElement7, .HomePanel .MainAreaWrap .Enhance2ContentElement8, .SpecialPrices1, .SpecialPrices2, .SpecialPrices3, .SpecialPrices4, .SpecialPrices5, .SpecialPrices6, .SpecialPrices7, .SpecialPrices8, .SpecialPrices9, .SpecialPrices10, .SpecialPrices11, .SpecialPrices12').wrapAll('<ul></ul>').wrap('<li></li>');
			DatePickerDestroy();
			ReadJCList();
			$('.Home1 .MainAreaWrap > div > div > div > ul').jcarousel({
				scroll: 1,
				visible: 4,
				initCallback: mycarousel_initCallback,
				wrap: 'circular',
				itemVisibleInCallback: {onBeforeAnimation: mycarousel_itemVisibleInCallback},
				itemVisibleOutCallback: {onAfterAnimation: mycarousel_itemVisibleOutCallback}
			});
			$('.Home1 .MainAreaWrap > ul').jcarousel({
				scroll: 1,
				visible: 4,
				initCallback: mycarousel_initCallback,
				wrap: 'circular',
				itemVisibleInCallback: {onBeforeAnimation: mycarousel_itemVisibleInCallback},
				itemVisibleOutCallback: {onAfterAnimation: mycarousel_itemVisibleOutCallback}
			});
			$('.SpecialOffers .MainAreaWrap > .MainContent > div > div > div > ul').jcarousel({
				scroll: 1,
				visible: 1,
				initCallback: mycarousel_initCallback,
				wrap: 'circular',
				itemVisibleInCallback: {onBeforeAnimation: mycarousel_itemVisibleInCallback},
				itemVisibleOutCallback: {onAfterAnimation: mycarousel_itemVisibleOutCallback}
			});
			$('.SpecialTest .MainAreaWrap > .MainContent > div > div > div > ul').jcarousel({
				scroll: 1,
				visible: 1,
				initCallback: mycarousel_initCallback,
				wrap: 'circular',
				itemVisibleInCallback: {onBeforeAnimation: mycarousel_itemVisibleInCallback},
				itemVisibleOutCallback: {onAfterAnimation: mycarousel_itemVisibleOutCallback}
			});
			$('.MainAreaWrap').css({'overflow': 'visible'});
		}
		
		$(window).resize(function() {
			$('.MainAreaWrap ul').css({'left': '0px'});
		});
		
		$('.Heading a').click(function(){
			var href = $(this).attr('href');
			if(href.indexOf("#") == 0){
				ScrollToMonth(href);
				return false;	
			}
		});
		
		DatePicker();
	}
}

var mycarousel_itemList = new Array();
var monthlist = new Array();
var monthcount;
var mycarousel;
ReadJCList = function() {
	if($('.Home1').length > 0){
		var JCList;
		if ($('.MainAreaWrap > div > div > div > ul > li')!=null){
			JCList=$('.MainAreaWrap > div > div > div > ul > li');
			for (var i = 0; i < JCList.length; i++) {
				mycarousel_itemList[i]=JCList[i].innerHTML;
			}
		}
		if ($('.MainAreaWrap > ul > li')!=null){
			JCList=$('.MainAreaWrap > ul > li');
			for (var i = 0; i < JCList.length; i++) {
				mycarousel_itemList[i]=JCList[i].innerHTML;
			}
		}
	}
	if($('.SpecialOffers, .SpecialTest').length > 0){
		var JCList;
		if ($('.MainAreaWrap > .MainContent > div > div > div > ul > li')!=null){
			JCList=$('.MainAreaWrap > .MainContent > div > div > div > ul > li');
			for (var i = 0; i < JCList.length; i++) {
				mycarousel_itemList[i]=JCList[i].innerHTML;
				monthlist[i]=$(JCList[i]).find('h2').text();
			}
			monthcount = JCList.length;
		}
	}
}

function mycarousel_itemVisibleInCallback(carousel, item, i, state, evt)
{
    // The index() method calculates the index from a
    // given index who is out of the actual item range.
    var idx = carousel.index(i, mycarousel_itemList.length);
    carousel.add(i, mycarousel_itemList[idx - 1]);

	if(typeof pngFix == 'function') {
		pngFix();
	}
	
	DatePickerDestroy();
	DatePicker();
};

function mycarousel_itemVisibleOutCallback(carousel, item, i, state, evt)
{
    carousel.remove(i);
    carousel.startAuto();
};

function mycarousel_initCallback(carousel)
{
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });			   
	mycarousel = carousel;
};


function SpecialOffers(){
	if($('.SpecialOffers, .SpecialTest').length > 0){
		$('.MainContent td').each(function(){
			var count = 1;
			$(this).find('p').each(function(){
				$(this).addClass('Paragraph'+count);				
				count++;
			});
		});
	}
}

function ScrollToMonth(url){
	var hash = url.substr(1).replace('-',' ');
	var targetid;
	for(var i = 0; i < monthlist.length; i++){
		if(hash.toLowerCase() == monthlist[i].toLowerCase()){
			targetid = (i + 1);
		}
	}
	if(targetid != null){
		mycarousel.scroll(targetid);
	}
}




function SitemapCols(){
	$('.Home1 .FooterWrap .Enhance2ContentElement3 .Enhance2MenuWrap > ul').makeacolumnlists({cols:5,colWidth:189,equalHeight:false,startN:1});
}



/* ----- Tabs ----- */

function ActivateTabs(){
	$(".Tab1 .TabHeading").click(function(){
		if($(".Tab1 .TabContent").height() == 1){
        	$(".Tab1 .TabContent").animate({ height: "350px" }, 1500 );
			collapse($(".Tab2 .TabContent"));
			collapse($(".Tab3 .TabContent"));
			collapse($(".Tab4 .TabContent"));
	  	}
	  	if($(".Tab1 .TabContent").height() == 350){
        	$(".Tab1 .TabContent").animate({ height: "1px" }, 1500 );
	  	}
	});
	
	$(".Tab2 .TabHeading").click(function(){
		if($(".Tab2 .TabContent").height() == 1){
        	$(".Tab2 .TabContent").animate({ height: "350px" }, 1500 );
			collapse($(".Tab1 .TabContent"));
			collapse($(".Tab3 .TabContent"));
			collapse($(".Tab4 .TabContent"));
	  	}
	  	if($(".Tab2 .TabContent").height() == 350){
        	$(".Tab2 .TabContent").animate({ height: "1px" }, 1500 );
	  	}
	});
	
	$(".Tab3 .TabHeading").click(function(){
		if($(".Tab3 .TabContent").height() == 1){
        	$(".Tab3 .TabContent").animate({ height: "350px" }, 1500 );
			collapse($(".Tab1 .TabContent"));
			collapse($(".Tab2 .TabContent"));
			collapse($(".Tab4 .TabContent"));
	  	}
	  	if($(".Tab3 .TabContent").height() == 350){
        	$(".Tab3 .TabContent").animate({ height: "1px" }, 1500 );
	  	}
	});
	
	$(".Tab4 .TabHeading").click(function(){
		if($(".Tab4 .TabContent").height() == 1){
        	$(".Tab4 .TabContent").animate({ height: "350px" }, 1500 );
			collapse($(".Tab1 .TabContent"));
			collapse($(".Tab2 .TabContent"));
			collapse($(".Tab3 .TabContent"));
	  	}
	  	if($(".Tab4 .TabContent").height() == 350){
        	$(".Tab4 .TabContent").animate({ height: "1px" }, 1500 );
	  	}
	});
}

function collapse(element){
	if(element.height() > 10){
		element.animate({ height: "1px" }, 1500 );
	}
}



/* ----- Date Picker ----- */

function DatePicker(){
	//$('#ArrivalDate').datepicker( 'option' , 'dateFormat' , 'dd/mm/yy' );
	//$('#ArrivalDate').datepicker( 'option' , 'showOn' , 'button' );
	//$('#ArrivalDate').datepicker( 'option' , 'buttonImage' , '/skin/images/calendar-icon.gif' );
	//$('#ArrivalDate').datepicker( 'option' , 'buttonImageOnly' , 'true' );
	if(window.location == 'http://www.hermitage-hotel.co.uk/Default.aspx?pagename=Hermitage-Hotel-bournemouth-in-dorset'){	
		if($('.Enhance2BrilliantAvailabilityFeedWrap #ArrivalDate').length > 0){
			$('.Enhance2BrilliantAvailabilityFeedWrap #ArrivalDate').datepicker({
				dateFormat: 'dd/mm/yy',
				showOn: 'button',
				buttonImage: '/skin/images/calendar-icon.gif',
				buttonImageOnly: true
			});
		}
	}
}

function DatePickerDestroy(){
	if(window.location == 'http://www.hermitage-hotel.co.uk/Default.aspx?pagename=Hermitage-Hotel-bournemouth-in-dorset'){	
		if($('.Enhance2BrilliantAvailabilityFeedWrap #ArrivalDate').length > 0){
			$('.Enhance2BrilliantAvailabilityFeedWrap #ArrivalDate').datepicker('destroy');
		}
	}
}




/* ----- Image Gallery ----- */
function ImageGallery(){
	if($('.Gallery').length > 0){
		$('.Gallery .MainContent ul li:first').addClass("active");
		$('.Gallery .MainContent ul').galleria();
	}
}




/* ----- Bing Map ----- */
var map = null;
var hermitageMap = null;
var birdsEyeLocation = null;
var pinLocation = null;

function BingMap()
{
	if($('#BingMap').length > 0){
		hermitageMap = new VELatLong(50.71636969271924, -1.876437469676896);
		birdsEyeLocation = new VELatLong(50.71636969271924, -1.876437469676896); 
		pinLocation = new VELatLong(50.717179012377464, -1.8770340085029606);
		
		map = new VEMap('BingMap');
		map.LoadMap(hermitageMap, 15);
		map.SetMapStyle(VEMapStyle.Oblique);
		
		// Pushpin
		var shape = new VEShape(VEShapeType.Pushpin, pinLocation);
		shape.SetTitle("<h2>Hermitage Hotel</h2>");
		shape.SetDescription("<p>This is the location of the hotel</p>");
		map.AddShape(shape);
		
		// Polygon
		var shape = new VEShape(VEShapeType.Polygon, [new VELatLong(50.71761206093467, -1.8767791986465547),
												   new VELatLong(50.71690899184001, -1.8766719102859535),
												   new VELatLong(50.71681728639792, -1.8769884109497177),
												   new VELatLong(50.71697352519169, -1.8775033950805684),
												   new VELatLong(50.71746601363314, -1.8771761655807608)
												   ]);
		shape.SetTitle('<h1>Hermitage Hotel</h1>');
		shape.SetDescription('<p>This is the location of the hotel</p>');
		shape.SetFillColor(new VEColor(255,255,255,0.0));
		shape.SetLineColor(new VEColor(255,255,255,1.0));
		
		// map.AddShape(shape);
		
		// Let me know if a birdseye scene is available
		map.AttachEvent("onobliqueenter", OnObliqueEnterHandler);
	}
}

function OnObliqueEnterHandler()
{
	if(map.IsBirdseyeAvailable())
	{
	   map.SetBirdseyeOrientation(VEOrientation.West);
	   map.SetBirdseyeScene(birdsEyeLocation);
	}
}




/* ----- Twitter ----- */

function Twitter()
{
	$(".TwitterWidget").getTwitter({
		userName: "TheHermitageUK",
		numTweets: 1,
		loaderText: "Loading tweets...",
		slideIn: false,
		slideDuration: 750,
		showHeading: true,
		headingText: "<a href='http://www.twitter.com/TheHermitageUK'>Latest Tweets</a>",
		showProfileLink: false,
		showTimestamp: true
	});
}


/* ----- Booking System ----- */

function BookingSystem(){
	$('.Enhance2BrilliantChildrenOptionsWrap *').css({'display': 'none'});
	$('.Enhance2BrilliantChildrenOptionsWrap').append('<p>If you are bringing children please call the hotel in advance on 01202 557363</p>');
	
	$('.Enhance2BrilliantSummaryDetailsWrap .Enhance2BrilliantSummaryItem:last').css({'display': 'none'});
}