if(!Number.toFixed){
  Number.prototype.toFixed = function(x){
    var temp=this;
    temp=Math.ceil(temp*Math.pow(10,x))/Math.pow(10,x);
    return temp;
  };
}

var slider = new Slider();
var sliderHome1 = new SliderHome();
var sliderHome2 = new SliderHome();
var sliderHome3 = new SliderHome();
var History = new HistoryApi();
var cache = {};
cache.page = new Cache();
cache.img = new Cache();
var loader = new Loader();
var bannerImg, bannerUrl, investmentIds = new Array();

function loadUrl(url, options){
  var settings = $.extend({
    type: "get",
    dataType: "json",
    noCache: false
  }, options);

  if(false){
//  if(cachedUrl = cache.page.get(url)){
    updatePage(cachedUrl.value);
  }
  else {
    $.ajax({
      type: settings.type,
      dataType: settings.dataType,
      url: url,
//      data: { ajax: true },
      success: function(data, textStatus, XMLHttpRequest){
        if(!settings.noCache){
          cache.page.add(url, data);
        }
        updatePage(data);
      },
      error: function(XMLHttpRequest, textStatus, errorThrown){
        data = {};
        try {
          data = $.parseJSON(XMLHttpRequest.responseText);
        } catch(error){
          data.content = XMLHttpRequest.responseText;
        }
        updatePage(data);
      },
      beforeSend: function(XMLHttpRequest){
        loader.show();
      },
      complete: function(XMLHttpRequest, textStatus){
        loader.hide();
      }
    });
  }
}

function initWmode(){
  $("#content iframe").each(function(){
    var url = $(this).attr("src");
    $(this).attr("src",url+"?wmode=transparent");
  });
}

function initBanner(){
  if(typeof bannerImg != "undefined" && bannerImg){
    $.fancybox({
      "href": bannerImg,
      overlayColor: "#262d61",
      onComplete: function(){
        if(typeof bannerUrl != "undefined" && bannerUrl){
          $a = $("<a/>");
          if(bannerUrl.charAt(0) == "/"){
            $a.addClass("local");
          }
          $a.attr("href", bannerUrl);
          $("#fancybox-img").wrap($a);
        }
      }
    });
  }
}

var updatePage = function(data){
  if(data.title != undefined){
    document.title = data.title;
  }
  if(data.content != undefined){
    $("#content").html(data.content);

    initFancyBox();
    initStickyfloat();
    initTableSorter();
    initWmode();
  }

  if(data.wrapperClass != undefined){
    $("#wrapper").attr("class", data.wrapperClass);
  }

  if(data.slider != undefined){
    $("#slider-container").html("").html(data.slider);
    initSliders();
  }

  if(data.banner != undefined){
    bannerImg = data.banner.bannerImg;
    bannerUrl = data.banner.bannerUrl;
    if($.inArray(data.banner.investmentId, investmentIds) < 0){
      investmentIds.push(data.banner.investmentId);
      initBanner();
    }
  } else {
    bannerImg = "";
    bannerUrl = "";
  }
}

function highlightRequired(object){
  bg = object.css("background-color");
  color = object.css("color");
  object.animate({
    "background-color": "#f00",
    "color": bg
  }).animate({
    "background-color": bg,
    "color": color
  });
}
function initSliders(){
  slider.initialize();
  sliderHome1.initialize("investment-1", -3500);
  sliderHome2.initialize("investment-2", -2000);
  sliderHome3.initialize("investment-3", -500);

//  sliderHome1.initialize("investment-1", -3500);
//  sliderHome2.initialize("investment-2", -2000);
//  sliderHome3.initialize("investment-3", -500);

//  setTimeout("sliderHome.start(0)", 500);
//  setTimeout("sliderHome.start(1)", 2000);
//  setTimeout("sliderHome.start(2)", 3500);
}
function initFancyBox(){
  $("a.fancy").fancybox({
    autoScale : false,
    overlayColor: "#262d61"
  });

  $("a.spacer").fancybox({
    autoScale : false,
//    autoDimensions: true,
    type: "iframe",
    padding: 1,
    width: 1186,
    height: 592,
    overlayColor: "#262d61"
  });
}
function initMagnifier(){
  $("#footer-mid img, #pzfd").each(function(){
    var width = $(this).width();
    var height = $(this).height();
    $(this).hover(
      function(){
        $(this).stop().animate({
          'width': width * 1.5,
          'height': height * 1.5
        }, 500, 'jswing');
      },
      function(){
        $(this).stop().animate({
          'width': width,
          'height': height
        }, 500, 'jswing');
      }
    );
  });
}

function initStickyfloat(){
  $submenu = $("#submenu");
  if($submenu.length){ // hack - bug in stickyfloat when jQuery does not return any element$(window).scroll(function() {
    $submenu.stickyfloat({
      duration: 400
      ,offsetY: 50
    });
  }
}

function initTableSorter(){
  $("#local-table table").tablesorter();
}

$(function(){
  initFancyBox();
  initMagnifier();
  initStickyfloat();
  initSliders();
  initTableSorter();
  initWmode();
  initBanner();

  $(".gallery a.video").live("click", function(event){
    event.preventDefault();
    $.fancybox({
      href: this.href,
      padding: 0,
      autoScale: false,
      width: 850,
      height: 510,
      overlayColor: "#262d61",
      href: this.href.replace(new RegExp("watch\\?v=", "i"), "v/"),
      type: "swf",
      swf: {
        "wmode": "transparent",
        "allowfullscreen": "true"
      }
    });
  });

//  $(window).bind("focus", function(){
////    focus = true;
////    slider.start();
////    sliderHome1.start(-3500);
////    sliderHome2.start(-2000);
////    sliderHome3.start(-500);
//    console.log("focus");
//  }).bind("blur", function(){
////    if(!focus){
////      console.log("was not focused before!");
////      return;/
////    }
////    focus = false;
////    slider.stop();
////    sliderHome1.stop();
////    sliderHome2.stop();
////    sliderHome3.stop();
//    console.log("blur");
//  });

  $("#survey-home-alert .alert-close").live("click", function(event){
    event.preventDefault();
    $("#survey-home-alert").hide();
  });
  $("#survey-home input[type=text], #call-order-form .survey input[type=text]").live("blur", function(){
    $this = $(this);
    if(!$this.val()){
      $this.prev().prop("checked", false);
    }
  });
  $("#survey-home input[type=text], #call-order-form .survey input[type=text]").live("focus", function(){
    $this = $(this);
    if($this.hasClass("empty")){
      $this.prev().prop("checked", true);
    }
  });

  $("#map-home div.map-home-zoom").live("click", function(){
    $.fancybox({
      overlayColor: "#262d61"
      ,content: '<div style="height:354px;"><object type="application/x-shockwave-flash" data="/files/mapa_dom.swf" width="570" height="350"><param name="movie" value="/files/mapa_dom.swf"/><param name="wmode" value="opaque" /></object></div>'
    });
  });
  $("#order-call span").live("click", function(){
    $.fancybox({
      overlayColor: "#262d61"
      ,href: "/zamow_rozmowe.html"
    });
  });

  $("#suggest").live("click", function(event){
    event.preventDefault();
    $.fancybox({
      overlayColor: "#262d61"
      ,href: $(this).attr("href")
    });
  });

});

