var initial_height = 0;
function collapseBox(){
	$('.toggle').click(function(e){
		tp = $(this).parent();
		var to = $(this).offset().top;
		if($(this).hasClass('hide') && $('div.main-page').height()>initial_height)
			$(document).scrollTo('-=170',300,function(){tp.parent().next('.holder').slideToggle('slow');});
		else
			tp.parent().next('.holder').slideToggle('slow');
		if ($(this).hasClass('hide')){
			$(this).removeClass('hide');
			$(this).addClass('show');
		} else if ($(this).hasClass('show')){
			$(this).removeClass('show');
			$(this).addClass('hide');
		}
		return false;
	});	
	$('.subtoggle').click(function(e){
		if ($(this).hasClass('hide')){
			var scrollto = $(this).parent().next('.text-hide').height()+30;
			move_obj = $(this).parent().next('.text-hide');
			if(($(document).height() - $(this).parent().next('.text-hide').height()) < $(document).scrollTop()+$(window).height())
			{
				$(document).scrollTo('-='+scrollto,300);
				setTimeout("move_obj.slideToggle('slow');",300);
			}
			else
			{
				move_obj.slideToggle('slow');
			}
		}	
		else
		{
			$(this).parent().next('.text-hide').slideToggle('slow');	
		}
		
		if ($(this).hasClass('hide')){
			$(this).removeClass('hide');
			$(this).addClass('show');
			$(this).parents('.text-hide').removeClass('hiden');
		} else if ($(this).hasClass('show')){
			$(this).removeClass('show');
			$(this).addClass('hide');
			$(this).parents('.text-hide').addClass('hiden');
		}
		return false;
	});	
	$('.sidetoggle').click(function(e){
		var bh = $(this).parents('.block-hide');
		$(this).parent().next('.holder').slideToggle('slow');
		if ($(this).hasClass('hide')){
			$(this).removeClass('hide');
			$(this).addClass('show');
			$(bh).children('.holder').removeClass('hiden');
		} else if ($(this).hasClass('show')){
			$(this).removeClass('show');
			$(this).addClass('hide');
			$(bh).children('.holder').addClass('hiden');
		}
		return false;
	});	
}
function initHovers(){	
	$('#pict-select li').mouseover(function(e){
		$(this).addClass("hover");
	});	
	$('#pict-select li').mouseout(function(e){
		$(this).removeClass("hover");
	});
	hoverLinks();
}

function hoverLinks() {
	if (document.getElementById("next")) {
		var item_next = document.getElementById("next");
		var item_prev = document.getElementById("prev");
		
		var item_nspan = document.getElementById("snext");
		var item_pspan = document.getElementById("sprev");
		
		item_next.onmouseover = function() {item_nspan.style.visibility = 'visible';};
		item_next.onmouseout = function() {item_nspan.style.visibility = 'hidden';};
		
		item_prev.onmouseover = function() {item_pspan.style.visibility = 'visible';};
		item_prev.onmouseout = function() {item_pspan.style.visibility = 'hidden';};
	}
}
/*
function collapseBox(){
	$('.toggle').click(function(e){
		tp = $(this).parent();
		tp.parent().next('.holder').slideToggle('slow');
		if ($(this).hasClass('hide')){
			$(this).removeClass('hide');
			$(this).addClass('show');
		} else if ($(this).hasClass('show')){
			$(this).removeClass('show');
			$(this).addClass('hide');
		}
		return false;
	});	
	$('.subtoggle').click(function(e){
		$(this).parent().next('.text-hide').slideToggle('slow');
		if ($(this).hasClass('hide')){
			$(this).removeClass('hide');
			$(this).addClass('show');
		} else if ($(this).hasClass('show')){
			$(this).removeClass('show');
			$(this).addClass('hide');
		}
		return false;
	});	
	$('.sidetoggle').click(function(e){
		$(this).parent().next('.holder').slideToggle('slow');
		if ($(this).hasClass('hide')){
			$(this).removeClass('hide');
			$(this).addClass('show');
		} else if ($(this).hasClass('show')){
			$(this).removeClass('show');
			$(this).addClass('hide');
		}
		return false;
	});	
}
*/

function footerDown(){
	var footer = $('#footer');	
	var hfooter = footer.height();
	var hwrap = $('#wrap').height();
	var hdoc = $(document).height();	
	var hwin = $(window).height();
	
	if ((hfooter+hwrap) < hwin) {
		$(footer).css('margin-top',hwin-(hfooter+hwrap));
	} else {
		$(footer).css('margin-top',0);
	}
}

function loadPager(){
	$.ajax({
		type: "GET",
		url: "../xml.php",
		data: "id=0",
		async: true,
		success:function(responseText){
			$('.pict-list').empty();
			$('.pict-list').append(responseText);
			showPict();
			initHovers();
			initPager();
		}
	});	
}

function initPager(){	
	$('ul.pict-pager li.page a').click(function(e){
		var id = $(this).attr('rel');
		$.ajax({
			type: "GET",
			url: "../xml.php",
			data: "id="+id,
			async: true,
			success:function(responseText){
				$('.pict-list').empty();
				$('.pict-list').append(responseText);
				showPict();
				initHovers();
				initPager();
			}
		});
		return false;
	});
}

function display_img(path)
{
	$('.holder .pict-zoom img').unbind('load');
	$('.holder .pict-zoom img').hide();
	$('#ajax-loader').show();
	
	$('.holder .pict-zoom img').attr({src:path});
	
	$('.holder .pict-zoom img').bind('load',function(){
		$('#ajax-loader').hide();
		$('.holder .pict-zoom img').show();
	});
}

function showPict(){
	var images = [];
	var actImg = 0;
	$('#pict-select a').each(function(i){
		$(this).attr('index', i);
		images[i] = $(this).attr('href');
	});
	
	display_img($('#pict-select a').get(0))
	// next button click
	$('#next').click(function(e){		
		if (actImg < images.length-1) {
			actImg++;
		} else {
			actImg = 0;
		}
		var link = $('#pict-select a').get(actImg);
		display_img($(link).attr('href'));
		return false;
	});
	
	// back button click
	$('#prev').click(function(e){	
		if (actImg <= images.length-1 && actImg > 0) {
			actImg --;
		} else {
			actImg = images.length-1;
		}
		var link = $('#pict-select a').get(actImg);
		display_img($(link).attr('href'));
		return false;
	});

	// thumbnail click
	$('#pict-select a').click(function(e){
		actImg = $(this).attr('index');
		var largePath = $(this).attr('href');
		display_img(largePath);
		return false;
	});	
}

function initMain(){
	collapseBox();
	loadPager();
	$(window).resize( footerDown );
	footerDown();
}

$(document).ready(function(){
	initial_height = $('div.main-page').height();
	initHovers();
	initMain();
});