/* elmenybirtok.hu jQuery */

$(document).ready(function(){
	
	//// Book --------------
	var footerbanner = $('.footerbanner').html();
	$('.footerbanner').flash({ 
		src: footerbanner, 
		width: 468, 
		height: 60,
		wmode: 'transparent'
		}, { version: 9 }
	);
	////--------------------
	
	//// Footer Banner -----
	
	var xmlFile = $('#flashbook .xmlfile').text();
	var slogan = $('#flashbook .slogan').text();
	$('#flashbook').flash({ 
		src: '/wp-content/themes/elmenybirtok/flash/book.swf', 
		width: 1000, 
		height: 380,
		wmode: 'transparent',
		flashvars: { xmlPath: xmlFile, loaderText: slogan }
		}, { version: 9 }
	);
	////--------------------
	
	//// Menu --------------
	menuInnit('#topmenu');
	////--------------------
	
	//// Overlay Gallery ----
	$("a[rel^='prettyPhoto']").prettyPhoto({
		opacity: 0.40,
		theme: 'dark_square'
	});
	////--------------------
	
	//// Festival Details Toggle ----
	$('.more').click(function() {
		var thisContent = $(this).attr('href');
		$('#'+thisContent).slideToggle('slow');
		return false;
	});
	////--------------------
	
	//// Open images in articles
	$('.entry a:has(img)').click(function() {
		$.prettyPhoto.open( $(this).attr('href') );
		return false;
	});
	////--------------------
	
	//// Open up group interst layer
	
	/*
	$("#egyedi_ajanlat_trigger").click(function(e) {
		e.preventDefault();
		var height = $('#egyedi_ajanlat_content').height();
  	var width = $('#egyedi_ajanlat_content').width();
  	leftVal=e.pageX-(width/2)+"px";
  	topVal=e.pageY-(height/2)+"px";
  	$('#egyedi_ajanlat_content').css({left:leftVal,top:topVal}).show(1500);
	});
	
	
	if ($('#frmEgyedi').length > 0) {
		$('#frmEgyedi').formManager();
		
		$("#e_close").click(function(e) {
			e.preventDefault();
			$("#egyedi_ajanlat_content").hide(1500);
		});
		
	}
	*/
	
	//Élménycsomag más színű
	$('.page-item-551').addClass('highlighted');
	

	
});

function menuInnit(topParent) {
	// set submenuitems hidden
	$(topParent + ' li ul').hide();
	// set topmenuitems events
	$(topParent + ' li').mouseover(function() { $('ul', this ).show(); });
	$(topParent + ' li').mouseout(function() { $('ul', this ).hide(); });
	// Disable topmenuitems a wich has submenuitems
	$(topParent + " li.page_item:has('ul')").click(function() { return false; });
	// Enable submenus a
	$(topParent + " li.page_item ul li.page_item a").click(function() { document.location = $(this).attr('href'); });
}


var ticketOrder = {

	orderData: null,


	getOrderForm: function() {
		
		$.ajax({
			type: 'POST',
			url: '/wp-content/plugins/elmenybirtok/callService.php',
			data: {form: 'order'},
			dataType: 'html',
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				ticketOrder.ajaxError('getOrderForm', textStatus);
			},
			success: ticketOrder._showOrderForm
		});	
		
		
		/* MAINTENCE SCRIPT TO SHOW THE 2ND STEP  */
		/* DELETE FROM HERE */
		/*
		
		var data = 'form=reserve&jelleg=egesznap&type1=1&type2=0&type3=0&type4=0&type5=0&nev=Kiss+P%C3%A1k%C3%B3&varos=Budapest&irszam=1111&utcahaz=Seholsincs+u.+12&email=vasoczki.ferenc%40aera.hu&datum=2010.06.15.&check=igen';
		
		this.orderData = {
		date: $('#ticketDate').val(),
		name: $('#ticketName').val(),
		address: $('#ticketPostCode').val() + ' ' + $('#ticketCity').val() + ', ' +
				$('#ticketAddress').val(),
		email: $('#ticketEmail').val()
		};
		
		
		//var data = 'form=reserve&' + $('#orderForm').serialize();
		$.ajax({
			type: 'POST',
			url: '/wp-content/plugins/elmenybirtok/callService.php',
			data: data,
			dataType: 'html',
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				ticketOrder.ajaxError('order', textStatus);
			},
			success: ticketOrder._showReserveForm
		});
		*/
		
		/* DELETE UNTIL THIS */
		
	},


	_showOrderForm: function(xmlResponse, textStatus) {
		$('#orderForm').html(xmlResponse);
		numberInput.init();
		$('#ticketDate').datepicker({
			showOn: 'both',
			buttonImage: '/wp-content/themes/elmenybirtok/img/calendar.gif',
			buttonImageOnly: true,
			minDate: 1
		});
		$('.ticket-type').blur(ticketOrder._ticketBlur);
	},


	_ticketBlur: function() {
		var o = $(this);
		if(!o.val().length) o.val('0');
	},


	order: function() {
		$('html, body').animate({ scrollTop: 0 }, 'normal');
		if(!this.checkForm()) return;
		this.orderData = {
			date: $('#ticketDate').val(),
			name: $('#ticketName').val(),
			address: $('#ticketPostCode').val() + ' ' + $('#ticketCity').val() + ', ' +
					$('#ticketAddress').val(),
			email: $('#ticketEmail').val()
		};

		var data = 'form=reserve&' + $('#orderForm').serialize();
		$.ajax({
			type: 'POST',
			url: '/wp-content/plugins/elmenybirtok/callService.php',
			data: data,
			dataType: 'html',
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				ticketOrder.ajaxError('order', textStatus);
			},
			success: ticketOrder._showReserveForm
		});
	},


	_showReserveForm: function(xmlResponse, textStatus) {
		$('#orderForm').hide();
		$('#reserveForm').html(xmlResponse);
		var o = $('#orderData');
		
		o.append('<table cellspacing="0" cellpadding="0" border="0" id="reserve_table">');
		o.append('	<tr>');
		o.append('		<td class="reserve_label">' + messages.dateOfIssueOfTickets + ':</td>');
		o.append('		<td class="reserve_value">' + ticketOrder.orderData.date + '</td>');
		o.append('	</tr>');
		
		o.append('	<tr>');
		o.append('		<td class="reserve_label">' + messages.nameOfProcurer + ':</td>');
		o.append('		<td class="reserve_value">' + ticketOrder.orderData.name + '</td>');
		o.append('	</tr>');
		
		o.append('	<tr>');
		o.append('		<td class="reserve_label">' + messages.addressOfProcurer + ':</td>');
		o.append('		<td class="reserve_value">' + ticketOrder.orderData.address + '</td>');
		o.append('	</tr>');
		
		o.append('	<tr>');
		o.append('		<td class="reserve_label">' + messages.emailOfProcurer + ':</td>');
		o.append('		<td class="reserve_value">' + ticketOrder.orderData.email + '</td>');
		o.append('	</tr>');

		o.append('</table>');
		
		$('#orderDate').html(ticketOrder.orderData.date);
		$('#reserveForm').show();
	},


	reserve: function() {
		$('html, body').animate({ scrollTop: 0 }, 'normal');
		$.ajax({
			type: 'POST',
			url: '/wp-content/plugins/elmenybirtok/callService.php',
			data: $.extend(this.orderData, {
				form: 'notification',
				order_number: $('#orderNumber').val()
			}),
			dataType: 'html',
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				ticketOrder.ajaxError('order', textStatus);
			},
			success: ticketOrder._showConfirmation
		});
	},


	_showConfirmation: function(xmlResponse, textStatus) {
		$("#reserveForm input[type='button']").hide();
		$('#stepReserve').removeClass('current');
		$('#stepConfirm').addClass('current');
		$('#orderInfo').html(xmlResponse);
	},


	return2Order: function() {
		$('#orderForm').show();
		$('#reserveForm').hide();
	},


	ajaxError: function(callName, status) {
		var msg = status + ' - ' + callName;
		if(console != 'undefined') console.error(msg);
		else alert(msg);
	},


	checkForm: function() {
		var form = $('#orderForm');
		// Check the input element with .req css class and send message for the user if something is missing.
		var error = false;
		form.find('.req').each(function(i) {
			if(!this.value.length) {
				this.focus();
				ticketOrder.formError(messages.pleaseEnterAllRequiredData);
				error = true;
				return false;
			}
		});
		if(error) return false;

		var guests = 0;
		form.find('.guesttype').each(function() {
			guests += parseInt(this.value);
		});
		if(!guests) {
			form.find('.guesttype').focus();
			ticketOrder.formError(messages.noGuests);
			return false;
		}

		var postCode = form.find('#ticketPostCode');
		if(postCode.val().length < 4) {
			postCode.focus();
			ticketOrder.formError(messages.incorrectPostCode);
			return false;
		}

		re = new RegExp("^[0-9a-z]+[0-9a-z._\-]*@([0-9a-z]+)([.\-]([0-9a-z]+))+$", "gi");
		var email = form.find('#ticketEmail');
		if(!re.test(email.val())) {
			email.focus();
			ticketOrder.formError(messages.incorrectEmail);
			return false;
		}

		return true;
	},


	formError: function(errorText) {
		$('#formMessage').slideUp('fast').text(errorText).slideDown('slow');
	}

}