jQuery.noConflict();

urlForSearcher = "";

function loadMain(){
	loadSkiBookBanner();
}

function readyMain(){
	jQuery("#oferta-box-dialog-text:empty").parent().parent().hide();
	if(jQuery("#oferta-box-dialog-text").text() != ""){
		jQuery("#oferta-box-dialog-wrapper").hide();
		infoDialog(jQuery("#oferta-box-dialog-text").html(), "info");
	}
	initMiniSearcher();
	initTabsBox();
	secure_reservation();
	if(jQuery("#hotel-tourop").css("display") == "block")
		initImages("#hotel-photo a");
	if(jQuery("#hotel").css("display") == "block")
		initImages("#country-photo > a");
	if (jQuery("#gmap").css("display") == "block") {
		showMap('mapaOkolicy');
		loadScript();
	}
	initImageCarousel();

	initMeteo(); // Kod zabezpieczony od wewnątrz
	initZoover(); // Kod zabezpieczony od wewnątrz

	var rodzajStrony = getParameter('p');
        if (rodzajStrony == 'opinie') {
                jQuery("#tabs-box").tabs('select', "zoover");
                var targetOffset = jQuery("#tabs-box").offset().top;
                jQuery("html, body").animate({scrollTop: targetOffset}, 400);
        }
}

/* Odczytywanie parametru GET */
function getParameter(paramName) {
  var searchString = window.location.search.substring(1), i, val, params = searchString.split("&");
  for (i = 0; i < params.length; ++i) {
    val = params[i].split("=");
    if (val[0] == paramName) {
      return unescape(val[1]);
    }
  }
  return null;
}

function turnOnReserveButton(){
	jQuery("#oferta-box-reserve-btn > img").fadeOut(1000,function(){
		jQuery("#offer-reserve").fadeIn(2000);
	});
}

function initUrls(){
	jQuery("#oferta-box-more-doplata").toggle(
		function(){
			jQuery("#oferta-box-properties").show();
			jQuery("#oferta-box-more-doplata").html("ukryj dodatkowe informacje");
		},function(){
			jQuery("#oferta-box-properties").hide();
			jQuery("#oferta-box-more-doplata").html("zobacz dodatkowe informacje");
		});	
	jQuery("#oferta-price-val a").click(function(){
		if(jQuery("#oferta-box-properties").css("display") == "none"){
			jQuery("#oferta-box-more-doplata").click();
			jQuery("#oferta-box-properties").show();
			window.location = "#k2_doplata";
		}
	});
	jQuery(".oferta-box-more-tourop").click(function(){
		if(jQuery("#hotel-tourop").css("display") == "none"){
			jQuery("#tabs-box").tabs("select","hotel-tourop");	
		}
	});
}

function loadInfoBanner(serwis, nr_tel){
	var params = {
		quality: "high",
		allowscriptaccess: "always",
		wmode: "transparent"
	};
	swfobject.embedSWF("http://www." + varArray["serwis"] +"/flash/oferta/infolinia_oferta_small.swf?ClickTag=http://www."+ serwis +"&nr="+ nr_tel +"&panstwo="+ serwis, "oferta-box-infolinia", "183", "70", "9.0.0", "", "", params, "", "");
	jQuery("#oferta-box-reserve-btn:empty").next().css("float","none");
}

function loadSkiBookBanner(){
		var params = {
		quality: "high",
		allowscriptaccess: "always",
		wmode: "transparent"
	};
	swfobject.embedSWF("http://www." + varArray["serwis"] +"/flash/bannery/billboard_183x140.swf", "hotel-secure-reserve-box-banner", "183", "140", "9.0.0", "", "", params, "", "");
}

function initMiniSearcher(){
	var searcher = "&searcher=1";
	urlForSearcher = jQuery("#url-for-searcher").text();
	var direct = jQuery("#direct-name").text();
	//alert(direct);
	jQuery.ajax({
		url: urlForSearcher+searcher,
		dataType: 'html',
		success: function(data) {
			jQuery(".form_zaplanuj_wakacje").replaceWith(data);
			// jeżeli mamy hotel direct i ma on aktualne terminy to wyświetlamy box info o rezerwacji itp itd.. 
			if (direct != "" && jQuery(".plan_wakacje_term select option").size() != 0){ 
				jQuery("#oferta-box-direct").fadeIn(1000);
				jQuery("#obiekt-box-direct-text").fadeIn(1000);
				jQuery("#tabs-box ul.ui-tabs-nav li").children().each(function(index,val){
						if(jQuery(this).filter("[title='offers']").size() == 1){
							jQuery("#tabs-box").tabs("enable",index);
							var newURL = urlForSearcher+"&date="+jQuery("select[name='dlugosc_pobytu'] option:eq(1)").val();
							//alert(newURL);
							jQuery("#tabs-box").tabs( 'url' , index , newURL);
						}
				});
			
			
			} 
			// zwykłe hotel - pojawia się wyszukiwarka + zakładka opcje wyajzdu jest aktywna
			if (jQuery(".plan_wakacje_term select option").size() != 0 && direct==""){
				jQuery(".plan_wakacje_term").fadeIn(1000);
				jQuery("#tabs-box ul.ui-tabs-nav li").children().each(function(index,val){
						if(jQuery(this).filter("[title='offers']").size() == 1){
							jQuery("#tabs-box").tabs("enable",index);
							var newURL = urlForSearcher+"&date="+jQuery("select[name='dlugosc_pobytu'] option:eq(1)").val();
							//alert(newURL);
							jQuery("#tabs-box").tabs( 'url' , index , newURL);
						}
				});
			}
			jQuery("select[name='dlugosc_pobytu']").change(function(){
				jQuery("#tabs-box ul.ui-tabs-nav li").children().each(function(index,val){
						
					if(jQuery(this).filter("[title='offers']").size() == 1){
						var newURL = urlForSearcher+"&date="+jQuery("select[name='dlugosc_pobytu'] option:selected").val();
						//alert(newURL);
						jQuery("#tabs-box").tabs( 'url' , index , newURL);
						if(index == jQuery("#tabs-box").tabs('option', 'selected')){
							jQuery("#tabs-box").tabs('load',index);
						}
						jQuery("#tabs-box").tabs("enable",index);
						jQuery("#tabs-box").tabs('select',index);
						
					}
				});
			});
		},
		error: function(){
			jQuery(".plan_wakacje_term").remove();
		}
	});	
}

function initImageCarousel(){
	jQuery('#oferta-box-carousel').jcarousel({
        size: mycarousel_itemList.length,
        itemLoadCallback: {onBeforeAnimation: mycarousel_itemLoadCallback}
    });	
}

function initTabsBox(){
	var indexOfOffers;
	jQuery("#tabs-box ul li").children().each(function(index){
			//alert(index);
			if(jQuery(this).filter("[title='offers']").size() == 1){
				indexOfOffers=index;
			};
	});
	
	jQuery("#tabs-box").tabs({ 
		spinner: '<em class="tabs-loader">Wczytuję&#160;&#160;&#160;&#160;&#160;&#160;</em>',
		cache: true,
		disabled: [indexOfOffers],
		load: function(event,ui){
				jQuery("#hotel-offers tr:[href]").each(function(){
					if(jQuery(this).attr("id") == jQuery("#oferta-box-kod span").html()){
						jQuery(this).remove();
					}else{
						jQuery(this).click(function() {
							url = jQuery(this).attr("href");
							location.href = url;
						});
					}
				});
				if(jQuery("#hotel-offers tr:[href]").size() == 0)
					jQuery("#tabs-box").tabs("disable",ui.index);
		}
	});
	//if (jQuery(".plan_wakacje_term select option").size() == 0){	
	//	
	//}
		
	jQuery("#tabs-box ul.ui-tabs-nav li").each(function(index){
		jQuery(document.createElement('li')).css({ 	'background-image':'url(/imgs/obiekt/separator_05.png)',
								'background-repeat':'no-repeat',
								'float':'left',
								'height':'22px',
								'width':'2px',
								'border-bottom-width':'thin',
								'border-top-style':'none',
								'border-right-style':'none',
								'border-bottom-style':'solid',
								'border-left-style':'none',
								'border-bottom-color':'#56a7f8' }).insertAfter(this);
	});
	jQuery("#tabs-box").bind('tabsselect', function(event, ui){
		if (ui.panel.id == "gmap"){
			if (map == undefined) {
				showMap('mapaOkolicy');
				loadScript();
			}
		}
		if (ui.panel.id == "hotel-tourop"){
			initImages("#hotel-photo a");
		}
		if (ui.panel.id == "hotel"){
			initImages("#country-photo > a");	
		}
	});
	jQuery("#hotel-photo a, #country-photo > a, #country-photo > div > a").click(function(){
		showDialog(jQuery(this).attr("href"), 580, 750); return false;
		});
			
}

function initImages(imagesDiv){
	//alert(imagesDiv);
	jQuery(imagesDiv).each(function(id){
		if (jQuery(this).children().size() == 0) {
			//alert(imagesDiv);
			imgURL = jQuery(this).text();
			jQuery(this).html('<img src="' + imgURL + '" width="174"/>');
		}
	});
}

function showMap(nazwa){
	if(nazwa=='mapaOkolicy') {
		jQuery("#gmap").show();
    }
}

function hideMap(){
        jQuery("#gmap").hide();
	clearMarkers();
	removeGoogleMapsDynamic();
}

function mycarousel_itemLoadCallback(carousel, state)
{
    for (var i = carousel.first; i <= carousel.last; i++) {

    	if (carousel.has(i)) {
            continue;
        }
        if (i > mycarousel_itemList.length) {
            break;
        }
        //var preloadImg = new Image();
		newElem = carousel.add(i, mycarousel_getItemHTML(mycarousel_itemList[i-1]));
        newElem.children().click(function(){
			newIndex = jQuery("ul#oferta-box-carousel li").index(jQuery(this).parent());
			newImg = mycarousel_itemList[newIndex];
			jQuery("#oferta-box-img-wrapper tr td img").hide();
			jQuery("#oferta-box-img-wrapper tr td").addClass("loading");
			
			var nImg = new Image();
			jQuery(nImg).load(function(){
				
				if (this.width > 300) {
					this.width = 300;
				}
				if (this.height > 200) {
					this.height = 200;
				}
				jQuery("#oferta-box-img-wrapper tr td").removeClass("loading");
				jQuery("#oferta-box-preview-navi:hidden").show();
				jQuery("#oferta-box-img-wrapper tr td img").replaceWith(this);
				jQuery(this).fadeIn("normal");
				
				jQuery("#oferta-box-preview img, #oferta-box-preview span").unbind("click");
				jQuery("#oferta-box-preview img, #oferta-box-preview span").click(function(){
					//url = window.location + '&slide=' + newImg.slide;
					//jQuery("#dialog:empty").append('<iframe width="750px" height="580px" frameborder="0" src/>');
					//jQuery("#dialog iframe").attr("src",newImg.slide);
					//jQuery("#dialog").dialog("open");
					showDialog(newImg.slide, 580, 750);
				});
			}).attr("src",newImg.ref);
			
			jQuery("#oferta-box-preview-navi1").html("Zdjęcie " + (newIndex + 1) + "/" + mycarousel_itemList.length);
		});
		
		if(i==1){
			newElem.children().click();	
		}
	}
};


function mycarousel_getItemHTML(item)
{
	return '<img src="' + item.url + '" width="75" height="50"/>';
};

function secure_reservation(){
			jQuery("#pokaz").click(function(){
				jQuery("#secure-rsv-pokaz").hide();
				jQuery("#secure-rsv-ukryj").show();
				jQuery("#secure-rsv-btn-pokaz").hide();
				jQuery("#secure-rsv-btn-ukryj").show();
				jQuery("#secure-rsv-full").slideDown("slow");
			});
			jQuery("#ukryj").click(function(){
				jQuery("#secure-rsv-full").slideUp("slow", function(){
					jQuery("#secure-rsv-ukryj").hide();
					jQuery("#secure-rsv-btn-ukryj").hide();
					jQuery("#secure-rsv-pokaz").show();
					jQuery("#secure-rsv-btn-pokaz").show();	
				});
				});
};



