$(document).ready(function() {
	
	$('.addHasLayout').css({zoom: '1'});
	
	// Open page in new window
	$('.openNewWindow').click(function() { 
		window.open(this.href, 'new_window');
		return false;
	});
	
	// HOMEPAGE
	$('#offers_list').css({overflow: 'hidden'});
	
	$('#offers_list td').hover(
		function() { 
			var $parentTag = $(this).parent();
			if($parentTag.hasClass('row_top')) { var $otherParentTag = $parentTag.next(); }
			else { var $otherParentTag = $parentTag.prev(); }
			$parentTag.children('td').css({backgroundColor: '#ececda'});
			$otherParentTag.children('td').css({backgroundColor: '#ececda'}); 
		
		},
		function() { $('#offers_list td').css({backgroundColor: 'transparent'}); }
	);
	
	$('#offers_pagination a').click(function() {
		
		if($(this).parent().hasClass('more')) { 
			window.location.href = $(this).attr('href'); 
			return false;
		}

		var pageNumber = Number($(this).attr('href'));
		var startRow = (pageNumber * 4) - 3;
	
		$('#offers_list tr').hide();
		
		for (i = startRow; i <= (startRow + 3); i ++) {
			$('#offerRow'+i+'_top').show().next().show();
		}

		return false;
	});
	
	$('#destinations_list').hide();
	
	$('#open_destinations_list').click(function() {
		$('#destinations_list').slideToggle();	
		return false;
	});

	// NEWS AND OFFERS
	$('#news_articles .button_middle, #offers_holder .details').hide();
	
	$('#news_articles .link').click(function() { 
		var $parentTag = $(this).parent();

		if($parentTag.hasClass('sliderOpen')) {
			$('.sliderOpen .open').html('open +');
			$('.sliderOpen .button_middle').slideUp();
			$('.sliderOpen').removeClass('sliderOpen');
		}
		else {
			$('.sliderOpen .open').html('open +');
			$('.sliderOpen .button_middle').slideUp();
			$('.sliderOpen').removeClass('sliderOpen');
			
			$(this).children('.open').html('close +');
			$parentTag.addClass('sliderOpen').children('.button_middle').slideDown();
		}
		
		return false;
	});
	
	$('#offers_holder .offer_button').click(function () {
		var parentTag = $(this).parent().get(0).id;
		doOffersTransition(parentTag);
		return false;
	});
	
	$('#offers_holder .details .closeLink').click(function () {
		var parentTag = $(this).parent().parent().parent().get(0).id;
		doOffersTransition(parentTag);
		return false;
	});
	
	// Popup up date seletor (calendar)
	var dateSelectorOptions = {
		minDate: 1, 
		maxDate: 365,
		dateFormat: "dd/mm/yy",
    	showOn: "both", 
		speed: 'fast', 
		buttonImage: base_url+"public/images/calendars/white.png", 
    	buttonImageOnly: true
	}
	
	if(selectedController == 'fly') {
			dateSelectorOptions.maxDate = 730;
	}
	else if(selectedController == 'rooms') {
			dateSelectorOptions.maxDate = 730;
		}
	
	
	
	if($.browser.msie && ($.browser.version == 6.0)) { 
		if(selectedController == 'taxis') dateSelectorOptions.buttonImage = base_url+"public/images/calendars/white-on-green.jpg"; 
		else if(selectedController == 'fly') {
			dateSelectorOptions.buttonImage = base_url+"public/images/calendars/white-on-purple.jpg"; 
		}
		else if(selectedController == 'rooms') {
			dateSelectorOptions.buttonImage = base_url+"public/images/calendars/white-on-orange.jpg"; 
		}
	}
	
	$(".addDateSelector").datepicker(dateSelectorOptions);
	
	// IE fix of search form styling
	if($.browser.msie && ($.browser.version < 8.0)) { 
		$('#search_form .skinned-select').each(function() {
			var $parentTag = $(this).parent();
			if($parentTag.is('li')) {
				$parentTag.css({marginBottom : '7px'});
			}
		});
	}
		
	// Destination list filter
	$('#destinations_list .europe_filter, #destinations_list .america_filter').click(function() {
		
		var selection = $(this).attr('href');
			
		if($('#destinations_list').hasClass(selection)) {		
			$('#destinations_list').removeClass();
			selection = 'all';
		}
		else {
			$('#destinations_list').removeClass();
			$('#destinations_list').addClass(selection);
		}
	
		$("#dest_airports_list").load(site_url+'home/load_destination_airports', {selection: selection});
			
		return false;
	});
	
	// Bargins list on homepage
	$('#offers_list td').click(function() {
		var $parentTag = $(this).parent();
		if($parentTag.hasClass('row_bottom')) { $parentTag = $parentTag.prev(); }
		getbargain($parentTag.attr('rel'));
	});
});

function doOffersTransition(parentTag)
{
	if($('#'+parentTag+' .details').hasClass('offerOpen'))
	{
		$('#'+parentTag+' .details').slideUp().removeClass('offerOpen');
	}
	else 
	{
		$('.offerOpen').slideUp().removeClass('offerOpen');
		
		$('#'+parentTag+' .details').slideDown().addClass('offerOpen');
	}
}

function getbargain(bargainId)
{
	removebargain();
		
	if(!$('#offers_list tr[rel='+bargainId+'] .title').length) { return false; }
		
	var title = $('#offers_list tr[rel='+bargainId+'] .title').html()+' &nbsp;&nbsp; '+$('#offers_list tr[rel='+bargainId+'] .price').html()+'<br/>'+$('#offers_list tr[rel='+bargainId+']').next().children('.nights').html()+' &nbsp;&nbsp; '+$('#offers_list tr[rel='+bargainId+']').next().children('.date_from').html();
	
	var date_to = $('#offers_list tr[rel='+bargainId+']').next().children('.date_to').html();
	if(date_to != '') { title += ' - '+date_to; }
	
	var description = $('#offers_list tr[rel='+bargainId+'] .description').html();

	$('#offers_list table, #offers_pagination').hide();
	
	$('#offers_list').prepend('<div id="bargainDescriptionPanel" class="customScrollerDark"><div class="scroller"><div id="findTheHeight"><h5>'+title+'</h5>'+description+'</div></div></div>').after('<a href="#" id="bargainBackButton" onclick="removebargain(); return false;">Return</a>');
	
	if(Number($('#findTheHeight').height()) > 165) { $('#bargainDescriptionPanel .scroller').jScrollPane({showArrows:true, scrollbarWidth: 30, arrowSize: 12}); }
	else { $('#bargainDescriptionPanel .scroller').css({overflow: 'hidden'}); }
}

function removebargain() 
{
	$('#bargainDescriptionPanel, #bargainBackButton').remove();
	$('#offers_list table, #offers_pagination').show();
}