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')
	} 
}; 

selectedProg = false;
selectedLang = false;
selectedTime = false;



$(document).ready(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_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());
			}
		}
	);

	$('select#program').change(function(){
			
			$(".guided-tour-info div.item").hide()
			
			selectedProg = $(' option:selected', this).attr('rel');
			
			$(".guided-tour-info div.item#section-"+selectedProg).show()
			
			$('select#guidedlang option').removeAttr('disabled')
			
			
			
			if(selectedProg == 3 || selectedProg == 4){
				$('select#guidedlang option').attr('disabled', 'disabled')
				$('select#guidedlang option:nth-child(3)').removeAttr('disabled')
		}
			
			$('select#guidedlang option:nth-child(1)').attr('selected', 'selected')
			$('select#time_guided option:nth-child(1)').attr('selected', 'selected')
			$('.date-pick#date_guided').val('')
			
			
		})


	$('#date_guided').bind(
		'dpClosed',
		function(e, selectedDates)
		{
			var d = selectedDates[0];
		  	//selectedProg = $('select#program option:selected', this).attr('rel');

			if (d) {
				
				d = new Date(d);
				var month = d.getMonth() + 1
				
				var isWinter = ((month > 10) || (month < 3))
				var isFriday = d.isFriday()
				
				$('select#time_guided option:nth-child(1)').attr('selected', 'selected')
				
				
				if ((selectedProg == 1 || selectedProg == 2) ){
				
					if(selectedLang == 'magyar'){
						$('select#time_guided option').attr('disabled', 'disabled')
						$('select#time_guided option:nth-child(8)').removeAttr('disabled')
					}else{
						$('select#time_guided option').attr('disabled', 'disabled')
						$('select#time_guided option:nth-child(2)').removeAttr('disabled')
						$('select#time_guided option:nth-child(4)').removeAttr('disabled')
						$('select#time_guided option:nth-child(6)').removeAttr('disabled')
						
						if((!isFriday && isWinter) || (isFriday && !isWinter)){
							$('select#time_guided option:nth-child(8)').removeAttr('disabled')
							$('select#time_guided option:nth-child(10)').removeAttr('disabled')
						}else if(!isFriday && !isWinter){
							$('select#time_guided option:nth-child(8)').removeAttr('disabled')
							$('select#time_guided option:nth-child(10)').removeAttr('disabled')
							$('select#time_guided option:nth-child(12)').removeAttr('disabled')
							$('select#time_guided option:nth-child(14)').removeAttr('disabled')
						}else if(isFriday && isWinter){
						}
					}
					
					
					
					
				}
				else{
				
					{
						$('select#time_guided option').attr('disabled', 'disabled')
						$('select#time_guided option:nth-child(3)').removeAttr('disabled')
						$('select#time_guided option:nth-child(5)').removeAttr('disabled')
						
						if((!isFriday)){
							$('select#time_guided option:nth-child(7)').removeAttr('disabled')
							$('select#time_guided option:nth-child(9)').removeAttr('disabled')
							
							if(!isWinter){
								$('select#time_guided option:nth-child(11)').removeAttr('disabled')
								$('select#time_guided option:nth-child(13)').removeAttr('disabled')
							}
							
						}else{
						
							if(!isWinter){
								$('select#time_guided option:nth-child(7)').removeAttr('disabled')
								$('select#time_guided option:nth-child(9)').removeAttr('disabled')
							}
						
						}
					}
				}
				
				
				
				$('#time_guided option:first').attr('selected', true).trigger('change')
				

			}
		}
		
	)

	
	
	$('select#guidedlang').change(function(){
		selectedLang = $('option:selected', this).val()
		$('select#time_guided option:nth-child(1)').attr('selected', 'selected')
		$('.date-pick#date_guided').val('')
	})
	
	$('.date-pick').bind('keypress keyup', function(e){
		e.preventDefault();
	})
	
	
	$('.date-pick#date_guided').datePicker({
			clickInput:true
			,renderCallback:function($td, thisDate, month, year)
							{	
								
							
								if (thisDate.isSaturday()) {
									
									$td.addClass('disabled');
								}
									
								if(
										(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') == '11-04-18')
									|| (thisDate.asString('yy-mm-dd') == '11-04-19')
									|| (thisDate.asString('yy-mm-dd') == '11-04-20')
									|| (thisDate.asString('yy-mm-dd') == '11-04-21')
									|| (thisDate.asString('yy-mm-dd') == '11-04-22')
									|| (thisDate.asString('yy-mm-dd') == '11-04-23')
									|| (thisDate.asString('yy-mm-dd') == '11-04-24')
									|| (thisDate.asString('yy-mm-dd') == '11-04-25')
									|| (thisDate.asString('yy-mm-dd') == '11-04-26')

									|| (thisDate.asString('yy-mm-dd') == '11-06-08')
									|| (thisDate.asString('yy-mm-dd') == '11-06-09')

									
									|| (thisDate.asString('yy-mm-dd') == '11-08-09')
									|| (thisDate.asString('yy-mm-dd') == '11-09-28')
									|| (thisDate.asString('yy-mm-dd') == '11-09-29')
									|| (thisDate.asString('yy-mm-dd') == '11-09-30')


									|| (thisDate.asString('yy-mm-dd') == '11-10-07')
									|| (thisDate.asString('yy-mm-dd') == '11-10-08')

									|| (thisDate.asString('yy-mm-dd') == '11-10-13')
									|| (thisDate.asString('yy-mm-dd') == '11-10-14')

									|| (thisDate.asString('yy-mm-dd') == '11-10-20')
									|| (thisDate.asString('yy-mm-dd') == '11-10-21')

									|| (thisDate.asString('yy-mm-dd') == new Date().addDays(2).asString('yy-mm-dd'))
									|| (thisDate.asString('yy-mm-dd') == new Date().addDays(1).asString('yy-mm-dd'))
									
								
								
								) {
									
									$td.addClass('disabled');
								}
								
	
								if (
									(
										(selectedProg == 1 || selectedProg == 2)
										&& (thisDate.isMonday() || thisDate.isWednesday() || thisDate.isFriday())
										&& (selectedLang == 'magyar')
									)
									
								){
									$td.addClass('disabled');
									
									//$(times[6]).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;
	}
}
