var Gezisitesi = window.Gezisitesi || {};

Gezisitesi.HotelPriceDetailsHelper = function( settings ) {
	this.settings = $.extend({
		productid: 0,
		pricedetailurl: '/forms/hotelroomchoose.aspx',
		containerprefix: '', // ContainerNames will be; [containerprefix]RoomNormal, [containerprefix]RoomCombinations, [containerprefix]RoomPrice
		statuscontainer: ''
	}, settings || {});

	if ( this.settings.productid == null || this.settings.productid == 0 ) {
		alert( 'HotelPriceDetailsHelper module not initialized!' );
		return;
	}
	
	this.BookingDetails = {};
	this.ResetBookingDetails( 'NA', '01.01.1900', '01.01.1900' );
};

Gezisitesi.HotelPriceDetailsHelper.prototype.ResetBookingViews = function( requestTypes ) {
	for( var i=0; i < requestTypes.length; i++ )
		$('#'+this.settings.containerprefix + requestTypes[i] ).html('');
};

Gezisitesi.HotelPriceDetailsHelper.prototype.ResetLoginView = function( ) {
	$('#dvLoginInner').hide();
	$('#dvPriceInner').show();
};

Gezisitesi.HotelPriceDetailsHelper.prototype.ResetBookingDetails = function( priceQualifier, checkInDate, checkOutDate ) {
	this.BookingDetails.PriceQualifier = priceQualifier;
	this.BookingDetails.CheckInDate = Utilities.ToSpecDate( checkInDate );
	this.BookingDetails.CheckOutDate = Utilities.ToSpecDate( checkOutDate );
	this.BookingDetails.HotelPricesId = 0;
	this.BookingDetails.HotelCombinationId = 0;
	this.BookingDetails.RequestType = 'RoomNormal';
	this.BookingDetails.CampaignId = 0;
	this.BookingDetails.Price = 0;
	this.BookingDetails.PriceUnit = '';
	this.BookingDetails.SupplierPrice = 0;
	this.BookingDetails.SupplierPriceUnit = '';
	this.BookingDetails.MP = 0;
	this.BookingDetails.DisplayTransportationOptions = false;
	this.ResetPaymentDetails();
};

Gezisitesi.HotelPriceDetailsHelper.prototype.ResetPaymentDetails = function(  ) {
	this.BookingDetails.VA = '';
	this.BookingDetails.PaymentType = 'CreditCard';
	this.BookingDetails.Installment = 0;
};

Gezisitesi.HotelPriceDetailsHelper.prototype.GetRooms = function( priceQualifier, checkInDate, checkOutDate ) {
	this.ResetLoginView();
	this.ResetBookingViews( ['RoomNormal','RoomCombinations','RoomPrice'] );
	this.ResetBookingDetails(priceQualifier, checkInDate, checkOutDate);
	this.LoadData( this );
	
	$.colorbox({inline:true, href:"#dvPriceInner",  width:"565", initialWidth:"565", autoResize:true});	
};

Gezisitesi.HotelPriceDetailsHelper.prototype.GetCombinations = function( hotelPricesId ) {
	this.ResetBookingViews( ['RoomCombinations','RoomPrice'] );
	this.BookingDetails.HotelPricesId = hotelPricesId;
	this.BookingDetails.RequestType = 'RoomCombinations';
	this.LoadData( this );
};

Gezisitesi.HotelPriceDetailsHelper.prototype.GetPriceDetails = function( hotelCombinationId ) {
	this.ResetBookingViews( ['RoomPrice'] );
	this.BookingDetails.HotelCombinationId = hotelCombinationId;
	this.BookingDetails.RequestType = 'RoomPrice';
	this.LoadData( this );
};

Gezisitesi.HotelPriceDetailsHelper.prototype.GetRequestUrl = function( ) {
	if ( this.BookingDetails.CheckInDate == '19000101' || this.BookingDetails.CheckOutDate == '19000101' ) {
		return;
	}

	var url = this.settings.pricedetailurl + '?';
	
	url += 'hid=' + this.settings.productid;
	url += '&cin=' + this.BookingDetails.CheckInDate;
	url += '&cout=' + this.BookingDetails.CheckOutDate;
	url += '&type=' + this.BookingDetails.RequestType;
	url += '&qlfr=' + this.BookingDetails.PriceQualifier;

	if ( this.BookingDetails.RequestType == 'RoomCombinations' ) {
		url += '&hpid=' + this.BookingDetails.HotelPricesId;
	}

	if ( this.BookingDetails.RequestType == 'RoomPrice' ) {
		url += '&hpid=' + this.BookingDetails.HotelPricesId;
		url += '&combid=' + this.BookingDetails.HotelCombinationId;
	}

	url += '&fpr=' + GetRandomNumberString();
	return url;
};

Gezisitesi.HotelPriceDetailsHelper.prototype.GetStatusDetail = function( ) {
	var statusHtml = '<img id=\"imgloading\" src=\"/img/logowaitsmall.gif\">&nbsp;&nbsp;';

	if ( this.BookingDetails.RequestType == 'RoomNormal' ) statusHtml += GezisitesiResources.StaticText.m038 + ' ' + GezisitesiResources.StaticText.m017;
	if ( this.BookingDetails.RequestType == 'RoomCombinations' ) statusHtml += GezisitesiResources.StaticText.m039 + ' ' + GezisitesiResources.StaticText.m017;
	if ( this.BookingDetails.RequestType == 'RoomPrice' ) statusHtml += GezisitesiResources.StaticText.m040 + ' ' + GezisitesiResources.StaticText.m017;
	if ( this.BookingDetails.RequestType == 'BasicInstallment' ) statusHtml += GezisitesiResources.StaticText.m006 + ' ' + GezisitesiResources.StaticText.m017;
	
	return statusHtml;
};

Gezisitesi.HotelPriceDetailsHelper.prototype.AnalyzeResponseDetails = function( ) {
	this.BookingDetails.HotelPricesId = $('#optionRoom').selectedValues()[0];
	this.BookingDetails.HotelCombinationId = $('#optionCombination').selectedValues()[0];

	if ( this.BookingDetails.RequestType == 'RoomPrice' ) {
		if ( this.BookingDetails.PriceQualifier == 'NORMAL' ) {
			this.CheckCampaigns();
		} else {
			this.AssignDefaultPrice();
		}
	}
	
	this.CheckPaymentMethod();
};

Gezisitesi.HotelPriceDetailsHelper.prototype.AssignDefaultPrice = function( ) {
	var $activePrice = $('#InstallmentPrice');
	
	if ( $activePrice.length > 0 ) {
		this.BookingDetails.CampaignId = 0;
		this.BookingDetails.Price = $activePrice.attr('price');
		this.BookingDetails.PriceUnit = $activePrice.attr('priceunit');
		this.BookingDetails.MP = $activePrice.attr('mp');
		this.BookingDetails.SupplierPrice = $activePrice.attr('supplierprice');
		this.BookingDetails.SupplierPriceUnit = $activePrice.attr('supplierpriceunit');
		
		$('.gPrice').html( $activePrice.attr('pricetext') );
		$('.gPriceDescription').html( $activePrice.attr('description') );
		
		var $eftPrice = $('#EFTPrice');
		
		if ( $eftPrice.length > 0 ) {
			$('.ePrice').html( $eftPrice.attr('eftpricetext') );
		}
		
	}
};

Gezisitesi.HotelPriceDetailsHelper.prototype.AssignSupplierPrice = function( ) {
	var $activePrice = $('#InstallmentPrice');
	
	if ( $activePrice.length > 0 ) {
		this.BookingDetails.SupplierPrice = $activePrice.attr('supplierprice');
		this.BookingDetails.SupplierPriceUnit = $activePrice.attr('supplierpriceunit');
	}
};

Gezisitesi.HotelPriceDetailsHelper.prototype.CheckTransportationOption = function( checkedItem ) {
	this.BookingDetails.DisplayTransportationOptions = $(checkedItem).attr('checked');
}

Gezisitesi.HotelPriceDetailsHelper.prototype.CheckCampaigns = function( checkedItem ) {
	
	var oldBookingPrice = this.BookingDetails.Price;
	var $campaignItems = $('input[name=list]');
	
	if ( $campaignItems.length > 0 ) {
		$checkedItem = ( checkedItem != null ) ? $(checkedItem) : 'NA';
		
		if ( $checkedItem != 'NA' && $checkedItem.attr('checked') == true ) {
			$campaignItems.not('[value=\''+$checkedItem.val()+'\']').attr('checked',false);
		} else {
			var $earlyReservationItem = $campaignItems.filter('[value=\'earlyreservation\']');
			
			if ( $earlyReservationItem.length > 0 ) {
				$earlyReservationItem.attr('checked',true);
			}
		}
		
		var $selectedItem = $campaignItems.filter('[checked]');
		
		if ( $selectedItem.length > 0 ) {
			this.BookingDetails.CampaignId = parseInt( $selectedItem.attr('campaignid') );
			this.BookingDetails.Price = $selectedItem.attr('price');
			this.BookingDetails.PriceUnit = $selectedItem.attr('priceunit');
			this.BookingDetails.MP = $selectedItem.attr('mp');
			
			$('.gPrice').html( $selectedItem.attr('pricetext') );
			$('.gPriceDescription').html( $selectedItem.attr('description') );
			
			var $eftPrice = $('#EFTPrice');
			
			if ( $eftPrice.length > 0 ) {
				$('.ePrice').html( $selectedItem.attr('eftpricetext') );
			}
			
			this.AssignSupplierPrice();
		} else {
			this.AssignDefaultPrice();
		}
		
	} else {
		this.AssignDefaultPrice();
	}

	if ( oldBookingPrice != 0 && oldBookingPrice != this.BookingDetails.Price )
	{
		this.GetBasicInstallments( this.GetCurrentVA() );
	}
	
};

Gezisitesi.HotelPriceDetailsHelper.prototype.AddCampaignParameter = function( url ) {
	var tempUrl = url;
	
	if ( this.BookingDetails.PriceQualifier == 'NORMAL' && this.BookingDetails.CampaignId != 0 ) {
		tempUrl += '&cp=' + this.BookingDetails.CampaignId.toString();
	}
	return tempUrl;
};

Gezisitesi.HotelPriceDetailsHelper.prototype.AddTransportationParameter = function( url ) {
	var tempUrl = url;
	
	if ( this.BookingDetails.DisplayTransportationOptions == true ) {
		tempUrl += '&dto=true';
	}
	return tempUrl;
};

Gezisitesi.HotelPriceDetailsHelper.prototype.AddPaymentParameters = function( url ) {
	var tempUrl = url;

	if ( this.BookingDetails.VA != '' ) {
		tempUrl += '&vac=' + this.BookingDetails.VA;
	}
	
	if ( this.BookingDetails.PaymentType != 'CreditCard' ) {
		tempUrl += '&pyt=' + this.BookingDetails.PaymentType;
	}
	
	if ( this.BookingDetails.Installment != 0 ) {
		tempUrl += '&ins=' + this.BookingDetails.Installment.toString();
	}
	
	return tempUrl;
};

Gezisitesi.HotelPriceDetailsHelper.prototype.StartSale = function( target, lgn ) {
	var url = this.AddCampaignParameter( target );
	url = this.AddTransportationParameter( url );
	url = this.AddPaymentParameters( url );
	
	if ( lgn ) {
		ASBTarget = url;
		BuyNow('winHotelPrices');
	} else {
		$('#winHotelPrices').block(GezisitesiResources.StaticText.m006 + GezisitesiResources.StaticText.m017, {'font-size':'14px','font-weight':'bold','background-color':'#eff5f7',border:'1px solid #cccccc'});
		window.location.href = url;
	}
};

Gezisitesi.HotelPriceDetailsHelper.prototype.AddItemToShoppingBag = function( target ) {
	var url = this.AddCampaignParameter( target );
	url = this.AddTransportationParameter( url );
	url = this.AddPaymentParameters( url );
	ASBTarget = url;
	AddSB('winHotelPrices');
};

Gezisitesi.HotelPriceDetailsHelper.prototype.CheckPaymentMethod = function( checkedItem, callbackBase ) {
	var baseHandler = (callbackBase != null && callbackBase != 'undefined') ? callbackBase : this;
	var $paymentItems = $('input[name=paymentmethod]');
	
	if ( $paymentItems.length > 0 ) {
		var $installmentItems = $('[pyt=\'CreditCardInstallment\']');
		
		if ( $installmentItems.length < 1 )
		{
			$installmentItems = $('[pyt=\'CreditCard\']');
		}
		
		var $checkedItem = ( checkedItem != null ) ? $(checkedItem) : $installmentItems.eq(0).attr('checked', true);
		
		if ( $checkedItem.length > 0 ) {
			baseHandler.SetPaymentMethod( $checkedItem.attr('pyt'), $checkedItem.attr('vac'), $checkedItem.attr('ins') );
		}
	} else {
		baseHandler.ResetPaymentDetails();
	}
};

Gezisitesi.HotelPriceDetailsHelper.prototype.GetCurrentVA = function( ) {
	var currentVA = '';
	
	var $paymentItems = $('input[name=paymentmethod][vac!=""]');
	
	if ( $paymentItems.length > 0 )
	{
		currentVA = $paymentItems.eq(0).attr('vac');
	}
	
	return currentVA;
}

Gezisitesi.HotelPriceDetailsHelper.prototype.SetPaymentMethod = function( paymentType, vposAccount, installment ) {
	this.ResetPaymentDetails();

	this.BookingDetails.PaymentType = paymentType;
	this.BookingDetails.VA = vposAccount;
	this.BookingDetails.Installment = installment;
};

Gezisitesi.HotelPriceDetailsHelper.prototype.GetBasicInstallments = function( va ) {
	if ( va == null || va == 'undefined' || va == '' ) {
		return;
	}

	this.CheckPaymentMethod();

	var basicInstallmentManager = new Gezisitesi.BasicInstallmentHelper({
			RequestUrl: this.settings.pricedetailurl,
			RequestType: 'BasicInstallment',
			ParentContainerClass: 'PDetails',
			InsertAfterSelector: '#rowEFTandCreditCard',
			ItemClass: 'BInstallmentRow',
			PriceDetailHelper: this
	});

	basicInstallmentManager.GetBasicInstallments( va, this.BookingDetails.Price, this.BookingDetails.PriceUnit, this.BookingDetails.SupplierPrice, this.BookingDetails.SupplierPriceUnit, this.BookingDetails.MP );
};

Gezisitesi.HotelPriceDetailsHelper.prototype.ShowInstallmentModule = function ( showEftPrice ) {
	
	showProductInstallments(	
		this.BookingDetails.Price.toString().replace(',','.'),
		this.BookingDetails.PriceUnit,
		'Hotel',
		showEftPrice,
		true,
		this.BookingDetails.MP.toString().replace(',','.'),
		this.BookingDetails.SupplierPrice.toString().replace(',','.'),
		this.BookingDetails.SupplierPriceUnit,
		true
	);
}

Gezisitesi.HotelPriceDetailsHelper.prototype.DisableRequestComponents = function() {
	$('#optionRoom').attr('disabled','disable');
	$('#optionCombination').attr('disabled','disable');
	$('#creditCardImages').block('', {'background-color':'#eff5f7',border:'1px solid #cccccc'});
}

Gezisitesi.HotelPriceDetailsHelper.prototype.EnableRequestComponents = function() {
	$('#optionRoom').removeAttr('disabled').blur();
	$('#optionCombination').removeAttr('disabled').blur();
	$('#creditCardImages').unblock();
}

Gezisitesi.HotelPriceDetailsHelper.prototype.LoadData = function( sender ) {

	var targetUrl = this.GetRequestUrl();
	var container = $('#'+this.settings.containerprefix + this.BookingDetails.RequestType );
	var statusContainer = $('#'+this.settings.statuscontainer);
	container.html('');
	sender.DisableRequestComponents();
	statusContainer.html( this.GetStatusDetail() );

	/*
	if (!pricepanel) {
		initpricepanel();$('#winHotelPrices').show();
	} else {
		sposition=GetContainerWidth('450');
		pricepanel.cfg.setProperty("x",sposition); 
		

	}
	pricepanel.render();
	pricepanel.show();
	*/

	this.OnSuccessfulRequest = function( data ) {
		if ( data.indexOf('<html') != -1 )
			{ container.html('<span class="text33">'+GezisitesiResources.StaticText.m041+'</span>'); }
		else
			{ container.html( data ); }
		
		statusContainer.html('');
		$('#dvAddShoppingBagMessage').hide();
		sender.AnalyzeResponseDetails();
		sender.EnableRequestComponents();	
		$.colorbox.resize();	
	}
	
	this.OnError = function() {
		container.html('');
		statusContainer.html('');
		sender.EnableRequestComponents();
	}

	$.ajax({
		type:'GET',
		url: targetUrl,
		dataType: 'html',
		async: true,
		success: this.OnSuccessfulRequest,
		error: this.OnError
	});
};

Gezisitesi.HotelPriceDetailsHelper.prototype.SetDatesCookie = function() {
	
	if (this.BookingDetails.CheckInDate != '19000101' && this.BookingDetails.CheckOutDate != '19000101')
	{
		var COOKIE_NAME = "gs_dcv";
		var options = { path: '/', expires: 5 };
		var gs_cid = this.BookingDetails.CheckInDate ;
		var gs_cod = this.BookingDetails.CheckOutDate;
		
		$.cookie(COOKIE_NAME, gs_cid + '-' + gs_cod , options);
    }
};


