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() {
  var cookieValue = readCookie('stylesheet');
    changeStylesheet(cookieValue);
});

var displaymode ;

function changeStylesheet(stylesheet)
{
 
  displaymode = stylesheet;

	$('#stylesheet').attr('href',stylesheet);

  if( stylesheet == 'blind' ){ 
   
    $('#stylesheet').attr('href',URI_STATIC + 'images/blind.css');
    $('#layoutcss').attr('rel','alternate stylesheet');
    $('#layoutcss').attr('title','noblind');
    $('.CmsPageMenu2').show();
    $('.CmsPageMenu1 > li').unbind('mouseenter mouseleave');

  }else{  
     $('.CmsPageMenu1 > li').hover(function() {
        $(this).addClass('hover').children('ul').stop(true,true).slideDown('fast');
      },function() {
        $(this).removeClass('hover').find('ul').stop(true,true).slideUp('fast');
      });
     
     $('#stylesheet').attr('href',URI_STATIC + 'images/main.css');
     $('#layoutcss').attr('rel','stylesheet');
     $('#layoutcss').removeAttr('title');
      
  }
  
	var expires = new Date();
	expires.setDate(expires.getDate() + 30);
	
	document.cookie = 'stylesheet=' + stylesheet + ';expires=' + expires.toGMTString() + ';path=/';
}


var newsIndex = 1,
	featuredNewsTimer = setInterval('loopFeaturedNews()',5000);

function loopFeaturedNews()
{
	if (newsIndex++ == 3) newsIndex = 1;
	changeFeaturedNews($('#featured-news li:nth-child(' + newsIndex + ') a'));
}

$(function() {
	$('#featured-news li a').bind('click',function() {
		clearTimeout(featuredNewsTimer);
		changeFeaturedNews(this);
		return false;
	});
});

function changeFeaturedNews(obj)
{
	$('#featured-news li.active').removeClass('active');
	$(obj).parent().addClass('active');
	
	$('#featured-news .left').animate({scrollTop: $($(obj).attr('href')).outerHeight() * $(obj).parent().index()},500);
}

$( function(){
var $thumbs = $('#gallery .thumbs');

$('#gallery .arrow.left').bind('click', {}, function( event )
{
  event.preventDefault();
	if ($thumbs.get(0).scrollLeft >= 80) $thumbs.animate({scrollLeft: '-=80'},500);
});

$('#gallery .arrow.right').bind('click', {}, function( event )
{
  event.preventDefault();
	if ($thumbs.get(0).scrollWidth - ($thumbs.get(0).scrollLeft + $thumbs.width()) >= 80) $thumbs.animate({scrollLeft: '+=80'},500);
});

$('#gallery .thumbs a').bind('click',function()
{
	var image = new Image();
	image.onload = function()
	{
		$('#gallery-image').animate({opacity: 0.3},300,function()
		{
			$(this).attr('src',image.src);
			$(this).animate({opacity: 1},300);
		});
	};
	
	image.src = $(this).attr('href');
	
	return false;
});
});
