$(document).ready(function() {
    var total = $("#morepics .items a").length;
    //console.log("total #morepics .items a: " + total);
	
	if (total < 2) {
		$('.hotelpic div.nextpic').hide();
	}
	
	if (total < 5) {
		$('#morepics a.next').hide();
	}
	
	$("#morepics").scrollable({ circular: true });
	
	var imgs = $(".hotelpic img");
	
	$(imgs[0]).load(function() {
		var mgtop = ( $('.hotelpic').height() - $(imgs[0]).height()) / 2;
		$(imgs[0]).css('margin-top',mgtop+'px');		
		var mgleft = ( $('.hotelpic').width() - $(imgs[0]).width()) / 2;
		$(imgs[0]).css('margin-left',mgleft+'px');	 
	});	
		
	$("#morepics .items:first-child a:first-child").addClass("active");
	
    var url = $(imgs[0]).attr("src");

    $("#morepics .items a").click(function() {
	
	// see if same thumb is being clicked
	// if ($(this).hasClass("active")) { return false; }
	/*
	if ($(this).parent().hasClass('cloned')) { 
		console.log("click comes from cloned");
		//return false; 	
	}
	*/
	
	//get the large images url
	if (url == $(this).attr("href")) {
		//console.log("url is same url: " + url + " $(this).attr: "  + $(this).attr("href"));
		return false; 	
	}
	
	url = $(this).attr("href");	
	//console.log("load url: " + url);
	
	// get handle to element that wraps the image and make it semi-transparent
	var wrap = $(".hotelpic");
	var wrapHeight = wrap.height();
	var wrapWidth = wrap.width();
	
	$(imgs[1]).attr("src", 	$(imgs[0]).attr("src"));
	$(imgs[1]).width($(imgs[0]).width());
	$(imgs[1]).height($(imgs[0]).height());
	var mgtop = ( wrapHeight - $(imgs[0]).height()) / 2;
	$(imgs[1]).css('margin-top',mgtop+'px');
	var mgleft = ( wrapWidth - $(imgs[0]).width()) / 2;
	$(imgs[1]).css('margin-left',mgleft+'px');
		
	// tload new image
	var img = new Image();
	
	// call this function after it's loaded
	img.onload = function() {			
		
		$(imgs[1]).show();
		$(imgs[1]).fadeOut(400);		
		
		
		$(imgs[0]).attr("src", url);
		var mgtop = ( wrapHeight - $(imgs[0]).height()) / 2;
		$(imgs[0]).css('margin-top',mgtop+'px');		
		var mgleft = ( wrapWidth - $(imgs[0]).width()) / 2;
		$(imgs[0]).css('margin-left',mgleft+'px');		
		$(imgs[0]).hide();
		$(imgs[0]).fadeIn(400);
		
		//console.log("loaded url: " + url);					
		
		/*wrap.find("img").fadeOut(200,function() {
				// Animation complete
				wrap.find("img").attr("src", url);
				var mgtop = ( $('.hotelpic').height() - wrap.find("img").height()) / 2;
	    		wrap.find("img").css('margin-top',mgtop+'px');
				
				wrap.find("img").fadeIn(200);
			  });
		*/
	};

	// load the image
	img.src = url;
	
	/*
	index = ($(this).parent().parent().children().index($(this).parent()) * 4) + $(this).parent().children().index(this);
	if (index) {
		$('.hotelpic div.prevpic').show();
		$('.hotelpic div.nextpic').show();
	} else {
		$('.hotelpic div.prevpic').hide();
	}
	
	if (index == total - 1)  $('.hotelpic div.nextpic').hide();
	*/
	//alert(index);	
	
	// activate item
	$(".items a").removeClass("active");
	$(this).addClass("active");	

	return false;

    // when page loads simulate a "click" on the first image
    });

	//$('.hotelpic div.prevpic').hide();
	// '.tx-importhotels-pi1'
	
	if ($.browser.msie) {
		$('.hotelpic div').dblclick(function() {
			$(this).click();
		})
	}
	
    $('.hotelpic div').click(function () {
		
		var currentImg = $(this).parent().find('img')[0];
		//var currentSrc = $(currentImg).attr('src');
		
		var currentSrc = url;
		//console.log("currentSrc: " + currentSrc);
		
		var currentItem = $("#morepics .items a[href='" + currentSrc + "']");
		//console.log("hotelpic.click: " + currentItem);
		
		var direction = $(this).hasClass('nextpic') ? 'next' : 'prev';
		var childrenVar = $(this).hasClass('nextpic') ? ':first' : ':last';
		
		if(currentItem[direction]().length) {
			currentItem[direction]().click();			
		} else if (currentItem.parent()[direction]().length) {
			currentItem.parent()[direction]().children(childrenVar).click();
			$('#morepics .' + direction).click();
		}		
		
    });

	// tab slider
	var slidespeed = 500;
	$('.hotelinfos li > a').click(function() {
		$('.hotelinfos li div.slideWrapper').slideUp(slidespeed, function() {
			$(this).removeAttr("style");
		  });
		$('.hotelinfos li').removeClass('active');
		
		if ($(this).next().is(":hidden"))  {
			$(this).next().slideDown(slidespeed);
			$(this).parent().addClass("active");			
		}
		
		return false;
	});
		
	// hide h2
	$(".hotelinfos li div h2:first-child").hide();
	
	/*
	$('#backlink').click(function () {
		this.href = $('#breadcrumb a:eq(3)').attr('href');
	})
	*/

});


// ajax request 
/*
function submitOfferForm(_url) {
	//alert(_url);
	var jxhr = $.ajax({ url: _url }).success(function(html) { alert(html); }).error(function() { alert("error"); }).complete(function() { alert("complete"); });
	return false;	
}
*/

function call_tb_show(e,hotelId) {
	var offerId = $("input:radio:checked[name='checkedoffer']").val();
	tb_show('', 'index.php?id=374&tx_importhotels_pi6[offerId]='+offerId+'&tx_importhotels_pi6[hotelId]='+hotelId+'&no_cache=1&KeepThis=true&TB_iframe=true&height=1080&width=630') ;
}



