/*-------------------------------------------------------------------------------------
	*
	*	@copyright:_________2010, PS-Planungsstudio
	*	@link:______________http://www.ps-planungsstudio.de
	*	@author:____________Patrick Voelcker, www.herontronic.de
	*
-------------------------------------------------------------------------------------*/

var sc = 0, sch = 0, containerHeight = 0;
var scrollContent = '';

function getContainerHeight(o) {
	containerHeight = $(o).parent().height();
	//$(o).parent().css("border","1px solid #f00");
	containerHeight -= 50;
	}
function getScrollContent(o) {
	sc = $(o).parent().children("div.scroll-contents"); sch = sc.height();//sc.css("border","1px solid #f00");
	}
	
/*
$(window).load(function() {
	$("#regal").css("width",(parseInt($(".sichtbar:last").offset().left)+600)+"px");
	if (parseInt($("#regal").css("width"))<parseInt($(window).width())) $("#regal").css("width","100%");
	$("#regalZeile").css("width",(parseInt($(".sichtbar:last").offset().left)+600)+"px");
	var filter=".marked";
		// Breite anpassen
		$(".opened:not('.ordner_leer'):not('.ordner_leer_gross') .ordnerOeffner").each(function(index){
			var w="96px", m="452px";
			if ($(this).parent().hasClass("ordner_gross")) { w="128px"; m="452px"; }
			$(this).parent().css("width",w);
			$(this).css("margin-left","0px");
			$(this).parent().css("background-position","0px 0px");
			$(this).parent().removeClass("opened");
		})
		
		// gefilterte Ordner darstellen
		$("#navi a"+filter).each(function(index)
			{
			filter=$(this).attr("rel");

			$(".ordner").each(function(index)
				{
				if(typeof meta[index] != "undefined")
					{
					for (i=0;i<meta[index].length;i++) if (meta[index][i]==filter)
						{
						$(this).css("display","block"); // Ordner anzeigen
						$(this).addClass("sichtbar"); // Ordner anzeigen
						}
					}
				});
			});

		// Gesamtbreite der Seite neu setzen
		$("#regal").css("width",(parseInt($(".sichtbar:last").offset().left)+600)+"px");
		if (parseInt($("#regal").css("width"))<parseInt($(window).width())) $("#regal").css("width","100%");
		$("#regalZeile").css("width",(parseInt($(".sichtbar:last").offset().left)+600)+"px");


		$("body").scrollLeft(0);

});
*/

$(document).ready(function(){
	// Gesamtbreite der Seite zu Beginn setzen
	$("#regal").css("width","99999px");
	$("#regalZeile").css("width","99999px");
	$("#regal").css("width",(parseInt($(".sichtbar:last").offset().left)+600)+"px");
	if (parseInt($("#regal").css("width"))<parseInt($(window).width())) $("#regal").css("width","100%");
	$("#regalZeile").css("width",(parseInt($(".sichtbar:last").offset().left)+600)+"px");
	var filter=".marked";
		// Breite anpassen
		$(".opened:not('.ordner_leer'):not('.ordner_leer_gross') .ordnerOeffner").each(function(index){
			var w="96px", m="452px";
			if ($(this).parent().hasClass("ordner_gross")) { w="128px"; m="452px"; }
			$(this).parent().css("width",w);
			$(this).css("margin-left","0px");
			$(this).parent().css("background-position","0px 0px");
			$(this).parent().removeClass("opened");
		})
		
		// gefilterte Ordner darstellen
		$("#navi a"+filter).each(function(index)
			{
			filter=$(this).attr("rel");

			$(".ordner").each(function(index)
				{
				if(typeof meta[index] != "undefined")
					{
					for (i=0;i<meta[index].length;i++) if (meta[index][i]==filter)
						{
						$(this).css("display","block"); // Ordner anzeigen
						$(this).addClass("sichtbar"); // Ordner anzeigen
						}
					}
				});
			});

		// Gesamtbreite der Seite neu setzen
		$("#regal").css("width",(parseInt($(".sichtbar:last").offset().left)+600)+"px");
		if (parseInt($("#regal").css("width"))<parseInt($(window).width())) $("#regal").css("width","100%");
		$("#regalZeile").css("width",(parseInt($(".sichtbar:last").offset().left)+600)+"px");


		$("body").scrollLeft(0);





	$(".sortabledropitem").each(function(index) {
		if (parseInt($(this).css("height"))<=parseInt($(this).parent().parent().css("height")))
			{
			$(this).parent().parent().children(".scroll-handle").css("display","none");
			//$(this).parent().parent().css("background-image","none");
			}
	});





	// Klick auf Ordnerruecken
	$(".ordner:not('.ordner_leer'):not('.ordner_leer_gross') .ordnerOeffner").click(function(event){ 
		$(this).blur();

		if ($(this).parent().hasClass("opened"))
			{
			var w="96px", m="452px";
			if ($(this).parent().hasClass("ordner_gross")) { w="128px"; m="452px"; }
			$(this).parent().children(".ordnerInhalt").css("display","none");
			$(this).parent().css("width",w);
			$(this).css("margin-left","0px");
			$(this).parent().css("background-position","0px 0px");
			$(this).parent().removeClass("opened");
			}
		else
			{
			var w="542px", m="-98px";
			if ($(this).parent().hasClass("ordner_gross")) { w="572px"; m="-129px"; }
			$(this).parent().css("width",w);
			$(this).css("margin-left",m);
			$(this).parent().children(".ordnerInhalt").css("display","block");
			$(this).parent().css("background-position",m+" 0px");
			$(this).parent().addClass("opened");
			}

		// Gesamtbreite der Seite neu setzen
		$("#regal").css("width","99999px");
		$("#regalZeile").css("width","99999px");
		$("#regal").css("width",(parseInt($(".sichtbar:last").offset().left)+600)+"px");
		if (parseInt($("#regal").css("width"))<parseInt($(window).width())) $("#regal").css("width","100%");
		$("#regalZeile").css("width",(parseInt($(".sichtbar:last").offset().left)+600)+"px");
		
		return false;
	})


	// Textscroller
	$('.scroll-handle').Draggable(
		{ zIndex: 1000, containment: 'parent', axis: 'vertically', /*grid: [1, 5], opacity: 0.7,*/
		onStart: function() 
			{
			getContainerHeight( this );
			getScrollContent( this )
			},
		onDrag : function(x,y)
			{
			var move = Math.abs(sch - containerHeight);
			if (move<0) move= Math.abs(containerHeight-sch);
			// scrollt Text
			// document.title="containerheight: "+containerHeight+" | move: "+move+" | sch: "+sch+" | x,y: "+x+","+y;
			if (move>0)
				{
				if(y>0 && y<(containerHeight)) sc.css("top",-y/containerHeight*move + "px");
				}
			else if(y>0 && y<(containerHeight)) sc.css("top",y/containerHeight*move + "px");
				
			return {x: x,y: y}
		}
	});
	
	// Mousewheel-Scroller
	$(".scroll-container").mousewheel(function(objEvent, intDelta){
		var sc = $(this).children().next();

		if (parseInt(sc.css("height"))>parseInt(sc.parent().css("height")))

		if ( ( (intDelta==-1) && (20-parseInt(sc.css("top"))<parseInt(sc.css("height"))) ) || 
		     ( (intDelta==1) && (-parseInt(sc.css("top"))>0) ) )
			{
			sc.css("top",parseInt(sc.css("top"))+(intDelta*10)+"px");
			// Cursor anpassen (not exactly)
			delta=(parseInt(sc.css("height"))-23) / parseInt(sc.parent().css("height"));
			$(this).children(".scroll-handle").css("top",-parseInt(sc.css("top"))/delta+"px");
			}

	});

	// rotes Mouseover ueber Label
	$(".ordnerOeffner").hover(function(){ $(this).children().children().css("background-position","-14px 0");
	}, function() { $(this).children().children().css("background-position","0 0"); }
	);



	// NAVIGATION
	$("#navi > li > a").click(function(i)
		{
		$("#regal").css("width","99999px");
		$("#regalZeile").css("width","99999px");

		// NAVIGATION Jonglage
		$(this).blur();
		if ($(this).hasClass("marked")) $(this).removeClass("marked"); else $(this).addClass("marked");

		var filter=".marked";

		// Filter ALL checken
		if ($(this).attr("rel")=="alle")
			{
			$("#navi > li > a.marked").removeClass("marked");
			$(this).addClass("marked");
			filter="";
			}
		else $(".filterAll a").removeClass("marked");

		if ($("#navi a.marked").size()==0) {$(".filterAll a").addClass("marked"); filter="";}

		// ORDNER
		// Alle Ordner kurz verschwinden lassen
		$(".ordnerInhalt").css("display","none"); // Orderninhalte ausblenden
		$(".ordner").removeClass("sichtbar"); // Ordner schliessen
		$(".ordner").css("display","none"); // Ordner schliessen

		// Breite anpassen
		$(".opened:not('.ordner_leer'):not('.ordner_leer_gross') .ordnerOeffner").each(function(index){
			var w="96px", m="452px";
			if ($(this).parent().hasClass("ordner_gross")) { w="128px"; m="452px"; }
			$(this).parent().css("width",w);
			$(this).css("margin-left","0px");
			$(this).parent().css("background-position","0px 0px");
			$(this).parent().removeClass("opened");
		})
		
		// gefilterte Ordner darstellen
		$("#navi a"+filter).each(function(index)
			{
			filter=$(this).attr("rel");

			$(".ordner").each(function(index)
				{
				if(typeof meta[index/*+1*/] != "undefined")
					{
					for (i=0;i<=meta[index/*+1*/].length;i++) if (meta[index/*+1*/][i]==filter)
						{
						$(this).css("display","block"); // Ordner anzeigen
						$(this).addClass("sichtbar"); // Ordner anzeigen
						}
					}
				});
			});

		// Gesamtbreite der Seite neu setzen
		$("#regal").css("width",(parseInt($(".sichtbar:last").offset().left)+600)+"px");
		if (parseInt($("#regal").css("width"))<parseInt($(window).width())) $("#regal").css("width","100%");
		$("#regalZeile").css("width",(parseInt($(".sichtbar:last").offset().left)+600)+"px");


/*		while (parseInt($("#regal").css("height"))>600)
			{
			$("#regal").css("width",(parseInt($("#regal").css("width"))+100)+"px");
			$("#regalZeile").css("width",(parseInt($("#regal").css("width"))+100)+"px");
			}
*/

		$("body").scrollLeft(0);
		return false;

		});




  // gallery: change image
  $("img").filter(".igallery").click(function()
    {
    var e=0;
    for (var i=0; i<$(".ordner").size(); i++) if ($(".ordner:eq("+i+") img.igallery").index(this)  !=  -1) e=i;//+1;
    //document.title=e;
    zaehler[e]=(zaehler[e]+1)%(gallery[e].length);
    // Bild austauschen
    $(this).attr("src", 'images/'+gallery[e][zaehler[e]]);
    // Bildunterschrift austauschen
    $(this).parent().children(".gallerysub").children().filter(":first-child").text(gallerytext[e][zaehler[e]]);
    // Pagina austauschen
    $(this).parent().children(".gallerysub").children(".pagina").text( (zaehler[e]+1)+"/"+gallery[e].length );
    return false;
    })

     
   
});

var gallery=new Array();
var gallerytext=new Array();
var zaehler=new Array();
var meta=new Array();


