/**
 * Zoom function
 */
function Zoom(element, maxSteps) {

    this.element = element;
    this.zoom = parseFloat(element.css("font-size"));
    this.startValue = this.zoom;
    this.standart = element.css("font-size").replace(/[0-9]*\.[0-9]*|[0-9]*/,"");
    this.maxSteps = maxSteps;

    this.zoomIn = function() {
        if(this.startValue + (this.maxSteps / 2) > this.zoom) {       
            this.zoom += 1;
            this.resizeText();
        }

    }
    
    this.zoomOut = function() {
        if(this.startValue - (this.maxSteps / 2) < this.zoom) {       
            this.zoom -= 1;
            this.resizeText();
        }
    }
    
    this.standartZoom = function() {
        this.zoom = this.startValue;
        this.resizeText();
    }
    
    this.resizeText = function() {
        this.element.css("font-size",this.zoom + this.standart);
    }

}

function initZoom() {
    zoom = new Zoom(jQuery("#content"), 4);
    jQuery("#text_small").click(function(){zoom.zoomOut()});
    jQuery("#text_middle").click(function(){zoom.standartZoom()});
    jQuery("#text_big").click(function(){zoom.zoomIn()});

}

/**
 * Print view
 */
function printSelection(node){
  var content=jQuery(node).html();
  var pwin=window.open('','print_content','width=1024,height=768');

  pwin.document.open();
  pwin.document.write('<html><body onload="window.print()">'+content+'</body></html>');
  pwin.document.close();
 
  setTimeout(function(){pwin.close();},1000);
}

/**
 * JPlayer
 */
function initJPlayer(id, audio) {
    jQuery("#"+id).jPlayer({
		ready: function () {
			jQuery(this).jPlayer("setMedia", {
				mp3: audio
			});
                        jQuery(this).jPlayer("play");
		},
		swfPath: "fileadmin/lib/jQuery.jPlayer.2.1.0/Jplayer.swf",
		supplied: "mp3",
                cssSelectorAncestor: getContainer(id),
		wmode: "window"
	});

}

function getContainer(id) {
    return "#jp_container_"+id.replace(/[\D]*/,"");
}

jQuery(document).ready(function() {
    initZoom();
    
    jQuery("h6").click(function() {
        jQuery(this).next().slideToggle('fast');
    });
    
    jQuery('.bwPrinterRO').click(function(){
        printSelection(jQuery(this).parent().parent().prev());
    });
    
    jQuery(".bwReaderRO").click(function(){
        jQuery(this).next().slideToggle("slow", function() {
            var id = jQuery(this).children(".jp-jplayer").attr("id");
            var href = jQuery(this).children(".jp-jplayer").attr("href");
            if(!jQuery("#" + id).hasClass("init")) {
                initJPlayer(id,href);
            }
            jQuery("#" + id).addClass("init");
        });
    });
    
    var slideNav = 0;

//IE FIX
if(jQuery("BODY").hasClass("easyView") == false) {
    jQuery('.bwReader').mouseenter(function() {
        jQuery(jQuery(this).children(".bwReaderNO")).hide();
        jQuery(jQuery(this).children(".bwReaderRO")).show();
    });
    
    jQuery('#content .bwReader').mouseleave(function() {
        jQuery(jQuery(this).children(".bwReaderNO")).show();
        jQuery(jQuery(this).children(".bwReaderRO")).hide();
        
    });
    
    jQuery('#content .bwPrinter').mouseenter(function() {
        jQuery(jQuery(this).children(".bwPrinterNO")).hide();
        jQuery(jQuery(this).children(".bwPrinterRO")).show();
    });
    
    jQuery('#content .bwPrinter').mouseleave(function() {
        jQuery(jQuery(this).children(".bwPrinterNO")).show();
        jQuery(jQuery(this).children(".bwPrinterRO")).hide();
    });
}

    jQuery(".mainMenu-lvl2-ACT").nextUntil('.mainMenu-lvl2').slideDown('fast');
    jQuery(".mainMenu-lvl2").mouseenter(function() {
        if(!jQuery(this).hasClass('mainMenu-lvl2-ACT')) {
            slideNav = 1;
            jQuery(this).nextUntil('.mainMenu-lvl2').slideDown('fast');
        }
    });
    jQuery("#leftNav").mouseleave(function() {
        if(slideNav == 1) {
            jQuery("#leftNav .mainMenu-lvl3").slideUp('fast', function() {
                jQuery(".mainMenu-lvl2-ACT").nextUntil('.mainMenu-lvl2').slideDown('fast');
            });
        }
    });

});
