/* top menu */
function initCat(menuID) {
	var mid = document.getElementById(menuID);
	var arr = mid.getElementsByTagName('div');
	var i=0;
	for (i=0;i<arr.length;i++) {
		if (arr[i].className=="submenu") arr[i].style.display = "none";
		else if (arr[i].className=="submenu_sel") arr[i].style.display = "block";
	}
}
function openCat(cat_id) {
	//this.className = (this.className == "item") ? "item_chosen" : "item";
	var idcat = document.getElementById(cat_id);
	idcat.style.display = (idcat.style.display == "none") ? "block" : "none";
}

isNS4 = (document.layers) ? true : false;
isIE4 = (document.all && !document.getElementById) ? true : false;
isIE5 = (document.all && document.getElementById) ? true : false;
isNS6 = (!document.all && document.getElementById) ? true : false;

cm=null;
show_delay=100;
hide_delay=500;
tstat=1;

// new
var opentimer, closetimer;
var opened = null;

function getelementbyid(myid) {
   if (isNS4){
        objElement = document.layers[myid];
     }else if (isIE4) {
        objElement = document.all[myid];
     }else if (isIE5 || isNS6) {
             objElement = document.getElementById(myid);
     }
return(objElement);
}

function showsub(el,m) {
    if (m!=null) {
        if (opened!=null) {
            getelementbyid(opened).style.display = 'none';
        }
        opened=m;
        m=getelementbyid(m);
        m.style.display = 'block';
    }
    else {
        if (getelementbyid(opened)) getelementbyid(opened).style.display = 'none';
        opened = null;
    }
}
function opensub(el,showed_id) {
    //el.className="item1_sel";
    if (closetimer) clearTimeout(closetimer);
    opentimer=setTimeout("showsub(this,\'"+showed_id+"\')",show_delay);
}
function closesub(el,showed_id) {
    if (opentimer) {
        clearTimeout(opentimer);
    }
    closetimer=setTimeout("showsub(this,null)",hide_delay);
}
function cancelclose(showed_id) {
    if (opened == showed_id) {
        if (closetimer) clearTimeout(closetimer);
    }
}
/* end of top menu */

if (window.jQuery /*&& window.jQuery.fancybox*/) {
    $(function () {
        $('#atobasket').click(addToBasket);
        photoGalery();
    });
    
    function addToBasket() {
        var ta = $(this);
        //        ta.toggleClass('proccess');
        if (!ta.hasClass('proccess')) {
            ta.addClass('proccess');
            $.ajax('/ajax/basket/add.html', {
                type: 'POST',
                dataType: 'json',
                data: $('#formtobasket').serialize(),
                success: function (data, textStatus) {
                    switch (data.status) {
                        case 'ok':
                            if ("update" in data) {
                                for (var did in data.update) {
                                    $('#'+did).html(data.update[did]);
                                }
                            }
                            if ("javascript" in data) {
                                $.globalEval(data.javascript.toString());
                            }
                            this.activeRequest = null;
                            break;
                        case 'error':
                            alert(data.errormessage);
                            break;
                    }
                    ta.removeClass('proccess');
                }
            });
        }
        return false;
    }
    
    var tecGalIndex = 0;
    var movingElement = null;
    
    function photoGalery() {
        var sheight = $('#galleryphoto').height();
        var allwidth = 0;
        $('#galleryphoto a').wrap('<div class="phgallery" />').fancybox({onClosed : onClosedFancybox}).each(function(index) {
            var t = $(this);
            t.parent().css({'paddingTop': ((sheight-t.height())/2)+'px'});
            allwidth += t.width();
        });
//        $('div.phgallery').height(sheight);
//        $('div.phgallery');
        movingElement = $('#inphoto');
        movingElement.css({'position' : 'relative'});
        movingElement.width(allwidth*1.5);
        $('#goLeft').click(goLeft);
        $('#goRight').click(goRight);
        moveToElement(tecGalIndex);
    }
    
    function goLeft() {
        if (movingElement) {
            // поиск следующиего
            var elts = $('#galleryphoto div.phgallery');
            if (tecGalIndex < elts.length-1) {
                moveToElement(++tecGalIndex);
            } else {
//                alert('no left');
            }
        }
        return false;
    }
    function goRight() {
        if (movingElement) {
            // поиск следующиего
            
            if (tecGalIndex >0) {
                moveToElement(--tecGalIndex);
            } else {
//                alert('no right');
            }
        }
        return false;
    }
    function moveToElement(index){
        var elts = $('#galleryphoto div.phgallery');
        var w = $('#galleryphoto').width() - $(elts[index]).outerWidth();
        w /= 2;
        w = $(elts[index]).position().left - w;
        movingElement.animate({'left': (-w)+'px'});
    }
    
    function onClosedFancybox (arr, index, opts) {
        tecGalIndex = index;
        moveToElement(tecGalIndex);
    }
    
}

	var winH;
	var winW;
	var winUrl;
	var winProp;
	var scrol;

function popUpOpen (winUrl, winW, winH)
{
//функция открывает заданный файл в заданных размерах окна
		scrol = 1;
		winProp = "toolbar=0,resizable=0,fullscreen=0,scrollbars="+scrol+",left=50,top=50,height="+winH+",width="+winW;
		window.open(winUrl,'',winProp);
}

function popUpOpenNS (winUrl, winW, winH)
{
//функция открывает заданный файл в заданных размерах окна без скроллинга
		scrol = 0;
		winProp = "toolbar=0,resizable=0,fullscreen=0,scrollbars="+scrol+",left=50,top=50,height="+winH+",width="+winW;
		window.open(winUrl,'',winProp);
}

function popUpOpenARS (winUrl)
{
//функция открывает заданный файл в окне с возможностью ресайза.
		winProp = "width=300,height=200,scrollbars=no,left=50,top=50, toolbar=no,location=no,status=no,resizable=yes,screenX=50,screenY=50";
		window.open(winUrl,'',winProp);
}

function callmeform() {
    var name = document.getElementById('callmename').value;
    var phone = document.getElementById('callmephone').value;
    if (!phone || phone == 'Ваш телефон') {
        alert('Заполните, пожалуйста, ваш телефон');
        return;
    }
    if (!name || name == 'Ваше имя') {
        alert('Заполните, пожалуйста, ваше имя');
        return;
    }
    document.getElementById('callmeform').submit();
}
function subscribeform() {
    var name = document.getElementById('subformname').value;
    var email = document.getElementById('subformmail').value;
    if (!email || email == 'Ваш e-mail') {
        alert('Заполните, пожалуйста, ваш e-mail');
        return;
    }
    if (!name || name == 'Ваше имя') {
        alert('Заполните, пожалуйста, ваше имя');
        return;
    }
    document.getElementById('signform').submit();
}
