﻿var photoCarousel = {
  // jQuery 1.3.2, jquery.jcarousel.pack.js, jquery.qtip-1.0.0-rc3.min.js required.

  hotelphoto_itemVisibleInCallback : function (carousel, item, i, state, evt)    {
    // The index() method calculates the index from a
    // given index who is out of the actual item range.
	var idx = carousel.index(i, hotelphoto_itemList.length);
    carousel.add(i, photoCarousel.hotelphoto_getItemHTML(hotelphoto_itemList[idx - 1]));
	var selectedInd = $("#j_recent_photos_hotel_big").attr("selectedInd");
	if ($(item).children("[id^='hotelPhoto_']").get(0).id.replace("hotelPhoto_cms_","").replace("hotelPhoto_user_","")==selectedInd) {
		$(item).prepend(photoCarousel.hotelphoto_getSelectedItem());
	}
  },
       
  hotelphoto_itemVisibleOutCallback : function (carousel, item, i, state, evt) {
    carousel.remove(i);
  },
                
  hotelphoto_getItemHTML : function (item) {
	var index = 0;
    for (var it in hotelphoto_itemList) {
	  if (hotelphoto_itemList[it].id_postfix == item.id_postfix) {
        index = parseInt(it) + 1;
        break;
	  }
    }
    var html = '';
	if (item.type == 'img') {
	  html += '<img id="hotelPhoto_'+item.id_postfix+'" ';
	} else {
	  html += '<div ';	
	}
	if (item.url_big && item.url_big != '') {
	  html += 'onclick="photoCarousel.hotelphoto_itemClick('+index+',this);return false;" class="show-cursor" ';
	}
	if (item.type == 'img') {
	  html += 'alt="" src="'+item.url+'" style="height: 120px; width: 120px" />';
	} else {
	  html += 'id="hotelPhoto_'+item.id_postfix+'" style="background: url('+item.url+') center center no-repeat; height: 120px; width: 120px;"> </div>';
	}
	return html;
  },

  hotelphoto_itemClick : function (index,ctrl) {
	$(ctrl).parent().prepend(photoCarousel.hotelphoto_getSelectedItem());
    var item = hotelphoto_itemList[index - 1];
	if (item.type == 'img') {
	  $('#j_recent_photos_hotel_big').find('img').attr('src', item.url_big);
	} else {
	  $('#j_recent_photos_hotel_big.b-big-photo .b-recent-photos').css('background','url('+item.url_big+') center center no-repeat');
	}
    $('#j_recent_photos_hotel_big.b-big-photo').show().attr("selectedInd",$(ctrl).attr("id").replace('hotelPhoto_cms_',"").replace('hotelPhoto_user_',""));
	if (item.text && item.text != '') {
		$('#j_recent_photos_hotel_paging.photos-paginator .photo-sign').html(item.text);
	} else {
		$('#j_recent_photos_hotel_paging.photos-paginator .photo-sign').html('');
	}
    $('#j_recent_photos_hotel_paging.photos-paginator .photo-page').html(index + '/' + hotelphoto_itemList.length);
    $('#j_recent_photos_hotel_paging.photos-paginator .photo_author').html(item.author);
    $('#j_recent_photos_hotel_paging.photos-paginator').show();
    iTravel.resizeTourDetailsPopup();
  },

   hotelphoto_getSelectedItem : function () {	   
	   if ($("#id-selected-photo-1").length==0) {
		   return '<img alt="" src="/images/photo-selected-1.gif" id="id-selected-photo-1" />';
	   } else {
		   return $("#id-selected-photo-1");	   
	   }
   }  
};

var textCarousel = {
  initSeoPage : function (i, size) {
	$(".j-text-paging").find("li").hide();
	if (size == 1) {
		$("#btn_next, #btn_prev").hide();
		return;
	}
	var min = 0;
	var max = size;

	if (i > 4) {
		$("div.j-text-paging li:first").show();
		var q = 5;
		var w = 1;
		while ((i-q) > 2) {
			q = q+3;
			w++;
		}
		min = 3*w;
		max = ((min+4) > size) ? size : (min+4);
	}

	if ((min+4) < size) {
		max = min+4;
		$("div.j-text-paging li:last").show();
	}
	if (min == 0) {
		min = 1;
	}
	for (j=min; j<=max; j++) {
		$('.j-text-paging li').eq(j).show();
	}

	$(".j-text-paging .active").removeClass("active");
	$(".j-text-paging li").eq(i).find("span").addClass("active");
  },
  
  text_initCallback : function (carousel) {
	var size = $(".j-text-paging").find("li").length - 2;

    $(".j-text-paging li a").bind("click", function() {
		var page = parseInt($(".j-text-paging .active").find("a").text());
		var ind = $(this).text();
		var new_index = 0;
		if (ind == "...") {			
			$span = $(this).parent();
			$span.parent().hide();
			var index = $(".j-text-paging").find("li:visible").length;
			if ($span.hasClass("prev")) {
				new_index = parseInt($(".j-text-paging").find("li:visible").eq(0).find("a").text())-1;
			}
			if ($span.hasClass("next")) {
				new_index = parseInt($(".j-text-paging").find("li:visible").eq(index-1).find("a").text())+1;
			}
		} else {
			new_index = parseInt(ind);
		}
		carousel.scroll(new_index);
		textCarousel.initSeoPage(new_index, size);
        return false;
    });

	$("#btn_prev").bind("click", function() {
		var page = parseInt($(".j-text-paging .active").find("a").text());
	    carousel.prev();
		textCarousel.initSeoPage(page-1, size);
        return false;
	});

	$("#btn_next").bind("click", function() {
		var page = parseInt($(".j-text-paging .active").find("a").text());
	    carousel.next();
		textCarousel.initSeoPage(page+1, size);
        return false;
	});
}
};