﻿/// <reference path="jquery-1.3.2-vsdoc2.js" />

function SetExternalLinks() {
    $('a[rel*=external]').click(function() { window.open(this.href); return false; });
}

var Colorbox = {
    Init: function() {
        $('a.colorbox').colorbox();
    }
}

var homepage = {
	init: function() {

		$('.slideshow').each(function() {
			var ss = $(this);
			if (ss.find('img').length >= 2) {
				ss.parents('.current-feature').append('<div class="timer"></div>');
				var controls = '<li class="controls"><ul></ul></li>';
				ss.append(controls);
				for (var i = 0; i < ss.find('img').length; i++) {
					ss.find('.controls ul').append('<li><a href="#">' + (i + 1) + '</a></li>');
				}
				ss.find('.controls a').eq(0).addClass('sel');
			}
		});

		whitespace.homepage.next($('.current-feature').eq(0).find('.timer'), false);

		$('.slideshow img:not(.first)').hide();

		$('.slideshow .controls a').click(function() {
			var ss = $(this).parents('.slideshow');
			ss.find('img').fadeOut(250);
			ss.find('img').eq($(this).text() - 1).fadeIn(350);
			$(this).parents('.slideshow').find('.controls .sel').each(function() { $(this).removeClass('sel'); });
			$(this).addClass('sel');
			return false;
		});

		if ($('.current-feature').length > 1) {
			$('.current-feature').eq(0).addClass('first');
			$('.current-feature:not(.first)').hide();
			$('.current-feature').each(function() {
				var infoControls = '<div class="controls"><a class="next" href="#">next</a><a class="prev" href="#">prev</a></div>';
				$(this).find('.info').append(infoControls);
			});

			$('.info .controls a').click(function() {
				var feat = $(this).parents('.current-feature');
				var selFeat;
				feat.find('img').hide();
				$('.current-feature .timer').each(function() { $(this).animate({ 'width': '180px' }, 500).stop(); });

				if ($(this).hasClass('prev')) {
					if (feat.prev('.current-feature').length > 0) {
						selFeat = feat.prev('.current-feature');
					} else {
						selFeat = $('.current-feature').eq($('.current-feature').length - 1);
					}
				} else {
					if (feat.next('.current-feature').length > 0) {
						selFeat = feat.next('.current-feature');
					} else {
						selFeat = $('.current-feature').eq(0);
					}
				}
				var timer = selFeat.children('.timer');

				feat.hide();
				selFeat.show();

				selFeat.find('img').eq(0).stop().show().css('opacity', '1');
				selFeat.find('.slideshow .controls .sel').removeClass('sel');
				selFeat.find('.slideshow .controls a').eq(0).addClass('sel');

				whitespace.homepage.next(timer, false);
				return false;
			});
		}

	},
	next: function(trigger, doAction) {
		var timerSpeed = 7000;
		if (doAction) whitespace.homepage.nextClick(trigger.parents('.current-feature'));
		trigger.attr('style', '').css({ 'width': '0px', 'opacity': '1' }).animate({ 'width': '180px', 'opacity': '.1' }, timerSpeed, 'linear', function() { whitespace.homepage.next(trigger, true); });
	},
	nextClick: function(feature) {
		if (feature.is(':visible')) {
			var item = feature.find('.controls a.sel').parent('li');
			if (item.next('li').length > 0) {
				item.next('li').find('a').trigger('click');
			} else {
				if ($('.info .controls a').length > 0) {
					feature.find('.info .controls a.next').trigger('click');
					var timeout = setTimeout(whitespace.homepage.next($('.slideshow:visible').find('.timer'), false), 40);
				} else {
					feature.find('.slideshow .controls a').eq(0).trigger('click');
				}
			}
		}
	}
}


var whitespace = {
    Colorbox: Colorbox,
    homepage: homepage
}


/* -- 2009-11-22 --

$('.info .controls a').click 
   var dir = 1;
   var index = 0;
   var total = ss.find('.controls a').length - 1;
   if ($(this).hasClass('prev'))
   dir = -1;

for (var i = 0; i <= total; i++) {
   if (ss.find('.controls a').eq(i).hasClass('sel')) {
   index = i;
   break;
   }
   }
   if (index == 0 && dir == -1)
   ss.find('.controls a').eq(total).trigger('click');
   else if (index == total && dir == 1)
   ss.find('.controls a').eq(0).trigger('click');
   else
   ss.find('.controls a').eq(index + dir).trigger('click');
*/
