function initProductImages(){
	$('div.images ul a').each(function(){
		$(this).click(function(){
			var large = $(this).attr('href')
			var small = $(this).attr('rel')
			var holder = $('a.large', $(this).parent().parent().parent())
			var image = $('<img src="'+small+'"/>').css({opacity: 0})

			holder.attr('href',large)		
			holder.empty().append(image)
			
			image.animate({opacity: 1}, 500)
			return false;
		})
	})
}

function initGuidedFormValidate(){
	if($('#subscribe-guided-tour-form').length > 0){
		$("#subscribe-guided-tour-form").validate({
			rules: {
				first: {
					required: true,
					minlength: 2
				},
				last: {
					required: true,
					minlength: 2
				},
				tel: {
					required: true,
					minlength: 2
				},
				date_guided: {
					required: true,
					minlength: 2
				},
				email: {
					required: true,
					email: true
				},
				captcha: {
					required: true
				}
			},
			messages: {
				first: {
					required: "Kérem adja meg a vezetéknevét!",
					minlength: "A vezetéknévnek minimum 2 betű hosszúnak kell lennie!"
				},
				last: {
					required: "Kérem adja meg a keresztnevét!",
					minlength: "A keresztnévnek minimum 2 betű hosszúnak kell lennie!"
				},
				tel: {
					required: "Kérem adja meg a telefonszámát!",
					minlength: "A telefonszám nem megfelelő!"
				},
				date_guided: {
					required: "Kérem adja meg a dátumot!",
					minlength: "A dátum nem megfelelő!"
				},
				captcha: {
					required: "Kérem írja be a képen látható szöveget!"
				},
					email: "Kérem érvényes e-mail címet adjon meg!"
				}
		});
		

		
	}
}


function initDateToggle(){
	$('div.yellow-box ul.dates li.first').click(function(){
		$('li', $(this).parent()).show()
		$(this).removeClass('first').show()
	})
}

(function($){

   $.fn.linkedSelect = function(url,destination,params) {

      var params = $.extend({

         firstOption : 'Please Select',

         loadingText : 'Loading...'

      },params);

      var $dest = $(destination);
	
      return this.each(function(){

         $(this).bind('change', function() {
            var $$ = $(this);

            $dest.attr('disabled','false')
                 .append('<option value="">' +params.loadingText+ '</option>')
                 .ajaxStart(function(){

                //    $$.show();

            });

            $.getJSON(url,{str: $$.val() }, function(j){
				
               if (j.length > 0) {

                  var options = '<option value="">' +params.firstOption+ '</option>';

                  for (var i = 0; i < j.length; i++) {

                     options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';

                  }

               }

			

               $dest.removeAttr('disabled').html(options)
				
				v = $dest.find('[value='+$dest.attr('val')+']')
				
				
				if(v.val()){
 					v.attr('selected', 'selected')
				} else {						
				    $dest.find('option:first')
                    .attr('selected', 'selected')                
				}
				
				$('#city_id_container, #city_id_input').remove()
				
				$dest.selectbox({debug: true});

            }); // end getJSON

			


         });  // end change
			$(this).trigger('change')

      }); // end return each

   };  // end function

})(jQuery);

var tourInfoFormOptions = { 
    clearForm: false,        // clear all form fields after successful submit 
    dataType:  'json', 
	beforeSubmit: function(data, $form, opts){
			$('.error').removeClass('error')
			$('#tour-info-form form').css({opacity: 0.6}, 300)
	},
    success:   function(data, $form, opts){ 
	
		$('#tour-info-form form').css({opacity: 1}, 300)
	
		if (!data.success){
			$(data.wrongfields).each(function(i){
				if (i==0) $('input' + this).focus()
				$('input' + this).addClass('error')
			})
		}else{
			$('#tour-info-form form').clearForm().hide()
			$('#tour-info-form ').append('<h3 style="text-align: center">Köszönjük az érdeklődést, hamarosan jelentkezünk!</h3>').animate({opacity: 1}, 2000, function(){
				$(this).animate({opacity: 0}, 2000, function(){
					$(this).hide().css({opacity: 1})
					$('#mask').slideUp()
					$('#tour-info-form form').show()
					$('#tour-info-form > h3').empty()
				})
			})
		}
			
	},
	failure: function(){
		alert('hiba')
	} 
}; 




jQuery(window).load(function(){

	initGuidedFormValidate()


	Date.firstDayOfWeek = 1;
	Date.format = 'yyyy-mm-dd';

	$('#tour-info-form form').ajaxForm(tourInfoFormOptions); 
	
	
	
	$('a[name=modal]').modal()
      
	$('a[@rel*=lightbox]').lightBox();
	
	$('.date-pick').datePicker({clickInput:true})

	$('.date-pick#date_guided').datePicker({
		clickInput:true
		,renderCallback:function($td, thisDate, month, year)
						{	
							if (thisDate.isSaturday()
								
								|| (thisDate.asString('mm-dd') == '01-01')
								|| (thisDate.asString('mm-dd') == '03-15')
								|| (thisDate.asString('mm-dd') == '12-24')
								|| (thisDate.asString('mm-dd') == '12-25')
								|| (thisDate.asString('mm-dd') == '10-23')
								|| (thisDate.asString('mm-dd') == '11-01')
								
								/// Pészáh
								|| (thisDate.asString('yy-mm-dd') == '09-04-09')
								|| (thisDate.asString('yy-mm-dd') == '09-04-10')
								|| (thisDate.asString('yy-mm-dd') == '10-03-30')
								|| (thisDate.asString('yy-mm-dd') == '10-03-31')
								
								|| (thisDate.asString('yy-mm-dd') == '09-04-15')
								|| (thisDate.asString('yy-mm-dd') == '09-04-16')
								|| (thisDate.asString('yy-mm-dd') == '10-04-05')
								|| (thisDate.asString('yy-mm-dd') == '10-04-06')
								
								// Sáuvot
								
								|| (thisDate.asString('yy-mm-dd') == '09-05-29')
								|| (thisDate.asString('yy-mm-dd') == '09-05-30')
								|| (thisDate.asString('yy-mm-dd') == '10-05-19')
								|| (thisDate.asString('yy-mm-dd') == '10-05-20')
								
								// Ros Hásáná
								
								|| (thisDate.asString('yy-mm-dd') == '09-09-18')
								|| (thisDate.asString('yy-mm-dd') == '09-09-19')
								|| (thisDate.asString('yy-mm-dd') == '09-09-20')
								|| (thisDate.asString('yy-mm-dd') == '10-09-08')
								|| (thisDate.asString('yy-mm-dd') == '10-09-09')
								|| (thisDate.asString('yy-mm-dd') == '10-09-10')
								
								// Jom Kipur
								
								|| (thisDate.asString('yy-mm-dd') == '09-09-27')
								|| (thisDate.asString('yy-mm-dd') == '09-09-28')
								|| (thisDate.asString('yy-mm-dd') == '10-09-17')
								|| (thisDate.asString('yy-mm-dd') == '10-09-18')
								
								// Szukot
								
								|| (thisDate.asString('yy-mm-dd') == '09-10-03')
								|| (thisDate.asString('yy-mm-dd') == '09-10-04')
								|| (thisDate.asString('yy-mm-dd') == '10-09-23')
								|| (thisDate.asString('yy-mm-dd') == '10-09-24')
								
								// Smini Áceret
								
								|| (thisDate.asString('yy-mm-dd') == '09-10-10')
								|| (thisDate.asString('yy-mm-dd') == '10-09-30')
								
								// Szimhát Torá
								
								|| (thisDate.asString('yy-mm-dd') == '09-10-11')
								|| (thisDate.asString('yy-mm-dd') == '10-10-01')
							
							
							
							) {
								
								$td.addClass('disabled');
							}
						}
	})
	
	
	$('#date_start').bind(
		'dpClosed',
		function(e, selectedDates)
		{
			var d = selectedDates[0];
			if (d) {
				d = new Date(d);
				$('#end-date').dpSetStartDate(d.addDays(1).asString());
			}
		}
	);
	$('#date_end').bind(
		'dpClosed',
		function(e, selectedDates)
		{
			var d = selectedDates[0];
			if (d) {
				d = new Date(d);
				$('#start-date').dpSetEndDate(d.addDays(-1).asString());
			}
		}
	);

	$('#date_guided').bind(
		'dpClosed',
		function(e, selectedDates)
		{
			var d = selectedDates[0];
			
			if (d) {
				
				$('#time_guided_container li').show()
				
				var tourTimesArr = $('#time_guided_container li')
				var numTourTimes = tourTimesArr.length
				
				d = new Date(d);
				var month = d.getMonth() + 1
				
				var isWinter = ((month > 10) || (month < 3))
				var isFriday = d.isFriday()
				
				if (isWinter && isFriday){
				
					$(tourTimesArr[numTourTimes-1]).hide()
					$(tourTimesArr[numTourTimes-2]).hide()
					$(tourTimesArr[numTourTimes-3]).hide()
					$(tourTimesArr[numTourTimes-4]).hide()
				
				}else if (isWinter && !isFriday){
				
					$(tourTimesArr[numTourTimes-1]).hide()
					$(tourTimesArr[numTourTimes-2]).hide()
				
				}else if (!isWinter && isFriday){

					$(tourTimesArr[numTourTimes-1]).hide()
				
				
				}else if (!isWinter && !isFriday){
				
				
				}

				
				


			}
		}
		
	);


	$('select').selectbox({debug: 0});
	
	
	$('select#program').change(function(){
		
		$(".guided-tour-info div.item").hide()
		$(".guided-tour-info div.item#section-"+$('option:selected', this).attr('rel')).show()
	})
	$('select#program').trigger('change')
	
	/*
	$('input#date_guided').change(function(){
		Date.format = 'yyyy-mm-dd'
		var d = Date.fromString($(this).val())
		if(d.isFriday){
			if(d.isFriday()){
				$('#time_guided_container li:last').hide()
			}else{
				$('#time_guided_container li:last').show()
			}
		}
		
	})
	
	
	*/
	$('select#zsin_id').trigger('change')
	
	
	
	
	
	$('#country_id').linkedSelect('/async/process.php?action=getCities','#city_id',{firstOption: 'Összes város'});
	initProductImages()
	initDateToggle()
});

// IMAGE REFRESHING

function refreshimg()
{
	
	$('#captchaimage span.captcha-imgholder').load('/async/process.php?action=recaptcha');
}


function trace() {
	var debug = document.getElementById('debug');
	if (!debug) {
		var debug = document.createElement('div');
		debug.setAttribute('id', 'debug');
		document.body.appendChild(debug);
		
		debug.onclick = function() {
			if (this.closed) {
				this.style.height = '200px';
				this.closed = false;
			} else {
				this.style.height = '2px';
				this.closed = true;
			}
		}
		
	}
	if (debug) {
		var o = '';
		o = 'arguments.length = ' + arguments.length + '<br />';
//		o += arguments;
		for (var i=0; i<arguments.length; i++) {
			o += '<div style="margin-left: 16px;">';
			o += arguments[i];
			for (j in arguments[i]) {
				o += '<div style="margin-left: 16px;">';
				o += '<b>' + j +'</b>&nbsp;=&nbsp;' + String(arguments[i][j]).split('<').join('&lt;').split('>').join('&gt;').split('\"').join('&raquo;');
				o += '</div>';
			}				
			o += '</div>';
		}
		debug.innerHTML = o;
	}
}