	/*
	 *	tourismguide.ro Main Action Library
	 *	Author: Justin Valceanu @ ODM
	 *	Implemented by: Justin Valceanu @ ODM / RTFM / CND-GROUP / NETMEDIA 
	 *	justin.valceanu@odm.ro
	 *
	 *	For: NETMEDIA TS RO SRL
	 * 	
	 *  Date: 21.06.2010
	 *
	 *	Hosting By: www.unixteacher.org
	 *
	 */

	$(document).ready(function() {
	 
		/* Info Box -> Top-Right: Scroller */
		$(".workWithUsItem").css("display", "none");
		$(".workWithUsItem:first").css("display", "block");

		$(".workWithUsItem:first").before($(".workWithUsItem:last"));
		$("#workWithUs-wwunALeft").click(function() {
			$(".workWithUsItem:first").before($(".workWithUsItem:last"));								  
			$('.workWithUsItem:visible').css("display", "none").prev().fadeIn(300);
			return false; 
		})

		$("#workWithUs-wwunARight").click(function() {
			$('.workWithUsItem:last').after($('.workWithUsItem:first'));  									  
			$('.workWithUsItem:visible').css("display", "none").next().fadeIn(300);
			return false; 
		})

		/* Oferte Recomandate: Functions */
		$("#worldOffers").css("display", "none");
		$("#worldOffers tr:gt(4)").css("display", "none");		
		$("#roOffers tr:gt(4)").css("display", "none");		
		
		$("#showWorldOffers").click(function() { 
			$("#showRoOffers").removeClass("active"); 
			$("#showWorldOffers").addClass("active"); 
			$("#roOffers").css("display", "none").next("#worldOffers").fadeIn(300);
			
			return false;
		});
		
		
		$("#showRoOffers").click(function() { 
			$("#showWorldOffers").removeClass("active"); 
			$("#showRoOffers").addClass("active"); 
			$("#worldOffers").css("display", "none").prev("#roOffers").fadeIn(300);
			
			return false;
		})
		
		/* Oferte Recomandate: Scroller */
		$('#roOffers tr:first').before($('#roOffers tr:last')); $('#worldOffers tr:first').before($('#worldOffers tr:last'));		
		$("#oferteRecScrollerTop").click(function () {
			if ($("#roOffers").is(":visible")) {										  
				$('#roOffers tr:first').before($('#roOffers tr:last'));									   
				$("#roOffers tr:visible:last").css("display", "none");
				$("#roOffers tr:visible:first").prev().fadeIn(500);
			} else {
				$('#worldOffers tr:first').before($('#worldOffers tr:last'));									   
				$("#worldOffers tr:visible:last").css("display", "none");
				$("#worldOffers tr:visible:first").prev().fadeIn(500);
			}
			
			return false;
		})
		
		$("#oferteRecScrollerBottom").click(function () {
			if ($("#roOffers").is(":visible")) {										  
				$('#roOffers tr:last').after($('#roOffers tr:first'));	
				$("#roOffers tr:visible:first").css("display", "none");
				$("#roOffers tr:visible:last").next().fadeIn(500);
			} else {
				$('#worldOffers tr:last').after($('#worldOffers tr:first'));	
				$("#worldOffers tr:visible:first").css("display", "none");
				$("#worldOffers tr:visible:last").next().fadeIn(500);
			}
			
			return false;
		})
		
		/* Browse By Destination: Functions */
		$("#browseByDestWorld").css("display", "none");

		$("#showDestinationsWorld").click(function() { 
			$("#showDestinationsRo").removeClass("active"); 
			$("#showDestinationsWorld").addClass("active"); 
			$("#browseByDestRo").css("display", "none").next("#browseByDestWorld").fadeIn(300);
			
			return false;
		});
		
		
		$("#showDestinationsRo").click(function() { 
			$("#showDestinationsWorld").removeClass("active"); 
			$("#showDestinationsRo").addClass("active"); 
			$("#browseByDestWorld").css("display", "none").prev("#browseByDestRo").fadeIn(300);
			
			return false;
		})

		/* Top Offers: Functions */	
		$("#topOffersWorld").css("display", "none");
		$("#topOffersWorld .toCarouselItem:gt(3)").css("display", "none");		
		$("#topOffersRo .toCarouselItem:gt(3)").css("display", "none");		
		
		$("#showTopOffersWorld").click(function() { 
			$("#showTopOffersRo").removeClass("active"); 
			$("#showTopOffersWorld").addClass("active"); 
			$("#topOffersRo").css("display", "none").next("#topOffersWorld").fadeIn(300);
			
			return false;
		});
		
		
		$("#showTopOffersRo").click(function() { 
			$("#showTopOffersWorld").removeClass("active"); 
			$("#showTopOffersRo").addClass("active"); 
			$("#topOffersWorld").css("display", "none").prev("#topOffersRo").fadeIn(300);
			
			return false;
		})
		

		/* Top Offers: Scroller */
		$('#topOffersRo .toCarouselItem:first').before($('#topOffersRo .toCarouselItem:last')); 
		$('#topOffersWorld .toCarouselItem:first').before($('#topOffersWorld .toCarouselItem:last'));		
		
		$("#leftScrollTopOffers").click(function () {
			if ($("#topOffersRo").is(":visible")) {										  
				$('#topOffersRo .toCarouselItem:first').before($('#topOffersRo .toCarouselItem:last'));									   
				$("#topOffersRo .toCarouselItem:visible:last").css("display", "none");
				$("#topOffersRo .toCarouselItem:visible:first").prev().fadeIn(500);
			} else {
				$('#topOffersWorld .toCarouselItem:first').before($('#topOffersWorld .toCarouselItem:last'));									   
				$("#topOffersWorld .toCarouselItem:visible:last").css("display", "none");
				$("#topOffersWorld .toCarouselItem:visible:first").prev().fadeIn(500);
			}
			
			return false;
		})
		
		$("#rightScrollTopOffers").click(function () {
			if ($("#topOffersRo").is(":visible")) {										  
				$('#topOffersRo .toCarouselItem:last').after($('#topOffersRo .toCarouselItem:first'));	
				$("#topOffersRo .toCarouselItem:visible:first").css("display", "none");
				$("#topOffersRo .toCarouselItem:visible:last").next().fadeIn(500);
			} else {
				$('#topOffersWorld .toCarouselItem:last').after($('#topOffersWorld .toCarouselItem:first'));	
				$("#topOffersWorld .toCarouselItem:visible:first").css("display", "none");
				$("#topOffersWorld .toCarouselItem:visible:last").next().fadeIn(500);
			}
			
			return false;
		})


		/* SEARCH Engine Functions */
			/* Check In Functionality */
			$("#arr_input").datepicker({
				dateFormat: 'yy-m-d',
				minDate: new Date(),
				maxDate: '+365D',
				onSelect: updateArrDates,
				showOn: 'button', buttonImage: '/new2/images/calIn.gif', buttonImageOnly: true
			});
		
			function updateArrDates(date) {
				var sdate = date.split('-');
		
				$('#ar_month_year').val(sdate[1]+'_'+sdate[0]); 
				$('#ar_day').val(sdate[2]); 
				
				changeDays('ar',sdate[0],sdate[2]);
				setDep();
				updateDepCal();
		
				var newDepDay = $('#dep_year').val()+'-'+$('#dep_month').val()+'-'+$('#dep_day').val();
				$("#dep_input").val(newDepDay);
			} 
		
			/* ======================= */
			
			
			/* Check Out Functionality */
			$("#dep_input").datepicker({
				dateFormat: 'yy-m-d',
				minDate: '+1D',
				maxDate: '+365D',
				onSelect: updateDepDates,
				showOn: 'button', buttonImage: '/new2/images/calOut.gif', buttonImageOnly: true
			});
		
			function updateDepDates(date) {
				var sdate = date.split('-');
		
				$('#dep_month_year').val(sdate[1]+'_'+sdate[0]); 
				$('#dep_day').val(sdate[2]); 
			}
		
			function setDep() {
				var ard = $('#ar_day').val();
				var arm = $('#ar_month_year').val();
				
				var x = $('#ar_month_year').val().split('_');
				var month = x[0];
				var year = x[1];
				
				$('#dep_month_year').val(arm);
		
				changeDp();
		
				// Happy new year addon
				if(ard == 31 && month == 12) {
					year = parseInt(year)+1;
					$("#dep_day").val(1);
					$("#dep_month_year").val(1+'_'+year);
				}
			}
			
			function changeDp() {
				var ard = $('#ar_day').val();
				var x = $('#ar_month_year').val().split('_');
				var arm = x[0];
				var ary = x[1];
				
				var dpd = $('#dep_day').val();
				var z = $('#dep_month_year').val().split('_');
				var dpm = z[0];
				var dpy = z[1];
				
				var i;
				
				// set dpd to ard + 1
				if( $("select#ar_day").val() == $("select#ar_day option:last").val() && arm != 12 ) {
					i = 1;
					$('#dep_month_year').val((parseInt(arm)+1)+'_'+ary);
					dpd = 1;
				} else if ($("select#ar_day").val() == $("select#ar_day option:last").val() && arm == 12) {
					i = 1;
				} else {
					i = parseInt(ard)+1;	
				}
				
				if(parseInt(dpm) > parseInt(arm)) i = 1;
				if(parseInt(dpy) > parseInt(ary)) i = 1;
				
				changeDays('dep',dpy,i);
				$('#dep_day').val(i);
						
			}
			
			/* ======================= */
			$("select#ar_day").change(function(){
				var x = $("select#ar_month_year").val().split('_');							   
				changeDays('ar',x[1],$("select#ar_day").val());
				
				setDep();
				updateArCal();
				updateDepCal();
			});
		
			$("select#ar_month_year").change(function(){
				var x = $("select#ar_month_year").val().split('_');							   
				changeDays('ar',x[1],$("select#ar_day").val());
				setDep();
				updateArCal();
				updateDepCal();
			});
			
			$("select#dep_day").change(function(){
				updateDepCal();
			});
			
			$("select#dep_month_year").change(function(){
				changeDp();
				updateDepCal();
			});
		
			function updateDepCal() {
				var x = $('#dep_month_year').val().split('_');
				var updateCal = x[1]+'-'+x[0]+'-'+$('#dep_day').val();
				//alert(updateCal);
				$("#dep_input").val(updateCal);
			}
			
			function updateArCal() {
				var x = $('#ar_month_year').val().split('_');
				var updateCal = x[1]+'-'+x[0]+'-'+$('#ar_day').val();
				//alert(updateCal);
				$("#arr_input").val(updateCal);
			}
				
			
			
			/* Usefull Functions */
			function changeDays(f,y,i) { // ar,dep + year + start day
				var field = f+'_day';
				var x = $("select#"+f+"_month_year").val().split('_');
				var m = x[0];
				
				/* Update Disabled Days */	
				var now = new Date();
				var cDay = now.getDate()+1;
				var cMonth = now.getMonth()+1;
				var cYear = now.getFullYear();
		
				$("select#"+f+"_day").find('option').remove().end();
				
				var day = i;
				if (cYear == y || cMonth != m) {
					//alert("F: "+field+" Y: "+y+" M: "+m+" D: "+i+" CYEAR: "+cYear+" cMonth: " +cMonth);
					i = 1;
				}
				
				if(m == 2) {
					var year = y-2000;
					if (Math.floor(year/4)==year/4) days=29; else days=28;
					for(i;i<=days;i++) {
						if (cYear == y && cMonth == m && i < cDay) {			
							$("select#"+f+"_day").append('<option value="'+i+'" disabled="disabled">'+i+'</option>');
						} else if (cYear != y && cMonth == m && i > cDay) {
							$("select#"+f+"_day").append('<option value="'+i+'" disabled="disabled">'+i+'</option>');
						} else if (day == i) {
							$("select#"+f+"_day").append('<option value="'+i+'" selected="selected">'+i+'</option>');
						} else {
							$("select#"+f+"_day").append('<option value="'+i+'">'+i+'</option>');	
						}
					}
				} else if(m==1 || m==3 || m==5 || m==7 || m==8 || m==10 || m==12) {
					for (i ; i<=31 ; i++){
						if (cYear == y && cMonth == m && i < cDay) {			
							$("select#"+f+"_day").append('<option value="'+i+'" disabled="disabled">'+i+'</option>');
						} else if (cYear != y && cMonth == m && i > cDay) {
							$("select#"+f+"_day").append('<option value="'+i+'" disabled="disabled">'+i+'</option>');
						} else if (day == i) {
							$("select#"+f+"_day").append('<option value="'+i+'" selected="selected">'+i+'</option>');
						} else {
							$("select#"+f+"_day").append('<option value="'+i+'">'+i+'</option>');	
						}
					}
				} else {
					for (i ; i<=30 ; i++){
						if (cYear == y && cMonth == m && i < cDay) {			
							$("select#"+f+"_day").append('<option value="'+i+'" disabled="disabled">'+i+'</option>');
						} else if (cYear != y && cMonth == m && i > cDay) {
							$("select#"+f+"_day").append('<option value="'+i+'" disabled="disabled">'+i+'</option>');
						} else if (day == i) {
							$("select#"+f+"_day").append('<option value="'+i+'" selected="selected">'+i+'</option>');
						} else {
							$("select#"+f+"_day").append('<option value="'+i+'">'+i+'</option>');	
						}
					}	
				}
				
			}
			
			$("select#country_select").change(function() {
				var cid = $(this).val();
				$("select#citySelect").html("<option>Va rugam asteptati ...</option>");
				$.ajax({
					dataType: "jsonp", 
					url: "http://hoteluri.tourismguide.ro/ajax/all/getcitiesjson/cid/"+cid,
					cache: true,
					success: function(data) {
					var options = '';
					for (var i = 0; i < data.length; i++) {
						options += '<option value="' + data[i].id + '">' + data[i].name + '</option>';
					}
					$("select#citySelect").html(options);
					$('select#citySelect option:first').attr('selected', 'selected');
				}   
				});
			})	
			
			$("#searchAccommodation").submit(function(){
				$("input").hide();
				$("select").hide();
				
				var arr = $("select#ar_day option:selected").text() +' '+ $("select#ar_month_year option:selected").text().replace(/,/gi, "");
				var dep = $("select#dep_day option:selected").text() +' '+ $("select#dep_month_year option:selected").text().replace(/,/gi, "");
											
				// $('.preloader').height($(document).height());		
				$('div#preloader').show();
				$("#preloader_arrdate").html(arr);
				$("#preloader_depdate").html(dep);
				$("#preloader_city").html($("select#citySelect option:selected").text());
				$("#preloader_roomtype").html($("select#roomType option:selected").text());
				$("#preloader_numofrooms").html($("select#numOfRooms option:selected").text());
			});
	});

