 var blank = new Image();
 blank.src = 'fileadmin/jq/blank.gif';

// when the DOM is ready...
$(document).ready(function () {

    var $panels = $('#slider .scrollContainer > div');
    var $container = $('#slider .scrollContainer');

    // if false, we'll float all the panels left and fix the width
    // of the container
    var horizontal = true;

    // float the panels left if we're going horizontal
    if (horizontal) {
        $panels.css({
        	'float' : 'left','position' : 'relative' // IE fix to ensure overflow is hidden
        });

        // calculate a new width for the container (so it holds all panels)
        if (typeof ($panels[0]) != 'undefined')
        	$container.css('width', $panels[0].offsetWidth * $panels.length);
    }

    // collect the scroll object, at the same time apply the hidden overflow
    // to remove the default scrollbars that will appear
    var $scroll = $('#slider .scroll').css('overflow', 'hidden');

    // apply our left + right buttons
    //$scroll
    //    .before('<img class="scrollButtons left" src="images/scroll_left.png" />')
    //    .after('<img class="scrollButtons right" src="images/scroll_right.png" />');

    // handle nav selection

    function setClass ()
    {
    	$(this).parent().find('li.selected').removeClass('selected')
        .end().end()
        .addClass('selected');
    }

	// unload content
    function fadeThemOut()
    {
    	$('#slider .scrollContainer').find('div.panel').each(function ()
		{
			$(this).find('div').contents().fadeOut('fast');
		});
    }

    function fadeThemIn()
    {
            // load/fadeIn (all/current) content
		$('#slider .scrollContainer').find('div.panel').each(function ()
		{
			$(this).find('div').contents().fadeIn('slow');
		});
	}

    function selectNav() {
    	//var id = $(this).find('a').attr('href').substring(1);	// get current id
		setClass();
    }

    $('#slider .navigation').find('li').click(selectNav);

    // go find the navigation link that has this target and select the nav
    function trigger(data) {
        var el = $('#slider .navigation').find('span[title$="' + data.id + '"]').parent().get(0); // li.selected
        setClass.call(el);

        fadeThemIn();
    }

    /*if (window.location.hash) {
        trigger({ id : window.location.hash.substr(1) });
    } else {
        $('ul.navigation a:first').click();
    }*/

    // offset is used to move to *exactly* the right place, since I'm using
    // padding on my example, I need to subtract the amount of padding to
    // the offset. Try removing this to get a good idea of the effect
    var offset = parseInt((horizontal ?
        $container.css('paddingTop') :
        $container.css('paddingLeft'))
        || 0) * -1;


    var scrollOptions = {
        target: $scroll,	// the element that has the overflow
        items: $panels,		// can be a selector which will be relative to the target
        navigation: '.navigation li',
        //prev: 'img.left',	// selectors are NOT relative to document, i.e. make sure they're unique
        //next: 'img.right',
        axis: 'xy',			// allow the scroll effect to run both directions
        onAfter: trigger,	// our final callback
        onBefore: fadeThemOut,
        offset: offset,
        duration: 900,		// duration of the sliding effect
        easing: 'swing',	// easing - can be used with the easing plugin: http://gsgd.co.uk/sandbox/jquery/easing/
    	interval: 9999
    };

    // apply serialScroll to the slider - we chose this plugin because it
    // supports// the indexed next and previous scroll along with hooking
    // in to our navigation.
    $('#slider').serialScroll(scrollOptions);

    if (!(window.location.hash)) $('ul.navigation li:first').click();

    // now apply localScroll to hook any other arbitrary links to trigger
    // the effect
    //$.localScroll(scrollOptions);

    // finally, if the URL has a hash, move the slider in to position,
    // setting the duration to 1 because I don't want it to scroll in the
    // very first page load.  We don't always need this, but it ensures
    // the positioning is absolutely spot on when the pages loads.
    //scrollOptions.duration = 1;
    //$.localScroll.hash(scrollOptions);

    // COMMON SETTINGS
    // IMAGES MOUSEOVER/MOUSEOUTS
	$(function() {
	    $('.rollover').hover(function() {
	        var currentImg = $(this).attr('src');
	        $(this).attr('src', $(this).attr('hover'));
	        $(this).attr('hover', currentImg);
	    }, function() {
	        var currentImg = $(this).attr('src');
	        $(this).attr('src', $(this).attr('hover'));
	        $(this).attr('hover', currentImg);
	    });

	    // HIDES LINKS IF NOT SET
	    var $all_links = $('.block_view_link > a');
	    $.each($all_links, function()
	    {
	    	if ($(this).attr("href") == "" || $(this).attr("href") == "#")
		    {
		    	$(this).css("display","none");
		    }
		    else
		    // HELPS TO CREATE URL WITHOUT TEXT
		    	$(this).html("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
	    });

	    var $print_link = $('#print_it');
	    $print_link.attr("href", "javascript.void(0)");
	    $print_link.click(function ()
	    {
			// Print the DIV.
			$( ".printable" ).print();
			// Cancel click event.
			return( false );
		});

		$('#goodweave_subnav ul li').hover(
		function()
		{
			/*if (!$(this).find('div').hasClass("act"))*/
			$(this).find('div').addClass("hover");
		},
		function()
		{
			$(this).find('div').removeClass("hover");
		});

		$('#goodweave_subnav ul li div').click(
		function ()
		{
	    	window.location.href = $(this).find('a').attr("href");
	    });

	     // png opacity fix for IE6
	     function fixPng(png)
	     {
			   // get src
			   var src = png.src;
			   // set width and height
			   if (!png.style.width) { png.style.width = $(png).width(); }
			   if (!png.style.height) { png.style.height = $(png).height(); }
			   // replace by blank image
			   png.onload = function() { };
			   png.src = blank.src;
			   // set filter (display original image)
			   png.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
		 }

	       var badBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
		   if (badBrowser)
		   {
		         // get all pngs on page (fix of png opacity)
			     $('img[src$=.png]').each(function()
			     {
			       if (!this.complete)
			       {
			         this.onload = function() { fixPng(this) };
			       } else {
			         fixPng(this);
			       }
			     });

			     // if IE6 and text is in '.csc-textpic-text' close by '.csc-textpic-imagewrap'
	    		 // then apply this hack...
			     // change width of textblock close to img
			     $(".csc-textpic-text").each(function()
			     {
			     	if ( $(this).parent().hasClass("csc-textpic-intext-right") )
			     		$(this).find('p.bodytext').css("width","230px");
			     });
		   }
		   
		   //var ie7 = (/MSIE (7|8)/.test(navigator.userAgent) && navigator.platform == "Win32");
		   //if (ie7)
		   //{
     			 $(".csc-textpic-text").each(function()
			     {
			     	if ( $(this).parent().hasClass("csc-textpic-intext-right") )
			     	{
			     		$(this).parent().find('.csc-textpic-imagewrap').css(
			     		{
			     			"float": "right",
			     			"padding-top": "0px",
			     			"padding-bottom": "0px"
			     		});
			     		
			     		$(this).find('.indent:first p:first').css("width","205px");
			     		
			     		// Second paragraph
						if( $(this).find('.indent:first p:first').text().length < 345)
							$(this).find('.indent:first p:first').next().css("width","205px");
							
						// Next sibling (<div class='indent'>)
						if ($(this).find('.indent:first p:first').text().length < 200)
						{
							$(this).find('.indent:first').next().next().find('p:first').css("width","205px");
							//alert($(this).find('.indent:first').next().next().find('p:first'))
						}
			     		/*if ( $(this).find('.indent:first > p').length <= 1 )
			     			$(this).find('.indent:first').next().next().find('p:first').css("width","205px");*/
			     	}
			     });
     	   //}
		});

});