﻿var resized=0;
var newheight = 600;
var origheight = 380;
var time = 1000;
var ie6=0;
var i;
var j;
var AjaxScroll=0;
var page=0;
var startpage=0;
var perpage = 100;
var len;

newheight2 = newheight - 65;
newheight3 = newheight - 85;

origheight2 = origheight - 65;
origheight3 = origheight - 85;

function ownformat(str) {
	if (str==0) return "";
	var newstr="";
	str = parseInt(str)+"";
	for (i=0;i<str.length;i=i+3) {
		j=str.length-i;
		if (j<3) newstr = str.substring(0,j) + " " + newstr;
		else newstr = str.substring(j-3,j) + " " + newstr;
	}
	//newstr = newstr.substring(newstr.length,newstr.length+1);
	//newstr = newstr+" руб.";
	return newstr;
}

function priceParseInt(price) {
	while (price.indexOf(" ")>=0) price = price.replace(" ","");
	return parseInt(price);
}

function reinit() {
	//var scrhtml = $('#scrollable').html();
    //$('#promo-main').empty();
    //$('#promo-main').append('<div id="scrollable">'+scrhtml+'</div>');
	
	//if (ie6==0) $('#scrollable').jScrollPane({animateTo:true, showArrows:true, scrollbarWidth:17});
	//$('#promo-table tr:gt(1):even').css('background','#fff');
	$('#promo-table .toCount').each(function() { $(this).count({counter:$(this).parent().parent().parent().parent().parent().next(), price:priceParseInt($(this).parent().parent().parent().parent().prev().text()) }); } );	
	//$('#promo-table .table-cost').each(function() { var thistext = $(this).text(); $(this).text(ownformat(thistext)); });									  
}

function buttons_bind() {
	
	/*$('.promo-next').bind('click', function() {
		$.get('index.php?id=7&start=1',function(data){
			if (ie6==0) $('#promo-table').fadeOut(500);
			else $('#promo-table').hide();
			if ($.browser.msie) { $('.bottom-nav').hide(); }
			setTimeout(function() { 
				$('#promo-table').html(data); 
				if (ie6==0) $('#promo-table').fadeIn(500); 
				else $('#promo-table').show();
				if ($.browser.msie) $('.bottom-nav').show();
			}, 500);
			setTimeout(function() { reinit(); $('#scrollable').css('top','0px'); }, 500);
		});
		return false;
	});*/	
	
}

$(document).ready(function(){
// scroll

     var tmp = new Array();
     var tmp2 = new Array();
     var param = new Array();

	 var get = location.search;	// строка GET запроса
 	 if (get != '') {
		tmp = (get.substr(1)).split('&');	// разделяем переменные
		for(var i=0; i < tmp.length; i++) {
			tmp2 = tmp[i].split('=');		// массив param будет содержать
			param[tmp2[0]] = tmp2[1];		// пары ключ(имя переменной)->значение
		}
	}
	if (param['page']>0) {
	startpage=param['page'];
	page=param['page'];
	}
	//alert(page);

	$('#scrollable').scroll( function(e) { 
		 if (this.scrollHeight<=($(this).scrollTop()+$(this).innerHeight()+200) && AjaxScroll==0) {
		 	AjaxScroll = 1;
			page++;
			pages = page*perpage;
			startpages = (page-startpage)*perpage;
			var nextlink = 'index.php?id=7&start='+pages;
			
			if ((page-startpage)==5) $('#promo-table table').append("<tr class='loading-tr'><td colspan='8' align='center'><a href='?page="+page+"' class='topLink'>Смотреть список дальше</a></td></tr>");
			else {
				$('#promo-table table').append("<tr class='loading-tr'><td colspan='8' align='center'>Подождите, пожалуйста, список загружается ...<br /><img src='images/loadingAnimation.gif' alt='Загрузка ...'></td></tr>");
				$.get(nextlink,function(data){
					$('.loading-tr').remove();
					$('#promo-table table').append(data);
					$('#promo-table .toCount:gt('+(startpages-1)+')').each(function() { $(this).count({counter:$(this).parent().parent().parent().parent().next(), price:priceParseInt($(this).parent().parent().parent().parent().prev().text()) }); } );	
					tb_init('.table-foto:gt('+(startpages-1)+') a.thickbox');
					//$('.table-foto a').hide();
					
					var options = {counterField: 0,style: 'default'};
					$('.shk-item:gt('+(startpages-1)+')').shopkeeper(options);
					
					//reinit();
					//setCartActions();
					if (data.length<=50) { 
						$('#promo-table table').append("<tr class='loading-tr'><td colspan='8' align='center'><a href='#' class='pseudo topLink'>Вверх</a></td></tr>"); 
						$('.topLink').click(function() { $("div#scrollable").scrollTop(0); return false; }); 
					}
					else AjaxScroll = 0;
				});
			}
		 }
	});

	if ($.browser.msie && parseInt(jQuery.browser.version)==6) ie6=1;
	//else $('#scrollable').jScrollPane({animateTo:true, showArrows:true, scrollbarWidth:17});
	//$('#promo-table tr:gt(1):even').css('background','#fff');
	$('#promo-table .toCount').each(function() { $(this).count({counter:$(this).parent().parent().parent().parent().next(), price:priceParseInt($(this).parent().parent().parent().parent().prev().text()) }); } );	
	//$('#promo-table .table-cost').each(function() { var thistext = $(this).text(); $(this).text(ownformat(thistext)); });
	
	//buttons_bind();
	
	//positioning center
	if (!($.browser.msie && parseInt(jQuery.browser.version)<=7)) {
		var number_of_moto = parseInt(($(window).width()-40)/$('.shop li').outerWidth());
		$('.shop ul').each(function() {
			//var shophtml = $(this).html();
			//$(this).html('');
			//$('.shop .align_center_to_left .align_center_to_right').prepend(shophtml);
			len = $('li',this).length;
			for (var j=number_of_moto-1; j<len; j=j+number_of_moto) {
				$('li:eq('+j+')',this).after("<br clear='all' /><div class='line2'>&nbsp;</div>");
			}
			//if (len/number_of_moto!=parseInt(len/number_of_moto)) $('li:last',this).after("<br clear='all' /><div class='line2'>&nbsp;</div>");
			//$('li:eq(0)',this).after("<br clear='all' /><div class='line2'>&nbsp;</div>");
			//$('.shop li:lt('+(number_of_moto)+')').wrapAll("<ul></ul>");
			//$('.shop li:lt('+(number_of_moto*2)+'):gt('+(number_of_moto-1)+')').wrapAll("<ul></ul>");
			//$('.shop li:lt('+(number_of_moto*3)+'):gt('+(number_of_moto*2-1)+')').wrapAll("<ul></ul>");
		});
	}
	
    var thistext = $('#shopCart .cart-price').text();
    if (thistext) $('#shopCart .cart-price').text(ownformat(thistext));

	tb_init('a.thickbox, area.thickbox, input.thickbox');//pass where to apply thickbox
	imgLoader = new Image();// preload image
	imgLoader.src = tb_pathToImage;
	
	$('#expand a').bind('click',function() {
		//var scrhtml = $('#scrollable').html();
    	//$('#promo-main').empty();
    	//$('#promo-main').append('<div id="scrollable">'+scrhtml+'</div>');
		//$('#scrollable').attr('style','');
		if (resized==0) {
			$('#scrollable').height(newheight3); 
			//$('.jScrollPaneContainer').animate({ height: newheight2+"px" }, time ); 
			//$('#promo-main').height(newheight2); 
			$('#promo-center').height(newheight); 
			//setTimeout(function() {
				//if (ie6==0) $('#scrollable').jScrollPane({animateTo:true, showArrows:true, scrollbarWidth:17});
				//reinit();
				//$('#promo-table input').each(function() { $(this).animateCount({counter:$(this).parent().next().next(), price:priceParseInt($(this).parent().prev().text()) }); } );	
				$('.shop').hide();
				$(this).html('<img src="images/anti-expand.jpg" alt="Свернуть" title="Свернуть" border="0" />');
				//buttons_bind();
				resized=1;
			//	}, 0);
		}
		else {
			$('#scrollable').height(origheight3); 
			//$('.jScrollPaneContainer').animate({ height: origheight2+"px" }, time );
			//$('#promo-main').height(origheight2); 
			$('#promo-center').height(origheight); 
			//setTimeout(function() {
				//if (ie6==0) {
				//	if ($.browser.safari || $.browser.opera) $('#scrollable').jScrollPane({animateTo:true, showArrows:true, scrollbarWidth:17, innerWidth: true});
				//	else $('#scrollable').jScrollPane({animateTo:true, showArrows:true, scrollbarWidth:17});
				//}
				//reinit();
				//$('#promo-table input').each(function() { $(this).animateCount({counter:$(this).parent().next().next(), price:priceParseInt($(this).parent().prev().text()) }); } );	
				$('.shop').show();
				$(this).html('<img src="images/expand.jpg" alt="Развернуть" title="Развернуть" border="0" />');
				//buttons_bind();
				resized=0;
				
			//	}, time);
		}
		return false;
	});
});

/*$(window).resize(function(){
	if (!($.browser.msie && parseInt(jQuery.browser.version)==7)) {
		var number_of_moto = parseInt(($(window).width()-40)/$('.shop li').outerWidth());
		var shophtml = $('.shop ul').html();
		$('.shop ul').remove();
		$('.shop .align_center_to_left .align_center_to_right').html(shophtml);
		$('.shop li:eq('+(number_of_moto)+')').after("<br clear='all' /><div class='line2'>&nbsp;</div>");
		$('.shop li:eq(0)').after("<br clear='all' /><div class='line2'>&nbsp;</div>");
		$('.shop li:lt('+(number_of_moto)+')').wrapAll("<ul></ul>");
		$('.shop li:lt('+(number_of_moto*2)+'):gt('+(number_of_moto-1)+')').wrapAll("<ul></ul>");
		$('.shop li:lt('+(number_of_moto*3)+'):gt('+(number_of_moto*2-1)+')').wrapAll("<ul></ul>");
		tb_init('.shop li input.thickbox');
	}
});*/


