/*
	Tab enabled Greybox
*/

// Reference on the header background image
var headerCache;
// currently active tab: 		1 = kursdaten	2 = anbieter
var activeTab = 1;
// We need a GB_Window instance to build the path, therefore we cache it
var tab1ImgURL, tab2ImgURL;

// Frame cache
var frameCache;
var urlCache1, urlCache2;

GB_showCenter = function(caption, url1, url2, /* optional */ height, width, callback_fn) {
    var options = {
        caption: caption,
        center_win: true,
        height: 600,
        width: 376,
        fullscreen: false,
        callback_fn: callback_fn
    }
	urlCache1 = url1;
	urlCache2 = url2;
    var win = new GB_Window(options);
    var res = win.show("../" + url1);
	frameCache = win.iframe;
	return res;
}

GB_Window = GreyBox.extend({
    init: function(options) {
        this.parent({});
		tab1ImgURL = this.root_dir + "header_kursdaten.png";
		tab2ImgURL = this.root_dir + "header_anbieter.png";
        this.img_header = tab1ImgURL;
        this.img_close = this.root_dir + "close.png";
        this.show_close_img = true;
        AJS.update(this, options);
        this.addCallback(this.callback_fn);
    },

    initHook: function() {
		// Set active tab to 1
		activeTab = 1;
	
        AJS.addClass(this.g_window, 'GB_Window');

        this.header = AJS.TABLE({'class': 'olheader', 'cellpadding': '0', 'cellspacing': '0'});
        this.header.style.backgroundImage = "url("+ this.img_header +")";
		headerCache = this.header;

        var button1 = AJS.TD({'width': '115', 'height':'18'}, '');
        var span1 = AJS.SPAN('');
        var img1 = AJS.IMG({'src': this.root_dir + 'empty.png', 'width': '115', 'height': '18'});
        var div1 = AJS.DIV(img1, span1);
		AJS.ACN(button1, div1);

		// Switch to content 2
        var button2 = AJS.TD({'width': '115', 'height':'19'}, '');
        var span2 = AJS.SPAN('');
        var img2 = AJS.IMG({'class': 'handle', 'src': this.root_dir + 'empty.png', 'width': '115', 'height': '18'});
        var div2 = AJS.DIV(img2, span2);
        AJS.AEV([img2, span2], 'click', contentSwitch);
		AJS.ACN(button2, div2);
		
		var td_close = AJS.TD({'class': 'close'});
        if(this.show_close_img) {
            var img_close = AJS.IMG({'src': this.img_close});
            var span = AJS.SPAN('');

            var btn = AJS.DIV(img_close, span);

            AJS.AEV([img_close, span], 'mouseover', function() { AJS.addClass(span, 'on'); });
            AJS.AEV([img_close, span], 'mouseout', function() { AJS.removeClass(span, 'on'); });
            AJS.AEV([img_close, span], 'mousedown', function() { AJS.addClass(span, 'click'); });
            AJS.AEV([img_close, span], 'mouseup', function() { AJS.removeClass(span, 'click'); });
            AJS.AEV([img_close, span], 'click', GB_hide);

            AJS.ACN(td_close, btn);
        }

        tbody_header = AJS.TBODY();
        AJS.ACN(tbody_header, AJS.TR({}, button1, button2, td_close));

        AJS.ACN(this.header, tbody_header);
        AJS.ACN(this.top_cnt, this.header);

        if (this.fullscreen)
            AJS.AEV(window, 'scroll', AJS.$b(this.setWindowPosition, this));
    },

    setFrameSize: function() {
        if(this.fullscreen) {
            var page_size = AJS.getWindowSize();
            overlay_h = page_size.h;
            this.width = Math.round(this.overlay.offsetWidth - (this.overlay.offsetWidth/100)*10);
            this.height = Math.round(overlay_h - (overlay_h/100)*10);
        }

        AJS.setWidth(this.header, this.width);
        AJS.setWidth(this.iframe, this.width);
        AJS.setHeight(this.iframe, this.height);
    },

    setWindowPosition: function() {
        var page_size = AJS.getWindowSize();
        AJS.setLeft(this.g_window, ((page_size.w - this.width)/2)-13);

        if(!this.center_win) {
            AJS.setTop(this.g_window, AJS.getScrollTop());
        }
        else {
            var fl = ((page_size.h - this.height) /2) - 20 + AJS.getScrollTop();
            if(fl < 0)
                fl = 0;
            AJS.setTop(this.g_window, fl);
        }
    }
});

AJS.preloadImages(GB_ROOT_DIR+'w_close.gif', GB_ROOT_DIR+'header_bg.gif');

/** Switch Content */
function contentSwitch() {
	if (activeTab == 1) {
		activeTab = 2;
        headerCache.style.backgroundImage = "url("+ tab2ImgURL +")";
		frameCache.src = urlCache2;
	} else {
		activeTab = 1;
        headerCache.style.backgroundImage = "url("+ tab1ImgURL +")";
		frameCache.src = urlCache1;
	}
}

