jQuery.noConflict();

turnusAvail = "";

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

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();

	if(jQuery("#meteo__weather").length) // jeśli meteo istnieje
		initMeteo();

	if(jQuery("#zoover").length)
		initZoover();
}

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 < 62){
			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 initMeteo() {
	new Image().src = "/imgs/meteo/zajawka/button-pogoda-hover.png";
	new Image().src = "/imgs/meteo/zajawka/button-klimat-hover.png";

	//clock inicjacja
	time = new Date();
	
	var offset = jQuery(".main-box-clock-offset-hidden").html();
	var godzina = 0;
	var minuta = 0;
		
	if(offset != null && offset.indexOf(":") != -1) {
		var tab = offset.split(":");
		godzina = tab[0];
		minuta = tab[1];
		if(godzina < 0)
			minuta *= -1;
	}
	else
		godzina = offset;
			
	time.setTime(time.getTime() + 3600000*godzina + 60000*minuta);
	
	displayClock();
	
	//klimat kiedy-jechac
	jQuery("#meteo-zakladka-klimat-kiedy-jechac-box ul li").each(function(index) {
		if(index > 1) jQuery(this).hide();
	});
	var hiddenKiedyJechac = true;
	jQuery("#meteo-zakladka-kiedy-jechac-tail-link").click(function() {
		if (hiddenKiedyJechac) {
			jQuery("#meteo-zakladka-klimat-kiedy-jechac-box ul li").each(function(index) {
				if(index > 1) jQuery(this).show();
			});
			jQuery(this).html("ukryj");
			jQuery("#meteo-zakladka-kiedy-jechac-tail-arrow").html("&#x25B2;");
			jQuery("#meteo-zakladka-klimat-kiedy-jechac-text-box").show();
			hiddenKiedyJechac = false;
		}
		else {
			jQuery("#meteo-zakladka-klimat-kiedy-jechac-box ul li").each(function(index) {
				if(index > 1) jQuery(this).hide('slow');
			});
			jQuery(this).html("dowiedz się więcej");
			jQuery("#meteo-zakladka-kiedy-jechac-tail-arrow").html("&#x25BC;");
			jQuery("#meteo-zakladka-klimat-kiedy-jechac-text-box").hide('slow');
			hiddenKiedyJechac = true;
		}
		return false;
	});
}

function initZoover() {
	jQuery("#zoover-zajawka-tail-1-box a").click(function() {
		var url = jQuery("#zoover-zajawka-tail-1-box a").attr("href");
		window.open(url, 'zoover', 'left=50,top=50,width=770,height=550,scrollbars=1');
		return false;
	});
	
	new Image().src = "/imgs/zoover/button-hover.jpg";
	new Image().src = "/imgs/zoover/head-right-hover.jpg";
	
	jQuery("#zoover-zajawka-tail-1-box img").hover(function() {
		jQuery(this).attr("src", "/imgs/zoover/button-hover.jpg");
	}, function() {
		jQuery(this).attr("src", "/imgs/zoover/button.jpg");
	});

	jQuery("#zoover-zajawka-head-right-box").hover(function() { //IE6 nie obsługuje w CSS pseudoklasy hover dla elementu div
		jQuery(this).css("cursor", "pointer");
		jQuery(this).css("background-image", "url('/imgs/zoover/head-right-hover.jpg')");
	}, function() {
		jQuery(this).css("cursor", "auto");
		jQuery(this).css("background-image", "url('/imgs/zoover/head-right.jpg')");
	});
}

function loadInfoBanner(serwis, nr_tel){
	var params = {
		quality: "high",
		allowscriptaccess: "always",
		wmode: "transparent"
	};
	swfobject.embedSWF("http://www.travelone.pl/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.travelone.pl/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('.meteo__weatherLink').click(function() {
		jQuery("#tabs-box").tabs('select', "meteo__weather");
		return false;
	});

	jQuery('.meteo__climateLink').click(function() {
		jQuery("#tabs-box").tabs('select', "meteo__climate");
		return false;
	});

	jQuery('.zoover-tab-link').click(function() {
		jQuery("#tabs-box").tabs('select', "zoover");
		return false;
	});
	
	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();	
				});
				});
};


