var webcambox_visible = false;

function webcam(s, tab) {
  
  s = s == 1 ? true : false;
  
  if (s) {

    // GA
    try { pageTracker._trackPageview("/popup/webcam"); } catch(err) {}

    webcambox_visible = true;
    $('#webcam-box').before('<div id="dim-the-light"></div>');

    w = getUserViewPoint();

    var tab = typeof(tab) != "undefined" ? tab : 1;
    wc_switchtab(tab);
    
    $('#dim-the-light').css({
      'left'    : w[2] + 'px',
      'top'     : w[3] + 'px',
      'width'   : w[0] + 'px',
      'height'  : w[1] + 'px'
    });
    $('#dim-the-light').show();
    
    $('#webcam-box').show();
    
    var box_h = ($('#webcam-box div.main-webcam-box').height() + 73);
    var txt_h = ($('#webcam-box div.txt-part ').height() + 225);
          
    $('#webcam-box').height((txt_h < box_h ? box_h : txt_h));
    $('#webcam-box div.link-wrap').css('margin-top', ($('#webcam-box').height() - 83) + 'px');
    
    var width = $('#webcam-box').width();
    var height = $('#webcam-box').height();
    
    var itemLeft	= width > w[0] ? w[2] : (w[2] + Math.round((w[0] - width) / 2));
    var itemTop		= height > w[1] ? w[3] : (w[3] + Math.round((w[1] - height) / 2));
    
    $('#webcam-box').css({
      'left'   :		itemLeft,
    	'top'    :		itemTop,
    	'width'  :	width + 'px',
    	'height' :	height + 'px'
    });
    
    $('#webcam-box div.wrapper').css({
      'width'   : (width - 20)+ 'px',
      'height'  : (height - 50) + 'px'
    });
    
    $(window).bind("resize scroll", 
      function(){
        var w = getUserViewPoint();
        
        $('#dim-the-light').css({
          'left'    : w[2] + 'px',
          'top'     : w[3] + 'px',
          'width'   : w[0] + 'px',
          'height'  : w[1] + 'px'
        });
        $('#dim-the-light').show();
    
        $("#webcam-box").css('left', ($('#webcam-box').width()	> w[0] ? w[2] : (w[2] + Math.round((w[0] - $('#webcam-box').width()) / 2))));
        $("#webcam-box").css('top',  ($('#webcam-box').height() > w[1] ? w[3] : (w[3] + Math.round((w[1] - $('#webcam-box').height()) / 2))));
        
      }
    );
  } else {
    if (webcambox_visible) {
      $(window).unbind("resize scroll");
      $('#webcam-box').hide("slow");
      $('#dim-the-light').remove();
      webcambox_visible = false;
      clearInterval(wc_interval);
    }
  }
  return false;
}

function wc_resize(){
  var box_h = ($('#webcam-box div.main-webcam-box').height() + 73);
  var txt_h = ($('#webcam-box div.txt-part ').height() + 225);
          
  $('#webcam-box').height((txt_h < box_h ? box_h : txt_h));
  $('#webcam-box div.link-wrap').css('margin-top', ($('#webcam-box').height() - 83) + 'px');
    
  var width = $('#webcam-box').width();
  var height = $('#webcam-box').height();
    
  var itemLeft	= width > w[0] ? w[2] : (w[2] + Math.round((w[0] - width) / 2));
  var itemTop		= height > w[1] ? w[3] : (w[3] + Math.round((w[1] - height) / 2));
    
  $('#webcam-box').css({
    'left'   :		itemLeft,
    'top'    :		itemTop,
    'width'  :	width + 'px',
    'height' :	height + 'px'
  });
    
  $('#webcam-box div.wrapper').css({
    'width'   : (width - 20)+ 'px',
    'height'  : (height - 50) + 'px'
  });
}

function wc_updatepic(wc_url) {
  wc_url += "?" + Math.random();
  $('#wc-image').attr("src", wc_url);
}
function wc_updatethumb(thumbid, wc_url) {
  wc_url += "?" + Math.random();
  $(thumbid).css("background-image", 'url(' + wc_url + ')');
}


var wc_interval = null;
var wc_interval_thumb = null;
function wc_switchtab(s) {
  var wc_path = ROOT + '/i/webcam/live';
  var wc_url = '';
  if (typeof(wc_interval) != "null") {
    clearInterval(wc_interval);
    clearInterval(wc_interval_thumb);
    wc_interval = null;
  }
  $('.main-webcam-box').html('<img src="" id="wc-image"/>');
  
  // teksti
  var res = $.ajax({
    url: "/ajax/webcam-txt/",
    async: false
  }).responseText;
  var tmp = res.split("|");
  var wc_txt1 = tmp[0];
  var wc_txt2 = tmp[1];
  var wc_txt3 = tmp[2];
  
  if (s == 1) {
    $('#webcam-box div.txt-part').html(wc_txt1);
    $('#main-webcam-footer').html('Amigo dāvana Juglas suņu patversmei 10 gadu jubilejā – webkameras translācijas nodrošinājums');
    wc_url = wc_path + "/wc-amigo.jpg";
    wc_updatepic(wc_url);
    wc_interval = setInterval(function(){ wc_updatepic(wc_url); }, 3000);
    // $('#webcam-box div.main-webcam-box, #webcam-box #main-webcam-footer').css("margin-left", "0px");
    // $('#webcam-box div.main-webcam-box').height(360);

  // } else if (s == 2) {
  //   $('#webcam-box div.txt-part').html(wc_txt2);
  //   $('#main-webcam-footer').html('Sadarbībā ar <a href="http://www.liepaja.lv/">www.liepaja.lv</a>');
  //   wc_url = wc_path + "/wc-liepaja-medium.jpg";
  //   wc_updatepic(wc_url);
  //   wc_interval = setInterval(function(){ wc_updatepic(wc_url); }, 3000);
  //   $('#webcam-box div.main-webcam-box, #webcam-box #main-webcam-footer').css("margin-left", "0px");
  //   $('#webcam-box div.main-webcam-box').height(360);

  } else {
    $('#webcam-box div.txt-part').html(wc_txt3);
    $('#main-webcam-footer').html('Translāciju nodrošina datorsalons HAWK, t.&nbsp;64122022');
    // $('#main-webcam-footer').html('Sadarbībā ar <a href="http://www.zagarkalns.lv/">www.zagarkalns.lv</a>');
    wc_url = wc_path + "/wc-cesis-medium.jpg";
    wc_updatepic(wc_url);
    wc_interval = setInterval(function(){ wc_updatepic(wc_url); }, 3000);
    // $('#webcam-box div.main-webcam-box, #webcam-box #main-webcam-footer').css("margin-left", "70px");
    // $('#webcam-box div.main-webcam-box').height(360);

  }
  
  wc_url_thumb1 = wc_path + "/wc-amigo-small.jpg";
  wc_url_thumb2 = wc_path + "/wc-cesis-small.jpg";
  // wc_url_thumb3 = wc_path + "/wc-cesis-small.jpg";
  wc_interval_thumb = setInterval(function(){ 
    wc_updatethumb('#wc-thumb-1', wc_url_thumb1);
    wc_updatethumb('#wc-thumb-2', wc_url_thumb2);
    // wc_updatethumb('#wc-thumb-3', wc_url_thumb3);
  }, 30000);
  
  wc_resize();
  return false;
}


$(document).ready(function(){
  $('head').append('<link rel="stylesheet" type="text/css" href="' + ROOT + '/css/webcam.css?20090819"/>');
  
  // ja lapa atvērta ar /#webcam URLī, automātiski parāda webcam logu
  if (document.location.hash == "#webcam") {
    setTimeout(function() { webcam(1); }, 300);
    // webcam(1);
  }
  
});

// aizvērt ar Esc
$(window).bind("keydown", function(e){
  if (checkKeycode(e) == 27) {
    webcam(0);
  } 
});
