/*** MODIFIED BY LUKASZ  ***/


var $j = jQuery.noConflict();


$j(document).ready(function(){
	
	$j('#numberOfRoomsChanged').change(function(){
		//$j(".link-on-bar").click();
		var ilePokoi = $j(this).find('option:selected').val();
		//alert(ilePokoi);
		if (ilePokoi == '4') {
			$j(".link-on-bar").eq(0).click();
		}
			$j('.wp-goscie tr').each(function(i){
				if (i > ilePokoi) 
					$j(this).hide();
				else 
					$j(this).show();
			});
	});
	
	
	
	$j("select[name='childAges']").change(function(){
		var childAgesInRoomValue ="";
		$j("select[name='childAges']").each(function(){
			if($j(this).css('display') != 'none')
				childAgesInRoomValue += $j(this).val() + ",";
		});
		$j("#" + $j("#currentRoomSelected").val()).val(childAgesInRoomValue.substring(0,childAgesInRoomValue.length-1));
		//alert(childAgesInRoomValue.substring(0,childAgesInRoomValue.length-1));
	});
	
	$j("input[name='amenitiesCheckbox']").change(function(){
		if($j(this).is(':checked'))
			$j("#"+$j(this).val()).val('1');
		else
			$j("#"+$j(this).val()).val('0');
		//alert('');
	});
	
	// wyskakujace okienka przy parametrach
	
	$j('a.cluetip-icon').click(function(){
		$j('a.cluetip-icon').removeClass("cluetip-show");
		
		if ($j(this).attr("rel") == "#tooltip-content-rooms") {
			$j('#tooltip-content').text($j("#tooltip-content-rooms").text());
			
			var roomId = $j(this).parent().prev().prev().text();
			roomId = roomId.charAt(roomId.search(/\d/));
			var childAgesInRoomId = 'childAgesInRoom_' + roomId;
			var childesPerRoom = $j(this).parent().parent().parent().parent().parent().find(".wp-ile-dzieci");
			numberOfChildAges = childesPerRoom.eq(roomId-1).val();
			$j("#currentRoomSelected").val(childAgesInRoomId);
			
			x = $j("#"+childAgesInRoomId).val();
			selectedChildAges = tokenizer(x, ",");
		}
		if ($j(this).attr("rel") == "#tooltip-content-amenities") {
			$j('#tooltip-content').text($j("#tooltip-content-amenities").text());
			
			$j("#tooltip-content-amenities input").each(function(){
				if($j("#"+$j(this).val()).val() == 1)
					$j(this).attr('checked', 'checked');
				else
					$j(this).removeAttr('checked');
			});
		}
		if ($j(this).attr("rel") == "#tooltip-content-services") {
			$j('#tooltip-content').text($j("#tooltip-content-services").text());
			$j("#tooltip-content-services input").each(function(){
				if($j("#"+$j(this).val()).val() == 1)
					$j(this).attr('checked', 'checked');
				else
					$j(this).removeAttr('checked');
			});
			
		}
		if ($j(this).attr("rel") == "#tooltip-content-types") {
			$j('#tooltip-content').text($j("#tooltip-content-types").text());
			//$j("#tooltip-content-types").text('');
		}
		
		if($j(this).attr("rel") == "#tooltip-content-rooms-main"){
			$j('#tooltip-content').html($j("#tooltip-content-rooms-main").text());
			
			//var roomId = $j(this).parent().prev().prev().prev().text();
			//roomId = roomId.charAt(roomId.search(/\d/));
			var roomId = $j(this).attr("href");
			//alert(roomId);
			$j("#currentRoomSelected").val(roomId);
			
			numberOfChildAges = $j(this).parent().prev().find('select').val();
			
			x = $j(this).prev().val();
			selectedChildAges = tokenizer(x, ",");
			return false;
		}
		if($j(this).attr("rel") == "#tooltip-content-parametry-2"){
			
			return false;
		}
		
		if($j(this).attr("rel") == "#tooltip-content")
			$j('#tooltip-content').text('Brak zdefiniowanego źródła');

	})
	.cluetip({
		leftOffset: -100,
		topOffset: -30, 
		local:true,
		showTitle: true,
		cursor: 'pointer',
		sticky: true,
		dropShadow: false,
		activation: 'click', 
		closeText: 'zamknij',
		onShow: function(){
			$j(this).addClass("cluetip-show");
			$j('#cluetip-inner select').each(function(i){
				if(i>=numberOfChildAges)
					$j(this).hide();
				else{
					$j(this).val(selectedChildAges[i]);
					$j(this).show();
				}
			});
			
		},
		onHide: function(){
			$j(this).removeClass("cluetip-show");
			//if($j(this).attr("rel") == "#tooltip-content-types") alert('');
			/*
			if($j(this).attr("rel") == "#tooltip-content-amenities"){
				$j("#tooltip-content-amenities input").each(function(){
				alert($j(this).attr('name') + ' ' + $j(this).is(':checked'));
				//alert($j(this).attr('name'));
				//$j(this).attr('id', $j(this).attr('name'));
			});
			}
			*/
		}
	}); 
	

	//pokazywanie i ukrywanie ikonek przy selectach dla dzieci
	var select = $j(".wp-ile-dzieci");
	select.each(function(){
		if($j(this).val() > 0){
			$j(this).next("a").show();
		}else{
			$j(this).next("a").hide();
		}
	});
	select.change(function(){
		if($j(this).val() > 0){
			$j(this).next("a").show();
		}else{
			$j(this).next("a").hide();
		}
	});
	

	$j('.additional-content').hide();
	var allLists = $j(".additional-menu");
	$j('.additional-menu > li > a').click(function(e){
		$j(".additional-content-inner").hide();
		var thatList = $j(this).parent().parent(); 
		var subParent = thatList.parent();
		var mainParent = subParent.parent();
		var kidsToShow = mainParent.next().children(".additional-content-inner");
		var linkClass = $j(e.target).attr('class');
		allLists.css("top",22);
		allLists.parent().css("border-bottom","5px solid #565353");
		subParent.css("border-bottom","3px solid #A3A3A3");
		thatList.css("top",20);
		$j('.additional-content').hide();
		mainParent.next(".additional-content").show();
		
		var bufor = '';
		var hotelId = $j(this).parent().parent().parent().parent().parent().prev().attr('name');
		
		$j(kidsToShow).each(function(){
				if ($j(this).hasClass(linkClass)) {
					if ($j(this).is(":hidden")){
						$j('.additional-content').addClass("hidden");
						kidsToShow.hide();
						mainParent.next().removeClass("hidden");					
						$j(this).fadeIn("slow");
					}
				}
			});
				
		var kidGaleria = mainParent.next().children(".galeria");
		var kidMapa = mainParent.next().children(".mapa-dojazdu");
		var kidOpis = mainParent.next().children(".pelny-opis");
		
		if ($j(this).hasClass('galeria')) {
			$j(".additional-content-inner").html('<img src="resource/images/loading.gif" style="width: 24px; height: 24px; " />');
			$j.ajax({
				type: "GET",
				url: "./Hotels/hotel_information/" + hotelId + "/HotelImages",
				dataType: "xml",
				timeout: 3000,
				success: function(xml){
					$j(xml).find('images').find('image').each(function(){
						bufor += '<a href="' + $j(this).find('url').text() + '">';
						bufor += '<img src="' + $j(this).find('thumbUrl').text() + '" class="imageThumbnails" />';
						bufor += '</a>';
					});
					bufor += '<div id="photoDisclaimer">Za wszelkie różnice w standardzie widocznym na zdjęciach jest odpowiedzialny hotel/sieć hoteli/właściciel ośrodka, od którego zdjęcia pochodzą. <br />"Sky4fly.pl" nie ponosi odpowiedzialności za jakiekolwiek niezgodności występujące na zdjęciach.</div>';
					$j(kidGaleria).html(bufor);
					$j(kidGaleria).find('a').lightBox({txtImage: 'Zdjęcie', txtOf: 'z'});
					//$j('a.link2SB').lightBox({txtImage: 'Zdjęcie', txtOf: 'z'});
				}
			});
		};		
			
		if ($j(this).hasClass('mapa-dojazdu')){
			//$j(".additional-content-inner").html(xml);
			//$j(".additional-content-inner").html('<img src="resource/images/loading.gif" style="width: 24px; height: 24px; " />');
			//$j(".additional-content-inner").html('<img src="resource/images/loading.gif" style="width: 24px; height: 24px; " />');
			//var mapSource = "http://travel.ian.com/hotel/maps/" + hotelId + "?cid=273962";
			var mapSource = "http://www.hotel4hotel.pl/hotel/Hotels/map/" + hotelId;
			$j(kidMapa).html('<iframe  id="Iframe1"  frameborder="0"  vspace="0"  hspace="0"  marginwidth="0"  marginheight="0"  scrolling="no" height="400" width="780"  src="' + mapSource + '"></iframe>');
			
		}

		if ($j(this).hasClass('pelny-opis')){
			$j(".additional-content-inner").html('<img src="resource/images/loading.gif" style="width: 24px; height: 24px; " />');
			$j.ajax({
				type: "GET",
				url: "./Hotels/hotel_information/" + hotelId + "/propertyDescription",
				dataType: "text",
				timeout: 3000,
				success: function(xml){
					$j(kidOpis).html(xml.replace(/&amp;/g,'&').replace(/&lt;/g,'<').replace(/&gt;/g,'>'));
				}
			});
		}
		return false;
	});
	
	$j(".additional-content > .close").click(function(){
		$j(this).parent().hide();
		allLists.css("top",22);
		allLists.parent().css("border-bottom","5px solid #565353");
		return false;
	});
	
	
	function tokenizer(x, delimiter){
		var arr = new Array();
		j = 0;
		temp = "";
		for(i=0; i<x.length; ++i){
			if(x.charAt(i) != delimiter)
				temp += x.charAt(i);
			else{
				arr[j] = temp;
				temp = "";
				j++;
			}
		}
		arr[j] = temp;
		return arr;
	}

});

