﻿var slide_timer;
var slide_running = true;
var curr_work = 1;
var next_work = 0;
var prev_work = 1;
var works_count = 3;
var slide_interval = 3000;
var view_info = false;
var curr_img_class = 'one';
var can_open = true;

var works_box = [];
var works_ids = [];
var work;

var img_bar  = '<div class="img_bar" id="img_bar"><table border="0" cellpadding="0" cellspacing="0" class="img" id="img_table">';
	img_bar += '<tr><td class="top_left"></td><td class="top_middle"><div class="movable_place"></div></td><td class="top_right"></td></tr>';
	img_bar += '<tr><td class="middle_left"></td><td class="img_core" id="img_core"></td><td class="middle_right"></td></tr>';
	img_bar += '<tr><td class="bottom_left"></td><td class="bottom_middle"></td><td class="bottom_right"></td></tr>';
	img_bar += '</table></div>';

// Инициализация тестовых работ
//work = {work_id: 1, work_name: 'Half-Life', work_desc: 'Отличная игра, в главной роли которой доктор физико-математических наук, Гордон Фримен.', work_image: '4.jpg'}; works_box.push(work);


function startSliding() {
    if(works_count==3 && curr_work==1) {
	    return;
	    }
    var work_num = works_ids[curr_work];
	document.getElementById('work_'+works_box[work_num]['work_id']).className = 'li_choosed';
	
    // Определяем номер следующей работы
	if(works_box[works_ids[curr_work]+1]) {
	    next_work = works_box[works_ids[curr_work]+1]['work_id']; 
	    }
		else {
		    next_work = works_box[0]['work_id'];
		    } 
		
    slide_timer = setTimeout('setNextSliding();',slide_interval); 
    }

function setNextSliding() {
    // Меняем содержимое текущего слайда на содержимое следующего слайда
	curr_work = next_work;
	document.getElementById('work_'+works_box[works_ids[curr_work]]['work_id']).className = 'li_choosed';
	document.getElementById('work_'+works_box[works_ids[prev_work]]['work_id']).className = 'li';
	if(curr_img_class=='one') {
        can_open = false;	
	    $('#one').animate({opacity: 0.0},400,function() {
		    document.getElementById('one').src = '/images/works/'+works_box[works_ids[curr_work]]['work_image'];
			document.getElementById('one').onload = function() {
			    var w = 500;
				var h = 250;
				
				if(works_box[works_ids[curr_work]]['width']>w || works_box[works_ids[curr_work]]['height']>h) {
				    var ratio = w/h;
				    var src_ratio = works_box[works_ids[curr_work]]['width']/works_box[works_ids[curr_work]]['height'];
				    if (ratio<src_ratio) {
        		        h = w/src_ratio;
        		        }
        		        else {
        			        w = h*src_ratio;
        			        }				
			        }
				    else {
        		        w = works_box[works_ids[curr_work]]['width'];
        		        h = works_box[works_ids[curr_work]]['height'];
        		        }
						
				document.getElementById('one').style.width = w + 'px';
				document.getElementById('one').style.height = h + 'px';
				
				$('#one').animate({opacity: 1.0},400,function() {
                    setDescription();
                    can_open = true;					
			        });
			    }		
			});
		}	
    }

function setDescription() {
    if(view_info) {
	    // Если описание было закрыто, то сразу открываем
	    if(document.getElementById('description_content').style.display=='none') {
		    $('#description_content').html('<b>'+works_box[works_ids[curr_work]]['work_name']+'</b><br><br>'+works_box[works_ids[curr_work]]['work_desc']);
		    $('#description_content').fadeIn('fast',function() {
                setupNextSlide();			
			    });
		    }
			// Иначе сначала закрываем текущее описание, затем открываем следующее
			else {
			    $('#description_content').fadeOut('fast',function() {
				    $('#description_content').html('<b>'+works_box[works_ids[curr_work]]['work_name']+'</b><br><br>'+works_box[works_ids[curr_work]]['work_desc']);
                    $('#description_content').fadeIn('fast',function() {
                        setupNextSlide();			
			            });			
			        });
			    }
	    }
        else {
		    $('#description_content').html('<b>'+works_box[works_ids[curr_work]]['work_name']+'</b><br><br>'+works_box[works_ids[curr_work]]['work_desc']);
		    setupNextSlide();
            }
	}

function setupNextSlide() {
    // Определяем номер следующей работы
    prev_work = curr_work;
	
	if(works_box[works_ids[prev_work]+1]) {
	    next_work = works_box[works_ids[prev_work]+1]['work_id']; 
	    }
		else {
		    next_work = works_box[0]['work_id'];
		    }
	
	if(slide_running) {
	    slide_timer = setTimeout('setNextSliding();',slide_interval);
	    }	
    }

function PlayOrPause() {
    if(slide_running) {
	    slide_running = false;
	    if(slide_timer) {
	        clearTimeout(slide_timer);
	        }
		$('#play_btn').html('продолжить слайдшоу');
	    }
		else {
		    slide_running = true;
			slide_timer = setTimeout('setNextSliding();',slide_interval);
			$('#play_btn').html('остановить слайдшоу');
			}
    }

function HideOrShowInfo() {
    if(view_info) {
	    view_info = false;
		$('#hide_info_btn').html('показывать описание');
		document.getElementById('description_content').style.display = 'none';
	    }
		else {
		    view_info = true;
			$('#hide_info_btn').html('не показывать описание');
			document.getElementById('description_content').style.display = 'block';
		    }
    }

function getThisWork(work_id) {    
    if(slide_timer) {
	    clearTimeout(slide_timer);
	    }
	
	// Меняем содержимое текущего слайда на содержимое выбранного слайда
	prev_work = curr_work;
	curr_work = work_id;
	document.getElementById('work_'+works_box[works_ids[curr_work]]['work_id']).className = 'li_choosed';	
	if(curr_work!=prev_work) {
		document.getElementById('work_'+works_box[works_ids[prev_work]]['work_id']).className = 'li';
	    }
	
    $("#one").stop();	
    if(curr_img_class=='one') {
        can_open = false;	
	    $('#one').animate({opacity: 0.0}, 400, function() {
		    document.getElementById('one').src = '/images/works/'+works_box[works_ids[curr_work]]['work_image'];
			document.getElementById('one').onload = function() {
			    var w = 500;
				var h = 250;
				
				if(works_box[works_ids[curr_work]]['width']>w || works_box[works_ids[curr_work]]['height']>h) {
				    var ratio = w/h;
				    var src_ratio = works_box[works_ids[curr_work]]['width']/works_box[works_ids[curr_work]]['height'];
				    if (ratio<src_ratio) {
        		        h = w/src_ratio;
        		        }
        		        else {
        			        w = h*src_ratio;
        			        }				
			        }
				    else {
        		        w = works_box[works_ids[curr_work]]['width'];
        		        h = works_box[works_ids[curr_work]]['height'];
        		        }
						
				document.getElementById('one').style.width = w + 'px';
				document.getElementById('one').style.height = h + 'px';
				
				$('#one').animate({opacity: 1.0},400,function() {
                    setDescription();
					can_open = true;					
			        });
			    }			
			});
		}	
    }

function openFullPicture() {
    if(!can_open) {
	    return;
	    }
    if(slide_timer) {
	    clearTimeout(slide_timer);
	    }
		
	var winsize = getWindowSize();
	
	$('body').append('<div class="fade_bg" id="fade_bg"></div>');
	$('#fade_bg').fadeIn('fast', function() {
	    var imgLeft = winsize['Width']/2 - works_box[works_ids[curr_work]]['width']/2 - 20 + 'px';
		var imgTop = 50 + winsize['ScrollHeight'] + 'px';		
		var closeImgLeft = winsize['Width']/2 + works_box[works_ids[curr_work]]['width']/2 + 1 + 'px';
		
	    $('body').append(img_bar);
		document.getElementById('img_bar').style.left = imgLeft;
		document.getElementById('img_bar').style.top = imgTop;
		
		document.getElementById('img_table').style.width = works_box[works_ids[curr_work]]['width'] + 40 + 'px';
		document.getElementById('img_table').style.height = works_box[works_ids[curr_work]]['height'] + 40 + 'px';
		
		$('#img_core').append('<img src="/images/works/'+works_box[works_ids[curr_work]]['work_image']+'" class="big_image" style="top:20px;left:20px;display:block;position:absolute;">');
		
		var close_btn_top = winsize['ScrollHeight'] + 50 + 20 + 'px';
		
		$('#img_bar').fadeIn('fast', function() {
		    $('body').append('<div class="close_big_image" id="close_big_image" title="Закрыть изображение" onclick="closeFullPicture();" style="left:'+closeImgLeft+';top:'+close_btn_top+';">X</div>');
			$('#close_big_image').fadeIn('fast');
		    });
	    });
    }

function openFoto(foto_name,width,height) {
    var winsize = getWindowSize();
	
	$('body').append('<div class="fade_bg" id="fade_bg"></div>');
	$('#fade_bg').fadeIn('fast', function() {
	    var imgLeft = winsize['Width']/2 - width/2 - 20 + 'px';
		var imgTop = 50 + winsize['ScrollHeight'] + 'px';		
		var closeImgLeft = winsize['Width']/2 + width/2 + 1 + 'px';
		
		$('body').append(img_bar);
		document.getElementById('img_bar').style.left = imgLeft;
		document.getElementById('img_bar').style.top = imgTop;
		
		document.getElementById('img_table').style.width = width + 40 + 'px';
		document.getElementById('img_table').style.height = height + 40 + 'px';
		
		$('#img_core').append('<img src="/images/works/'+foto_name+'" class="big_image" style="top:20px;left:20px;display:block;position:absolute;">');
		
		var close_btn_top = winsize['ScrollHeight'] + 50 + 20 + 'px';
		
	    $('#img_bar').fadeIn('fast', function() {
		    $('body').append('<div class="close_big_image" id="close_big_image" title="Закрыть изображение" onclick="closeFullPicture();" style="left:'+closeImgLeft+';top:'+close_btn_top+';">X</div>');
			$('#close_big_image').fadeIn('fast');
		    });
	    });
    }

function closeFoto() {	
	$('#close_big_image').fadeOut('fast', function() {
	    $('#img_bar').fadeOut('fast', function() {
		    $('#fade_bg').fadeOut('fast', function() {
			    $('#fade_bg').remove();
				$('#img_bar').remove();
				$('#close_big_image').remove();
			    });
		    });
	    });
    }

function closeFullPicture() {
    $('#close_big_image').fadeOut('fast', function() {
	    $('#img_bar').fadeOut('fast', function() {
		    $('#fade_bg').fadeOut('fast', function() {
			    $('#fade_bg').remove();
				$('#img_bar').remove();
				$('#close_big_image').remove();
				
				if(slide_running) {
				    slide_timer = setTimeout('setNextSliding();',slide_interval);
					}
			    });
		    });
	    });    
    }

function getWindowSize() {
  var myWidth = 0, myHeight = 0, myScrollHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
	myScrollHeight = document.body.scrollTop;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
	myScrollHeight = document.documentElement.scrollTop;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
	myScrollHeight = document.body.scrollTop;
  }
  
  return {Width: myWidth, Height: myHeight, ScrollHeight: myScrollHeight};
}
