window.addEvent('domready', function(){									 
	// Tabs
	var tabs = $$('.detailTab','.detailTabCurrent');
	
	tabs.each(function(tab){
		tab.addEvent("click", function(event) {
			$('detail'+$$('.detailTabCurrent').get('html')).setStyle('display','none');
			$$('.detailTabCurrent').set('class','detailTab');
			$('detail'+tab.get('html')).setStyle('display','block');
			tab.set('class','detailTabCurrent');
			// Show/hide the fancy uploader
			if($('editImageUpload')){
				if(tab.get('html') == 'Photos'){
					$('editImageUpload').setStyle('visibility','visible');
				}else{
					$('editImageUpload').setStyle('visibility','hidden');
				}
			}
			// Show/hide the google map
			if($('detailGoogleMap')){
				if(tab.get('html') == 'Map'){
					$('detailGoogleMap').setStyle('visibility','visible');
				}else{
					$('detailGoogleMap').setStyle('visibility','hidden');
				}
			}
		});
	});
	
	// Photos
	var photos = $$('.detailImageThumb');
	
	photos.each(function(photo){
		photo.addEvent("click", function(event) {
			$('detailMainImage').setStyle('background-image',photo.getStyle('background-image').replace('thumbs/',''));
		});
	});
	
	// Google map
	if($('detailGoogleMap')){
		showGooglemap();
	}
	
	// Make a bid button
	if($('detailBidButton')){
		$('detailBidButton').addEvent("click", function(event) {
			if($$('.detailTabCurrent').get('html') != 'Availability'){
				$('detail'+$$('.detailTabCurrent').get('html')).setStyle('display','none');
				$$('.detailTabCurrent').set('class','detailTab');
				$('detailAvailability').setStyle('display','block');
				$('detailAvailabilityTab').set('class','detailTabCurrent');
	
				if($('detailGoogleMap')){
					$('detailGoogleMap').setStyle('visibility','hidden');
				}
				
				showBidHint();
			}else{
				confirmAuctions($('calListingID').value, $('calMonth').value);
			}
		});	
	}
});


// Highlights the auctions on hover
function highlightAuction(auctionID){
	var auctionDays = $$('.auction'+auctionID);
	
	auctionDays.each(function(auctionDay){
		if(auctionDay.hasClass('dayAuction')){
			auctionDay.removeClass('dayAuction');
			auctionDay.addClass('dayAuctionHover');
		}else{
			if(auctionDay.hasClass('dayFixedPrice')){
				auctionDay.removeClass('dayFixedPrice');
				auctionDay.addClass('dayFixedPriceHover');
			}else{
				if(auctionDay.hasClass('dayBooked')){
					auctionDay.removeClass('dayBooked');
					auctionDay.addClass('dayBookedHover');
				}
			}
		}
	});
}

// Un-Highlights the auctions on hover
function unhighlightAuction(auctionID){
	var auctionDays = $$('.auction'+auctionID);
	
	auctionDays.each(function(auctionDay){
		if(auctionDay.hasClass('dayAuctionHover')){
			auctionDay.removeClass('dayAuctionHover');
			auctionDay.addClass('dayAuction');
		}else{
			if(auctionDay.hasClass('dayFixedPriceHover')){
				auctionDay.removeClass('dayFixedPriceHover');
				auctionDay.addClass('dayFixedPrice');
			}else{
				if(auctionDay.hasClass('dayBookedHover')){
					auctionDay.removeClass('dayBookedHover');
					auctionDay.addClass('dayBooked');
				}
			}
		}
	});
}

//hides the card and fades the overlay out
function closeEvent(){
	var overlay = $('eventsOverlay');
	var card = $('eventsCard');
	
	overlay.setStyle('opacity',.3);
	var fade = new Fx.Morph(overlay, {duration: 200, transition: Fx.Transitions.Sine.easeOut}).start({'opacity':0});
	card.setStyle('display','none');
}


// show the current event card
function showAuction(auctionID){
	var overlay = $('eventsOverlay');
	var card = $('eventsCard');
	var info = $('eventsInfo');
	
	overlay.setStyle('display','block');
	card.setStyle('display','block');
	
	if(overlay.getStyle('opacity') != .3){
		overlay.setStyle('opacity',0);
		var fade = new Fx.Morph(overlay, {duration: 200, transition: Fx.Transitions.Sine.easeOut}).start({'opacity':.3});
	}
	
	info.set('text','Getting Details...');
	
	centerEventDialog();

	var changeCalendar = new Request({
		method: 'get', 
		url: 'components/com_room4rent_listings/ajax/listing_details.php',
		data: {
			'action':'getAuctionDetails',
			'auctionID':auctionID
		},
		
		onSuccess: function(response){	
			info.set('html',response);
			centerEventDialog();
			setTimeout(centerEventDialog,100);
			setTimeout(centerEventDialog,200);
			setTimeout(centerEventDialog,500);
			setTimeout(centerEventDialog,1000);
			setTimeout(centerEventDialog,1500);
			setTimeout(centerEventDialog,2000);
			setTimeout(centerEventDialog,2500);
			setTimeout(centerEventDialog,5000);
		}
	}).send();
	
}


//centers the dialog
function centerEventDialog(){
	var card = $('eventsCard');
	var win = window.getCoordinates();
	var car = card.getCoordinates();
	
	card.setStyle('top', (win.height / 2) - (car.height / 2));
	card.setStyle('left', (win.width / 2) - (car.width / 2));	
}


// switch the current month we are viewing
function eventsCalendarChangeMonth(date, listingID){
	
	if(date == null){
		date = $('calMonth').value;
		listingID = $('calListingID').value;
	}else{
		$('calMonth').value = date;
	}
	
	var calendar = $("eventsCalendar_"+listingID);
	var overlay = $("eventsCalendarOverlay_"+listingID);
	
	overlay.set('style','display:block');
	
	var selAuct = "";
	if($('selectedAuctions')){
		selAuct = $('selectedAuctions').value;
	}
	
	var changeCalendar = new Request({
		method: 'get', 
		url: 'components/com_room4rent_listings/ajax/listing_details.php',
		data: {
			'action':'changeDate',
			'listingID':listingID,
			'date':date,
			'selectedAuctions':selAuct
		},
		
		onSuccess: function(response){	
			calendar.set('html',response);
			overlay.set('style','display:none');
			
			/*var eventTips = new Tips('.eventLink', {
				className: 'eventTip',
				text: 'title'
			});*/
			
		}
	}).send();
}