jQuery.noConflict();

jQuery(document).ready(function(){

	jQuery(window).resize(function() {
		initLayout();
	});
	initLayout();
  
	if (jQuery("#home-data").length ) 
	{
		initHomePhotoRotator();
		initNewsRotator(); 
	}

	if (jQuery(".photogalleryItem").length) initImages();
});

function initImages()
{
	jQuery(".photogalleryItem img").wrap('<span class="rounded-image-container"></span>')
		.parent().append('<span class="tl"></span><span class="tr"></span><span class="bl"></span><span class="br"></span>')
		.addClass('rounded-image-visible');

}

function initNewsRotator()
{
	jQuery('#gando-news').append('<ul id="news-rotator"></ul>');

	jQuery("#home-data div.blog-post").each(function(index) {
		jQuery(this).wrap('<li></li>').parent().appendTo('#news-rotator');
	});

	jQuery('#news-rotator li:gt(0)').hide();
	setInterval(function(){
		jQuery('#news-rotator li:first-child').fadeOut()
		.next('li').fadeIn()
		.end().appendTo('#news-rotator');}, 
	5000);
	//jQuery('#gando-news>h2').css('font-size', '35px');
}

function initLayout() 
{
	h = jQuery(window).height() - 99 - 65;
	if (jQuery('#content-area').height() < h) jQuery('#content-area').css("min-height", h + "px"); 
}

var runSlider;
function initHomePhotoRotator()
{
	
	jQuery('#home-content-area').prepend('<div id="home-photo-rotator"></div>');
	
	jQuery("#home-photo-rotator").append('<ul></ul><div id="home-photo-rotator-nav"><p id="gandoSlideCaption"></p></div>');

	jQuery("#home-data table.photogalleryTable img").each(function(index) {
		var src = jQuery(this).attr('src');
		src = src.slice(0,src.indexOf("?"));
		var thumbSrc = src + '?Action=thumbnail&Width=69&Height=32&Algorithm=proportional&USM=1';
		var caption = jQuery(this).parent().attr('title');
		src = src + '?Action=thumbnail&Width=697&Height=319&Algorithm=proportional&USM=1';
		jQuery("#home-photo-rotator ul").append('<li class="gandoSlide-' + index + '" title="' + caption + '"><img src="' + src + '" /></li>');

		jQuery(document.createElement('a'))
		.attr("href", "#").html('<span><img src="'+ thumbSrc +'" /></span>')
		.appendTo('#home-photo-rotator-nav')
		.click(function() {
				if (jQuery(this).hasClass("selected")) return false;
				clearTimeout(runSlider);
				slideSwitch(index);
				return false;
			});

	});

	jQuery('#home-photo-rotator li:first').addClass('active');
	jQuery("#gandoSlideCaption").html(jQuery('#home-photo-rotator li:first').attr("title"));
	jQuery("#home-photo-rotator-nav a").eq(0).addClass("selected");
	runSlider = setTimeout('slideSwitch()', 4000);

}

function slideSwitch(i) {

	clearTimeout(runSlider);
	
	var $active = jQuery('#home-photo-rotator li.active');

    if ( $active.length == 0 ) $active = jQuery('#home-photo-rotator li:last');

    if (i != undefined) var $next =  jQuery('#home-photo-rotator li.gandoSlide-'+i);
	else {
		if ($active.next().length) {
			var $next =  $active.next();
		} else {
			var $next = jQuery('#home-photo-rotator li:first');
		}
	}
    $active.addClass('last-active');

	jQuery("#home-photo-rotator-nav a").removeClass("selected");
	jQuery("#home-photo-rotator-nav a").eq( $next.index() ).addClass("selected");
	
	var caption = $next.attr("title");
	//alert(caption);
	
    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
			runSlider = setTimeout('slideSwitch()', 4000);
       });
	   
	jQuery("#gandoSlideCaption").html(caption);
	   
}






