/* jQuery plugin http://www.bramstein.com/projects/jsizes/ */
(function ($) {
    var num = function (value) {
        return parseInt(value, 10) || 0;
    };

    $.each(['min', 'max'], function (i, name) {
        $.fn[name + 'Size'] = function (value) {
            var width, height;
            if (value) {
                if (value.width !== undefined) {
                    this.css(name + '-width', value.width);
                }
                if (value.height !== undefined) {
                    this.css(name + '-height', value.height);
                }
                return this;
            }
            else {
                width = this.css(name + '-width');
                height = this.css(name + '-height');
                return { 'width': (name === 'max' && (width === undefined || width === 'none' || num(width) === -1) && Number.MAX_VALUE) || num(width),
                    'height': (name === 'max' && (height === undefined || height === 'none' || num(height) === -1) && Number.MAX_VALUE) || num(height)
                };
            }
        };
    });

    $.fn.isVisible = function () {
        return this.is(':visible');
    };

    $.each(['border', 'margin', 'padding'], function (i, name) {
        $.fn[name] = function (value) {
            if (value) {
                if (value.top !== undefined) {
                    this.css(name + '-top' + (name === 'border' ? '-width' : ''), value.top);
                }
                if (value.bottom !== undefined) {
                    this.css(name + '-bottom' + (name === 'border' ? '-width' : ''), value.bottom);
                }
                if (value.left !== undefined) {
                    this.css(name + '-left' + (name === 'border' ? '-width' : ''), value.left);
                }
                if (value.right !== undefined) {
                    this.css(name + '-right' + (name === 'border' ? '-width' : ''), value.right);
                }
                return this;
            }
            else {
                return { top: num(this.css(name + '-top' + (name === 'border' ? '-width' : ''))),
                    bottom: num(this.css(name + '-bottom' + (name === 'border' ? '-width' : ''))),
                    left: num(this.css(name + '-left' + (name === 'border' ? '-width' : ''))),
                    right: num(this.css(name + '-right' + (name === 'border' ? '-width' : '')))
                };
            }
        };
    });
})(jQuery);
/* jQuery plugin end */


$(document).ready(function () {
    initialize();    

    var href = $(location).attr('href');
    var i = href.indexOf("#");

    var tabId = (i == -1) ? "#tabs-1" : href.substring(i);

    selectTab($("#tabs>div").index($("#tabs>" + tabId)));

    $("#tabs>ul>li>a").click(function () {
        selectTab($("#tabs>ul>li>a").index(this));
    });
});

function selectTab(tabIndex) {    
    if ($("#tabs>ul>li:eq(" + tabIndex + ")").hasClass("selected")) {
        return;
    }
    $("#tabs>div").addClass("hidden");
    $("#tabs>div:eq(" + tabIndex + ")").removeClass("hidden");

	$("#tabs>ul>li").removeClass("selected");
	$("#tabs>ul>li:eq(" + tabIndex + ")").addClass("selected");

	var div = $("#tabs>div:not(.hidden)");
	var right = div.width() + div.padding().left + div.padding().right - 5;
	var bottom = div.height() + div.padding().top + div.padding().bottom + 19;

	$("#tabs>div>div.topLeft, #tabs>div>div.topRight").css("top", ($("#tabs>ul>li").height()) + "px");
	$("#tabs>div>div.bottomLeft, #tabs>div>div.bottomRight").css("top", bottom + "px");
	$("#tabs>div>div.topLeft, #tabs>div>div.bottomLeft").css("left", "0px");
	$("#tabs>div>div.topRight, #tabs>div>div.bottomRight").css("left", right + "px");

	var w = $("#tabs>ul>li.selected").width()  + $("#tabs>ul>li.selected").padding().left + $("#tabs>ul>li.selected").padding().right - 2;
	$(".tabs-ul-li>div.bottom").css("width", "");
	$(".tabs-ul-li.selected>div.bottom").css("width", w + "px");
}

function initialize() {
    $("#tabs").css("visibility", "visible");
    $("#tabs>ul>li").append("<div class='bottom'></div><div class='left'></div><div class='right'></div>");
    $("#tabs>div").append("<div class='topLeft'></div><div class='topRight'></div><div class='bottomLeft'></div><div class='bottomRight'></div>");
   
    $("#tabs>ul>li").hover(
        function () { $(this).addClass("hover"); },
        function () { $(this).removeClass("hover"); }
    );

    $("#tabs>ul").addClass("tabs-ul");
    $("#tabs>ul>li").addClass("tabs-ul-li");
    $("#tabs>ul>li>a").addClass("tabs-ul-li-a");
    $("#tabs>div").addClass("tabs-div");

    
    $("#tabs>ul>li>div.right").each(function () {
        var w = $(this).parent().width() + 3;
        $(this).css("left", w + "px");
    });
    
}
