jQuery.noConflict();

turnusAvail = "";
searchCalendar = null;
var isGuide = false;

function loadMain(){
	turnusAvail = jQuery("#oferta-box-dialog-turnus-avail").text();
	checkAvail();
	
	//if(jQuery("#tabs-box").size() > 0)
		//jQuery("#tabs-box").tabs('load', jQuery("#tabs-box").tabs("length") - 1);
	loadSkiBookBanner();
	cal_li();
	preinit();
}

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");
	}
	
	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();
	initTerminSelectBox();
	//initDialog();
	initUrls();

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


	jQuery("#geoTreeMoreBox").hide();
        jQuery("#geoTreeToggleLess").hide();
	jQuery("#geoTreeMore").click(function(){
		jQuery("#geoTreeMore").hide();
		jQuery("#geoTreeMoreBox").show();
		jQuery("#geoTreeToggleLess").show();
	});
        jQuery("#geoTreeToggleLess").click(function(){
                jQuery("#geoTreeMore").show();
                jQuery("#geoTreeMoreBox").hide();
                jQuery("#geoTreeToggleLess").hide();
        });

//galeria zakladka
	jQuery("#galleryMoreLess").click(function(){
		if(jQuery(this).attr("src")=="/imgs/przewodnik/arrow_up.jpg"){
			jQuery("#otherLineGallery").hide();
			jQuery("#rewGallery").hide();
			jQuery("#forGallery").hide();
			jQuery("#galleryMoreLess").attr("src","/imgs/przewodnik/arrow_down.jpg");
		}else{
			jQuery("#otherLineGallery").show();
			jQuery("#rewGallery").show();
			jQuery("#forGallery").show();
			jQuery("#galleryMoreLess").attr("src","/imgs/przewodnik/arrow_up.jpg");
		}
	});

	var params = {
	        quality: "high",
	        allowscriptaccess: "always"
			
	    };
	bezdroza1 = "http://www." + varArray["serwis"] +"/flash/bezdroza/bezdroza.swf";
	bezdroza2 = "http://www." + varArray["serwis"] +"/flash/bezdroza/bezdroza2.swf";
	swfobject.embedSWF(bezdroza1, "bezdroza-banner", "174", "174", "9.0.0", "", "", params, "");
	swfobject.embedSWF(bezdroza2, "bezdroza-banner2", "174", "174", "9.0.0", "", "", params, "");	
	/*searchCalendar = new OneCalendar('searchCalendar', 0);
	searchCalendar.cal_d.setMonth(miesiac);
	searchCalendar.cal_d.setDays(dzien);*/
	/*if(!isGuide){
		jQuery("#tabs-box").tabs('select', "gmap");
		jQuery("#tabs-box").tabs("remove",0);
	}*/
	var rodzajStrony = getParameter('p');
                                if (rodzajStrony == 'az') {
					if (!isGuide)
						jQuery("#tabs-box").tabs("remove",0);
                                        jQuery("#tabs-box").tabs('select', "bezdroza_AZ");
                                        var targetOffset = jQuery("#tabs-box").offset().top;
                                        jQuery("html, body").animate({scrollTop: targetOffset}, 400);
                                } else if (rodzajStrony == 'inf') {
					if (!isGuide)
						jQuery("#tabs-box").tabs("remove",0);
                                        jQuery("#tabs-box").tabs('select', "bezdroza_info");
                                        var targetOffset = jQuery("#tabs-box").offset().top;
                                        jQuery("html, body").animate({scrollTop: targetOffset}, 400);
				} else {
					if (!isGuide) {
						jQuery("#tabs-box").tabs('select', "gmap");
						jQuery("#tabs-box").tabs("remove",0);
					}
				}
}

function checkAvail(){
	if(turnusAvail != ""){
		//turnOffReserveButton();
		infoDialog("Sprawdzam dostępność oferty ...", "wait");
		jQuery.ajax({
			url: turnusAvail,
			dataType: 'xml',
			success: function(xml) {
				msgNumber = jQuery(xml).find("MessageNumber").text();
				msgType = jQuery(xml).find("MessageType").text();
				statusDialog = jQuery(xml).find("DialogStatus").text();
				textDialog = jQuery(xml).find("DialogText").text();
				debug = jQuery(xml).find("Debug").text();
				
				//alert(textDialog);
				infoDialog(textDialog,statusDialog,debug);
				if(statusDialog=="ok"){
					turnOnReserveButton();
				}
			},
			error: function(){
				turnOnReserveButton();
			}
		});	
	}else if(jQuery("#oferta-box-dialog-text").text() == ''){
		turnOnReserveButton();
	}
}

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

function infoDialog(textDialog, status, debug){
	jQuery("#oferta-box-dialog-wrapper").fadeOut(1000, function(){
		jQuery("#oferta-box-dialog-text").css("line-height","normal");
		jQuery("#oferta-box-dialog-text").attr("class", "state-" + status);
		jQuery("#oferta-box-dialog-text").html(textDialog);
		jQuery("#oferta-box-dialog-text").append(debug);
		//alert(jQuery("#oferta-box-dialog-text").text().length);
		if(jQuery("#oferta-box-dialog-text").text().length < 50){
			jQuery("#oferta-box-dialog-text").css("line-height","30px");
		}
		jQuery("#oferta-box-dialog-wrapper").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 initDialog(){
	jQuery("#dialog").dialog({
		width: 754,
		autoOpen: false,
		bgiframe: true,
		draggable: false,
		resizable: false,
		title: 'Galeria zdjęć', 
		modal: true,
		open: function(event, ui) {
			hideFlashes();
			jQuery("#header4").hide();
            jQuery("#spr-clmn-phn-nmbr, #spr-clmn-pit").hide();
			jQuery(".ui-widget-overlay").click(function(){ 
				jQuery("#dialog").dialog('close'); 
			});
		},
		close: function(event, ui) {
			showFlashes();
			jQuery("#header4").show();
			jQuery("#spr-clmn-phn-nmbr, #spr-clmn-pit").show();
		}
	});
	
	//jQuery(".ui-dialog-titlebar-close")
	jQuery(".ui-dialog").append(jQuery(".ui-dialog-titlebar-close"));
	jQuery(".ui-dialog-titlebar").remove();
}*/

function initTerminSelectBox(){
	jQuery("#oferta-date-turnus-val select,#oferta-date-turnus-zb-val select").change(function(){
		newURL = jQuery(this).attr("value");
		if (newURL != "")
			window.location = newURL; 
	});	
	jQuery("#oferta-date-turnus-val span span").click(function(){
		jQuery("#oferta-date-turnus-val span").hide();
		jQuery("#oferta-date-turnus-val select").show();
	});
	jQuery("#oferta-date-turnus-val select").blur(function(){
		jQuery("#oferta-date-turnus-val span").show();
		jQuery("#oferta-date-turnus-val select").hide();
	});
}

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

function initTabsBox(){
	jQuery("#tabs-box").tabs({ 
		spinner: '<em class="tabs-loader">Wczytuję&#160;&#160;&#160;&#160;&#160;&#160;</em>',
		cache: true,
		load: function(event,ui){
			if(ui.panel.id == "offers"){
				
				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);
			}
		}
	});

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 ul.ui-tabs-nav li").each(function(index){
		jQuery(document.createElement('li')).css({ width:"1px", "background-color":"#ffffff", height:"22px" }).insertAfter(this);
	});*/
	//jQuery("#tabs-box ul.ui-tabs-nav li:first a").addClass("corner-top-left");
	//jQuery("#tabs-box ul.ui-tabs-nav li:first a").addClass("corner-top-left");
	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();	
				});
				});
};

/* 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;
}



