$(document).ready(function() {
  

  
  $("a.nomlink").hover(
    function () {
      $('#nompeek_0').hide();
      x = $(this).attr('id').split('_');
      $('#nompeek_'+x[1]).fadeIn('fast');
    },
    function () {
      x = $(this).attr('id').split('_');
      $('#nompeek_'+x[1]).hide();
      $('#nompeek_0').show();
    }
  );
  
  $("a.beltclip").click(
    function () {
      getItem($(this).attr('rel'));
      return false;
    }
  );
  
  $("a.videoclip").click(
    function () {
      getItemVideo($(this).attr('rel'));
      return false;
    }
  );
  
  $('a.filmclip').live('click', 
    function () {
      getPeek($(this).attr('rel'));
      return false;
    }
  );
  
  $("#big_left").click(
    function () {
      if ($(this).attr('rel') == 'video') moveleftvideo();
      else moveleft();
      return false;
    }
  );
  
  $("#big_left_film").click(
    function () {
      movefilmleft();
      return false;
    }
  );
  
  $("#big_right").click(
    function () {
      if ($(this).attr('rel') == 'video') moverightvideo();
      else moveright();
      return false;
    }
  );
  
  $("#big_right_film").click(
    function () {
      movefilmright();
      return false;
    }
  );
  
});


function getItem(x)
{
  $('#peek_src').hide();
  $('#insertionpoint a.bigzoom').remove();
  var xv = 0;
  var current_pos = 1;
  $('a.beltclip').each(
    function()
    {
      if ($(this).attr('rel') == x)
      {
        $(this).addClass('active');
        current_pos = xv - 1;
      }
      else
      {
        $(this).removeClass('active');
      }
      xv = xv + 1;
    }
  );
  if (current_pos < 1) current_pos = 1;

  $.ajax({
    type: "GET",
    url: "http://"+window.location.hostname+"/item/"+x,
    dataType: "xml",
    success: function(xml) {
	    var title = $(xml).find('title').text();
	    var content = $(xml).find('content').text();
	    var source = $(xml).find('source').text();
	    var sourcelink = $(xml).find('sourcelink').text();
	    var bigpic = $(xml).find('bigphoto').text();
	    var freepic = $(xml).find('freepic').text();
      if (sourcelink != '') $('#peek_source').empty().append('Фото &mdash; <a target="_blank" rel="nofollow" href="'+sourcelink+'">'+source+'</a>');
      else $('#peek_source').empty().append('Фото &mdash; '+source);
	    $('#global_count').attr('rel',$(xml).find('id').text());
	    if (parseInt(title) == 'NaN') $('#peek_title').empty().append(title);
	    $('#peek_description').empty().hide().append(content).slideDown('fast');
      if (freepic != '')
      {
        $('#peek_src').empty().append('<img class="fpc" src="'+bigpic+'" longdesc="'+freepic+'" alt=""/>');
        $('#insertionpoint').append('<a href="'+freepic+'" class="bigzoom" id="big_zoom_'+$(xml).find('id').text()+'"></a>');
        $('.bigzoom').fancybox();
      }
	    else
	    {
	      $('#peek_src').empty().append('<img src="'+bigpic+'" alt=""/>');
	    }
	    $('#peek_src').fadeIn('fast');

	    stepcarousel.stepTo('gallery', current_pos);
	  }
  });
}

function getItemVideo(x)
{
  var xv = 0;
  var current_pos = 1;
  $('a.videoclip').each(
    function()
    {
      if ($(this).attr('rel') == x)
      {
        $(this).addClass('active');
        current_pos = xv - 1;
      }
      else
      {
        $(this).removeClass('active');
      }
      xv = xv + 1;
    }
  );
  if (current_pos < 1) current_pos = 1;
  $.ajax({
    type: "GET",
    url: "http://"+window.location.hostname+"/item/"+x,
    dataType: "xml",
    success: function(xml) {
      //alert(xml);
	    var title = $(xml).find('title').text();
	    var content = $(xml).find('content').text();
	    var source = $(xml).find('source').text();
	    var sourcelink = $(xml).find('sourcelink').text();
	    var bigpic = $(xml).find('bigphoto').text();
	    var video = $(xml).find('video').text();
      $('#global_count').attr('rel',$(xml).find('id').text());
	    if (parseInt(title) == 'NaN') $('#peek_title').empty().append(title);
	    $('#peek_description').empty().hide().append(content).slideDown('fast');
      var flashvars = {};
      var params = {menu: "false", wmode: "opaque"};
      var attributes = {};
      swfobject.embedSWF(video, "peek_src", "490", "274", "9.0.0", flashvars, params, attributes);
	    stepcarousel.stepTo('gallery', current_pos);
	  }
  });
}

function getFilm(x)
{
  $.ajax({
    type: "GET",
    url: "http://"+window.location.hostname+"/film/"+x,
    dataType: "xml",
    success: function(xml) {
      //alert(xml);
	    var title = $(xml).find('title').text();
	    var engtitle = $(xml).find('engtitle').text();
	    var year = $(xml).find('year').text();
	    var genre = $(xml).find('genre').text();
	    var country = $(xml).find('country').text();
	    var director = $(xml).find('director').text();
	    var cast = $(xml).find('cast').text();
	    
	    $('#film_title').attr('rev',$(xml).find('id').text());
	    
	    $('#film_title').empty().append(title);
	    $('#film_engtitle').empty().append(engtitle);
	    $('#film_genre').empty().append('<b>'+genre+', '+country+', '+year+'</b>');
	    $('#film_director').empty().append('<b>Режиссёр:</b> '+director);
	    $('#film_cast').empty().append('<b>В ролях:</b> '+cast);
	    
	    if (($('#film_title').attr('rel') == 0) || ($('#film_title').attr('rel') == ''))
	    {
	      var bn = location.href.split("#");
        if ((bn.length > 1) && (bn[1] != ''))
        {
          if (bn[1] == 'last')
          {
            getPeek($(xml).find('last').text());
          }
          else
          {
            getPeek($(xml).find('first').text());
          }
        }
	      else
	      {
	        getPeek($(xml).find('first').text());
	      }
	    }
	  }
  });
}

function getPeek(x)
{
  var xv = 0;
  var current_pos = 1;
  $('a.filmclip').each(
    function()
    {
      if ($(this).attr('rel') == x)
      {
        $(this).addClass('active');
        current_pos = xv + 1;
      }
      else
      {
        $(this).removeClass('active');
      }
      xv = xv + 1;
    }
  );
  if (current_pos < 1) current_pos = 1;
  $.ajax({
    type: "GET",
    url: "http://"+window.location.hostname+"/filmpic/"+x,
    dataType: "xml",
    success: function(xml) {
      //alert(xml);
	    var content = $(xml).find('content').text();
	    var bigpic = $(xml).find('bigphoto').text();
	    var category_id = $(xml).find('category_id').text();
      
      if (category_id != $('#film_title').attr('rev')) getFilm(category_id);
      
      $('#film_title').attr('rel',x);
      
	    $('#peek_description').empty().hide().append(content).slideDown('fast');
      $('#peek_src').empty().append('<img src="'+bigpic+'" alt=""/>');
	    $('#peek_src').fadeIn('fast');
	    stepcarousel.stepTo('gallery', current_pos);
	  }
  });
}

function moveleft()
{
  var old_id = $('#global_count').attr('rel');
  var new_id = 0;
  var prev_id = 0;
  var new_path = '';
  var c = 1;
  $('a.beltclip').each(
    function()
    {
      if ($(this).attr('rel') == old_id)
      {
        if ((c > 0) && (prev_id > 0))
        {
          getItem(prev_id);
        }
        else
        {
          new_path = window.location.pathname.substring(1);

          for (var x = 0; x < categories.length; x++)
          {
            if (new_path == categories[x])
            {
              if (x >= 1)
              {
                window.location = "http://"+window.location.hostname+"/"+categories[x-1]+"#last";
              }
            }
          }
        }
      }
      c = c + 1;
      prev_id = $(this).attr('rel');
    }
  );
}

function moveleftvideo()
{
  var old_id = $('#global_count').attr('rel');
  var new_id = 0;
  var prev_id = 0;
  var new_path = '';
  var c = 1;
  
  $('a.videoclip').each(
    function()
    {
      if ($(this).attr('rel') == old_id)
      {
        if ((c > 0) && (prev_id > 0))
        {
          getItemVideo(prev_id);
        }
        else
        {
          new_path = window.location.pathname.substring(1);

          for (var x = 0; x < categories.length; x++)
          {
            if (new_path == categories[x])
            {
              if (x >= 1)
              {
                window.location = "http://"+window.location.hostname+"/"+categories[x-1]+"#last";
              }
            }
          }
        }
      }
      c = c + 1;
      prev_id = $(this).attr('rel');
    }
  );
}

function movefilmleft()
{
  var old_id = $('#film_title').attr('rel');
  var new_id = 0;
  var prev_id = 0;
  var c = 1;
  var new_path = '';
  $('a.filmclip').each(
    function()
    {
      if ($(this).attr('rel') == old_id)
      {
        if ((c > 0) && (prev_id > 0))
        {
          getPeek(prev_id);
        }
        else
        {
          new_path = window.location.pathname.substring(1);
          for (var x = 0; x < categories.length; x++)
          {
            if (new_path == categories[x])
            {
              if (x > 1)
              {
                window.location = "http://"+window.location.hostname+"/"+categories[x-1]+"#last";
              }
            }
          }
        }
      }
      c = c + 1;
      prev_id = $(this).attr('rel');
    }
  );
}

function moveright()
{
  var old_id = $('#global_count').attr('rel');
  var new_id = 0;
  var next_id = 0;
  var arr = new Array();
  var c = 1;
  var new_path = '';
  $('a.beltclip').each(
    function()
    {
      arr.push($(this).attr('rel'));
    }
  );
  $('a.beltclip').each(
    function()
    {
      if ($(this).attr('rel') == old_id)
      {
        if (c < arr.length)
        {
          getItem(arr[c]);
        }
        else
        {
          new_path = window.location.pathname.substring(1);
          if (new_path.indexOf('/' != -1))
          {
            var vb = new_path.split('/');
            new_path = vb[0];
          }
          for (var x = 0; x < categories.length; x++)
          {
            if (new_path == categories[x])
            {
              
              if (x < categories.length)
              {
                window.location = "http://"+window.location.hostname+"/"+categories[x+1];
              }
            }
          }
        }
      }
      c = c + 1;
    }
  );
}

function moverightvideo()
{
  var old_id = $('#global_count').attr('rel');
  var new_id = 0;
  var next_id = 0;
  var arr = new Array();
  var c = 1;
  var new_path = '';
  $('a.videoclip').each(
    function()
    {
      arr.push($(this).attr('rel'));
    }
  );
  $('a.videoclip').each(
    function()
    {
      if ($(this).attr('rel') == old_id)
      {
        if (c < arr.length)
        {
          getItemVideo(arr[c]);
        }
        else
        {
          new_path = window.location.pathname.substring(1);
          if (new_path.indexOf('/' != -1))
          {
            var vb = new_path.split('/');
            new_path = vb[0];
          }
          for (var x = 0; x < categories.length; x++)
          {
            if (new_path == categories[x])
            {
              
              if (x < categories.length)
              {
                window.location = "http://"+window.location.hostname+"/"+categories[x+1];
              }
            }
          }
        }
      }
      c = c + 1;
    }
  );
}

function movefilmright()
{
  var old_id = $('#film_title').attr('rel');
  var new_id = 0;
  var next_id = 0;
  var arr = new Array();
  var c = 1;
  var new_path = '';
  $('a.filmclip').each(
    function()
    {
      arr.push($(this).attr('rel'));
    }
  );
  $('a.filmclip').each(
    function()
    {
      if ($(this).attr('rel') == old_id)
      {
        if (c < arr.length)
        {
          getPeek(arr[c]);
        }
        else
        {
          new_path = window.location.pathname.substring(1);
          if (new_path.indexOf('/' != -1))
          {
            var vb = new_path.split('/');
            new_path = vb[0];
          }
          for (var x = 0; x < categories.length; x++)
          {
            if (new_path == categories[x])
            {
              
              if (x < categories.length)
              {
                window.location = "http://"+window.location.hostname+"/"+categories[x+1];
              }
            }
          }
        }
      }
      c = c + 1;
    }
  );
}

function getIt(c)
{
  var x = location.href.split("#");
  if ((x.length > 1) && (x[1] != ''))
  {
    if (x[1] == 'last')
    {
      getItem(last_item);
    }
    else
    {
      getItem(x[1]);
    }
  }
  else
  {
    getItem(c);
  }
}

function getFilmIt(c)
{
  var x = location.href.split("#");
  if ((x.length > 1) && (x[1] != ''))
  {
    if (x[1] == 'last')
    {
      getFilm(last_item);
    }
    else
    {
      getFilm(x[1]);
    }
  }
  else
  {
    getFilm(c);
  }
}

function getItVideo(c)
{
  var x = location.href.split("#");
  if ((x.length > 1) && (x[1] != ''))
  {
    if (x[1] == 'last')
    {
      getItemVideo(last_item);
    }
    else
    {
      getItemVideo(x[1]);
    }
  }
  else
  {
    getItemVideo(c);
  }
}
