var $j = jQuery.noConflict();
$j(document).ready(function() {

	/* Styleswitcher */
	// Onload Cookie laden und Klasse setzen
	$j("BODY").addClass(readCookie("css_size"));
	last_size = readCookie("css_size");
	
	// HTML-Links schreiben
	size = '<li class="css_size"><span id="normal">A</span> <span id="gross">A</span> <span id="sehr_gross">A</span></li>';
	$j(".s_metanav LI:last").after(size);
	
	// Links Size klickbar machen
	$j(".css_size SPAN").click(function() {
		$j("BODY").removeClass(last_size);
		$j("BODY").addClass(this.id);
		createCookie("css_size", this.id);
		last_size = this.id;
		return false;
	});
	
	// Slider Home Groß
	var anzahl_hs = $j('.jcarousel-skin-home').children().length;
	for (i=0; i<anzahl_hs; i++) {
		zahl = i+1;
		$j(".paging").append('<a href="javascript:;"><span>' + zahl + '</span></a>');
	}

	// Facebook
	/*$j(".s_nav").append('<div class="facebook"><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.siedl.net&amp;send=false&amp;layout=button_count&amp;width=120&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font&amp;height=21" scrolling="no" frameborder="0"></iframe></div><!--[if IE]><div class="facebook_ie"><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.siedl.net&amp;send=false&amp;layout=button_count&amp;width=120&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font&amp;height=21" scrolling="no" frameborder="0" allowTransparency="true"></iframe></div><![endif]-->');*/
	/*$j(".s_nav").append('<div class="facebook"><span><a href="//connect.facebook.net/de_DE/all.js#xfbml=1&appId=47790172168" target="_blank"><img src="fileadmin/templates/img/logo_facebook_fuo.png" alt="Finde uns auf Facebook"></a></span><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.siedl.net&amp;send=false&amp;layout=button_count&amp;width=120&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font&amp;height=21" scrolling="no" frameborder="0" allowTransparency="true"></iframe></div>');*/
	$j(".s_nav").append('<div class="s_fb_link_zum_profil"><a href="https://www.facebook.com/siedlnetworks" target="_blank"><img src="fileadmin/templates/img/logo_facebook_fuo.png" alt="Finde uns auf Facebook"></a></div><div class="fb-like" data-href="https://www.facebook.com/siedlnetworks" data-send="false" data-layout="button_count" data-width="105" data-show-faces="false" data-font="arial"></div>');
	
	$j('.jcarousel-skin-home').jcarousel({
		scroll: 1,
		wrap: 'circular',
		auto: 8,
        buttonNextHTML: null,
        buttonPrevHTML: null,
		initCallback: mycarousel_initCallback,
//		itemVisibleInCallback: home_itemVisibleInCallback
		itemVisibleInCallback: {
		  onBeforeAnimation: home_itemVisibleInCallback
//		  onAfterAnimation: home_itemVisibleInCallback
		}
	});
	
	// Slider Home Referenzen
	$j('.jcarousel-skin-related').jcarousel({
		scroll: 1,
		wrap: 'circular',
		auto: 0,
		initCallback: mycarousel_initCallbackRef
	});
	
	// Tagcloud
	/*
	$j('ul#jcloud-tags').jcloud({
		radius:135,          //    cloud radius
		size:30,             //    tags font size
		step:2,              //    cloud step
		speed:50,            //    cloud speed
		flats:1,             //    flats count
		clock:10,            //    timer interval
		areal: 275,           //    cloud areal
		splitX:0,          //    X-axis delta
		splitY:0,          //    Y-axis delta

		areal: 275,
        iterX: 0,
        iterY: 0,

		colors:['#000000','#403E3E','#006600','#0b5e2e']
		
	});
	*/
	
});

function mycarousel_initCallback(carousel)
{
	
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });
	

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
	
    // Home Scroller
    $j('.paging A').bind('click', function() {
        carousel.scroll($j.jcarousel.intval($j(this).text()));
		//$j(".paging A").removeClass('active'); //Remove all active class
		//$j(this).addClass('active'); //Add active class (the $jactive is declared in the rotateSwitch function)
        return false;
    });
    
};

function mycarousel_initCallbackRef(carousel)
{
	
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });
	

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
	
};

function home_itemVisibleInCallback(carousel,li,c_index) {
	//Remove all active class
	$j(".paging A").removeClass('active');
	
	//Add active class
	myitems = $j(".paging A").length;
	myindex = (c_index%myitems) -1;
	$j(".paging A").eq(myindex).addClass('active');
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

/**
 * jCloud
 * (C) 2011 Sawanna Team (http://sawanna.org)
 */

/*
$j.fn.jcloud=function(settings)
{
    jCloud={
        tags: new Array(),
        container_id: 'jcloud',
        radius: 135,
        size: 20,
        areal: 180,
        iterX: 0,
        iterY: 0,
        iterx: 1,
        itery: 1,
        step: 4,
        flats: 1,
        speed: 100,
        start_speed: 1,
        timer: 0,
        clock: 10,
        stop: true,
        splitX: 0,
        splitY: 0,
        colors: new Array(),
        init: function (elem,settings) {
            $j(elem).css('display','none');
            
            var i=0;
            $j(elem).find('li').each(function(){
                jCloud.tags[i]=$j(this).html();
                i++;
            });
            
            if (typeof(settings) != 'undefined')
            {
                try {
                    if (typeof(settings.container_id) != 'undefined')
                    {
                        this.container_id=settings.container_id;
                    }
                    if (typeof(settings.radius) != 'undefined')
                    {
                        this.radius=settings.radius;
                    }
                    if (typeof(settings.size) != 'undefined')
                    {
                        this.size=settings.size;
                    }
                    if (typeof(settings.areal) != 'undefined')
                    {
                        this.areal=settings.areal;
                    }
                    if (typeof(settings.step) != 'undefined')
                    {
                        this.step=settings.step;
                    }
                    if (typeof(settings.flats) != 'undefined')
                    {
                        this.flats=settings.flats;
                    }
                    if (typeof(settings.speed) != 'undefined')
                    {
                        if (settings.speed < 1)
                        {
                            settings.speed=1;
                        }

                        this.speed=settings.speed;
                    }
                    if (typeof(settings.clock) != 'undefined')
                    {
                        this.clock=settings.clock;
                    }
                    if (typeof(settings.splitX) != 'undefined')
                    {
                        if (Math.abs(settings.splitX) < 2*this.radius)
                        {
                            this.splitX=settings.splitX;
                        }
                    }
                    if (typeof(settings.splitY) != 'undefined')
                    {
                        if (Math.abs(settings.splitY) < 2*this.radius)
                        {
                            this.splitY=settings.splitY;
                        }
                    }
                    if (typeof(settings.colors) != 'undefined' && settings.colors instanceof Array)
                    {
                        this.colors=settings.colors;
                    }
                } catch (e) {}
            }
            
            $j(elem).replaceWith('<div id="'+this.container_id+'"></div>');
            
            $j('#'+this.container_id).css({
                        'position':'relative',
                        'width':this.radius*2,
                        'height':this.radius*2,
                        'overflow':'hidden',
                        'padding': '0px'
                        });
                        
            $j('#'+this.container_id).mousemove(function(e){
                var centerX=$j(this).offset().left+jCloud.radius;
                var centerY=$j(this).offset().top+jCloud.radius;
                
                jCloud.speed=jCloud.start_speed;
                jCloud.iterx=(centerX-e.pageX)/jCloud.radius;
                jCloud.itery=(centerY-e.pageY)/jCloud.radius;
            });
            
            $j('#'+this.container_id).mouseover(function()
            {
                jCloud.stop=false;
            });
            
            $j('#'+this.container_id).mouseout(function()
            {
                jCloud.stop=true;
            });
            
            $j('body').append('<div id="jcloud-buffer"></div>');
            $j('#jcloud-buffer').css({
                        'display': 'none',
                        'padding': '0px',
                        'white-space': 'nowrap'
                    });
        },
        render: function() {
            $j('#'+this.container_id).empty();
            
            this.start_speed=this.speed;
            
            for (var j in this.tags)
            {
                this.draw(j);
            }
            
            this.cloud();
        },
        draw: function(id) {
            $j('#jcloud-buffer').html('<div class="jtag" id="jtag-'+id+'">'+this.tags[id]+'</div>');
            
            var taghtml=$j('#jcloud-buffer').html();
            var width=$j('#jcloud-buffer').width();
            var height=$j('#jcloud-buffer').height();
            
            $j('#'+this.container_id).append(taghtml);
            
            $j('#jtag-'+id).attr('width',width);
            $j('#jtag-'+id).attr('height',height);
        },
        cloud: function(iter,flat_iter,update) {
            var count=this.tags.length;
            var per_flat=Math.ceil(count/this.flats);
            var delta=Math.round(360/per_flat);
            var deg=0;
            var flat_deg=0;

            if (typeof(iter) != 'undefined')
            {
                deg=Math.floor(iter);
            }
            
            if (typeof(flat_iter) != 'undefined')
            {
                flat_deg=Math.floor(flat_iter);
            }
            
            var flat_offset=Math.floor(this.areal/(this.flats+1))/2+flat_deg;
            var flat_delta=Math.floor(this.areal/this.flats);
            
            var flat_co=0;
            for (var j in this.tags)
            {
                this.pos(deg,j,parseInt($j('#jtag-'+j).attr('width')),parseInt($j('#jtag-'+j).attr('height')),flat_offset,update);
                deg+=delta;
                
                flat_co++;
                
                if (flat_co>=per_flat)
                {
                    flat_offset+=flat_delta;
                    flat_co=0;
                    deg+=flat_delta;
                }
            }
        },
        pos: function(degree,id,width,height,offset,update) {
            var radian=degree*Math.PI/180;
            var ell_offset=offset*Math.PI/180;
            var size_offset=0;
            
            var size=this.size+size_offset;
            
            var X=this.radius+Math.sin(radian)*this.radius*Math.cos(ell_offset)-width/2;
            var Y=this.radius-Math.cos(radian)*this.radius-height/2;
            
            size=(1+Math.cos(radian-1.57)*Math.sin(ell_offset))*size/2;
            
            if (size < 1)
            {
                size=1;
            }
            
            if (typeof(update) == 'undefined' || update==false)
            { 
                if (this.colors.length > 0)
                {
                    for (var i in this.colors)
                    {
                        if ((parseInt(id)+1) % (this.colors.length-i) == 0)
                        {
                            //$j('#jtag-'+id).css('color',this.colors[this.colors.length-i-1]);
                            //$j('#jtag-'+id).children('a').css('color',this.colors[this.colors.length-i-1]);
                            
                            $j('#jtag-'+id).attr('style','color:'+this.colors[this.colors.length-i-1]+' !important');
                            $j('#jtag-'+id).children('a').attr('style','color:'+this.colors[this.colors.length-i-1]+' !important');
                            break;
                        }
                    }
                }
                
                $j('#jtag-'+id).css({

                    'position':'absolute',
                    'white-space': 'nowrap',
                    'font-weight': 'normal'
                });
            }
            
            var opacity=1;
            opacity=(1+Math.cos(radian-1.57)*Math.sin(ell_offset))*opacity/2;
            
            if (opacity<.5)
            {
                opacity=.5;
            }
            
            if (this.splitX)
            {
                var offsetX=parseInt(this.splitX);
                if (id%2==0)
                {
                    X-=offsetX;
                } else
                {
                    X+=offsetX;
                }
            } 
            
            if (this.splitY)
            {
                var offsetY=parseInt(this.splitY);
                if (id%2==0)
                {
                    Y-=offsetY;
                } else
                {
                    Y+=offsetY;
                }
            } 
            
            $j('#jtag-'+id).css({
                    'left':Math.round(X),
                    'top':Math.round(Y),
                    'font-size':Math.floor(size)+'px',
                    'opacity': opacity
                });
            
            
                
        },
        update: function() {
            this.timer++;
            
            var interval=Math.floor(5/jCloud.speed);
            
            if (this.timer < interval)
            {
                return;
            }
            
            if (this.stop)
            {
                this.speed=this.speed-(this.speed*.1);
            }
            
            this.timer=0;
            
            if (this.iterX >= 360)
            {
                this.iterX=360-this.iterX;
            }
            
            if (this.iterY >= 360)
            {
                this.iterY=360-this.iterY;
            }
            
            this.iterX+=this.iterx;
            this.iterY+=this.itery;

            this.cloud(this.iterY,this.iterX,true);
        }
    }
    
    jCloud.init($j(this),settings);
    
    jCloud.render();
    
    jCloudInterval=window.setInterval("jCloud.update()",jCloud.clock);
}
*/
