/**
 * @author ruman
 */
jQuery.fn.extend({
    disableSelection: function(){
        this.each(function(){
            this.onselectstart = function(){
                return false;
            };
            this.unselectable = "on";
            jQuery(this).css('-moz-user-select', 'none');
        });
    },
    enableSelection: function(){
        this.each(function(){
            this.onselectstart = function(){
            };
            this.unselectable = "off";
            jQuery(this).css('-moz-user-select', 'auto');
        });
    }
});
 
var srcsIndex = 0;
function preload(srcs, cb){
	preImg = new Image();
	preImg.src = srcs[srcsIndex];
	preImg.onload = function(){
		if (srcsIndex < srcs.length-1)
		{
			srcsIndex++;
			preload(srcs,cb);
		} else {
			if (typeof cb == 'function'){
				cb();
			}
		}
	}
}

 
$(function(){
	//грузим тяжелые (критичные по времени появления) картинки
	//$('div.pult').hide();
	setTimeout(function(){
		preload(['/n/images/video-overlay.jpg','/n/images/tv.png','/n/images/photo-loader.png'], function(){
			//$('div.pult').show();
		});
		//setTimeout(function(){$('div.pult').show();},3000);//хак для IE8-
	},1000);
	
	/*setTimeout(function(){
		overlay = new Image();
		overlay.src = 'images/video-overlay.jpg';
		overlay.onload = function(){}
	},1000);*/
	
	$('ul.top-menu li').mousedown(function(){
		$(this).addClass('click');
	}).mouseup(function(){
		$(this).removeClass('click');
	}).mouseleave(function(){
		$(this).removeClass('click');
	});
	
	$('#m5').click(function(){
		$('#m5').addClass('activ');
		$('#langs').show();
		$('#m5 div.lang span').addClass('on');
	});
	$('#langs').mouseleave(function(){
		$('#m5').removeClass('activ');
		$('#langs').hide();
		$('#m5 div.lang span').removeClass('on');
	});
	$('#langs img').click(function(){
		$('#m5 div.lang').attr('class','lang '+$(this).attr('alt'));
		$('#m5').removeClass('activ');
		$('#langs').hide();
		$('div.lang span').removeClass('on');
	});
	
	$('ul.btns li').mousedown(function(){
	  $(this).addClass('active');
	}).mouseup(function(){
	  $(this).removeClass('active');
	}).mouseleave(function(){
	  if (!bubbleShow)
	  $(this).removeClass('active');
	});
	
	$('div.app-container div.buttons a, div.slider-wrap div.margin #slide-right, div.slider-wrap div.margin #slide-left').mousedown(function(){
    $(this).addClass('click');
  }).mouseup(function(){
    $(this).removeClass('click');
  }).mouseleave(function(){
    $(this).removeClass('click');
  });
  
  $('div.app-container div.buttons a.download').hover(function(){
	$('#bubble-version').fadeIn(200);
  },
  function(){
    $('#bubble-version').fadeOut(200);

  });
	
	$('div.app-container div.app-screen ul.pager li').mousedown(function(){
	  $('div.app-container div.app-screen ul.pager li').removeClass('active');
    $(this).addClass('active');
  });
  var m = false;
  $('div.slider-wrap div.apps-wrap div.pair div.app-block').mousedown(function(){
    //$('div.slider-wrap div.apps-wrap div.pair div.app-block').removeClass('active');
	if (!$(this).hasClass('active')){
		$(this).addClass('active');
		m = true;
	}
  }).mouseup(function(){
	if (m){
		$(this).removeClass('active');
		m = false;
	}
  }).mouseleave(function(){
	if (m){
		$(this).removeClass('active');
		m = false;
	}
  });
var flashContent;
  $('div.app').hover(function(){
  	//$('ul.btns',this).show();
  },
  function(){
  	if (PhotoLoader != null){
  		$thisButton.parent().addClass('show');
  	}
  });
  function showBubble(){
      $('div.bubble', $thisApp).show();
      $thisButton.addClass('active');
      $thisButton.parent().addClass('show');
      bubbleShow = true;
      $('div.video-content, div.imgs-content').hide();
      if ($thisButton.attr('class')=='video active'){
        $('div.video-content',$thisApp).show();
      } else {
        $('div.imgs-content',$thisApp).show();
      }
	  /*$('div.video-cont').addClass('temp');
	  flashContent = $('div.video-cont').html();
	  $('div.video-cont').html('')*/
  }
  function hideBubble(){
      $('div.bubble').hide();
      $('ul.btns li').removeClass('active');
      //$thisButton.parent().removeClass('show');
      $('ul.btns').removeClass('show');
      /*$('div.video-cont').removeClass('temp');
	  $('div.video-cont').html(flashContent);*/
  }
  function checkOut(){
      bubbleShow = true;
  }
  var topLeft = 0;
  var PhotoLoader = null;
  function animatePhotoLoader(btn){
  	PhotoLoader = setInterval(function(){
  		topLeft -=29;
  		btn.css({
  			backgroundPosition : '0px '+topLeft+'px'
  		});
  	},100);
  }
	var bubleTimet = null;
	var $thisApp;
	var $thisButton;
	var bubbleShow = false;
	
	$('ul.btns li.video, ul.btns li.imgs').click(function(){
	  $thisButton = $(this);
      $thisApp = $thisButton.parent().parent();
      hideBubble();
	  if (!bubbleShow){
	  var isrc = $('div.imgs-content img',$thisApp).attr('data-src');
	  animatePhotoLoader($thisButton);
	  imf = new Image();
	  imf.src = isrc;
	  $thisButton.addClass('loading');
	  imf.onload = function(){
	  	//$('div.bubble',$thisApp).css({left : $thisButton.offset().left - $thisApp.offset().left - 212});
	  	$('div.imgs-content img',$thisApp).attr('src',isrc);
	  	$thisButton.removeClass('loading').removeAttr('style');
	  	clearInterval(PhotoLoader);
	  	PhotoLoader = null;
	    showBubble();
	  }
	      //bubleTimet = setTimeout(checkOut , 500);
	  } else {
	    hideBubble();
	    bubbleShow = false;
      //clearTimeout(bubleTimet);
    }
    return false;
  });
  
  /*$('ul.btns li.link').click(function(){
    location.href = 'http://ticno.com/multibar';
  });
  $('ul.btns li.download').click(function(){
    location.href = $(this).attr('url');
  });*/
	$('div.bubble div.close').click(function(){
	  $(this).parent().hide();
	  $thisButton.removeClass('active');
    $thisButton.parent().removeClass('show');
	  bubbleShow = false;
	   $('div.video-cont').html(flashContent);
	});
	

  //галерея приложений
	$('div.margin').disableSelection();
  var flag = true;
  var appCount = $('div.long-wrap div.app-block').size();
	var longWrapWidth = (Math.ceil(appCount/2)*(424+18));
  $('div.long-wrap').width(longWrapWidth);
  $('#slide-right').click(function(){
    //if((parseInt($('div.long-wrap').css('margin-left')) > (-1*longWrapWidth+800)) && flag){
	if((parseInt($('div.long-wrap').css('margin-left')) > (-1*longWrapWidth+900)) && flag){
	  flag = false;
      $('div.long-wrap').animate({'margin-left':'-=442'},500, function(){flag = true;})
	}
  });
  $('#slide-left').click(function(){
    if((parseInt($('div.long-wrap').css('margin-left')) < 0) && flag){
	flag = false;
      $('div.long-wrap').animate({'margin-left':'+=442'},500, function(){flag = true;})
	}
  });
  
  //галерея скринов
  $('div.app-screen ul.pager li').click(function(){
	var th = $(this);
	$('#screens li').removeClass('show');
	$('#screens li').eq($(th).index()).addClass('show');
  });
  
  
  $('ul.version-tab li').click(function(e){
      e = e || window.event;
      if (!$(this).hasClass('current') && !$(e.target).is('a')){
        $('ul.version-tab li').removeClass('current');
        $(this).addClass('current');
      }
    });
    
    /*$('div.pay-block div.input div.select').click(function(){
      $('ul.select-list',$(this).parent()).show();
    });
    $('div.pay-block div.input ul.select-list').mouseleave(function(){
      $(this).hide();
    });
    $('div.pay-block div.input ul.select-list li').click(function(){
      $('span.loc', $(this).parent().parent()).text($('span',this).text());
      $('img.loc', $(this).parent().parent()).attr('src',$('img',this).attr('src'));
      $(this).parent().hide();
    });*/
 

var interv;
var canInitScroll = true;   
    
    $('div.pay-block div.input div.select').click(function(){
	clearInterval(interv);	
      $('div.dropdown-wrap',$(this).parent()).css('visibility','visible');
	  if (canInitScroll){
		//initScroll($(this).parent());
	  }
    });
    $('div.pay-block div.input div.dropdown-wrap').mouseleave(function(){
	  clearInterval(interv);
      $(this).css('visibility','hidden');
    });
    $('div.input div.dropdown-wrap div.dropdown div.scroll-wrap div').live('click',function(){
      clearInterval(interv);
      $('span.loc', $(this).parent().parent().parent().parent()).text($('span',this).text());
      $('img.loc', $(this).parent().parent().parent().parent()).attr('src',$('img',this).attr('src'));
      $('div.dropdown-wrap').css('visibility','hidden');
    });
    
/*
function initScroll(thisInput){    
  var scrollContainer = $('div.dropdown',thisInput).height();
  var scrollWrap = $('.scroll-wrap',thisInput).height();
  var content = $('.scroll-wrap',thisInput);
  var maxBottom = scrollContainer - scrollWrap;
  var canScroll = false;
  canInitScroll = false;
  
  /*if (maxBottom >= 0){
    $('#scroll-down, #scroll-up',thisInput).hide();
  }*/
/* 
  $('div.dropdown',thisInput).mousewheel(function(event, delta) {
      if (delta == 1){
        if (parseInt(content.css('top')) <= -6){
          content.animate({'top' : '+=6'},1);
        }
      } else if(delta == -1){
        if(parseInt(content.css('top')) >= maxBottom+8){
          content.animate({'top' : '-=6'},1);
        }
      }
      return false;
  });
  var startInt;
  $('.scroll-down',thisInput).hover(function(){
	startInt = setTimeout(function(){
    interv = setInterval(function(){
                  if(parseInt(content.css('top')) > maxBottom){
                    content.animate({'top' : '-=1'},0)
                  } else {
                    clearInterval(interv);					
                  }
                },10);
	},200);
  }).mouseleave(function(){
    clearInterval(interv);
	clearTimeout(startInt);
  });
  $('.scroll-up',thisInput).hover(function(){
	startInt = setTimeout(function(){
    interv = setInterval(function(){
                  if (parseInt(content.css('top')) < 0){
                    content.animate({'top' : '+=1'},0)
                  } else {
                    clearInterval(interv);
                  }
                },10);
	},200);
  }).mouseleave(function(){
    clearInterval(interv);
	clearTimeout(startInt);
  });
} 
*/

  $('div.features-list a').hover(function(){
    $('div.appabout',$(this).parent()).show();
  }, function(){
    $('div.appabout',$(this).parent()).hide();
  });
 
	
	$('a.tmb').hover(function(){
		$('div.blb',this).show();
	},
	function(){
		$('div.blb',this).hide();
	});
	
	$('div.award div.close').bind('click',function(){
		$(this).parent().hide();
	});
  
  
  //Слайдер приложений на странице оплаты
  
  var thumsWidth = $('div.thumbs ul li').length * (144+7);
  $('div.thumbs').width(thumsWidth);
  $('div.app-slider .left').click(function(){
  	if (parseInt($('div.thumbs').css('left')) < 0){
  		$('div.thumbs').animate({left:'+=151px'});
  	}
  });
  $('div.app-slider .right').click(function(){
  	if (parseInt($('div.thumbs').css('left')) > (906 - thumsWidth)){
  		$('div.thumbs').animate({left:'-=151px'});
  	}
  });
  
  
$('div.share .twitter').hover(function(){
  	$(this).css('background-position', ' left -57px');
  	$(this).animate({'width' : '166px'});
  },
  function(){
  	$(this).animate({'width' : '51px'}, function(){$(this).css('background-position', ' left top');});
  }).mousedown(function(){
  	$(this).css('background-position', ' left -114px');
  }).mouseleave(function(){
  	$(this).css('background-position', ' left -57px');
  	return false;
});
 
  $('div.share .vk').hover(function(){
  	$(this).css('background-position', ' left -57px');
  	$(this).animate({'width' : '186px'});
  },
  function(){
  	$(this).animate({'width' : '51px'}, function(){$(this).css('background-position', ' left top');});
  }).mousedown(function(){
  	$(this).css('background-position', ' left -114px');
  }).mouseleave(function(){
  	$(this).css('background-position', ' left -57px');
  	return false;
});


$('ul.app-list li').mousedown(function(){
  	$(this).addClass('press');
  }).mouseup(function(){
  	$('ul.app-list li').removeClass('active');
  	$(this).removeClass('press').addClass('active');
  	na = $(this).attr('title')
  	$('div.app-cont').hide();
  	$('#'+na).show();
  }).mouseleave(function(){
  	$(this).removeClass('press');
  });
  
   $('div.gift ul li').click(function(){
 		$('div.gift ul li').removeClass('current'); 	
	  	$(this).addClass('current');
		sd = $(this).attr('lang');
		$('div.gift-apps div.gift-app').hide();
		$('div.gift-apps #gift-'+sd).show();
  });

  $('div.play-video').click(function(){
  	$('div.video-sh').show();
  	$('html').css('overflow','hidden');
  });
  $('div.closeVideo').click(function(){
  	$('div.video-sh').hide();
  	$('html').css('overflow','auto');
  });
  
  
  //выпадайка на странице оплаты
  $('div.select div.selector').click(function(){
  //	$('div.select').removeClass('slct');
  	if (!$(this).parent().hasClass('slct')){
		$(this).parent().addClass('slct');
	}
  	else
  	{
		$(this).parent().removeClass('slct');
	}
  });
  
  $('div.select ul.list li').live('click', function(){
	$('div.result',$(this).parent().parent().parent().parent()).html($(this).html());
	$('div.select').removeClass('slct');
	
  });
  
  //полка программ на странице оплаты
	var canAnimated = false;
  $('div.polka div.right').click(function(){
  	if (parseInt($('div.polka ul').css('margin-left')) > ((188*4) - $('div.polka ul').width()) && !canAnimated){
	  	canAnimated = true;
	  	$('div.polka ul').animate({'margin-left':'-=188px'}, function(){canAnimated = false;});
	}
  });
  $('div.polka div.left').click(function(){
  	if (parseInt($('div.polka ul').css('margin-left')) < 0 && !canAnimated){
  		canAnimated = true;
  		$('div.polka ul').animate({'margin-left':'+=188px'}, function(){canAnimated = false;});
  	}
  });

});



