/* load add elements - when scroll activated */
$(window).scroll(function(){
  if($(this).scrollTop() > 300)
    loadPopGoods();
});

/* document is ready */
$(document).ready(function() {

  /*
    select radomize tab
  */
  $('#novelty li a').eq(Math.floor(Math.random()*$('#novelty li a').length)).trigger("click");

  /*
    ------------------ updown --------------------
    1. loading navigation arrows
    2. split new goods by line, and output first two lines
  */
//  $('#novelty ul').before("<a class='ud-up ud-up-inactive' href='#'></a><a class='ud-down ud-down-inactive' href='#'></a>");
	$('#novelty a.ud-down-inactive').removeClass("ud-down-inactive").addClass("ud-down-active");
	if($('#novelty').height() > 30)
  { 
	step = 20;
	count = 20;
	$('a.ud-down').live("click", function(){
		if ($("#novelty ul").outerHeight() > count+step || $("#novelty ul").outerHeight() == count+step)
			{
				$("#novelty ul").css("margin-top",(parseInt($("#novelty ul").css("margin-top"))-step) + "px");
				$("#novelty a.ud-down-inactive").removeClass("ud-down-inactive").addClass("ud-down-active");
				$("#novelty a.ud-up-inactive").removeClass("ud-up-inactive").addClass("ud-up-active");
				count = count+step;
				if ($("#novelty ul").outerHeight() - count < step || $("#novelty ul").outerHeight() - count == step)
				$("#novelty a.ud-down-active").removeClass("ud-down-active").addClass("ud-down-inactive");	
			}
		else 
		{
			$("#novelty a.ud-down-active").removeClass("ud-down-active").addClass("ud-down-inactive");
			return false;
		}
	})
	
	$('a.ud-up').live("click", function(){
		if (parseInt($("#novelty ul").css("margin-top")) != 0)
			{
				$("#novelty ul").css("margin-top",(parseInt($("#novelty ul").css("margin-top"))+step) + "px");
				$("#novelty a.ud-down-inactive").removeClass("ud-down-inactive").addClass("ud-down-active");
				$("#novelty a.ud-up-inactive").removeClass("ud-up-inactive").addClass("ud-up-active");
				if ((Math.abs(parseInt($("#novelty ul").css("margin-top")))) == 0)
				$("#novelty a.ud-up-active").removeClass("ud-up-active").addClass("ud-up-inactive");
			}
			else 
			{
				$("#novelty a.ud-up-active").removeClass("ud-up-active").addClass("ud-up-inactive");
				return false;
			}
			count = 20;	
	})
  }
    
  /*
    checking for terminal
  */
  var _tmp = null;
  _tmp = location.pathname.match(/~([\w]*)\//i);
  
  $.ajax({
      type: 'GET',
      url:  '/dcontent/?action=isterminal&dl='+((_tmp == null)?'':_tmp[1]),
      dataType: "json",
      cache: true,
      success: function(data) {
        if(data.inTerminal == 1)
        {
          /*
          if(data.redirect != undefined)
            location.href = data.redirect;
          */  
          $("div,a").live("mousemove", hackInaction);
          $("div,a").live("mousedown", hackInaction);
          $("div,a").live("keyup", hackInaction);
          $("div,a").live("keydown", hackInaction);
          $(window).scroll(hackInaction);
          hackInaction();
        } 
      }
  });
}); /* $(document).ready(function() { */

// --------------------------------------------------------------------------------------------------------------------------------

/* loading pop goods */
var popIsLoad = false;
function loadPopGoods()
{ if(popIsLoad) return; popIsLoad = true; // make load one time 
  $.ajax({
    type: 'GET',
    url:  '/dcache/'+location.host+location.pathname+'pop_goods.0.html',
    dataType: "html",
    cache: true,
    success: function(data){
      $('.pop-goods div.pop-loader').remove();
      $('.pop-goods div.goods-row:last').after(data);
      refreshIcons();
    },
    error: function(jqXHR, textStatus, errorThrown){
      $('.pop-goods div.pop-loader').remove();
    }
  });
}

function rand( min, max ) { // Generate a random integer
    if( max ) {
        return Math.floor(Math.random() * (max - min + 1)) + min;
    } else {
        return Math.floor(Math.random() * (min + 1));
    }
}

/* */
var idChangeScreensaver = null;
function showScreensaver()
{
  clearTimeout(idChangeScreensaver);
  idScreensaver = setTimeout("showScreensaver()", 30000);
  
  /*checkinbg for exists a container */
  if($("#terminal-screensaver").length < 1)
    $("body").append("<div id='terminal-screensaver' style='display:none;'></div>");
    
  $.ajax({
    type: 'GET',
    url:  '/dcache/'+my_city_code+'_screensavers/item-'+rand(0,64)+'.html',
    dataType: "html",
    cache: true,
    success: function(data){
      $("#terminal-screensaver").html(data);
      $("#terminal-screensaver").show();
      /*  
      $("#terminal-screensaver").fadeOut('fast', function(){
        $("#terminal-screensaver").html(data);
        $("#terminal-screensaver").fadeIn('fast');
      });
      */
    }
  });
}

/* clear basket */
function ajaxClearBasket()
{
  $.ajax({
      type: 'GET',
      url:  '/dcontent/?action=logout',
      dataType: "json",
      cache: false,
      success: function(data) {
      }
  });
  setTimeout(
    function(){
      $.ajax({
          type: 'GET',
          url:  '/dcontent/?action=clearbasket',
          dataType: "json",
          cache: false,
          success: function(data) {
            location.href="/";
          }
      });
    }, 2000
  );
}

/* hack to inaction */
var idScreensaver   = null;
var idClearBasket   = null;
function hackInaction()
{
  $("#terminal-screensaver").hide();
  /*
  clearTimeout(idScreensaver);
  idScreensaver = setTimeout("showScreensaver()", 30000);
  */

  clearTimeout(idClearBasket);
  idClearBasket = setTimeout("ajaxClearBasket()", 600000);
}

function startRotation(rotationClass)
{
  var _banners = $("div."+rotationClass);
  if(_banners.length > 0)
    _banners.eq(rand(1,_banners.length)-1).show();
  else
    _banners.show();
  $("div."+rotationClass.replace("rotation","default")).hide();
}


