$(document).ready(function(){

	$(".yellow #content, .red #content").overscroll({
		wheelDirection: 'horizontal',
		direction: 'horizontal',
		showThumbs: false
	});
	
	$("#bottom").overscroll({
		wheelDirection: 'vertical',
		showThumbs: false
	});
	
	
	$("#bottom").disableTextSelect();

	$("a.fancybox").fancybox({
		onStart : function(wtf) {
			active = $('#referenca_content').prev().find("li a").attr("rel");
			selected = wtf.attr("rel");
			
			if(active != selected)
				return false;
			
	    },
		'transitionIn'	: 'none',
		'transitionOut'	: 'none',
		'cyclic'        : true,
		'overlayOpacity': 0.8,
		'overlayColor'  : "#000000",
		'showNavArrows' : true,
		'titleShow'     : true,
		'titlePosition' 	: 'over',
		'titleFormat'       : function(title) {
		    return '<center><a style="font-weight:bold;background:black;color:white;display:block;width:100%;margin-bottom:-18px;" href="http://'+title+'" target="_blank">'+title+'</a></center>';
		}
	});
	
	$("a.fancybox_ajax").fancybox({
		onStart : function(wtf) {
			active = $('#referenca_content').prev().find("li a").attr("rel");
			selected = wtf.attr("rel");
			
			
			
			if(active != selected)
				return false;
			
	    },
		ajax : {
		    type	: "POST",
		    //url     : '/content/ajax/fancybox_content.php',
			data	: 'mydata=test'
		}
	});
	
	nastaviVelikostReferenc();
	
	
	$(window).bind('resize', function(){nastaviVelikostReferenc(true);});
	
	var gmdx = 0;
	
	$(".thumbs_b a").bind({
		'mousedown' : function(e){gmdx=e.pageX;},
		'mouseup' : function(e){
			//alert(e.pageX+' / '+gmdx);
			if(e.pageX==gmdx)
				scroll2First($(this).parent().parent().prev(), true, $(this));
		}
	});
	
	
	if(globalPage == 'kaj_delamo') sortReferences(getHash());
	
	//$("li#thumbs_b").attr("style", "display:none !important;");
	
	checkActiveC();

});

function callFlash()
{
	  var flash = document.getElementById("flash_content");
	   flash.goNext();
}

function googleMap() {
  var myOptions = {
    zoom: 16,
 	center: new google.maps.LatLng(46.558541,15.663076),
    disableDefaultUI: true,
	navigationControl: true,
    mapTypeId: google.maps.MapTypeId.ROADMAP  
  }
  var map = new google.maps.Map(document.getElementById("map_canvas"),
       myOptions);
  var marker = new google.maps.Marker({map: map, position:
        map.getCenter()});
}

function animationShow(content)
{
	$.ajax({
		type: "POST",
		url: "/content/ajax/animacija.php", 
		data: 'content='+content,
		success: function(msg){
			
			if(msg!=0)
			{
				$("#animationfade .anim_container .anim_ajax").html(msg);
				$("#animationfade").fadeIn(300);
				
			}
		}
	});
}

function animationNext()
{
	$("#animationfade").fadeOut(300, function(){
		callFlash();
		$("#animationfade .anim_container .anim_ajax").html("");
	});
}

function animationHide()
{
	$("#animationfade").fadeOut(300, function(){
		$("#animationfade .anim_container .anim_ajax").html("");
	});
}

function contentExists()
{
	if ($('#referenca_content').length) return true;
	return false;
}

function getFixedPosition(){
	
	var a = $("#holder").position().left;
	
	a = a * (-1);
	
	// newsitem  = width 805px + padding-left 45px
	var nv = 850;
	// nv/2
	var nvp = 425;
	
	var maxi = nv;
	var mini = 0;
	var center = 0;
	var goto = 0;
	
	while(a>maxi) maxi += nv;
	
	mini = maxi - nv;
	
	center = maxi - nvp;
	
	if(a>center)
		goto = maxi;
	else
		goto = mini;
	
	return goto;
	
}

function checkActiveC()
{
	colorActive($("#reference li.active").find('.thumbs'), true);
}

function colorActive(e, action)
{
	if(action)
	{
		e.find("li.thumbs_cb").attr("style", "display:none !important;");
		e.find("li.thumbs_b").attr("style", "display:block !important;");
	}
	else
	{
		e.find("li.thumbs_b").attr("style", "display:none !important;");
		e.find("li.thumbs_cb").attr("style", "display:block !important;");
	}
}

function scroll2First(e, act, fancy){
	
	if(!fancy)
		fancy = false;
		
	// PREVERI CE JE "active" referenca
	// Ce je "active" potem jo le zapre prvic
	var showC = false;
	if(e.parent().next().attr("id") == 'referenca_content')
	{
		showC = false;
	}
	else
	{
		showC = true;
	}
	
	if( act && contentExists() && !showC )
		return false;
	
	colorActive(e.next(), true);
	
	e.parent().removeClass();
	e.parent().addClass("active");

	if (contentExists()) {
		
		$('#referenca_content').animate({width: '0px'}, 500, function(){
		
			colorActive($("#reference .active2"), false);
			
			$(this).replaceWith("");
			
			var epos = e.position().left;
			
			if(showC)
			{
				$('#bottom').animate({scrollLeft: epos, scrollTop: 0}, 1000, function(){

					showContent(e, e.attr("id"), fancy);
																	  
				});
			}
			
		});
	}
	else
	{
		var epos = e.position().left;
		var rpos = $("#reference").position().left * (-1);
		
		var scrlTime = 1000;
		
		if(epos == rpos) scrlTime = 0; 
			
		$('#bottom').animate({scrollLeft: epos, scrollTop: 0}, scrlTime, function(){
															  
		 showContent(e, e.attr("id"), fancy);
															  
		});
	}
	
	return false;
	
}

function closeContent()
{
	if ($('#referenca_content').length) {
		
		$('#referenca_content').animate({width: '0px'}, 0, function(){
		
			colorActive($("#reference .active2"), false);
			$("#reference .active, #reference .active2").removeClass();
			$(this).replaceWith("");
			
		});
	}
}

function showContent(e, id, fancy){

	var content = '<li id="referenca_content"><div></div></li>';
	

	e.parent().after(content);
	
	$("#reference .active, #reference .active2").removeClass();
	e.parent().addClass("active");
	
	$.ajax({
		type: "POST",
		url: "/content/ajax/referenca_content.php", 
		data: 'id='+id,
		success: function(msg){
			
			$("#referenca_content div").html(msg);

			$("#referenca_content").animate({width: '770px'}, 500, function(){

				e.parent().addClass("active2");
				
				if(fancy!==false) fancy.trigger("click");
				
			});
				
		}
	});
	
	
}

function nastaviVelikostReferenc(test){

	if(!test)
	{
	var velikostOkna = $(window).width();

	var steviloReferenc = $("#reference > li").size();
	
	var velikostReference = $("#reference li").width();
	
	var velikostSkupna = (steviloReferenc * velikostReference) + velikostOkna;
	
	$("#reference").css("width", velikostSkupna+"px");
	
	
	$("#reference > li").css("height", $("#reference").height()+"px");
	}
	
	var visinaReferenc = $(window).height() - 363;
	
	$("#bottom").css("height", visinaReferenc+"px");
	
	//alert("stevilo referenc: "+steviloReferenc+"\nvelikost okna: "+velikostOkna+"\nvelikost reference:"+velikostReference+"\nskupna velikost: "+velikostSkupna);

}


/*
#######################################################
Filtriranje na "kako delamo"
*/

function getHash() {
  var hash = window.location.hash;
  return hash.substring(1); // remove #
}

function checkActive(){
	
	var hash = getHash();
	
	var arr = hash.split('/');
	$("ul.project-type div > li").removeClass();
	
	for(var x in arr)
	{
		//if(x>0) $("ul.project-type li#"+arr[x]).removeClass();
		if(x>0) $("ul.project-type li#"+arr[x]).addClass("active");
	}
	
	
}

function loadProjectType(e, id){
	
	$.ajax({
		type: "POST",
		url: "/content/ajax/filtri.php?action=loadprojecttype", 
		data: 'id='+id,
		success: function(msg){
			
			$("ul.project-filters li.active").removeClass();
			e.parent().addClass("active");
				
			$("ul.project-type").fadeOut(300, function(){
			
				$("ul.project-type").html(msg);
				checkActive();
				
				$("ul.project-type").fadeIn(300);
			
			});
		}
	});
	
	window.location.hash = '';
	sortReferences('');
	
}

function initProjectType(id){

	// dobi hash
	var hash = getHash();
	var newhash = '';
	
	if(!hash) newhash = '#filter/'+id;
	else
	{
		// razbij hash oz. preveri ce id ze obstaja v hash, potem ga odstrani ce ne ga doda
		var arr = hash.split('/');
		
		//preverit vsak id.. ce ga ne najde potem ga doda na konec.
		var found = false;
		
		
		for(var x in arr)
		{
			if(id == arr[x])
			{
				found = true;
			}
			else
			{
				if(x>0) newhash += '/';
				newhash += arr[x];
			}
		}
		
		if(!found)
		{
			newhash += "/"+id
		}
		
	}
	
	if(newhash == 'filter') newhash = '';
	
	window.location.hash = newhash;
	
	sortReferences(newhash);
	
}

function getProjectTypes()
{
	
	var result = $.ajax({
	  url: "/content/ajax/filtri.php?action=getProjectTypes", 
	  async: false
	 }).responseText;
	
	return result;

}

function checkAllTypes(hash)
{
	
	if(!hash)
		hash = getProjectTypes();
	
	var result = $.ajax({
	  type: "POST",
	  url: "/content/ajax/filtri.php?action=checkAllTypes", 
	  data: 'hash='+hash,
	  async: false
	 }).responseText;
	
	return result;

}

function sortReferences(hash)
{	
	checkActive();
	
	if(checkAllTypes(hash)=="0")
	{
	$.ajax({
		type: "POST",
		url: "/content/ajax/filtri.php?action=sortReferences", 
		data: 'filters='+hash,
		success: function(msg){
			
			var arr = msg.split('/');
			
			$("#reference > li").css("display", "none");

			for(var x in arr)
			{
				$("#reference > li#ref"+arr[x]).css("display", "block");
			}
		}
	});
	}
	else
	{
		$("#reference > li").css("display", "block");
	}
	
	closeContent();
	
	// tuki scroll na zacetek
	$('#bottom').animate({scrollLeft: 0, scrollTop: 0}, 0);
	
}




