$(document).ready(function(){

 /*
  * banners brand
  */
  
  $("#panel_byModels li a.tip_lm").each(function(index){
    $(this).html("<img src='images/_brand/"+$(this).text().toLowerCase().replace(' ', '-')+".gif' alt='"+
      $(this).text()+"' title='"+$(this).text()+"' width='70' /> ").removeClass("tip_lm");
  });
  
 /*
  *  price list
  */
  
  /*-- tooltip --*/


  var hideDelay = 700;  
  var hideTimer = 400;//null;

  // One instance that's reused to show info for the current person
  var container = $('<div id="PopupContainer">'
    + '<table class="PopupPopup">'
    + '<tr>'
    + '   <td class="corner topLeft">&nbsp;</td>'
    + '   <td class="top">&nbsp</td>'
    + '   <td class="corner topRight">&nbsp;</td>'
    + '</tr>'
    + '<tr>'
    + '   <td class="left">&nbsp;</td>'
    + '   <td><div id="PopupContent">&nbsp;</div></td>'
    + '   <td class="right">&nbsp;</td>'
    + '</tr>'
    + '<tr>'
    + '   <td class="corner bottomLeft">&nbsp;</td>'
    + '   <td class="bottom">&nbsp;</td>'
    + '   <td class="corner bottomRight">&nbsp;</td>'
    + '</tr>'
    + '</table>'
    + '</div>');

  $('body').append(container);

  var variant;
  var settings;
  // format of rel tag: rel = 'productID,personguid'
function rel_find(this_){
  var rel_len = this_.parent('[rel]').length;
  if (rel_len)
  {
    variant='cart';
    settings = this_.parent('[rel]').attr('rel').split(',');
  }
  else
  {
    variant='price';
    settings = this_.parent().parent('[rel]').attr('rel').split(',');
  }
  return settings;
}

$('.info').live('mouseover', function(){
  // format of rel tag: rel = 'productID,personguid'
  
  var productID = rel_find($(this))[0];
  var productShow = rel_find($(this))[1];
  // If no guid in url rel tag, don't popup blank
  if (productShow == '0') {
    return
  }
  if (hideTimer)
  {
    clearTimeout(hideTimer);
    $('#PopupContent').html('<img id="wait" src="images/loader.gif" style="display:block"/>');
    jQuery.ajax(
    {
      type: 'GET',
      url: '_products.php',
      data: 'pr=' + productID, //+ '&guid=' + productShow,
      success: function(data)
      {
        // alert(data);
        // Verify that we're pointed to a page that returned the expected results.
        if (data.indexOf('PopupResult') < 0)
        {
          $('#PopupContent').html('<span>Òîâàð ' + productID + ' íå íàéäåí!</span>');
        }

        // Verify requested person is this person since we could have multiple ajax
        // requests out if the server is taking a while.
        if (data.indexOf(productShow) > 0)
        {
          //filter var text = $(data).find("div.PopupResult").html();
          $('#PopupContent').html(data);
        }
      }
    });
  }

  var _left;
  var _top;

  if (variant=='price')
  {
    _left = $(this).offset().left + $(this).width() + 35;
    _top =  $(this).offset().top + 12 ;
  }
  if (variant=='cart')
  {
    _left = $('#cart').offset().left - $('#PopupContent').width() - 235;
    //$(container)
    //innerHeight/2 - 150
    //var _left = innerWidth/2 - 200;
    _top = $(this).offset().top + 12;
  }
  container.css({
    left: _left + 'px',
    top:  _top + 'px'
  });
  container.css('display', 'block');
});

/*- info hide -*/
  
  $('.info').live('mouseout', function(){
    if (hideTimer){
		
      clearTimeout(hideTimer);
      hideTimer = setTimeout(function()
      {
        container.css('display', 'none');
      }, hideDelay);
    };
  });

  // Allow mouse over of details without hiding details
  $('#PopupContainer').mouseover(function(){
    if (hideTimer)
      clearTimeout(hideTimer);
  });

  // Hide after mouseout
  $('#PopupContainer').mouseout(function(){
    if (hideTimer)
      clearTimeout(hideTimer);
    hideTimer = setTimeout(function()
    {
      container.css('display', 'none');
    }, hideDelay);
  });
  
/*- colapse category -*/
  
  $("#pricelist .name").live('click', function(){
    //if ($(this).is(':visible')){
    if ($(this).hasClass('tree_plus')){
      $(this).parent().find(".content:first").slideDown();
      $(this).removeClass('tree_plus');
      $(this).addClass('tree_minus');
    } else {
      $(this).parent().find(".content:first").hide();
      $(this).removeClass('tree_minus');
      $(this).addClass('tree_plus');
    }
  });

  $("#pricelist .add, #cart .add").live('click', function(){
    $('#cart').html('<img id="wait" src="images/loader.gif" style="display:block"/>');
      var productID = rel_find($(this))[0];
      jQuery.post('_shopping_cart.html?action=add_product',
    {
      products_id:productID
    },function(){
      jQuery.get('_shopping_cart.php',
        {},
        function(data){
          $("#cart").replaceWith(data);
        });
    });
  });
  
  $("#cart .del").live('click', function(){
    $('#cart').html('<img id="wait" src="images/loader.gif" style="display:block"/>');
    var productID = rel_find($(this))[0];
    $.post('_shopping_cart.html?action=del_product',
    {
      products_id:productID
    },
    function(){
      $.get('_shopping_cart.php',
      {},
        function(data){
          $("#cart").replaceWith(data);
        });
    });
  });


  /*-- columinize --*/
  $('#content').ajaxSuccess(function() {
    $('#content_page.columinaze').columnize({
      width:500
    });
  });

  /*-- colorbox --*/

  $("a[rel='cbox']").live("hover", function(){
    $("a[rel='cbox']").colorbox({
      opacity:0.5
    });
  });

  /*-- top menu --*/

  $("#header_menu_top dt").toggle(
    function(){
      $(this).parent().find("dd").slideDown();
      $(this).css({
        "background":"url('images/icons/icon_minus.gif') no-repeat scroll right center"
      });
    },
    function(){
      $(this).parent().find("dd").hide();
      $(this).css({
        "background":"url('images/icons/icon_plus.gif') no-repeat scroll right center"
      });
    });

  /*-- loader --*/

  $('body').append('<img id="wait" src="images/loader.gif" />');

  /*-- tabs menu --*/
/*
** 
*/
  $(".menu a").live("click", function(event){ 
    var page = $(this).attr("name");
    var menu = "#content"+$(this).attr("by");
    if(menu=="#contentundefined"){
      menu="#content"
    };
    //if(page=="main"){menu="html"};
    if(page=="main"){
      window.location = '/'
    };

    //	 alert (menu);
    $(this).parent().parent().find("dd").removeClass("active");
    $("#wait").show();
    $.get('_pages.php', {
      p:page
    }, function(data){
      $(menu).html(data);
      $("#wait").hide();
    });
    //event.preventDefault();
    $(this).parent().addClass("active");
    return false;
  });		


  /*-- partners menu --*/
  $('#content').ajaxSuccess(function() {
    if($("#kiev").index()!=-1){
			
      $("#content_page div").hide();
      $("#kiev").show();
      $("#partners_menu dd").bind("click", function(event){
														  
        var tab = "#"+$(this).attr("name");
        $("#partners_menu dd").removeClass("active")
        $("#content_page div").css("display", "none");
        $(this).addClass("active");
        $(tab).fadeIn();
      });
    }
  });

  /*-- left panel --*/
 
  $(".panel_name").css({
    "background":"url('images/icons/icon_minus.gif') no-repeat scroll 2px center"
  });
  $("#panel_byAcessoires .panel_name").css({
    "background":"url('images/icons/icon_plus.gif') no-repeat scroll 2px center"
  }).parent().find(".panel_content").hide();
	
  $(".panel_name").toggle(
    function(){
      $(this).parent().find(".panel_content").show();
						  
      $(this).css({
        "background":"url('images/icons/icon_minus.gif') no-repeat scroll 2px center"
      });
						  
    },
    function(){
      $(this).parent().find(".panel_content").hide();
							
      $(this).css({
        "background":"url('images/icons/icon_plus.gif') no-repeat scroll 2px center"
      });
							
    });
 
  /*-- carousel products --*/

  var products_itemList = [
  {
    title: 'ÀÊÁ iPhone 3G', 
    price:'20.00', 
    img:'battery-iphone-3g-1.jpg', 
    url: 'AKB-iphone-3g-p-455.html'
  },

  {
    title: 'ÀÊÁ HP rz1710 / 1715 / 1717  (950 mAh)', 
    price:'13.00', 
    img:'hp_1710_b.jpg', 
    url: 'AKB-hp-rz171017151717-950-mah-p-471.html'
  },

  {
    title: 'ÀÊÁ HP iPAQ 610  (1500 mAh)', 
    price:'23.00', 
    img:'HP610.jpg', 
    url: '4150-1100-p-470.html'
  },

  {
    title:'ÀÊÁ HP 4150 (1100 mAh)', 
    info:'ÀÊÁ HP 4150 (1100 mAh)', 
    price:'14.00', 
    price_sp:'13.00', 
    img:'HP4150.jpg', 
    url:'4150-1100-p-470.html'
  },
{
    title:'ÀÊÁ Eten X800  (1500 mAh)', 
    info:'ÀÊÁ Eten X800  (1500 mAh)', 
    price:'15.00', 
    price_sp:'15.00', 
    img:'Eten_X800.jpg', 
    url:'eten-x800-1500-p-324.html'
  },
{
    title:'ÀÊÁ Dell Axim X50 / X50V  (1000 mAh)', 
    info:'ÀÊÁ Dell Axim X50/X50V  (1000 mAh)', 
    price:'13.00', 
    price_sp:'13.00', 
    img:'Axim_X50_X50V.jpg', 
    url:'dell-axim-x50x50v-1000-p-322.html'
  },
{
    title:'ÀÊÁ Asus P550  (1530 mAh)', 
    info:'ÀÊÁ Asus P550  (1530 mAh)', 
    price:'18.00', 
    price_sp:'18.00', 
    img:'akb_asus_p550.jpg', 
    url:'asus-p550-1530-p-456.html'
  },
{
    title:'ÀÊÁ Asus P525 / P526/ 527 / P535 / P750 (1300 mAh)', 
    info:'ÀÊÁ Asus P525/P526/527/P535/P750  (1300 mAh)', 
    price:'19.00', 
    price_sp:'19.00', 
    img:'bat_Asus525.jpg', 
    url:'asus-p525p526527p535p750-1300-p-263.html'
  }

  ];

  function products_itemVisibleInCallback(carousel, item, i, state, evt)
  {
    // The index() method calculates the index from a
    // given index who is out of the actual item range.
    var idx = carousel.index(i, products_itemList.length);
    carousel.add(i, products_getItemHTML(products_itemList[idx - 1]));
  };

  function products_itemVisibleOutCallback(carousel, item, i, state, evt)
  {
    carousel.remove(i);
  };

  function products_initCallback(carousel)
  {
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
      carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
      carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
      carousel.stopAuto();
    }, function() {
      carousel.startAuto();
    });
  };

  /**
 * Item html creation helper.
 */
  function products_getItemHTML(item)
  {
    return '<a  href="'+item.url+'">'
    +'<div id="product_ban">'
    +'<img src="images/'+item.img+'" height="80" width="50" alt="' + item.title + '" />'
    +'<h4>'+item.title+'</h4>'
    +'<span>$'+item.price+'</span>'
    +'</div></a>';
  };

  $('#products_slider').jcarousel({
    auto: 5, 
    vertical: true,
    scroll: 1,
    wrap: 'circular',
    itemVisibleInCallback: {
      onBeforeAnimation: products_itemVisibleInCallback
    },
    itemVisibleOutCallback: {
      onAfterAnimation: products_itemVisibleOutCallback
    },
    initCallback: products_initCallback
  });

/*-- carousel partners banners--*/

  var partners_itemList = [
  {
    img: 'muk', 
    title: 'ÌÓÊ Ñåðâèñ', 
    url: 'http://www.muk.ua/support'
  },

  {
    img: 'oda', 
    title: 'ODA Ñåðâèñ', 
    url: 'http://oda-service.com.ua'
  },
  
  {
    img: 'temko', 
    title: 'Òåìêî Ñåðâèñ', 
    url: 'http://temko.com.ua'
  },

  {
    img: 'mobService', 
    title: 'ÌîáÑåðâèñ', 
    url: 'http://mobservice.com.ua'
  },
  
  {
    img: 'mobiService', 
    title: 'ÌîáèÑåðâèñ', 
    url: 'http://mobiservice.lviv.ua'
  },

  {
    img: 'mtService', 
    title: 'ÌÒÑåðâèñ', 
    url: 'http://mt-service.com.ua'
  },
  
  {
    img: 'remzona', 
    title: 'Ðåìçîíà Ñåðâèñ', 
    url: 'http://www.remzona.com.ua'
  },
  
  {
    img: 'upkeep', 
    title: 'NT Solutions Ñåðâèñ', 
    url: 'http://upkeep.com.ua'
  },
  
  {
    img: 'master+', 
    title: 'Ìàñòåð+ Ñåðâèñ', 
    url: 'http://masterplus.vn.ua/'
  }
  ];


  function partners_initCallback(carousel) {
  /*-- top menu - 
	$('.jcarousel-control a').bind('click', function() {
        carousel.scroll(jQuery.jcarousel.intval($(this).attr('news_id')));
		return false;
    });


    $('#partners-next').bind('click', function() {
        carousel.next();
        return false;
    });

    $('#partners-prev').bind('click', function() {
        carousel.prev();
        return false;
    });
	*/
    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
      carousel.stopAuto();
    }, function() {
      carousel.startAuto();
    });
  };


  function partners_itemVisibleInCallback(carousel, item, i, state, evt)
  {
    // The index() method calculates the index from a
    // given index who is out of the actual item range.
    var idx = carousel.index(i, partners_itemList.length);
    carousel.add(i, partners_getItemHTML(partners_itemList[idx - 1]));
  };

  function partners_itemVisibleOutCallback(carousel, item, i, state, evt)
  {
    carousel.remove(i);
  };

  /**
 * Item html creation helper.
 */

  function partners_getItemHTML(item)
  {
    return '<a target="_blank" href	="' + item.url + '"><img src="images/partners/carusel/' + item.img + '.png" width="120" alt="' + item.title + '" /></a>';
  };

  var rand = Math.floor(Math.random()*(partners_itemList.length))+1;
  $('#promo_partners_slider').jcarousel({
    auto: 3, 
    start: rand,
    scroll: 1,
    wrap: 'circular',
    buttonNextHTML: null,
    buttonPrevHTML: null,
    itemVisibleInCallback: {
      onBeforeAnimation: partners_itemVisibleInCallback
    },
    itemVisibleOutCallback: {
      onAfterAnimation: partners_itemVisibleOutCallback
    },
    initCallback: partners_initCallback
  });

 
  /*-- news --*/
  var container = $('#PopupContainer');
  var news_itemsList = new Array(); //[{title:'', content:'', created:''}];

  jQuery.get(
    '_news_xml.php', {
      n:'all', 
      nd:(new Date().getTime())
      },
    function(xml) {
      news_init(xml);
      news_render();
      news_hover();
    },
    'xml'
    );

  function news_init(xml){
    $(xml).find('new').each(function(i) {
      news_itemsList.push({
        title: $(this).attr('title'), 
        content: $(this).attr('content'), 
        created: $(this).attr('created')
        });
    });
  };


  // index(i, news_itemsList.length)
  function news_render(){
    var newsHTML = "<h3>Íîâîñòè ("+news_itemsList.length+")</h3>";
    newsHTML +="<ul>";
    var item;
    for (item in news_itemsList)
    {
      newsHTML += news_getItemHTML(item);
    };
    newsHTML +="</ul>";
    $('#news_text').html(newsHTML);
  };

  function news_getItemHTML(item){
    var new_item = news_itemsList[item];
    return "<li class='new' rel='" + item + ",1'>" +
    "<span class='date_min'>" + new_item.created + "</span>" +
    "<h4 class='new_title'>" + new_item.title + "</h4>" +
    //+ "<p class='new_content'>" + new_item.content + "</p>"
    "</li>";
  };

  function news_hover(){
    $("#news_text .new").click( function(){
      }
      ).hover(
      function(){
        $(this).addClass("new_hover");
        // format of 'rel' tag: newID,newShow
        var settings = $(this).attr('rel').split(',');
        var newID = settings[0];
        var newShow = settings[1];
        var item = news_itemsList[newID];

        // If no guid in url rel tag, don't popup blank
        if (newShow == '0')
          return;
        //if (hideTimer){
        //clearTimeout(hideTimer);
        //var pos = $(this).offset(); //(pos.left + width) //pos.top
        //var width = $(this).width();
        //var height = $(this).height();
        if (newShow > 0){
          var newHTML = "<div id='-'>"+
          "<h2>" + item.title + "</h2>" +
          "<p class='new_content'>" + item.content + "</p>" +
          "<span class='date_min'>" + item.created + "</span>" +
          "</div>";
          var pos = $(this).offset();
          var width = $(this).width();
          _left = (pos.left - $(container).width() - 5) + 'px';
          _top =  (pos.top - 5) + 'px';
          $('#PopupContent').html(newHTML);
          container.css({
            left: _left,
            top:  _top
          });
        };
        container.css('display', 'block');
      },
      function(){
        $(this).removeClass("new_hover");
        /*if (hideTimer){
                    clearTimeout(hideTimer);

                    hideTimer = setTimeout(function()
                    {
                        container.css('display', 'none');
                    }, hideDelay);
              };*/
        container.css('display', 'none');
      });
  };

/*
    // Allow mouse over of details without hiding details
    $('#PopupContainer').mouseover(function(){
        if (hideTimer)
            clearTimeout(hideTimer);
    });

    // Hide after mouseout
    $('#PopupContainer').mouseout(function(){
        if (hideTimer)
            clearTimeout(hideTimer);
        hideTimer = setTimeout(function()
        {
            container.css('display', 'none');
        }, hideDelay);
    });
       */
      
}); // end ready


/*-- - --*/

function popupWindow(url) {
  window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,width=100,height=100,screenX=150,screenY=150,top=150,left=150')
}
