/**
 * $HeadURL: svn://svn/islive_site/trunk/docs/www/privepagina/_default/js/script.js $
 * @version: $Rev: 971 $
 */
jQuery(document).ready(function($) {
	// detect external variables
	if (domain && cam) {
		img_url = "http://images." + domain + '/pic/' + cam + '/stats.json';
		$.getJSON(img_url + "?callback=?", function(data) {
			if (!data || !data.pages) return; 
			$("input[name=pages]").val(data.pages);
			$('#page_max').text(data.pages);
			$('.pics-pages').removeClass('hidden');
		});
	}
	
	$("a.nivoZoom").live('click', function(event) {
		event.preventDefault();
		startchat($(this).attr('rel'));
	});
	
	$(".arrow").click(function (event) {
		var img = $("img", this);

		$(this).next().slideToggle("slow", function(){
			if ($(this).is(":visible")) {
				img.attr("src", style_url + "img/arrow_up.jpg");
			}
			else
				img.attr("src", style_url + "img/arrow_down.jpg");
		});
	});
	
	$(".pic_select").click(function() {
		$(".big_picture").fadeOut("slow", "linear");
		$(".pic_select").attr('src', style_url + "img/pic_select.jpg");
		$(this).attr("src",style_url + "img/pic_select_ac.jpg");
		$("#big_picture" + $(this).attr("alt")).fadeIn("slow", "linear");
	});
	$(".pic_select2").live('click', function() {
		$(".big_picture2").fadeOut("slow", "linear");	
		$(".pic_select2").attr('src', style_url + "img/pic_select.jpg");
		$(this).attr("src",style_url + "img/pic_select_ac.jpg");
		$("#big_picture" + $(this).attr("alt")).fadeIn("slow", "linear");
	});
	$(".showfriend").click(function(event) {
		event.preventDefault();
		var url = (cam = $(this).attr('rel')) ? "./?page=friend&m=" + cam : "";		
		var elem = $(this).closest('div.friend');
      
		if($("#friend-block").is(':visible')) 
		  	elem.parent().find("img.showfriend").attr("src",style_url + "img/arrow.jpg");
		$(this).parent().parent().find("img.showfriend").attr("src",style_url + "img/arrow_up.jpg");
	  
		$("html").animate( { 
		  		scrollTop: $("#friend-block").offset().top + parseInt($("#friend-block").height(), 10) 
		  	}, function() {
				$("#friend-block:visible").slideUp("slow", function() {
					if (url)
						$(this).load(url).slideDown("slow");
					url = null;
				});
				if (url)
					$("#friend-block").load(url).slideDown("slow");
				url = null;
			});
	});

	$("#page_back,#page_forw").click(function(event) {
		event.preventDefault();

		
		var cur = parseInt($('#page_cur').text());
		var max = parseInt($("#page_max").text());
		
		step = parseInt($(this).attr('rel'));		
		if (step > 0) {
			if (cur >= max) return;
			cur++;
			
		} 
		if (step < 0) {
			if (cur < 2) return;
			cur--;
		}
		$('#page_cur').text(cur);
		
		// hack! Replace current batch series number with new one		
		$('#browsepic').fadeOut(function() {
			$("a.nivoZoom", this).each(function() {
				var img_path = $(this).attr("href").replace(/_[0-9]+([0-9]\.)/, '_' + cur + '$1');
				$(this).attr("href", img_path);
				$("img", this).attr('src', img_path);
			});
			$(this).fadeIn();
		});
		
		if (cur < 2) 
			$("#page_back").hide();
		else
			$("#page_back").show();
		
		if (cur >= max) 
			$("#page_forw").hide();
		else 
			$("#page_forw").show();
		
	});
	
	$(".close").live('click', function() {
		$("#friend-block:visible").slideUp("slow");
		$("img.showfriend").attr("src",style_url + "img/arrow.jpg");
	});
});
