$(document).ready(function() {

    $('#tab-image').cycle({
        fx:     'fade',
        speed:  'slow',
        timeout: 0,
        next:  '#next-tab'
    });
    $('#tabs').cycle({
        fx:     'fade',
        speed:  'slow',
        timeout: 0,
        next:  '#next-tab'
    });

    $('#slideshow').cycle({
        fx:     'fade',
        speed:  'slow',
        timeout: 5000,
        pager:	'#slideshow-nav ul',
        next:	'#next',
        prev:	'#prev',
        pagerAnchorBuilder: function(idx, slide) {
            var all_cnt = $("#slideshow > img").length;
    		var s = '';
    		var group_num = Math.ceil((idx + 1) / 15);

    		s = s + '<li class="group_' + group_num + '" style="display: none;"><a href="#">' + (idx + 1) + '</a></li>';
            $(".group_1").show();
            return s;
		},
		after: function(curr, next, opts) {
            var this_alt = $(this).attr('alt');
            var img_num = this_alt.substring(this_alt.lastIndexOf('-') + 1);
            var group_num = Math.ceil((parseInt(img_num) + 1) / 15);

            $("[class^=group_]").hide();
            $('.group_' + group_num).show();
		}
	});
	
	$('#pauseplay').click(function() {
		var obj = $(this);
		if (obj.hasClass('pause')) {
			obj.removeClass('pause').addClass('play');
			$('#slideshow').cycle('pause');
		} else if (obj.hasClass('play')) {
			obj.removeClass('play').addClass('pause');
			$('#slideshow').cycle('resume');
		}
		return false; // Get rid of # when clicked....
	});

	$('#next_group, #prev_group').live('click', function() {
		group_num = '';
		$("#slideshow-nav > ul li").each(function() {
			if ($(this).is(":visible")) {
				group_num = $(this).attr('class');
				return false;
			}
		});
		if (group_num != '') {
			if (group_num.indexOf(' ') != -1) {
				end = group_num.indexOf(' ');
			} else {
				end = group_num.length;
			}
			num = group_num.substring(group_num.indexOf('_') + 1, end);

			show_group = '';
			if (this.id == 'next_group') {
				show_group = parseInt(num) + 1;
			} else if (this.id == 'prev_group') {
				show_group = parseInt(num) - 1;
			}

			if ($(".group_" + show_group).length) {
	            $("[class^=group_]").hide();
				$(".group_" + show_group).show();

				goto_slide = 0;
				if (this.id == 'next_group') {
					goto_slide = parseInt(num) * 15;
				} else if (this.id == 'prev_group') {
					goto_slide = (show_group - 1) * 15;
				}

				$('#slideshow').cycle(goto_slide);
			}
		}

		return false; // Get rid of # when clicked....
	});
});

