// Add image preload support to jQuery
jQuery.preloadImages = function() {
	for(var i=0; i<arguments.length; i++)	{
		jQuery('<img>').attr('src', arguments[i]);
	}
}

var IMG_TOGGLE  = '/img/tab-toggle.gif';
var IMG_TOGGLED = '/img/tab-toggled.gif';

$.preloadImages(IMG_TOGGLED);

function hideTab(img) {
    $(img).attr('src', IMG_TOGGLE).parent().next().hide();
    if($(img).parent().parent().attr('id') == 'tabLast') {
        $('#tabLast div.tabTop').css('background', 'url(/img/tab_bg-bottom.gif) 0px 0px no-repeat');
        $('#tabLast div.tabBottom').hide();
    }
}
function showTab(img) {
    $(img).attr('src', IMG_TOGGLED).parent().next().show();
    if($(img).parent().parent().attr('id') == 'tabLast') {
        $('#tabLast div.tabTop').css('background', 'url(/img/tab_bg-middle.gif) 0px 0px no-repeat');
        $('#tabLast div.tabBottom').show();
    }
}
/*
function hideTabById(id) {
    //console.log($(id).prev().next());
    //console.log($(id).prev().next().children());
    $(id).hide();
}
*/

function popup(url) {
    win = window.open(url, 'Demo', 'width=800,height=635');
    win.focus();
}

$(document).ready(function() {
    // toggle tabs
    $('div#tabs div.tab div.tabTop img.toggle').click(function() {
        $(this).attr('src') == IMG_TOGGLED ? hideTab(this) : showTab(this);
    });
    
    $('div#tabs div.tab div.tabTop h2').click(function() {
        $(this).next().attr('src') == IMG_TOGGLED ? hideTab($(this).next()) : showTab($(this).next());
    });
    // hide specific tabs
    //hideTabById('div#callback');
    
    // bookmarks
    $('ul#pagebuttons li#bookmark a').click(function(e) {
        $('div#bookmarkPopup').css({
			'top' : e.pageY - ($('div#bookmarkPopup').height()+295) + 'px',
			'left': e.pageX - ($('div#bookmarkPopup').width()/2+380)  + 'px'
		});
        $('div#bookmarkPopup').toggle();
        return false;
    });
    $('div#bookmarkPopupTop').click(function() {
        $('div#bookmarkPopup').hide();
    });
});

function uncheckNoStatementIsTrue(question,answerNoStatement) {
	var element = 'answer'+question+'_'+answerNoStatement;
	if ( document.getElementById(element) ) {
		document.getElementById(element).checked = false;
	}
}
function uncheckOtherStatements(question,answerNoStatement,lastAnswer) {
	if ( lastAnswer == '' ) return;
	var answer = 'A';
	var element_pre = 'answer'+question+'_';
	while ( answer <= lastAnswer ) {
		if ( answer != answerNoStatement )
			document.getElementById(element_pre+answer).checked = false;
		answer = String.fromCharCode(answer.charCodeAt(0)+1);
	}
	// alert ( answer < lastAnswer );
	
}
