$(document).ready(function(){
	// Generate a random starting position for the carousel
	var itemCount = $("#mycarousel").children().size();
	var randomnumber = Math.ceil(Math.random()*itemCount);

	/*carousel*/
	$("#mycarousel").jcarousel({
       scroll:1,
       auto:8,
       start:randomnumber,
       initCallback: buildButtons,
       itemLastInCallback: updateButtons,
       wrap:'last',
       animation:'slow'
	});
	
	/*prev next pagebrowser*/

	$('.browseLinksWrap ul li').each(function(){
		var First = $(this).find('span.prev');
		if(First) First.parent().parent().addClass('prev1');
		var Last = $(this).find('span.next');
		if(Last) Last.parent().parent().addClass('next1');
	});
	
	$('.news-catmenu ul li').hover(function(){
	$(this).addClass('over');
	}, function(){
	$(this).removeClass('over');
	});
	
	/*catmenu*/
	$('.news-catmenu').each(function(){	
	 $('.news-catmenu > ul > a:first-child').hide();
	 var Link = $('.news-catmenu > ul > a:first-child').attr('href');
	 $('.news-catmenu li:first a').text('Alle');
	 $(this).children('ul').children('li:last-child').addClass('last');
	});
	
	$('.news-catmenu ul li ul').each(function(){
	       	if($(this).find('div').hasClass('news-catmenu-ACT')){
	       	$(this).parent().addClass('active');
	       	}else{
	       	$(this).parent().removeClass('active');
	       	}
	});
	
	/* google search 2 iframes */
	var iframref = $('#cse-search-results iframe').attr("src");
	var iframheight = $('#cse-search-results iframe').attr("height");
	$('#cse-search-results').append('<iframe style="float:left;" scrolling="no" frameborder="0" width="310" height="'+ iframheight +'" src="'+ iframref +'&cx=004340071650825883411:4huh3ygureo"></iframe>');
	$('#cse-search-results').append('<iframe style="float:right;" scrolling="no" frameborder="0" width="310" height="'+ iframheight +'" src="'+ iframref +'&cx=004340071650825883411:6pfvssqyu-g"></iframe>');

	$('#cse-search-results iframe').attr("height","2100");
	$('#cse-search-results iframe:eq(0)').remove();	
	
	//add sequental number to most popular
	$('.tx-lfttnewsyoutube-listcontainer .news-item-most').each(function(i){
   		 i = i+1;
    		$(this).prepend('<span class="number">'+ i + '</span>');
    	});


	
	//----- 	
// Switch stylesheets starts
//-----

var c = readCookie('style');

if (c) {
	
	switchStylestyle(c);
	 
	if (c == "font_11px") {
        	$('.resize1').each(function(i) {
			$(this).attr({
				rel: "font_12px"
        		});
        	});
        	$('.resize2').each(function(i) {
			$(this).attr({
				rel: "font_11px"
        		});
        	});
        } else if (c == "font_12px") {
        	$('.resize1').each(function(i) {
			$(this).attr({
				rel: "font_13px"
        		});
        	});
        	$('.resize2').each(function(i) {
			$(this).attr({
				rel: "font_11px"
        		});
        	});
        } else if (c == "font_13px") {
        	$('.resize1').each(function(i) {
			$(this).attr({
				rel: "font_13px"
        		});
        	});
        	$('.resize2').each(function(i) {
			$(this).attr({
				rel: "font_12px"
        		});
        	});
        }
}
	
$('.resize1').click(function() {

	var relatt = this.getAttribute("rel");
        
       	switchStylestyle(relatt);
        
        if (relatt == "font_12px") {
		$(this).attr({
			rel: "font_13px"
        	});
        }
        if (relatt == "font_13px") {	
        	$('.resize2').attr({
			rel: "font_12px"
        	});
	}
        return false;	

}); 	

$('.resize2').click(function() {

	var relatt = this.getAttribute("rel");
        
       	switchStylestyle(relatt);
        
        if (relatt == "font_12px") {
		$(this).attr({
			rel: "font_11px"
        	});
        }
        if (relatt == "font_11px") {	
        	$('.resize1').attr({
			rel: "font_12px"
        	});
	}
	
        return false;
}); 	

//-----
// Switch stylesheets ends
//-----


});

function buildButtons(carousel, state) {
	// Build buttons
	var carId = carousel.list.context.id;
	var carObj = jQuery('#' + carId);
	var buttonsNo = carousel.options.size;
	var howMany = carousel.options.scroll;
		
	if(buttonsNo > howMany){
	
		// Find containing div, and make a ul inside
		carObj.parents('.jcarousel-skin-red').prepend('<div class="jcarousel-control ' + carId + '"></div>');
		
		// Append controls to div
		for(button = 1; button <= buttonsNo; button = button+howMany){
			jQuery('.' + carId).append('<a href="#"><span>'+(button)+'</span>&nbsp;</a>');
		}
		// Bind click function to buttons
	 	jQuery('.' + carId + ' a').bind('click', function() {
			var scrollTo = jQuery.jcarousel.intval(jQuery(this).text());
			carousel.scroll(scrollTo);
		    return false;
		});
	} else {
		carObj.parents('.jcarousel-skin-red').find('.jcarousel-prev, .jcarousel-next').hide();
	}
	
	// Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
	
}

function updateButtons(carousel, item, idx, state) {
	// Update the position of the buttons
	var position = Math.ceil(idx / carousel.options.scroll);
	var carId = carousel.list.context.id;
	var carObj = jQuery('#' + carId);

//console.log(position + ' ' + idx);
	
	// Find the div, then find controls for that div and then update the active button
	carObj.parents('.jcarousel-skin-red').find('.jcarousel-control a').removeClass('active').eq(position-1).addClass('active');
}

function switchStylestyle(styleName)
{
        $('link[rel*=style][title]').each(function(i)        
        {
                this.disabled = true;
                if (this.getAttribute('title') == styleName) this.disabled = false;
        });
        createCookie('style', styleName, 365);
}


// cookie functions http://www.quirksmode.org/js/cookies.html
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 var 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;
}

function eraseCookie(name){
	createCookie(name,"",-1);
}
//cookie functions end
