
var number_format = /^\d*$/
var startTimer;
function startCheckGoodsLimit(evt,field_id) {
    var keyCode = window.event ? window.event.keyCode : evt.which;
	var evtobj=window.event? event : field_id 
	var key = evtobj.charCode? evtobj.charCode : evtobj.keyCode
	key = key ? key : keyCode;
    clearTimeout(startTimer);
    startTimer = setTimeout("checkGoodsLimit("+key+","+field_id+")",500);
}

function checkGoodsLimit(key,field_id){
    clearTimeout(startTimer);
    var value = $("#id_"+field_id).attr('value');
    var limit = parseInt($("#remains_"+field_id).html());
    if ( value > limit ){
        $("#id_"+field_id).attr('value',1);
        alert("Вы не можете заказать большее количество товара, чем в наличии. Укажите верное значение.");
		return;
    }
    else if( number_format.test(value) == false && value != '' && value != undefined ){
        $("#id_"+field_id).attr('value',1);
        alert("Неверный формат.");
		return;
    }
	if (key == '13' && !doit) {
		doit=true;
        editBasket();
    }
}

function submitCat(field_id) {
   //distinguish between IE's explicit event object (window.event) and Firefox's implicit.
    if (key == '13' ) {
        editBasket();
    }
}

//gallery script for immer-club
$(document).ready(function(){
	
	$('.photoGallery a').click(function(){
		showPhoto(this);
		return false;
	})
})

function onkey(evt,id,amount){
    var evtobj = window.event ? event : id //distinguish between IE's explicit event object (window.event) and Firefox's implicit.
    var unicode = evtobj.charCode? evtobj.charCode : evtobj.keyCode
	var keyCode = window.event ? window.event.keyCode : evt.which;
	var unicode = window.event ? window.event.keyCode : evtobj.which;
	unicode = unicode ? unicode : keyCode;
    if (unicode == '13') {
		$('.popupImgBtn').click();
		return false;
    }
}

function killbill(evt){
	var evtobj = event  //distinguish between IE's explicit event object (window.event) and Firefox's implicit.
    var unicode = evtobj.charCode? evtobj.charCode : evtobj.keyCode
	var keyCode = window.event ? window.event.keyCode : evt.which;
	var unicode = window.event ? window.event.keyCode : evtobj.which;
	unicode = unicode ? unicode : keyCode;
    if (unicode == '13') 
		return false;
	else
		return true;
}

function showPhoto(a) {
    a = $(a);
    ps_title = a.attr('title');
    ps_name = a.attr('name');
    ps_set = a.attr('rel');
    ps_currentPhoto = $('.photoGallery a[rel="'+ps_set+'"]').index(a);
    ps_setPhotos = $('.photoGallery a[rel="'+ps_set+'"]').length;
    var good_id = str_replace('cphoto_', '', a.attr('id'));
    if ( good_id.search('m') != '-1' ){
        good_id = str_replace('m', '', good_id);
        g_mark_items = 'y';
    }
    else{
        g_mark_items = 'n';
    }
    var cost = str_replace('руб.', '', $('#tr_'+good_id+' .price').text());
    var amount = str_replace('шт.', '', $('#tr_'+good_id+' .priceopt').text());
    var basket_num = $('#tr_'+good_id+' .amount input').attr('value');

    popup = $('<div class="photoPopup" id="photoPopup" style="text-align:left;">'+
    '	<span class="p-nav-close">&times;</span>'+
    '	<img src="/img/des/loader.gif" alt="" class="loader" />'+
    '	<img src="/img/des/spacer.gif" alt="" class="image" />'+
    '   <div class="title">'+ps_title+'</div>'+
    '	<div class="pnk"><span class="grey">Цена:</span> <span class="pa_cost">'+cost+'</span> <span class="grey">руб.</span> <span class="presencegrey">В наличии:</span> '+amount+' <span class="grey">шт.</span> <form class="makePopupOrder" action=""><table border=0 cellpadding=0 cellspacing=0><tr><td><label for="mo-order">Заказать</label></td><td><input type="text" class="textInput" id="mo-order" value="'+basket_num+'" onkeyup="onkey(event,\''+good_id+'\',this.value)" onkeypress="return killbill(event)" onchange="checkGoodsLimitPopup(this.value,\''+amount+'\')" /></td><td><input onclick="editBasketPopup(\''+good_id+'\'); closePhoto(popup,blind); showPopup(\'commentPopup\'); return false" class="popupImgBtn" type="image" src="/img/des/popupcart.gif" ></td></tr></table></form></div>'+
    '	<span class="g_photo_n">'+(ps_currentPhoto+1)+'</span>'+
    '	<span class="g_qnt">'+ps_setPhotos+'</span>'+
    '</div>')
    blind = $('<div class="blind"></div>');
    pageHeight = '150%';
    blind.height(pageHeight).click(function(){
        closePhoto(popup,blind);
        return false;
    }).appendTo('body');

    popup.appendTo('body');
    setCenter(popup);
    popup.show();

    if (!$.support.opacity) {
        $('.textInput').focus(function(){
        $(this).addClass('textInputFocus');
        });
        $('.textInput').blur(function(){
        $(this).removeClass('textInputFocus');
        })
        };

    loadPhoto(ps_set,ps_currentPhoto,popup);
    popup.find('.p-nav-close').click(function(){
        closePhoto(popup,blind);
        return false;
    })

    return false;
}
function checkSequence(ps_currentPhoto,ps_setPhotos) {
	if (ps_currentPhoto < 0) {
		return ps_setPhotos-1;
	} else if (ps_currentPhoto >= ps_setPhotos) {
		return 0;
	} else {
		return ps_currentPhoto;
	}
}
function setCenter(item) {
	windowHeight = document.documentElement.clientHeight;
	currentOffset = document.documentElement.scrollTop || document.body.scrollTop;
	currentOffset = currentOffset + parseInt((windowHeight - ($(item).height()+148)) / 2);
	pLeft = $(item).width() / 2;
	$(item).css({top:currentOffset,marginLeft:-pLeft}).show();
}
function loadPhoto(ps_set,ps_currentPhoto,popup){
	img = popup.find('.image');
	nav = popup.find('.nav');
	loader = popup.find('.loader');
	old_width = popup.width();
	old_height = popup.height();
	popup.css({
		width:old_width,
		height:old_height
	});
	src = $('.photoGallery a[rel="'+ps_set+'"]:eq('+ps_currentPhoto+')').attr('href');
    src = '/copy.php?image='+src;
	pv = $('.photoGallery a[rel="'+ps_set+'"]:eq('+ps_currentPhoto+')').hasClass('videoLink');
	//ps_title = $('.photoGallery .thumbs a[rel="'+ps_set+'"]:eq('+ps_currentPhoto+')').attr('title');
	//popup.find('.info span').html(ps_title);
	if (!pv) {
		img.animate({
			opacity:0
		},200,function(){
			popup.find('.title').html( $('.photoGallery a[rel="'+ps_set+'"]:eq('+ps_currentPhoto+')').attr('title') );
			popup.find('.name').html( $('.photoGallery a[rel="'+ps_set+'"]:eq('+ps_currentPhoto+')').attr('name') );
			
			var title_height = popup.find('.name').height();
			
			img.remove();
			img = $(new Image());
			img.load(function(){
				img.appendTo(popup);
				img_width = img.width();
				img_height = img.height();
				n_width = (img_width < 300) ? 300-old_width : (img_width)-old_width;
				n_height = title_height + ((img_height < 120) ? 120-old_height : (img_height)-old_height);
				n_left = parseInt(img_width/2);
				n_top = parseInt((n_height/2));
				loader.hide();
				popup.animate({
					width:'+='+n_width+'px',
					height:'+='+n_height+'px',
					marginLeft:-n_left,
					top:'-='+n_top+'px'
				},200,function(){
					popup.find('.g_photo_n').html(ps_currentPhoto+1);
					img.fadeIn(200);
				})
				
			}).addClass('image').hide().attr('src',src);

			loader.show();
		});
	} 
}
function closePhoto(popup,blind){
	popup.remove();
	blind.remove();
}
