$(document).ready(init);

function init() {
	$.supersized( {
		slides: [ { image: "/site/raints/images/static/bg_694x569.jpg" } ]
	} );
	
	var links = $( "#site-nav > li" );

	links.each( function() {
		var link = $( this );

		if ( link.find( "ul" ).length != 0 )
			link.addClass( "parent" );
	} );
	
	links.mouseover( function() {
		var link  = $( this ),
		    child = link.find( "ul" );

		if ( ! link.hasClass( "parent" ) )
			return;

		var width  = 0,
		    offset = 40;
		
		if ( $.browser.msie && parseInt($.browser.version) == 7 ) {
			child.css( "margin-left", -(link.width()+10) );
			return;
		}
		
		child.find( "a" ).each( function() {
			if ( child.outerWidth() - offset > width )
				width = child.outerWidth() - offset;
		} );

		child.find( "a" )
			.width( width );
	} );
	
	var height      = 0;
	var quick_links = $( "#quick-links .widget" );
	
	quick_links.each( function() {
		var widget = $( this );
		
		if ( widget.height() > height )
			height = widget.height() - 20;
	} );
	
	quick_links.height( height );
	
	quick_links.each( function() {
		var widget = $( this );
		
		widget.find( ".read-more" )
			.css( "margin-top", height - 208 - widget.find( "p" ).height() );
	} );
	
	var news_select = $( "#years select" );

	news_select.change( function() {
		var item = $( this );
		window.location.hash = item.val();
	} );

	var slideshow = $( "#slideshow" );

	if ( slideshow.length != 0 )
		initSlideshow( slideshow );
	
	var management = $( ".template-management" );
	
	if ( management.length != 0 )
		initManagement( management );
}

function initManagement( management )
{
	var items = management.find( ".image_text_wrapper" );
	
	items.each( function()
	{
		var item = $( this ),
		    more = $( this ).find( ".read-more a" );
		
		var href    = more.attr( "href" ),
		    content = item.html();
		
		var anchor = $( "<a>" ).addClass( "image_text_wrapper" )
			.html( content ).attr( "href", href );

		item.replaceWith( anchor );
	} );
}

function initSlideshow( slideshow ) {

	var speed = 1000,
	    pause = 4000;

	var active = slideshow.find( ".item:first" );

	var timer = setInterval( function() {

		next( active, false );
		active = active.next();

		if ( active.length == 0 )
			active = slideshow.find( ".item:first" );
	}, pause );

	$( window ).blur( function() {
		clearInterval( timer ); }
	);

	slideshow.find( ".prev" ).click(
		function() { prev( $( this ).parent().parent(), true ); }
	);

	slideshow.find( ".next" ).click(
		function() { next( $( this ).parent().parent(), true ); }
	);

	function prev( item, clear )
	{
		if ( clear )
			clearInterval( timer );

		var prev = item.prev();

		if ( prev.length == 0 )
			prev = item.parent().find( ".item:last" );

		item.fadeOut( speed );
		prev.fadeIn( speed );
	}

	function next( item, clear )
	{
		if ( clear )
			clearInterval( timer );

		var next = item.next();

		if ( next.length == 0 )
			next = item.parent().find( ".item:first" );

		item.fadeOut( speed );
		next.fadeIn( speed );
	}
}

$(function(){
  // Bind an event to window.onhashchange that, when the history state changes,
  // gets the url from the hash and displays either our cached content or fetches
  // new content to be displayed.
  $(window).bind( 'hashchange', function(e) {

    // Get the hash (fragment) as a string, with any leading # removed. Note that
    // in jQuery 1.4, you should use e.fragment instead of $.param.fragment().
		//var url = $.param.fragment();
		var url = e.fragment;
		if(url) {

			var url_length = url.length;
			//alert(url + ', length: ' + url_length + ' characters');

			if(url_length == 6) {
				listNews(url);
			} else {
				renderNews(url);
			}

		}

  })

  // Since the event is only triggered when the hash changes, we need to trigger
  // the event now, to handle the hash the page may have loaded with.
  $(window).trigger( 'hashchange' );

});

/**
 * news
 *
 */

function listNews(url) {
	if(url) {
		doAjaxRequest('CoMeT_function=run_module&module=ModNews&function=list_news&url=' + url, listNewsResponse);
	}
}
function listNewsResponse(html) {
	$("#news_list").html(html);
}


function renderNews(url) {
	if(url) {
		doAjaxRequest('CoMeT_function=run_module&module=ModNews&function=render_news&url=' + url, renderNewsResponse);
	}
}
function renderNewsResponse(html) {
	$("#side-area").html(html);
}

/**
 * slideshow - sub pages
 *
 */


function switchSlideshowSubPageImage(image_id, source) {

	$(source).parent().parent().siblings().find('a').removeClass('current');
	$(source).addClass('current');

	var src_parts= $(source).children('img').attr('src').split('/');
	src_parts.splice(6,1,"medium");
	var src = src_parts.join("/");

	$(source).parent().parent().siblings('.slideshow_sub_page_image').children('img').attr('src', src);

}



/**
 * common
 *
 */

function doAjaxRequest(pars, callback) {
  $.ajax({
    type: "POST",
    url: "?",
    data: pars,
    success: callback
  });
}
