var ctr = 0;
var ntCtr = -1;
var ntCtrx = 0;
var links = new Array();
var items = new Array();
var skip = 0;


document.observe('dom:loaded', function(e) {
	if($('information')) {
		newsTicker();
	}
	$$('#centreTabs a').each(function(el) {
        el.observe('click', function(e) {
            e.stop();
            id = e.findElement('li').identify().split('_');
            $$('.centreBox').each(function(b) {
                b.hide();
            });
            $$('#centreTabs li.selected').each(function(b) {
                b.removeClassName('selected');
            });
            $('centreBox_'+id[1]).show();
            $('centreTab_'+id[1]).addClassName('selected');
        });
    });
    $('centreLargeImage').src = slides[0].src;
    os = $('centreLargeImage').positionedOffset();
    $('centreLargeImage').insert({'before': '<img id="centreImageLoader"/>'});
    $('centreImageLoader').setStyle({
        'left': os.left,
        'top': os.top
    }).setOpacity(0).hide();
    for (var i=0,l=slides.length;i<l;i++) {
        $('centreThumbs').insert(
            '<li><img id="centreImage_'+i+'" src="'+slides[i].thumb+'" /></li>'
        );
    }
    $$('#centreThumbs li img').each(function(el) {
        el.observe('click', function(e) {
            skip++;
            if (skip > 2) skip = 2;
            e.element().fire('my:select');
        });
        el.observe('my:select', function(e) {
            e.stop();
            id = e.element().identify().split('_')[1]*1;
            $$('#centreThumbs li.selected').each(function(li) {
                li.removeClassName('selected');
            });
            e.element().up().addClassName('selected');
            img = slides[id].src;
            if (img.substring(img.length-4,img.length) == '.vid') {
                img = img.split('/');
                img = img[img.length-1];
                img = img.split('_');
                img = img[3].split('.')[0];
                doVideo(img);
            } else {
                if ($('youtubePlayer')) $('youtubePlayer').remove();
                height = $('centreImage').getStyle('height').replace('px', '')*1;
                if (height != 288) {
                    new Effect.Tween(
                        $('centreImage'),
                        height,
                        288,
                        {
                            'duration': 0.3,
                            'afterFinish': function(e) {
                                $('centreLargeImage').src = img;
                                $('centreLargeImage').show();
                            }
                        },
                        function(x) {
                            $('centreImage').setStyle({'height': x+'px'});
                        }
                    );
                } else {
                    $('centreImageLoader').src = img;
                    $('centreImageLoader').setOpacity(0).show();
                    new Effect.Tween(
                        $('centreImageLoader'),
                        0,
                        1,
                        {
                            'duration': 0.4,
                            'afterFinish': function(e) {
                                $('centreLargeImage').src = img;
                                $('centreLargeImage').show();
                                $('centreImageLoader').hide();
                            }
                        },
                        function(x) {
                            $('centreImageLoader').setOpacity(x);
                        }
                    );
                }
            }
        });
    });
    $('centreMoveLeft').observe('click', function(e) {
        e.stop();
        new Effect.Tween(
            $('centreThumbs'),
            $('centreThumbs').getStyle('marginLeft').replace('px', '')*1,
            0,
            {
                'duration': 0.3,
                'afterFinish': function(e) {
                    $('centreMoveLeft').setOpacity(0.3);
                    $('centreMoveRight').setOpacity(1.0);
                }
            },
            function(x) {
                $('centreThumbs').setStyle({'marginLeft': x+'px'});
            }
        );
    });
    $('centreMoveRight').observe('click', function(e) {
        e.stop();
        new Effect.Tween(
            $('centreThumbs'),
            $('centreThumbs').getStyle('marginLeft').replace('px', '')*1,
            -624,
            {
                'duration': 0.3,
                'afterFinish': function(e) {
                    $('centreMoveLeft').setOpacity(1.0);
                    $('centreMoveRight').setOpacity(0.3);
                }
            },
            function(x) {
                $('centreThumbs').setStyle({'marginLeft': x+'px'});
            }
        );
        //$('centreThumbs').setStyle({'marginLeft': '-624px'});
    });
    $('centreMoveLeft').setOpacity(0.3);
    $('centreImage_0').up().addClassName('selected');
    new PeriodicalExecuter(function(pe) {
        if (skip) {
            skip--;
            return;
        }
        height = $('centreImage').getStyle('height').replace('px', '')*1;
        if (height != 288) {
            $('centreImageLoader').hide();
            return;
        }
        img = $$('#centreThumbs li.selected img')[0];
        id = img.identify().split('_')[1]*1;
        found = false;
        while (found === false) {
            id++;
            if (id >= slides.length) id = 0;
            if (slides[id].src.substring(slides[id].src.length-4,slides[id].src.length) != '.vid') {
                found = id;
            }
        }
        $('centreImage_'+id).fire('my:select');
    }, 5);
});

var doVideo = function(id) {
    new Effect.Tween(
        $('centreImage'),
        $('centreImage').getStyle('height').replace('px', '')*1,
        425,
        {
            'duration': 0.3,
            'afterFinish': function(e) {
                if (!$('youtubePlayer')) {
                    $('centreLargeImage').insert({
                        'before': '<iframe id="youtubePlayer" title="YouTube video player" width="702" height="425" frameborder="0" allowfullscreen></iframe>'
                    });
                }
                $('youtubePlayer').src = 'http://www.youtube.com/embed/'+id+'?rel=0';
                $('centreLargeImage').hide();
                $('youtubePlayer').show();
            }
        },
        function(x) {
            $('centreImage').setStyle({'height': x+'px'});
        }
    );
}

var newsTicker = function() {
	b = $('informationText');
	if(ntCtr < 0) {
		if(links[ctr] == '') {
			b.replace(new Element('div',{'id': 'informationText'}).update('&nbsp;'));
		} else {
			b.replace(new Element('a',{'id': 'informationText'}).update('&nbsp;'));
			b = $('informationText');
			b.href = links[ctr];
		}
		ntCtr++;
	}
    // next character of current item
    if (ntCtr < items[ctr].length) {
		if(ntCtrx == 0) {
			ntCtr++;
			ntCtrx++;
			curr = items[ctr].substring(0,ntCtr) + '-';
		} else {
			ntCtrx = 0;
			curr = items[ctr].substring(0,ntCtr) + '_';
		}
		b.update(curr);
		setTimeout('newsTicker()',30);
		return;
    }
	b.update(items[ctr]);
	// new item
    ntCtr = -1;
    if (ctr < items.length-1) {
        ctr++;
    } else {
        ctr = 0;
    }
    setTimeout('newsTicker()',5000);
}

