function initCopycat() {
	$('#copycat').change(
		function () {
			if ($('#copycat').is(':checked'))
			{
				$('#booking\\[sex0\\]').val($('#booking\\[sex\\]').val());
				$('#booking\\[firstname0\\]').val($('#booking\\[firstname\\]').val());
				$('#booking\\[lastname0\\]').val($('#booking\\[lastname\\]').val());
				$('#booking\\[street0\\]').val($('#booking\\[street\\]').val());
				$('#booking\\[zip0\\]').val($('#booking\\[zip\\]').val());
				$('#booking\\[city0\\]').val($('#booking\\[city\\]').val());
				$('#booking\\[country0\\]').val($('#booking\\[country\\]').val());
			}	
		}
	);
}

function hookMultiselectEvent() {
  if ($('.multiselect:visible').length) {
    $('.multiselect').click(function(e) { //button click class name is myDiv
     e.stopPropagation();
    });
    $(document).bind('click', function() { $('.multiselect').fadeOut(); })
  } else {
    $(document).unbind('click');
  }
}

function initMultiselect() {
  if ($('.multiselect').length == 0) return false;
  
  $('.multiselect .clear').click(function() {
    $(this).closest('.multiselect').find('input[type=checkbox]').attr('checked', false);
    return false;
  });
  
  $('.multiselect input[type=checkbox]').change(function() {
    var li = $(this).closest('li');
    var parentLi = li.parents('li');
    var val;
    var all;
    var checked;

	var checkname;

	checkname = $(this).attr('name');
   
    if ($(this).is(':checked')) val = true;
                           else val = false;

	$('input[name="'+checkname+'"]').attr('checked', val);

	if ($('input[name="'+checkname+'"]').length > 1)
	{
		li = $('input[name="'+checkname+'"]').slice(1).first().closest('li');
		parentLi = li.parents('li');
	}
    
    if (li.find('ul').length) {
      li.find('ul input[type=checkbox]').attr('checked', val);
    } else if (parentLi.length) {
      all = parentLi.find('ul input[type=checkbox]').length;
      checked = parentLi.find('ul input[type=checkbox]:checked').length;
      parentLi.find('input[type=checkbox]').first().attr('checked', (checked == all));
	  checkname = parentLi.find('input[type=checkbox]').first().attr('name');
	  $('input[name="'+checkname+'"]').attr('checked', (checked == all));
    }
  });
  
  $('.multiselect input[type=button]').click(function() {
    // ZAPISZ WYBOR
    var multiselect = $(this).closest('.multiselect');
    var values = Array();
    multiselect.fadeOut(300, hookMultiselectEvent );
    
    multiselect.find('.container > ul > li > label > input[type=checkbox]').each(function() {
      var lbl = $.trim($(this).closest('label').text());
      if ($(this).is(':checked')) {
        values.push(lbl);
      } else {
        $(this).closest('li').find('ul > li > label > input[type=checkbox]:checked').each(function() {
          values.push(lbl + ' - ' + $.trim($(this).closest('label').text()));
        });
      }
    });
//    console.log(multiselect.closest('div'));
    multiselect.closest('div:not(.multiselect)').find('input[type=text]').val(values.join(', '));
    
  });

  $('.multiselect input[type=button]').each(function() {
	var multiselect = $(this).closest('.multiselect');
    var values = Array();
	
    multiselect.find('.container > ul > li > label > input[type=checkbox]').each(function() {
      var lbl = $.trim($(this).closest('label').text());
      if ($(this).is(':checked')) {
        values.push(lbl);
      } else {
        $(this).closest('li').find('ul > li > label > input[type=checkbox]:checked').each(function() {
          values.push(lbl + ' - ' + $.trim($(this).closest('label').text()));
        });
      }
    });

    multiselect.closest('div:not(.multiselect)').find('input[type=text]').val(jQuery.unique(values).join(', '));
  });
}

function initFilterForm() {
  if ($('form.frm-filter').length == 0) return false;
  
  $('.departure input, .return input').datepicker({
    dateFormat: "dd.mm.yy",
    showOn: "both",
    buttonImage: "images/lay-ico-calendar.png",
    buttonImageOnly: true
  });
  
  $('.destination > input, .destination > img').mousedown(function() {
    $('.destination .multiselect').fadeIn(300);
    hookMultiselectEvent();
    return false;
  });

  $('.travel-agent > input, .travel-agent > img').mousedown(function() {
    $('.travel-agent .multiselect').fadeIn(300);
    hookMultiselectEvent();
    return false;
  });
  
  $('.price-range').slider({
    range: true,
    min: 0,
    max: 10000,
	step: 500,
    values: [ $('.price-from').val(), $('.price-to').val() ],
    slide: function( event, ui ) {
      $('.price-from').val(ui.values[0]);
      $('.price-to').val(  ui.values[1]);
    }
  });

  $('.days-range').slider({
    range: true,
    min: 1,
    max: 21,
    values: [ $('.days-from').val(), $('.days-to').val() ],
    slide: function( event, ui ) {
      $('.days-from').val(ui.values[0]);
      $('.days-to').val(  ui.values[1]);
    }
  });

  $('.standard-range').slider({
    range: true,
    min: 1,
    max: 5,
    values: [ $('.standard-from').val(), $('.standard-to').val() ],
    slide: function( event, ui ) {
      $('.standard-from').val(ui.values[0]);
      $('.standard-to').val(  ui.values[1]);
    }
  });
}


var img_count;
function initOrbit() {
  if ($('.overview .gallery .slideshow').length == 0) return false;

  img_count = $('.gallery .slideshow img').length;
  $('.gallery .slideshow img').each(function(idx) {
    $(this).attr('rel', idx+1);
  });
  
  $('.gallery .counter').text('1 z '+img_count);
  $('.gallery .slideshow').orbit({
    animation: 'fade',
    afterSlideChange: function() {
      $('.gallery .counter').text($(this).attr('rel')+' z '+img_count);
    }
  });
  
}

function initSearchForm() {
  if ($('form.frm-search').length == 0) return false;

  $('.destination > input, .destination > img').mousedown(function() {
    $('.destination .multiselect').fadeIn(300);
    hookMultiselectEvent();
    return false;
  });

  $('.departure input, .return input').datepicker({
    dateFormat: "dd.mm.yy",
    showOn: "both",
    buttonImage: "images/lay-ico-calendar.png",
    buttonImageOnly: true
  });
}

function initSearchButtons() {
	$('#search-button').click(
		function () {
			$('#search\\[mode\\]').val('1');
		}
	);
	$('#filter-button').click(
		function () {
			$('#search\\[mode\\]').val('2');
		}
	);

	$('.depDateCalendar, .retDateCalendar').datepicker({
		dateFormat: "dd.mm.yy",
		showOn: "both",
		buttonImage: "images/lay-ico-calendar.png",
		buttonImageOnly: true
	}); 

}

function initPrintLink() {
	$('#printlink').click(
		function () {
			window.print();
			return false;
		}
	);
}

function initUdogodnienia() {
	$('img.udogodnieniaimg').qtip({
		content: {
		 text: false // Use each elements title attribute
		},
		style: 'light', // Give it some style
		position: {
			target: 'mouse',
			adjust: { mouse: true, screen: true  }

		}	  
   });
}

function initDepDateOnChange() {
	$('#search\\[trp_depDate\\]').change(
		function () {
			var d = $('#search\\[trp_depDate\\]').val();
			var daten = Date.parseExact (d+"","dd.MM.yyyy");
			var newdaten = daten.add(15).days();

			$('#search\\[trp_retDate\\]').val(newdaten.toString("dd.MM.yyyy"));
			
		}
	);
}

function initBasketLink() {
	$('.basketlink').click(
		function ()
		{
			$.post($(this).attr('href'));
			$("#dialog-message").dialog("open");
			return false;
		}
	);
}


function initApplication() {
  initFilterForm();
  initSearchForm();
  initMultiselect();
  initSearchButtons();
  initPrintLink();
  initUdogodnienia();
  initCopycat();
  initDepDateOnChange();
  initBasketLink();

  if ($.browser.msie)    { $("html").addClass("ie"+parseInt($.browser.version)) } else
  if ($.browser.opera)   { $("html").addClass("op"+parseInt($.browser.version)) } else
  if ($.browser.webkit)  { $("html").addClass("wk"+parseInt($.browser.version)) }
  $("input").attr('autocomplete','off');
  
  $('.item dl span.more[title]').qtip({
    content: { text: false },
    style: {
      name: 'blue',
      tip: true
    },
    position: {
      corner: {
         target: 'bottomRight',
         tooltip: 'topLeft'
      }
    },
  });
  
  $('.tabs').tabs();
  $('.slides').tabs({ fx: { opacity: 'toggle' } }).tabs( "rotate", 5000 );
  $('.recommended .offers').tabs({ fx: { opacity: 'toggle' } });
  $('.best .offers').tabs({ fx: { opacity: 'toggle' } });
  $('#topbslide').innerfade({ speed: 'slow', timeout: 5000, type: 'sequence', containerheight: '64px' });
}
$(document).ready( initApplication );
$(window).load( initOrbit );
