var bigPics = new Array();
var titles = new Array();
var loading = new Array();
var loaded = new Array();
var insImg = new Array();
var newImage = 0;
var timing = false;
var duration = "fast";

function hidePic(index) {
	newImage = index;
	if(!loaded[newImage]) {
		$(".ap-preloader").show();
	}
	$(".ap-big").animate({opacity: 0}, duration, function(){ showPic(); });
}

function showPic() {
	if(loaded[newImage]) {
		timing = false;
		/*$(".pre a").removeClass("active").eq(newImage).addClass("active");*/
		$(".api-sub").html(titles[newImage]);
		$(".ap-big").each(function(){ this.src = bigPics[newImage]; });
		$insImg = $(insImg[newImage]);
		$(".ap-big").css({
			width: $insImg.width(),
			height: $insImg.height(),
			marginLeft: (465 - $insImg.width()) / 2,
			marginTop: (465 - $insImg.height()) / 2
		}).animate({opacity: 1}, duration, function(){ 
			$(".ap-preloader").hide();
		});
	/*} else if(loading[newImage] && !loaded[newImage]) {
		if(!timing) {
			setTimeout(showPic, 100);
			timing = true;
		}*/
	} else {
		var img = document.createElement("IMG");
		loading[newImage] = true;
		/*img.className = "ap-big";*/
		$(img).css({display: 'none'});
		img.onload = function() {
			for (var i = 0; i < bigPics.length; i++) {
		                if (bigPics[i] == bigPics[newImage]) {
                			loaded[i] = true;
		                }
			$(".big").append(img);
			insImg[newImage] = img;
        	    }
	            showPic();
		}
		img.src = bigPics[newImage];
		/*if(!timing) {
			setTimeout(showPic, 100);
			timing = true;
		}*/
	}
}

function slidePre(direction) {
	var pcont = $(".p-content").get(0);
	$(".pre-next, .pre-prev").show();
	if(!pcont.sliding) {
		pcont.sliding = true;
		$(".p-content").animate({marginLeft: -(direction * $(".pre").get(0).frame) + parseInt($(".p-content").css("marginLeft"))},duration, function(){ 
			$(".p-content").get(0).sliding = false;
			if(parseInt($(".p-content").css('marginLeft')) >= 0) $(".pre-prev").hide();
			if(-parseInt($(".p-content").css('marginLeft')) + $(".p-content").parent().get(0).frame >= parseInt($(".p-content").width())) $(".pre-next").hide();
		});
	}
	return false;
}

function initPhoto() {
    bigPics = new Array();
    titles = new Array();
    loading = new Array();
    loaded = new Array();
    insImg = new Array();
    newImage = 0;
    timing = false;

    $(".ap-preloader").hide();
    $(".photo_thumb a").each(function(){
    	var newIndex = bigPics.length;
	this.index = newIndex;
    	bigPics[newIndex] = $(this).attr("rel");

	var ih = $(this).attr("header").replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
	var ib = $(this).attr("title").replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
	var id = $(this).attr("date");
	if ( id ) t = id ? "<span>Загружено " + id + "</span>" : "";
	if ( ih ) t += ih ? "<br /><b>" + ih + "</b>" : "";
	if ( ib ) t += t ? "<br />" + ib : "";
    	titles[newIndex] =  t;
    	loading[newIndex] = loaded[newIndex] = false;
    }).click(function(i){
	$("#photos")[0].scrollIntoView( true );
    	if( this.index != newImage)
    		hidePic( this.index );
    	return false;
    });
    $(".api-sub").html(titles[newImage]);
    $(".artist-photo .big a:first").click(function(){
    	hidePic((newImage == 0 || newImage == null) ? (bigPics.length - 1) : newImage - 1);
    	return false;
    });
    $(".artist-photo .big a:last").click(function(){
    	hidePic((newImage == bigPics.length - 1) ? 0 : newImage + 1);
    	return false;
    });
    $(".pre").css({overflow: "hidden"}).find(".p-content").each(function(){
    	var $this = $(this);
    	$this.parent().get(0).frame = (79) * 5;
    	$this.css({width: (Math.floor($("img",$this).size() / 5) + 1) * $this.parent().get(0).frame})
    });
    if($(".p-content").width() > $(".pre").width()) $(".pre-next").show();
    $(".pre-next").click(function(){ this.blur(); return slidePre(1); });
    $(".pre-prev").click(function(){ this.blur(); return slidePre(-1); });

    if ( bigPics.length > 0 ) {
            $(".ap-big")[0].style.cursor = "pointer";
	    $(".ap-big").click(function(){
    		hidePic((newImage == bigPics.length - 1) ? 0 : newImage + 1);
	    	return false;
	    });
    }
}
