var isIE = /*@cc_on!@*/false;

jQuery(document).ready(function() {

	jQuery('.korp .box h2').each(function() {
		jQuery(this).wrapInner('<span class="in"><span class="in2"><span class="in3"></span></span></span>');
	});
	
	jQuery('ul#mainnav').find('li').append(jQuery('<span />').addClass('sep'));


	jQuery('#intro .gallery').append('<div class="title"><div class="prev"><div class="inside"></div></div><h3>'+ jQuery('#intro .gallery > ul > li > img:first').attr('alt') +'</h3><div class="next"><div class="inside"></div></div></div>');
	
	var defTitleHWidth = parseInt(jQuery('#intro .gallery .title h3').width() / 2);
	var defMoveTitleDist = 620;
	var moveTitleDist = defMoveTitleDist - defTitleHWidth;
	
	jQuery('#intro .gallery li.active').removeClass('active').addClass('current').show();
	
	var galleryMode = false;
	jQuery('#intro .gallery .title > div').mousedown(function() {
		jQuery(this).addClass('mousedown');
	}).mouseup(function() {
		jQuery(this).removeClass('mousedown');
	}).click(function() {
		if (!galleryMode) {
			jQuery('#intro .boxSpecOffer').animate({
				left: '+=339'
			}, 500);
			jQuery('#intro .gallery .title').animate({
				left: moveTitleDist
			}, 500);
			galleryMode = true;
		}
		
		if (jQuery(this).hasClass('next')) {
			jQuery('#intro .gallery li.current').each(function() {
				jQuery(this).fadeOut(500).removeClass('current');
				if (jQuery(this).next().size() > 0) {
					jQuery(this).next().fadeIn(500).addClass('current');
				} else {
					jQuery('#intro .gallery li:first').fadeIn(500).addClass('current');
				}
			});
		} else if (jQuery(this).hasClass('prev')) {
			jQuery('#intro .gallery li.current').each(function() {
				jQuery(this).fadeOut(500).removeClass('current');
				if (jQuery(this).prev().size() > 0) {
					jQuery(this).prev().fadeIn(500).addClass('current');
				} else {
					jQuery('#intro .gallery li:last').fadeIn(500).addClass('current');
				}
			});
		}
		if (jQuery('#intro .gallery .title h3').text() != jQuery('#intro .gallery li.current img').attr('alt')) {
			jQuery('#intro .gallery .title h3').text(jQuery('#intro .gallery li.current img').attr('alt'));
			Cufon.replace('#intro .gallery .title h3');
			jQuery('#intro .gallery .title').css('left', defMoveTitleDist - parseInt(jQuery('#intro .gallery .title h3').width() / 2));
		}
	});
	
	/*
	jQuery('.tabsC .tabs a').click(function() {
		if (!jQuery(this).hasClass('active')) {
			jQuery('.tabsC .tabs a').removeClass('active');
			jQuery(this).addClass('active');
			jQuery('.tabsC .parts > .part').hide();
			var partID = jQuery(this).attr('href');
			jQuery(partID).fadeIn(300);
		}
		return false;
	});
	*/
	
	if (jQuery('.mGallery').size() > 0) {
		jQuery('.mGallery').jCarouselLite({
			btnNext: '.mgNext',
			btnPrev: '.mgPrev',
			visible: 5,
			scroll: 3
		});
	}
	
	jQuery('.mgPrev, .mgNext').fadeTo(1, 0.4);
	jQuery('.mgPrev, .mgNext').hover(function() {
		jQuery(this).fadeTo(1, 0.7);
	}, function() {
		jQuery(this).fadeTo(1, 0.4);
	}).mousedown(function() {
		jQuery(this).fadeTo(1, 1);
	}).mouseup(function() {
		jQuery(this).fadeTo(1, 0.7);
	});
	
	jQuery('.acc .accDetails').hide();
	jQuery('.acc li h5').hover(function() {
		jQuery(this).parent().addClass('hover');
	}, function() {
		jQuery(this).parent().removeClass('hover');
	}).toggle(function() {
		jQuery(this).parent().addClass('full').find('.accDetails').slideDown(200);
	}, function() {
		jQuery(this).parent().removeClass('full').find('.accDetails').slideUp(200);
	});
	
	jQuery('thead th:first-child').addClass('first');
	jQuery('thead th:last-child').addClass('last');
	jQuery('tbody > tr:first-child').each(function() {
		jQuery(this).find('th:first-child, td:first-child').addClass('firstInFirstRow');
		jQuery(this).find('td:last-child').addClass('lastInFirstRow');
	});
	jQuery('tbody > tr:last-child').each(function() {
		jQuery(this).find('th:first-child, td:first-child').addClass('firstInLastRow');
		jQuery(this).find('td:last-child').addClass('lastInLastRow');
	});
	jQuery('table tr:first-child').addClass('firstRow');
	
	if(typeof jQuery(this).tabs == 'function') {
		jQuery('body.korp div.tabs > ul').each(function() {
			jQuery(this).tabs(jQuery(this).parent().parent().find('div.panes > div'));
		});
	}
														
	if(typeof jQuery(this).tabs == 'function') {
		jQuery('div.tabs > ul:not(.headTabs)').not('body.korp ul').each(function() {
			jQuery(this).tabs(jQuery(this).parent().parent().find('div.panes > div'), {
		rotate: true
	}).slideshow({
		autoplay:true,
		interval:6000,
		clickable:false
	});
		});
		jQuery('div.tabs > ul > li > a.active').click();
	}
	
	jQuery("div.tabs ul.headTabs").tabs(".panes > div");

	
	jQuery('input.text, textarea').focus(function() {
		jQuery(this).addClass('textFocus');
	}).blur(function() {
		jQuery(this).removeClass('textFocus');
	});
	
/*	=adresy pocztowe
---------------------------------------------------------------------------- */

	//  - => .
	// () => @
	jQuery('.adresPocztowy').each(function() {
		var adres = jQuery(this).text();
		adres = adres.replace('()', '@');
		adres = adres.replace(/-/g, '.');
		if (jQuery(this).hasClass('klikalny')) {
			adres = '<a href="mailto:' + adres + '">' + adres + '</a>';
		}
		jQuery(this).html(adres);
	});

/*	=
---------------------------------------------------------------------------- */

	jQuery('h1.hOFirmie').parent().css('min-height', '312px');
	jQuery('div.corporateMain').css('min-height', '359px');

	jQuery('.boxNaszeOfertyT .oferta').each(function() {
		jQuery(this).append('<div class="corners"><div class="c1"/><div class="c2"/><div class="c3"/><div class="c4"/></div>');
	});
	
	var ph = 0;
	jQuery('.tabContainer > .panes > div').each(function() {
		if (jQuery(this).height() > ph) {
			ph = jQuery(this).height();
		}
	});
	jQuery('.tabContainer > .panes > div').height(ph + 15);
	
	var ph2 = 0;
	jQuery('.tabsC > .panes > div').each(function() {
		if (jQuery(this).height() > ph2) {
			ph2 = jQuery(this).height();
		}
	});
	jQuery('.tabsC > .panes > div').not('.boxNaszeOfertyT .panes > div, .boxInGruStar .panes > div').height(ph2 + 15);
	
	jQuery('.kartyProgramy li').not('.wide').css('cursor', 'pointer').click(function() {
		location.href = jQuery(this).find('h3 > a').attr('href');
	});
	
	var recomendations = jQuery('.boxRecommendations');
	
	function getXML(path, div){
		var table = div.find('table');
		jQuery.ajax({
			type: 'get',
			url: path,
			dataType: 'xml',
			beforeSend: function(){
				div.append($('<div />').addClass('loader'));
			},
			success: function(data){
				div.find('.loader').remove();
				var xml = jQuery.xml2json(data),
					rekomendacje = xml.rekomendacja,
					size = rekomendacje.length,
					j=1,
					arr = [];
					
				for (var i in rekomendacje){
					var r = rekomendacje[i];
					odd = i%2==1 ? ' class="odd"' : '';
					if (i%20==0){ arr.push('<tbody class="page_0'+(j++)+'">'); }
					arr.push('<tr'+odd+'>');
					arr.push('<td class="data">'+r.data.split(' ')[0]+'</td>');
					arr.push('<td>'+r.instytucja+'</td>');
					arr.push('<td>'+r.charakter+'</td>');
					arr.push('<td><strong>'+r.cena_docelowa+'</strong></td>');
					arr.push('<td><strong>'+r.cena_w_dniu_wydania+'</strong></td>');
					arr.push('</tr>');
					if (i%20==20){ arr.push('</tbody>'); }
				}
				var html = arr.join('');
				table.append(html);
				createNav(table);
			}
		});
	}
	
	if (recomendations.length)
		getXML('/themes/orbis/xml/rekomendacje.xml', recomendations);
	
	
	function createNav(element){
		pageSize = element.find('tbody').size();
		$pagination = jQuery('<div class="pagination paginationMP"><ul></ul></div>');
		for (i=1; i<=pageSize; i++) {
			$pagination.find('ul').append('<li><a href="#">'+i+'</a></li>');
		}
		$pagination.find('li:first').addClass('active').before('<li class="prev" style="display: none;"><a href="#">&lsaquo;</a></li>');
		$pagination.find('li:last').after('<li class="next" style="display: none;"><a href="#">&rsaquo;</a></li>');
		$pagination.insertAfter(element);
	}
	
	var $tableMultipage = jQuery('table.multiPage');
	$tableMultipage.find('tbody').not('.page_01').hide();
	$tableMultipage.each(function() {
		createNav(jQuery(this));
	});
	
	jQuery('div.paginationMP a').live('click', function() {
		acMPNo = $tableMultipage.find('tbody:visible').attr('class').substr(5, 2);
		//console.log(acMPNo);
		if (jQuery(this).parent().hasClass('prev')) {
			//
		} else if (jQuery(this).parent().hasClass('next')) {
			//
		} else {
			$tableMultipage.find('tbody').hide();
			$tableMultipage.find('tbody.page_0'+jQuery(this).text()).show();
			jQuery('div.paginationMP li').removeClass('active');
			jQuery(this).parent().addClass('active');
		}
		return false;
	});
	

	
	if (isIE) {
		
		
		
		jQuery('.korp .box').each(function(i) {
			var pos = jQuery(this).position();
			jQuery(this).after('<div class="IEBoxShadow" id="IEBoxShadow'+i+'"></div>');
			if (jQuery.browser.msie && !jQuery.browser.version == "8.0") {
				jQuery('#IEBoxShadow'+i).width(jQuery(this).width()+11).height(jQuery(this).height()).css('left', (pos.left - 5) + 'px').css('top',(pos.top - 4) + 'px');
			}
		});		
		
		jQuery('#mainnav span').wrap('<em class="m"></em>');
		jQuery('#mainnav a')
		.append('<em class="c c101"/><em class="c c102"/><em class="c c103"/><em class="c c104"/><em class="c c105"/><em class="c c106"/>')
		.append('<em class="c c201"/><em class="c c202"/><em class="c c202"/><em class="c c203"/><em class="c c204"/><em class="c c205"/>');
		
		jQuery('.boxAkcje, .boxRightInformacje').filter('.boxRight').next().filter('.IEBoxShadow').each(function() {
			var pos2 = jQuery(this).position();
			jQuery(this).css('left', (pos2.left + 15));
		});

	}
	
	
	//
	//		readMoreSlideUpDown
	//
	
	
	jQuery("a.readMoreSlide").toggle(function(){
			
		jQuery(this).parent().prev().animate({height: "200px"}, 500);
		jQuery(this).parent().removeClass("moreBtn").addClass("lessBtn");
				  
	}, 
	function(){
			
		jQuery(this).parent().prev().animate({height: "40px"}, 500);
		jQuery(this).parent().removeClass("lessBtn").addClass("moreBtn");
						  
	});
	
	//
	//
	//
	
	var regex = /[a-z]+/
	
	jQuery("input.validate").bind("focusout", function(){
								   
								   
				if(jQuery(this).val() == "")
				{
					if(jQuery(this).parent().children("p.errorP").css("display") == "block"){
						
						
						
					} else {
					
						jQuery(this).parent().prepend('<p class="errorP">Wprowadź nazwę</p>');
					
					}
					
					jQuery(this).addClass("errorInput");
					
				}
				
				if(regex.test(jQuery(this).val()) == true)
				{
					
					jQuery(this).parent().children("p.errorP").slideUp(function(){jQuery(this).remove();});
					jQuery(this).removeClass("errorInput");
					
				}
			
	
	});
	
	//
	//		hotelsPop
	//
	
	jQuery(".hotelsPop").hide();
	
	jQuery("a.hotelsPopShow").toggle(function(){
		jQuery(this).parent().next().show();
		return false;
	}, function(){
		jQuery(this).parent().next().hide();
	});
	
	jQuery("#intro2Section .colFull .panes .part:eq(0)").show();

	jQuery('.boxMiceList').append('<span class="miceTop" /><span class="miceBottom" />');
	
	jQuery('.infoTable').append('<span class="top" /><span class="bottom" />');
	
	jQuery('.pdfBtn').append('<span />');

	jQuery(".miceGallery").scrollable({
		circular: true
	});
	
	jQuery(".galNav").find('.nav').click(function(){
		return false;
	});


	//////////////
	jQuery('.btns.compare').click( function(){
		var count;
		jQuery('.listContent').find('input:checked').each( function(i){ count = i+1; });

		if (count == undefined){
			alert('Wybierz hotele do porównania');
			return false;
		} else {
			return true;
		}
    });
	jQuery('input[type=checkbox][name=obiekt[]]').click( function(){
		
		var count;
		jQuery('.listContent').find('input:checked').each( function(i){ count = i+1; });

		if (count >= 3){
			jQuery('.listContent').find('input[type=checkbox]').each(function(){
				var t = jQuery(this),
				unchecked = t.filter(function(){
					return jQuery(this).attr('checked') == false;
				});
				unchecked.parents('.compares').hide();
			});
		} else {
			jQuery('.listContent').find('.compares').show();
		}
		
		if (count == undefined){
			jQuery('.btns.compare').hide();
		} else {
			jQuery('.btns.compare').show();
		}
		
    });
	
	if (jQuery('.miceGallery').length)
		jQuery('.miceGallery').find('a').fancybox();
	
	var fbcaro =  jQuery('.fbCaroInside'),
		fbglow = jQuery('.fbGlow'),
		fbimg = fbcaro.find('img').length;
	
	fbcaro.css({ width: fbimg*130+'px' });
	var fbcaro_width = fbcaro.width();
	
	jQuery('.boxGalleryFB').find('.next').click(function(){
		var t = jQuery(this), prev = t.prev('.prev'), position_left = fbcaro.position().left;
		if (!fbcaro.is(':animated')) {
			if (fbcaro_width > -(position_left-520)){
				t.removeClass('disabled');
				prev.removeClass('disabled');
				fbcaro.animate({ left: '-=130px'  }, function(){
				 	if (fbcaro_width == -(fbcaro.position().left-520)) {
						t.addClass('disabled');
						fbglow.fadeOut();
					}
				 });
			}
		}
		return false;
	});
	
	jQuery('.boxGalleryFB').find('.prev').click(function(){
		var t = jQuery(this), next = t.next('.next'), position_left = fbcaro.position().left;
		if (!fbcaro.is(':animated')){
			if (position_left < 0) {
				t.removeClass('disabled');
				next.removeClass('disabled');
				if (!fbglow.is(':visible'))
					fbglow.fadeIn();
				fbcaro.animate({
					left: '+=130px'
				}, function(){
					if (fbcaro.position().left == 0) {
						t.addClass('disabled');
					}
				});
			}	
		}
		return false;
	});
	
	jQuery('.ifIata').click(function(){
		var t = jQuery(this), div = t.parents('.bpForm'), p = div.find('.hidden');
		if(t.hasClass('iataTrue')){
			p.slideDown();
		} else {
			p.slideUp();
		}
	});
	
});

// cufón
Cufon.replace('h2, #intro .gallery .title h3, .brand #intro3Section .boxOferta h3, #rezContainer .header h3, .kartyProgramy li h3 a, .boxNaszeOfertyT .oferta h3, .b2b .boxInGruStar .tabs a, ul.wOfercie a, .minisite h1, .minisite h3, .minisite h4, .box h3, .paybackPartners .title, .listTitle ul li, .pdfBtn, .bgImg .text');
Cufon.replace('#intro .tabs a', {
	fontSize: '16px'
});
Cufon.replace('#mainnav span', {
	fontSize: '14px'
});
Cufon.replace('#intro3Section .col h2, .bpForm span.cdh', {
	fontSize: '15px'
});
Cufon.replace('.korp .box h2', {
	fontSize: '14px'
});
Cufon.replace('.korp .boxI h2', {
	fontSize: '20px'
});
Cufon.replace('.korp #content h1', {
	fontSize: '15px'
});
Cufon.replace('#kbnav li', {
	fontSize: '12px',
	hover: true
});
Cufon.replace('.korp table.kalendarz tbody th', {
	fontSize: '13px'
});
Cufon.replace('.btnD a', {
	fontSize: '13px'
});
Cufon.replace('.btnD a, .btnS a', {
	fontSize: '12px'
});
Cufon.replace('.boxNaszeOfertyT .oferta h4', {
	fontSize: '14px'
});

if (isIE) {
	//DD_roundies.addRule('.korp #main, .korp .box, .korp .box h2, .korp .box h2 span', '7px');
	DD_roundies.addRule('.boxR .inSec', '5px');
	//DD_roundies.addRule('.korp .tabs', '7px 7px 0 0');
	//DD_roundies.addRule('#intro .boxRezerwacja .tabs .first a', '5px 0 0 0');
}


