function sizeDiv() {
    div = document.getElementById('content');
    div.style.width = (window.innerWidth - 368) + "px";
}
$(window).bind('resize', sizeDiv);

Shadowbox.init({
    handleOversize: "drag",
    modal: true
});

var fm = new FeedMenu();fm.write('#feed_menu');


$(document).ready(function() {
    
    $('#tabs').tabs({
      select: function(event, ui) {
        $("#slider").slider("destroy");
        $("#tabs ul li a").blur();
        $("#slider").slider();
      }
    });

    $("#accordion").accordion({
        autoHeight: false,
        navigation: true
    });
    $("#accordion2").accordion({
    autoHeight: false,
    navigation: true
    });
    $("#accordion3").accordion({
        autoHeight: false,
        navigation: true
    });
    $("#accordion4").accordion({
        autoHeight: false,
        navigation: true
    });
    $("#accordion5").accordion({
        autoHeight: false,
        navigation: true
    });
    $("#accordion6").accordion({
        autoHeight: false,
        navigation: true
    });
    $("#accordion7").accordion({
        autoHeight: false,
        navigation: true
    });

    $('#game_dialog').dialog({
        autoOpen: false,
        width: 450,
        modal: true,
        resizable: false,
        draggable: false,
        buttons: {
            "Ok": function() {
                $(this).dialog("close");
                window.open('http://game.stellar-legends.net/');
            },
            "Abbrechen": function() {
                $(this).dialog("close");
            }
        }
    });
    $('#forum_dialog').dialog({
        autoOpen: false,
        width: 450,
        modal: true,
        resizable: false,
        draggable: false,
        buttons: {
            "Ok": function() {
                $(this).dialog("close");
                window.open('http://forum.stellar-legends.net/');
            },
            "Abbrechen": function() {
                $(this).dialog("close");
            }
        }
    });

    // Dialog Link
    $('#game_link').click(function(){
        $('#game_dialog').dialog('open');
        return false;
    });
    $('#forum_link').click(function(){
        $('#forum_dialog').dialog('open');
        return false;
    });

});


// Namespace für Schiffe, Waren, Komponenten
$.Gameworld = {
   Overview: {
      ships: new Array(),
      
      initShips: function(shipsArray) {
          $(shipsArray).each(function() {
             $.Gameworld.Overview.ships[$(this).get(0)] = {
                 name:  $(this).get(1),
                 price: $(this).get(2),
                 desc:  $(this).get(3)
             };
          });
      },

      setShip: function(shipId) {
          this.displayInfos({
              image: 'ships/ship_m_'+shipId+'.jpg',
              name:  $.Gameworld.Overview.ships[shipId].name,
              price: $.Gameworld.Overview.ships[shipId].price,
              desc:  $.Gameworld.Overview.ships[shipId].desc
          });
      },

      goods: new Array(),

      initGoods: function(goodsArray) {
          $(goodsArray).each(function() {
             $.Gameworld.Overview.goods[$(this).get(0)] = {
                 name:    $(this).get(1),
                 price:   $(this).get(2),
                 desc:    $(this).get(3),
                 concern: $(this).get(4)
             };
          });
      },

      setGoods: function(goodId) {
          this.displayInfos({
              image:    'goods/goods_m_'+goodId+'.jpg',
              name:     $.Gameworld.Overview.goods[goodId].name,
              price:    $.Gameworld.Overview.goods[goodId].price,
              desc:     $.Gameworld.Overview.goods[goodId].desc,
              concern:  $.Gameworld.Overview.goods[goodId].concern
          });
      },

      comps: new Array(),

      initComps: function(compsArray) {
          $(compsArray).each(function() {
             $.Gameworld.Overview.comps[$(this).get(0)] = {
                 name:  $(this).get(1),
                 price: $(this).get(2),
                 desc:  $(this).get(3)
             };
          });
      },

      setComps: function(compId) {
          this.displayInfos({
              image: 'components/'+compId+'.png',
              name:  $.Gameworld.Overview.comps[compId].name,
              price: $.Gameworld.Overview.comps[compId].price,
              desc:  $.Gameworld.Overview.comps[compId].desc
          });
      },

      buildings: new Array(),

      initBuildings: function(buildingsArray) {
          $(buildingsArray).each(function() {
             $.Gameworld.Overview.buildings[$(this).get(0)] = {
                 name:  $(this).get(1),
                 price: $(this).get(2),
                 desc:  $(this).get(3)
             };
          });
      },

      setBuildings: function(buildId) {
          this.displayInfos({
              image: 'buildings/building_m_'+buildId+'.jpg',
              name:  $.Gameworld.Overview.buildings[buildId].name,
              price: $.Gameworld.Overview.buildings[buildId].price,
              desc:  $.Gameworld.Overview.buildings[buildId].desc
          });
      },

      displayInfos: function(obj) {
          $("#singleInfoNothingSelected").hide();
          $("#singleImage").attr('src', '/images/'+obj.image);
          
          if (obj.name != undefined && obj.name != '') {
              $("#singleInfoName").show();
              $("#singleInfoNameSpan").text(obj.name);
          } else {
              $("#singleInfoName").hide();
          }

          if (obj.price != undefined && obj.price != '') {
              $("#singleInfoPrice").show();
              $("#singleInfoPriceSpan").text(obj.price);
          } else {
              $("#singleInfoPrice").hide();
          }

          if (obj.concern != undefined && obj.concern != '') {
              $("#singleInfoConcern").show();
              $("#singleInfoConcernSpan").text(obj.concern);
          } else {
              $("#singleInfoConcern").hide();
          }

          if (obj.desc != undefined && obj.desc != '') {
              $("#singleInfoDesc").show();
              $("#singleInfoDescSpan").text(obj.desc);
          } else {
              $("#singleInfoDesc").hide();
          }
      }
   }
}

// TabSwitching with JQuery-UI-Buttons
// Author: dex
// Date:   05.09.2010
$.fn.tabswitch = function(options)
{
    var main = "#"+$(this).attr("id");

    if (options != undefined) {
        if (options.open != undefined) {
            $(main+" [id*="+$(this).attr("id")+"_content_]").hide();
            $(main+" #"+$(this).attr("id")+"_content_"+options.open).show();
            return;
        }

        if (options.init != undefined) {
            var callback = function() {
                $(main+" label").removeClass("ui-state-active");
                $("#"+options.init).next('label').addClass("ui-state-active");
                $("#"+options.init).click();
            }
        }
    }

    $(main+" [id*="+$(this).attr("id")+"_content_]").hide();

    var count = 0;
    $(main+" input").each(function() {
        $(this).attr('name', $(main).attr('id')+'btn');
        $(this).click(function() {
            $(main).tabswitch({open: $(this).attr('id')});
        });
        $(this).after('<label for="'+$(this).attr('id')+'">'+$(this).attr('value')+'</label>');

        if (count == 0) {
            $(this).click();
        }
        count++;
    });

    $(main+" ul").buttonset();

    $(main).show();

    if (callback != undefined) {
        callback.call();
    }
}


// SL-JQuery-Pager
// Author: dex
// Date:   13.09.2010
$.fn.pager = function(containerId)
{
    this.main           = "#"+$(this).attr("id");
    this.selected       = 1;
    this.containerId    = containerId || 'pager-container';
    this.containerCount = 0;

    this.__construct = function(instance)
    {
        // Bar begin
        $(this.main).append('<ul class="pager-bar"></ul>');

        $(this.main+" .pager-bar").append(
            '<li class="pager-first">&laquo;</li>'+
            '<li class="pager-prev">&lt;</li>'
        );

        $(this.main+" .pager-first").click(function() {
            instance.select(1);
        });

        $(this.main+" .pager-prev").click(function() {
            instance.select('prev');
        });

        // Pagenumbers
        instance.containerCount = $("[id*="+instance.containerId+"]").length;

        var i=1;
        for(i;i<instance.containerCount+1;i++) {
            $(this.main+" .pager-bar").append('<li class="pager-number" alt="pagenumber'+i+'">'+i+'</li>');
            
            $(this.main+" li[alt=pagenumber"+i+"]").click(function() {
                instance.select(parseInt($(this).attr('alt').substr(10, $(this).attr('alt').length)));
            });
        }

        // Bar end
        $(this.main+" .pager-bar").append(
            '<li class="pager-next">&gt;</li>'+
            '<li class="pager-last">&raquo;</li>'
        );

        $(this.main+" .pager-next").click(function() {
            instance.select('next');
        });

        $(this.main+" .pager-last").click(function() {
            instance.select(instance.containerCount);
        });

        instance.select(1);
    };

    this.select = function(select)
    {
        var selectedNumber = this.selected;
        if (select == 'prev') {
            selectedNumber--;
        } else if (select == 'next') {
            selectedNumber++;
        } else {
            selectedNumber = select;
        }

        if (selectedNumber <= 0) {
            selectedNumber = 1;
        }

        if (selectedNumber > this.containerCount) {
            selectedNumber = this.containerCount;
        }

        $(this.main+" .pager-number").removeClass('pager-number-active');
        $(this.main+" li[alt=pagenumber"+selectedNumber+"]").addClass('pager-number-active');

        $("[id*="+this.containerId+"]").hide();
        $("#"+this.containerId+selectedNumber).show();

        this.selected = selectedNumber;
    };

    this.__construct(this);
};
