$(document).ready(function(){
	
	$('.backtotop_radovi').click(function(){
		$.scrollTo('#breadcrumbs', 800);
	});
	
	$('.backtotop_rad').live('click', function(){
		$.scrollTo('#rad_holder', 800);
	});
	
	
	$(window).hashchange( function(){
		var hash = location.hash;
		uri = hash.split('/');
		if(uri[2])
			var opt = uri[2];
		else
			var opt = '';
		
		if(uri[0] == '#' && parseInt(uri[2]) > 0)
		{
			load_referenca(uri[2], location.href);
			$.scrollTo('#rad_holder', 800);
		}
		
	});

	$(window).hashchange();
});


var start_n_projects = 0;
function reload_projects_n(type, count, url)
{
	if(url)
		var add_url = url+'/';
	else
		var add_url = '';
	
	if(type == 1)
	{
		var add = '';
		var refresh = 'refresh.gif';
	}
	else
	{
		var add = '2';
		var refresh = 'refresh_grey.gif';
	}
	
	start_n_projects = start_n_projects + 2;
	if(start_n_projects >= count)
		start_n_projects = 0;
	
	$('#reload_img_1').attr('src', add_url+'images/spinner.gif');
	
	$('#novi_projekti_ajax_container').fadeOut(500, function(){
		$.get(add_url+"ajax/box_projekti.php?type"+add+"=novi_projekt&limit="+start_n_projects+"&count_id="+count, function(responseText, textStatus, XMLHttpRequest){
			$('#reload_img_1').attr('src', add_url+'images/'+refresh);
			document.getElementById('novi_projekti_ajax_container').innerHTML = responseText;
			$('#novi_projekti_ajax_container').fadeIn(500);
		});
	});
	
	clearTimeout(tn);
	start_projekti_n_loop(type, count);
}

var start_i_projects = 0;
function reload_projects_i(type, count, url)
{
	if(url)
		var add_url = url+'/';
	else
		var add_url = '';
	
	if(type == 1)
	{
		var add = '';
		var refresh = 'refresh.gif';
	}
	else
	{
		var add = '2';
		var refresh = 'refresh_grey.gif';
	}
	
	$('#reload_img_2').attr('src', add_url+'images/spinner.gif');
	
	$('#izdvojeni_projekti_ajax_container').fadeOut(500, function(){
			start_i_projects = start_i_projects + 2;
			if(start_i_projects >= count)
				start_i_projects = 0;
			
			$.get(add_url+"ajax/box_projekti.php?type"+add+"=izdvojeni_projekt&limit="+start_i_projects+"&count_id="+count, function(responseText, textStatus, XMLHttpRequest){
				
				document.getElementById('izdvojeni_projekti_ajax_container').innerHTML = responseText;
				$('#izdvojeni_projekti_ajax_container').fadeIn(500, function(){
					$('#reload_img_2').attr('src', add_url+'images/'+refresh);
				});
			});
		});
		
		clearTimeout(ti);
		start_projekti_i_loop(type, count);
}

function start_projekti_n_loop(type, count)
{
	tn = setTimeout("reload_projects_n("+type+","+count+")",4000);
}

function start_projekti_i_loop(type, count)
{
	ti = setTimeout("reload_projects_i("+type+","+count+")",7000);
}

function load_radovi(direction, pages, query_string)
{
	var curr_page = $('#radovi_stranica').val();
	
	if(direction == 'f')
	{
		var next_page = parseInt(curr_page) + 1;
	}
	else
	{
		if(curr_page > 1)
			var next_page = parseInt(curr_page) - 1;
		else
			var next_page = 1;
	}
	
	if(pages <= next_page)
		$('#radovi_next').css('display','none');
	else
		$('#radovi_next').css('display','block');
	
	$('#radovi_stranica').val(next_page);
	$('#radovi_curr_page').text(next_page);
	
	$('#radovi_loader').fadeIn(800);
	$('#radovi_fade').fadeOut(800, function(){
		$.post('index.php'+query_string, { start: next_page }, function(data, textStatus, XMLHttpRequest){
			$('#radovi_fade').html(data);
			$('#radovi_fade').fadeIn(800);
			$('#radovi_loader').fadeOut(500);
		});
	});
}

function load_radovi_grafika(direction, pages)
{
	var curr_page = $('#radovi_stranica').val();
	
	if(direction == 'f')
	{
		var next_page = parseInt(curr_page) + 1;
	}
	else
	{
		if(curr_page > 1)
			var next_page = parseInt(curr_page) - 1;
		else
			var next_page = 1;
	}

	if(pages <= next_page)
		$('#radovi_next').css('display','none');
	else
		$('#radovi_next').css('display','block');
	
	$('#radovi_stranica').val(next_page);
	$('#radovi_curr_page').text(next_page);
	
	$('#radovi_loader').fadeIn(800);
	$('#radovi_fade').fadeOut(800, function(){
		$.post('index.php', { start: next_page }, function(data, textStatus, XMLHttpRequest){
			$('#radovi_fade').html(data);
			$('#radovi_fade').fadeIn(800);
			$('#radovi_loader').fadeOut(500);
			
			$(".show_image").fancybox({
				'autoScale'			: true,
				'cyclic'			: true,
				'transitionIn'		: 'fade',
				'transitionOut'		: 'fade',
				'type'				: 'image',
				'autoDimensions'	: true,
				'titleShow'			: true,
				'titlePosition'		: 'over',
				'padding'			: 40
			});

		});
	});
}

function load_referenca(id, uri)
{
	$('#radovi_loader').fadeIn(800);
	$('#rad_holder').fadeOut(800, function(){
		$.get('http://www.virtus-dizajn.com/nasi-radovi/detalji.php', { ajax: 1, id : id, uri : uri }, function(data, textStatus, XMLHttpRequest){
			$('#rad_holder').html(data);
			$('#rad_holder').fadeIn(800);
			$('#radovi_loader').fadeOut(500);
			
			$(".posalji_popup").fancybox({
				'width'				: 480,
				'height'			: 360,
				'transitionIn'		: 'fade',
				'transitionOut'		: 'fade',
				'type'				: 'iframe',
				'titleShow'			: false,
				'overlayColor'		: '000000'
			});

		});
	});
}





