﻿/* © Just Be Nice studio | http://www.justbenice.ru */

var bg;
var holder;
var photo;
var is_previewed = false;
var gallery_top;
var top;
var variable_speed;
var ul_clone;
var current_li_id;
var current_li_html;
var img;
var bigimage;

var slideshow_speed = 3000;
var timer = null;
var mode = 'manual';
var btnState = new Array();
btnState['manual'] = '<span class="pseudo_link" style="margin-right:0.8em;">Включить&#160;слайдшоу</span>';
btnState['auto'] = '<span class="pseudo_link">Выключить&#160;слайдшоу</span>';

//show image
function preview(a){
	var src = a.href;
	var div = a.parentNode.parentNode.parentNode;
	
	var ul = a.parentNode.parentNode;	
	var li = a.parentNode;
	var index = lindex(ul,li);
	var lid = 'small'+index;
	
	a.blur();
	if(!is_previewed){
		//show gallery
		is_previewed = true;
				
		bg = this.document.getElementById('bg');
		bg.style.height = this.document.getElementById('container').offsetHeight+'px';
		bg.className = '';

		var gallery_id = "#"+div.id;
		var gallery_position = $(gallery_id).position();
		var movie_position = $("#movie").position();
		var gallery_offset = (div.className=='advertising')? 0 : 40;
		gallery_top = movie_position['top'] + gallery_position['top'] + gallery_offset;
		top = $(window).scrollTop()+30;
			
		holder = this.document.getElementById('preview');
		$("#preview").css({top:gallery_top+"px"});
		holder.innerHTML = '<div>'+div.innerHTML+'</div><div id="photo"></div>';
				
		var child = holder.childNodes[0].childNodes;
		for(var i=0;i<child.length;i++){
			if(child[i].nodeName=='UL'){
				ul_clone = child[i];
			}
			if(child[i].nodeName=='H2'){
				child[i].innerHTML += '<div id="auto" onclick="auto();">'+btnState['manual']+'</div>';
			}
		}
		var child = ul_clone.childNodes;
		for(var i=0;i<child.length;i++){
			if(child[i].nodeName=='LI'){
				child[i].id = 'small'+i;
				if(child[i].id==lid){
					current_li_id = 'small'+i;
					current_li_html = child[i].innerHTML;
					child[i].innerHTML = '<div id="close" class="clickable" onclick="hide();"></div>';
					resize_close(src);
				}
			}
		}	
			
		holder.className = div.className;
		
		variable_speed = 400+Math.abs(gallery_top-top);		
		$("#preview").animate({top:top+"px"},variable_speed);
		photo = this.document.getElementById('photo');
		bigimage = new Image;
		bigimage.src = src;
		bigimage.onload = checkLoad;
		if(jQuery.browser.msie)
			checkLoad();
	}
	else{		
		//show photo
		var child = ul_clone.childNodes;
		for(var i=0;i<child.length;i++){
			if(child[i].nodeName=='LI'){
				if(child[i].id==current_li_id){
					child[i].innerHTML = current_li_html;
				}
			}
		}		
		for(var i=0;i<child.length;i++){
			if(child[i].nodeName=='LI'){
				if(child[i].id==lid){
					current_li_id = 'small'+i;
					current_li_html = child[i].innerHTML;
					child[i].innerHTML = '<div id="close" class="clickable" onclick="hide();"></div>';
					resize_close(src);
				}
			}
		}
		bigimage = new Image;
		bigimage.src = src;
		bigimage.onload = checkLoad;
		if(jQuery.browser.msie)
			checkLoad();
	}
}
function lindex(ul,li){
	var index = -1;
	var child = ul.childNodes;
	for(var i=0;i<child.length;i++){
		if(child[i]==li)
			index = i;
	}
	return index;
}
//preload
function checkLoad(){
	if(bigimage.complete){
		$("#photo").fadeOut("fast",function(){
			photo.innerHTML = '<img src="'+bigimage.src+'" alt="" title="" />';
			$("#photo").fadeIn("slow");
		});		
		return
	}			
	timer_ID = setTimeout("checkLoad()",10);
}
//hide image
function hide(){
	if(mode=='auto')
		auto();
	$("#photo").fadeOut("slow");
	$("#preview").animate({top:gallery_top+"px"},variable_speed,function(){
		var child = ul_clone.childNodes;
		for(var i=0;i<child.length;i++){
			if(child[i].nodeName=='LI'){
				if(child[i].id==current_li_id){
					child[i].innerHTML = current_li_html;
				}
			}
		}
		is_previewed = false;
		holder.className = 'hidden';
		bg.className = 'hidden';
	});
}
//draw close button
function resize_close(src){
	var small_src = src.substr(0,src.length-4)+"s.jpg";
	img = new Image;
	img.src = small_src;
	img.onload = checksLoad;
	checksLoad();
}
function checksLoad(){
	if(img.complete){
		this.document.getElementById('close').style.width = img.width + 'px';
		this.document.getElementById('close').style.height = img.height + 'px';
		drawLine('close',0,0,img.width-1,img.height-1,'#393939');
		drawLine('close',0,img.height-1,img.width-1,0,'#393939');		
		return
	}			
	timerID = setTimeout("checksLoad()",10);
}
//slideshow
function auto(){
	if(mode=='auto'){
		clearTimeout(timer);
	}
	mode = (mode=='manual')? 'auto' : 'manual';
	this.document.getElementById("auto").innerHTML = btnState[mode];
	if(mode=='auto')
		play();
}
function play(){
	var a = null;
	var current_found = false;
	var child = ul_clone.childNodes;
	for(var i=0;i<child.length;i++){
		if(child[i].nodeName=='LI'&&current_found){
			var ch = child[i].childNodes;
			for(var j=0;j<ch.length;j++)
				if(ch[j].nodeName=='A')
					a = ch[j];
			current_found = false;
		}
		if(child[i].id==current_li_id)
			current_found = true;			
	}
	if(!a){
		current_found = false;
		for(var i=0;i<child.length;i++){
			if(child[i].nodeName=='LI'&&!current_found){
				var ch = child[i].childNodes;
				for(var j=0;j<ch.length;j++)
					if(ch[j].nodeName=='A')
						a = ch[j];
				current_found = true;
			}
		}
	}	
	if(a){
		preview(a);
	}
	else{
		auto();
		return
	}
	timer = setTimeout("play();", slideshow_speed);
}