﻿jQuery(document).ready(function() {
    jQuery('head').append('<script type="text/javascript" src="' + GB_ROOT_DIR + 'AJS.js" />');
    jQuery('head').append('<script type="text/javascript" src="' + GB_ROOT_DIR + 'AJS_fx.js" />');
    jQuery('head').append('<script type="text/javascript" src="' + GB_ROOT_DIR + 'gb_scripts.js" />');
    jQuery('head').append('<link href="' + GB_ROOT_DIR + 'gb_styles.css" rel="stylesheet" type="text/css" media="all" />');
});

function wrapper(style) {
    jQuery('#GB_window').wrapInner('<table class="gb-wrapper"><tr><td class="gb-center"></td></tr></table>');
    jQuery('.gb-wrapper > tbody > tr').prepend('<td class="gb-left" />');
    jQuery('.gb-wrapper > tbody > tr').append('<td class="gb-right" />');

    jQuery('.gb-wrapper > tbody').prepend('<tr><td class="gb-top-left" /><td class="gb-top-center" /><td class="gb-top-right" /></tr>');
    jQuery('.gb-wrapper > tbody').append('<tr><td class="gb-bottom-left" /><td class="gb-bottom-center" /><td class="gb-bottom-right" /></tr>');

    jQuery('.header').css('background-image', 'none');
    jQuery('.header > tbody > tr').prepend('<td class="gb-header-left" />').append('<td class="gb-header-right" />');

    jQuery('.gb-header-left').css('background-image', 'url(' + GB_ROOT_DIR + style + '.png)');
    jQuery('.close').css('background-image', 'url(' + GB_ROOT_DIR + style + '.png)');
    jQuery('.caption').css('background-image', 'url(' + GB_ROOT_DIR + style + '.png)');
    jQuery('.gb-header-right').css('background-image', 'url(' + GB_ROOT_DIR + style + '.png)');

    jQuery('.close span').empty();
    jQuery('.close img').attr('src', GB_ROOT_DIR + 'close.png').appendTo('.close span');
    switch (style) {
        case 'blue':
        case 'red':
            jQuery('.caption').css('color', '#fff');
            break;
    }
}

function GB_popUp(title, url, /* optional */width, height, fullscreen, reload, style) {
    var options = {
        title : title,
        url : url,
        width : width || 640,
        height : height || 480,
        reload: reload || false,
        style : style || 'silver'
    };

    if (fullscreen) {
        return _gbFull(options);
    } else {
        return _gbCenter(options);
    }
}

function _gbCenter(options) {
    if (options.reload) {
        GB_showCenter(options.title, options.url, options.height, options.width, reload);
    } else {
        GB_showCenter(options.title, options.url, options.height, options.width);
    }
    wrapper(options.style);
    return false;
}

function _gbFull(options) {
    if (options.reload) {
        GB_showFullScreen(options.title, options.url, reload);
    } else {
        GB_showFullScreen(options.title, options.url);
    }
    wrapper(options.style);
    return false;
}

function gbCenter(objectLink, width, height,/* optional */ reload, style) {
    var options = {
        title: objectLink.title,
        url: objectLink.href,
        width: width || 640,
        height: height || 480,
        reload: reload || false,
        style: style || 'blue'
    };

    return _gbCenter(options);
}

function gbFull(objectLink, /* optional */ reload, style) {
    var options = {
        title: objectLink.title,
        url: objectLink.href,
        reload: reload || false,
        style: style || 'blue'
    };

    return _gbFull(options);
}

function reload() {
    window.location.reload();
}
