
$j = $; //jQuery.noConflict();

$j(document).ready(function(){
	// disable old events
	/*
	$j('li.Level0Item > ol').hide();

	$j('li.Level0Item').mouseover(function(){
		console.log('hover');
		$j('> ol', this).slideDown();
	}).mouseout(function(){
		$j('> ol', this).slideUp();
	});
	*/

	/*
	if (TransMenu.isSupported())
	{
		function init_transmenu_subitems(transmenu, subitems)
		{
			subitems.each(function(i){
				var link = $j('> a', this);
				var item = transmenu.addItem(link.text(), link.attr('href'), link.attr('target'));

				var subitems = $j('> ol > li', this);
				if (subitems.length)
				{
					var subtransmenu = transmenu.addMenu(transmenu.items[i], i);
					init_transmenu_subitems(subtransmenu, subitems);
				}
			});
		}

		function hide_transmenu()
		{
			TransMenuSet.registry[0].hide();
		}

		var menuitems = $j('li.Level0Item').add('li.TopMenuItem').add('#MoilHeader');

		var ms = new TransMenuSet(TransMenu.direction.down, 1, 24, TransMenu.reference.topLeft);

		// generate transmenu from html
		menuitems.each(function(){
			var subitems = $j('> ol > li', this);
			if (!subitems.length)
			{
				this.onmouseover = hide_transmenu;
			}
			else
			{
				var link = $j('> a', this);
				var transmenu = ms.addMenu(link[0], 0);

				init_transmenu_subitems(transmenu, subitems);
			}
		});

		// remove menu from html
		menuitems.find('> ol').remove();

		/ *
		menu.onactivate = function() {};
		menu.ondeactivate = function() {};
		* /

		// init transmenu
        var aMenuHtml = [];
        for (var i = 0, menu = null; menu = TransMenu.registry[i]; i++) {
                aMenuHtml[i] = menu.toString();
        }
        $j('body').append(aMenuHtml.join(""));

		TransMenu.initialize();
	}
	*/
	/*
	$('a.image-popup').lightBox();
	*/

	if($j("#ProductContent").length>0) {
		
		if($j("#ProductContent")[0].offsetWidth>($j("#ProductsContainer")[0].offsetWidth+15)) {
			$j("#arrow_right").show();
		}
	}
	
	if($j("#ProductsContainer .prod").length>0) {
	   $j("#arrow_left").show();
	   $j("#arrow_right").show();
	}

	if($j("#Menu-Level1 li").length>0) {
        //selektierter Menüpunkt
        $j(".borderBottom").append("<div class=\"borderBottom_verlaufAnfang_parent\"><div class=\"borderBottom_verlaufAnfang\"><img src=\"/images/menu/endung_links.png\" /></div></div>");
        
        $j(".borderBottom").append("<div class=\"borderBottom_strich_links_parent\"><div class=\"borderBottom_strich_links\"></div></div>");
        $j(".borderBottom_strich_links_parent").css("width",($j(".borderBottom_linkesEnde")[0].offsetLeft+14)+"px");
        $j(".borderBottom_strich_links").css({"position":"absolute", "right": "0px"});
        
        $j(".borderBottom").append("<div class=\"borderBottom_blank\"></div>");
        
        if($j(".borderBottom_rechtesEnde").length>0) {
            if($.browser.msie && $.browser.version=="7.0") {
                $j(".borderBottom_blank").css("width",($j(".borderBottom_rechtesEnde")[0].offsetLeft-$j(".borderBottom_linkesEnde")[0].offsetLeft)+14+"px");
            } else
                $j(".borderBottom_blank").css("width",($j(".borderBottom_rechtesEnde")[0].offsetLeft-$j(".borderBottom_linkesEnde")[0].offsetLeft)+parseInt($j(".borderBottom_strich_links_parent").css("width"))+14+"px");
 
            $j(".borderBottom").append("<div class=\"borderBottom_strich_rechts_parent\"><div class=\"borderBottom_strich_rechts\"></div></div>");
        
            var width = parseInt($j(".borderBottom").css("width"));
            var offsetLeft = $j(".borderBottom_rechtesEnde")[0].offsetLeft;
    
            $j(".borderBottom_strich_rechts_parent").css({"width":(width-offsetLeft-60)+"px","float":"left"});
            
            $j(".borderBottom").append("<div class=\"borderBottom_verlaufEnde\"><img src=\"/images/menu/endung_rechts.png\" /></div>");
            
            $j(".borderBottom_rechterContent").append("<img src=\"/images/menu/strich_mit_rundung.png\" />");
        }
        
        $j(".borderBottom_linkerContent").append("<img src=\"/images/menu/strich_mit_rundung_links.png\" />");
        
        var fade = 10;
        var animation = 500;
        
        if($j(".borderBottom_rechtesEnde").length>0) {
            $j(".borderBottom_linkerContent").fadeIn(fade);
        
            $j(".borderBottom_rechterContent").fadeIn(fade, function() {
                var to_width = parseInt($j(".borderBottom_strich_links_parent").css("width"));
    
                $j(".borderBottom_strich_links").animate ({
                    width: to_width+"px"
                },animation, "linear", function() {
                    $j(".borderBottom_verlaufAnfang").fadeIn(1);
                });
                
                var to_width = parseInt($j(".borderBottom_strich_rechts_parent").css("width"));
                $j(".borderBottom_strich_rechts").animate ({
                    width: to_width+"px"
                },animation, "linear", function() {
                    $j(".borderBottom_verlaufEnde").fadeIn(1);
                });
            });
        } else {   
            $j(".borderBottom_linkerContent").fadeIn(fade, function() {
                var to_width = parseInt($j(".borderBottom_strich_links_parent").css("width"));
    
                $j(".borderBottom_strich_links").animate ({
                    width: to_width+"px"
                },animation, "linear", function() {
                    $j(".borderBottom_verlaufAnfang").fadeIn(1);
                });
            });
        }
	}
	
	if(jQuery.browser.version < 8 && $.browser.msie) {

		if($j(".jScrollPaneTrack").length>0)
			$j(".jScrollPaneTrack").css({"background-image" : "none", "filter" : "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/images/scrollPaneDrag_bg.png')"});
	   //$j("#Popup").css({"background-image" : "none", "filter" : "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/images/popup.png')"});

		if($j("div.link_rec_prod").length>0)
    	   $j("div.link_rec_prod,div.to_rec_prod").css({"background-image" : "none", "filter" : "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/images/arrow.png')"});
	   
		$j("img").each(function() {
            $this = $(this);
            
            if($this.attr("class")!="noSpacer") {
	            var orig_src = $this.attr("src");
	
	            $this.attr("src","/images/spacer.gif");
	            $this.css({"filter" : "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+orig_src+"')"});
            }
	   });
	}
});

start_scrolling = 0;
var product_fade_speed = 1000;

//////// PRODUCT SWITCHER //////////
function next_product(back) {
	
	if(start_scrolling==1)
		return;
	
	start_scrolling=1;
	
	if($j("#ProductsContainer .prod:visible").nextAll(".prod").length==0) 
	{
		if(back > 0)
			document.location.href='products.php?page_id='+back;
		else {
		   $j("#ProductsContainer .prod:visible").fadeOut(product_fade_speed,function() {
	    	   start_scrolling=0;
	       });
		   $j("#ProductsContainer .prod:first").fadeIn(product_fade_speed, function() {
	
				$j(".prod:visible .jScrollPaneContent").jScrollPane({scrollbarWidth:5});
			});
		}
	}
	else
	{
       $j("#ProductsContainer .prod:visible").fadeOut(product_fade_speed,function() {
    	   start_scrolling=0;
       });
       $j("#ProductsContainer .prod:visible").nextAll(".prod:first").fadeIn(product_fade_speed, function() {

			$j(".prod:visible .jScrollPaneContent").jScrollPane({scrollbarWidth:5});
		});
	}
}

function previous_product() {
	
	if(start_scrolling==1)
		return;
	
	start_scrolling=1;
	
	if($j("#ProductsContainer .prod:visible").prevAll(".prod").length==0)
	{
	   $j("#ProductsContainer .prod:visible").fadeOut(product_fade_speed,function() {
    	   start_scrolling=0;
       });
	   $j("#ProductsContainer .prod:last").fadeIn(product_fade_speed, function() {
			$j(".prod:visible .jScrollPaneContent").jScrollPane({scrollbarWidth:5});
		});
	}
	else
	{
       $j("#ProductsContainer .prod:visible").fadeOut(product_fade_speed,function() {
    	   start_scrolling=0;
       });
       $j("#ProductsContainer .prod:visible").prevAll(".prod:first").fadeIn(product_fade_speed, function() {
		$j(".prod:visible .jScrollPaneContent").jScrollPane({scrollbarWidth:5});
		});
	}
}


//////// RECEIPT SWITCHER //////////
function next_receipt()
{
	if(start_scrolling==1)
		return;
	
	start_scrolling=1;
	
	if($j("#ProductsContainer .rec:visible").nextAll(".rec").length==0) 
	{
	   $j("#ProductsContainer .rec:visible").fadeOut(product_fade_speed,function() {
    	   start_scrolling=0;
       });
	   $j("#ProductsContainer .rec:first").fadeIn(product_fade_speed, function() {
			$j(".prod:visible .jScrollPaneContent").jScrollPane({scrollbarWidth:5});
		});
	}
	else
	{
       $j("#ProductsContainer .rec:visible").fadeOut(product_fade_speed,function() {
    	   start_scrolling=0;
       });
       $j("#ProductsContainer .rec:visible").nextAll(".rec:first").fadeIn(product_fade_speed, function() {
			$j(".prod:visible .jScrollPaneContent").jScrollPane({scrollbarWidth:5});
		});
	}
}

function previous_receipt()
{
	if(start_scrolling==1)
		return;
	
	start_scrolling=1;
	
	if($j("#ProductsContainer .rec:visible").prevAll(".rec").length==0)
	{
	   $j("#ProductsContainer .rec:visible").fadeOut(2000,function() {
    	   start_scrolling=0;
       });
	   $j("#ProductsContainer .rec:last").fadeIn(2000, function() {
			$j(".prod:visible .jScrollPaneContent").jScrollPane({scrollbarWidth:5});
		});
	}
	else
	{
       $j("#ProductsContainer .rec:visible").fadeOut(2000,function() {
    	   start_scrolling=0;
       });
       $j("#ProductsContainer .rec:visible").prevAll(".rec:first").fadeIn(2000, function() {
			$j(".prod:visible .jScrollPaneContent").jScrollPane({scrollbarWidth:5});
		});
	}
}

function to_history(historyid) {
	if(start_scrolling==1)
		return;
	
	start_scrolling=1;
	
	$j("#ProductsContainer .prod:visible").fadeOut(1500,function() {
		start_scrolling=0;
	});
	
	$j("#prod_"+historyid).fadeIn(1500, function() {
		$j(".prod:visible .jScrollPaneContent").jScrollPane({scrollbarWidth:5});
	});
}

//////// PRODUCT / RECEIPT SWITCHER /////////
function to_prod(prodid) {
	if(start_scrolling==1)
		return;
	
	start_scrolling=1;
	
	$j("#jScrollPaneContent_"+prodid).jScrollPane();
	
	$j("#rec_"+prodid).fadeOut(2000,function() {
 	   start_scrolling=0;
    });
	$j("#prod_"+prodid).fadeIn(2000, function() {
		$j(".prod:visible .jScrollPaneContent").jScrollPane({scrollbarWidth:5});
	});
	
	$j("#arrow_left").unbind("click", previous_receipt);
	$j("#arrow_left").click(previous_product);
	
	$j("#arrow_right").unbind("click", next_receipt);
	$j("#arrow_right").click(next_product);
}

function to_rec(recid) {
	if(start_scrolling==1)
		return;
	
	start_scrolling=1;
	
	$j("#jScrollPaneContent_"+recid).jScrollPane();
	
	$j("#prod_"+recid).fadeOut(2000,function() {
 	   start_scrolling=0;
    });
	$j("#rec_"+recid).fadeIn(2000, function() {
		$j(".prod:visible .jScrollPaneContent").jScrollPane({scrollbarWidth:5});
	});
	
	$j("#arrow_left").unbind("click", previous_product);
	$j("#arrow_left").click(previous_receipt);
	
	$j("#arrow_right").unbind("click", next_product);
	$j("#arrow_right").click(next_receipt);
}

//////// SCROLLER /////////////////

scrollstep = "7";
scrollspeed = "30";
scrolling = 0;


function moveright(onestep) {

	if (scrolling == 0) return;
	
	if ($("#ProductContent")[0].offsetWidth < $("#ProductsContainer")[0].offsetWidth)
	{
		return;
	}

	scroll_to = ($("#ProductContent")[0].offsetLeft-scrollstep);

	if (scroll_to < ($("#ProductContent")[0].offsetWidth-$("#ProductsContainer")[0].offsetWidth)*-1) {
		scroll_to = ($("#ProductContent")[0].offsetWidth-$("#ProductsContainer")[0].offsetWidth)*-1;
		
		$("#arrow_left").show();
		$("#arrow_right").hide();
		$("#ProductContent").animate({left: scroll_to},30);
	} else {
		$("#ProductContent").animate({left: scroll_to},30, function() {
			$("#arrow_left").show();
			$("#arrow_right").show();
			if (!onestep)
				moveright();
		});
	}
	
}
function moveleft(onestep) {

	if (scrolling == 0) return;
	
	if ($("#ProductContent")[0].offsetWidth < $("#ProductsContainer")[0].offsetWidth)
		return;

	scroll_to = parseInt($("#ProductContent")[0].offsetLeft)+parseInt(scrollstep);
	
	if (scroll_to >= 0) {
		//alert(scroll_to);
		scroll_to = 0;
		//$("#Content").animate({top: 0},30);
		$("#arrow_left").hide();
		$("#arrow_right").show();
		
		$("#ProductContent").css({left: "0px"});
		
	} else {
		//alert(scroll_to);
		$("#ProductContent").animate({left: scroll_to},30, function() {
		$("#arrow_right").show();
			if (!onestep)
				moveleft();
		});
	}
}

function start_scroll(div,direction) {
	scrolling = 1;
	//scrollstep = "5";

	
	if (direction == "left") 
		moveleft(false);
	else if (direction == "right")
		moveright(false);
}

function stop_scroll(div) {
	scrolling = 0;
}
