/*
window.onerror = function(){
  return true;
}
*/
var el_width=200;
var fxes=new Array(3);
window.addEvent('domready',function(){
    if ($($('out_center')))
    {
        var vs=$$('div[id^=div_vitrina_]');
        var wd=Math.floor((($('out_center').getSize().x)/el_width))*el_width;
        vs.each(function(v){
            var cab=v.getElements('div[id^=vitrina_]');
            cab.each(function(item,index){
                item.setStyle('width',wd+'px');
                var b=item.getChildren('div')[0];
                fxes[item.get('id')]=b.get('tween',{
                    property:'margin-left',
                    link:'ignore'
                });
                b.setStyle('width',b.childElementCount*el_width);
                var l=v.getChildren('a[id$=_2]')[0];
                l.setStyle('visibility',(b.getSize().x<=item.getSize().x)?"hidden":"visible");
            });
        });
    }
});
window.addEvent('resize',function(){
    var cab=$$('div[id^=vitrina_]');
    var wd=Math.floor((($('out_center').getSize().x)/el_width))*el_width;
    cab.each(function(item,index){
        item.setStyle('width',wd+'px');
    });
});
function rotate_vitrina(type,direction,wdth){
    var vt=$('vitrina_'+type);
    var v=vt.getChildren('div')[0];
    var current=v.getStyle('margin-left').toInt();
    var shift=Math.floor((vt.getSize().x/el_width))*el_width;
    if(direction==2){
        $('vr_'+type+'_1').setStyle('visibility',(current-shift>=0)?"hidden":"visible");
        $('vr_'+type+'_2').setStyle('visibility',(Math.abs(current-shift)>=wdth-shift)?"hidden":"visible");
        fxes['vitrina_'+type].start((Math.abs(current-shift-shift)>wdth)?-wdth+shift+'px':current-shift+'px');
    }
    else{
        $('vr_'+type+'_1').setStyle('visibility',(current+shift>=0)?"hidden":"visible");
        $('vr_'+type+'_2').setStyle('visibility',(Math.abs(current+shift)>=wdth-shift)?"hidden":"visible");
        fxes['vitrina_'+type].start((current+shift>0)?0:current+shift+'px');
    }

}
function check_form_order(obj)
{

    if ( obj.user_phone.value == '' || obj.first_name.value=='' || obj.user_adress.value==''  )

    {
        alert( "Вы не заполнили все поля, отмеченные звездочками.");
        return false;
    }
    obj.submit();
}


function form_show(price_id) {

    var pars = $H({
        price_id:price_id
    }).toQueryString();

    var myRequest = new Request.HTML({
        url: 'ajax/form_show.php',
        method: 'get',
        update: 'form_show',
        evalScripts: true,
        onComplete: function(){
            form_amount2(price_id);
        }
    }).send(pars);

}

function form_amount2(price_id) {

    var f_id = 'f_'+price_id;
    var amount_price_id = 'amount_'+price_id;

    var myValues = $(f_id).getCoordinates();
    var y = myValues.top;
    var x = myValues.left;

    var x2 = x;
    $('form_amount').setStyle('top', y+38);
    $('form_amount').setStyle('left', x-3);
    $('form_amount').style.display = 'block';
    //document.getElementById('form_amount').style.display = 'block';
    $(amount_price_id).focus();

}

function form_close() {
    $('form_amount').style.display = 'none';
}


function showR() {

    $('layer2').fade('hide'); //Fades "myElement" out.
    $('layer2').style.display = 'block';
    $('layer2').fade('in'); //Fades "myElement" out.
    $('layer2').fade(1.0); //Fades "myElement" to 70% opacity.
}

function hideQ()
{
    $('layer2').style.display = 'none';
}

function showList()
{
    $('login_form').style.display = 'block';
    $('autoForm').login.focus();
}

function hideList()
{
    $('login_form').style.display = 'none';
}


function login_form()
{
    $('login_form').empty().addClass('ajax-loading');
    var myRequest = new Request.HTML({

        url: 'ajax/login_form.php',
        method: 'get',
        update: 'login_form',
        evalScripts: true,
        onComplete: function(response){

            $('login_form').removeClass('ajax-loading');


        }
    }).send();

    $('loginForm').login.focus();
}

/*
function login_form()
{
var myRequest = new Request.HTML({
	url: 'ajax/login_form.php',
	method: 'get',
	update: 'login_form',
	evalScripts: true,
	onComplete: function(){
 }
}).send();
}
*/


function clear_basket() {
    var myRequest = new Request.HTML({
        url: 'ajax/clear_basket.php',
        method: 'get',
        update: 'basket',
        evalScripts: true,
        onComplete: function(){ }
    }).send();
}

function add_goods(goods_id) {

    var out_msg = 'out_msg'+goods_id;  //id всей формочки

    var pars = $H({
        goods_id2:goods_id
    }).toQueryString();
    var myRequest = new Request.HTML({
        url: 'ajax/add_goods.php',
        method: 'get',
        update: 'basket',
        evalScripts: true,
        onComplete: function(){
            form_close();
        }
    }).send(pars);

    $(out_msg).style.display = 'block';
    $(out_msg).fade('out');
    $(out_msg).fade(0.0);
    $(out_msg).fade('hide');

    $(out_msg).fade(1.0);
}


function add_goods2(goods_id, code) {

    var amount_price_id = 'amount_' + goods_id;
    var amount = document.getElementById(amount_price_id);
    var amount2 = amount.value;

    //var out_msg = 'out_msg'+goods_id;  //id всей формочки

    var pars = $H({
        amount:amount2,
        goods_id:goods_id,
        code:code
    }).toQueryString();
    var myRequest = new Request.HTML({
        url: 'ajax/add_goods2.php',
        method: 'get',
        update: 'basket',
        evalScripts: true,
        onComplete: function(){
            form_close();
        }
    }).send(pars);

}












function search_insert(el) {
    var innerText = el.innerHTML;
    var SearchText = document.getElementById('name');
    SearchText.value = innerText;
}

function comment_add(id, code, comments_type) {


    var author_email = document.getElementById('author_email');
    var author_email2 = author_email.value;
    var author_name = document.getElementById('author_name');
    var author_name2 = author_name.value;
    var message = document.getElementById('message');
    var message2 = message.value;

    var random1 = document.getElementById('random');
    var random1 = random1.value;

    var random2 = document.getElementById('random2');
    var random2 = random2.value;

    var pars = $H({
        id:id,
        code:code,
        comments_type:comments_type,
        author_email:author_email2,
        author_name:author_name2,
        message:message2,
        random1:random1,
        random2:random2
    }).toQueryString();
    var myRequest = new Request.HTML({
        url: 'ajax/comment_add.php',
        method: 'post',
        update: 'out_comments',
        evalScripts: true,
        onComplete: function(){ }
    }).send(pars);
}

function vitrina(vitrina_type, owner) {
    var pars = $H({
        vitrina_type:vitrina_type,
        owner:owner
    }).toQueryString();
    var myRequest = new Request.HTML({
        url: 'ajax/vitrina.php',
        method: 'get',
        update: 'out_center',
        evalScripts: true,
        onComplete: function( ){ }
    }).send(pars);
}

function vitrina_del(type, code) {
    var pars = $H({
        type:type,
        code:code
    }).toQueryString();

    var myRequest = new Request.HTML({
        url: 'ajax/vitrina_del.php',
        method: 'get',
        update: 'div_vitrina_'+type,
        evalScripts: true,
        onComplete: function(){ }
    }).send(pars);
}

function vitrina_add(price_vitrina_type_id, code, id) {
    var pars = $H({
        type:price_vitrina_type_id,
        code:code,
        id:id
    }).toQueryString();
    var myRequest = new Request.HTML({
        url: 'ajax/vitrina_add.php',
        method: 'get',
        update: 'out_vitrina_msg',
        evalScripts: true,
        onComplete: function(){ }
    }).send(pars);
}

function watermark_add(w_id, id) {

    var pars = $H({
        w_id:w_id,
        id:id
    }).toQueryString();
    var myRequest = new Request.HTML({
        url: 'admin/ajax/watermark_add.php',
        method: 'get',
        update: 'out_watermark',
        evalScripts: true,
        onComplete: function(){ }
    }).send(pars);
}



function setChecked(obj)
{
    var str = document.getElementById("text").innerHTML;
    str = (str == "отметить" ? "снять" : "отметить");
    document.getElementById("text").innerHTML = str;

    var check = document.getElementsByName("current[]");
    for (var i=0; i<check.length; i++)
    {
        check[i].checked = obj.checked;
    }
}

if (document.images) {
    var img = new Object();
    img["bg_form_amount"] = new Image(190, 90);
    img["bg_form_amount"].src = "images/bg_form_amount.gif";
    img["bas1"] = new Image(86, 25);
    img["bas1"].src = "images/bas_ar3.gif";
    img["bas2"] = new Image(86, 25);
    img["bas2"].src = "images/bas_ar4.gif";
    img["search"] = new Image(84, 85);
    img["search"].src = "images/search_btn_h.gif";

}


function fu_form(title,div_id,panel_id, comment_text, w){
    var ua = navigator.userAgent.toLowerCase();
    var isOpera = (ua.indexOf('opera')  > -1);
    var isIE = (!isOpera && ua.indexOf('msie') > -1);


    function getBodyScrollTop()
    {
        return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
    }

    function getBodyScrollLeft()
    {
        return self.pageXOffset || (document.documentElement && document.documentElement.scrollLeft) || (document.body && document.body.scrollLeft);
    }


    function getDocumentHeight() {
        return Math.max(document.compatMode != 'CSS1Compat' ? document.body.scrollHeight : document.documentElement.scrollHeight, getViewportHeight());
    }

    function getViewportHeight() {
        return ((document.compatMode || isIE) && !isOpera) ? (document.compatMode == 'CSS1Compat') ? document.documentElement.clientHeight : document.body.clientHeight : (document.parentWindow || document.defaultView).innerHeight;
    }

    function fadeIn(elem)
    {
        if (elem)   {
            if (Browser.name == 'ie'&&elem!==null&&elem!==undefined)   $(elem.id).setStyle('display', 'block');
            else

            {
                elem.setStyles({
                    opacity:0,
                    display:'block'
                });
                elem.tween('opacity', '1');
            }
        }
    }

    function fadeOut(elem)
    {
        if (elem)  {
            if (Browser.name == 'ie'&&elem!==null&&elem!==undefined)  $(elem.id).setStyle('display','block');   else   elem.tween('opacity', '0');
        }
    }

    function sizeabley(el, height)
    {
        el.set('morph', {
            duration: '100'

        //,transition: 'bounce:in'
        });
        el.morph({
            height: height
        });
        el.get('morph');
    }

    function sizeablexy(el, width, height)
    {
        el.set('morph', {
            duration: "100"
            ,
            transition: Fx.Transitions.Circ.easeInOut
        });
        el.morph({
            height: height,
            width:width
        });
        el.get('morph');
    }

    if (div_id==undefined)    div_id = 'block_other';
    if (panel_id==undefined)  panel_id = 'block_other_panel';
    if (w==undefined)  w = 30;
    if ($(panel_id)) $(panel_id).destroy();
    if ($(div_id))      $(div_id).setAttribute("id", div_id+"_");

    var shade = new Element('div', {
        id:panel_id,
        'class': "window"
    });
    shade.setStyles({
        display:'block',
        height:getDocumentHeight()+"px",
        width:'100%',
        position:'absolute',
        left:"0px",
        top:"0px",
        'z-index':'1000'
    });
    shade.inject(document.body);



    var shade = new Element('div',{
        id:panel_id+"_shade"
    });
    shade.setStyles({
        display:'block',
        height:getDocumentHeight() +"px",
        width:'100%',
        background:'#000',
        position:'absolute',
        margin:"0 auto",
        left:"0px",
        top:"0px",
        'z-index':'1',
        opacity: 0.6
    });
    shade.inject($(panel_id));

    //sizeablexy(shade, document.body.getSize().x, getDocumentHeight());

    var trashield = new Element('div', {
        id:panel_id+"_form",
        'class':panel_id+"_form"
    });
    trashield.setStyles({
        //   display:'none',
        position:'absolute',
        zIndex:'50',
        'min-height':'150px',
        visibility:'visible',
        width:w<100?w+'%':w+"px",

        //    border:'#ccc 1px solid',
        top:getBodyScrollTop()+100+($$('.handle').length*40)+"px",
        left: document.body.getSize().x/2-((w<100)?w*document.body.getSize().x/100/2:w/2)+"px"
    });
    trashield.addEvent('destroy', function(e) {
        fadeOut($('shade'));

    });
    trashield.inject($(panel_id));


    var x = (w<100)?window.getSize().x*w/100:w;

    $(panel_id+"_form").innerHTML = "<div id='"+panel_id+"_wrap' style='opacity:1;margin:0 auto;width:"+w+"px;overflow:hidden;'>"+
    "<table id='"+panel_id+"_table' style='background:#eee;height:100%;width:100%;'><tr><td id='"+panel_id+"_top' style=\"height:30px; background: #eee;filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#eeeeee', endColorstr='#aaa');background: -webkit-gradient(linear, left top, left bottom, from(#eee), to(#aaa));background: -moz-linear-gradient(top,  #eee,  #aaa);\"></td></tr>"+
    "<tr><td style='vertical-align:top'><div style='overflow:auto;' id='"+panel_id+"_container' class='window_container'></div></td></tr></table></div>";


    var handle = new Element('div', {
        id:panel_id+"handle",
        'class':"handle",
        style:'overflow:hidden; '
    });
    handle.inject($(panel_id+"_top"));

    var artform_title = new Element('span', {
        id:'artform_title',
        'class':"artform_title",
        style:'float: left; padding: 4px 0 0 5px;'
    });
    artform_title.innerHTML = "<h3 style='color:#333;padding:0px; text-shadow:1px 1px 1px #fff; '>"+title+"</h3>";
    artform_title.inject($(panel_id+"handle"));
    var comment = new Element('div', {
        id:'form_comment'
    });
    comment.setStyles({
        display:'block',
        background: '#ccc',
        color: '#333',
        'margin':'10px 0 0   0',
        padding:'0 0 0 5px',
        'text-shadow':'1px 1px 1px #fff'
    });
    comment.innerHTML=comment_text;
    comment.inject($(panel_id+"_top"));
    var artform_close = new Element('img', {
        id:'artform_close',
        src:'images/icons/close.png',
        style:'float: right;padding:5px 5px 0 0; position:relative; z-index:10;'

    });
    artform_close.addEvent('mouseup', function(e) {

        fadeOut($(panel_id));
        setTimeout(function(){
            $(panel_id).destroy();
        },1000);

        if ($(div_id+"_")) {
            $(div_id+"_").setAttribute("id", div_id);
        }
    });
    artform_close.inject($(panel_id+"handle"));



    var catalog_out = new Element('div', {
        id:div_id
    });
    catalog_out.setStyles({
        display:'block',
        visibility:'visible',
        overflow:'auto',
        margin:'0px 0px -10px 0px',
        position:'relative'
    });
    catalog_out.inject($(panel_id+"_container"));

    var effect = false;
    if (!effect)
        var ij = setInterval(function(){
            if ($(div_id))
            {
                var y= $(div_id).getSize().y;
                $(panel_id+'_form').setStyle('height',y+$(panel_id+'_top').getSize().y+"px");
                $(panel_id+'_container').setStyle('height',y+18+"px");
                $(panel_id+'_wrap').setStyle('height',$(panel_id+'_table').getSize().y-18+"px");

                var top=getBodyScrollTop()+100+($$('.handle').length*40);
                if (getViewportHeight()>(y+$(panel_id+'_top').getSize().y)&&$$('.window')[$$('.window').length-1].id==panel_id){
                    top=getViewportHeight()/2-(y+$(panel_id+'_top').getSize().y)/2;
                    top = getBodyScrollTop()+top;
                    $(panel_id+'_form').setStyle('top', top+"px")
                }
            }
            else clearInterval(ij);
        },10);
    else

        var ij = setInterval(function(){
            if ($(div_id)&&$$('.window')[$$('.window').length-1].id==div_id)
            {
                var y= $(div_id).getSize().y;
                sizeabley( $(panel_id+'_form'),y+50);
                sizeabley( $(panel_id+'_container'),y+18) ;
                sizeablexy($(panel_id+'_wrap'),$(panel_id+'_table').getSize().x, $(panel_id+'_table').getSize().y-18);
                var top=getBodyScrollTop()+100+($$('.handle').length*40);
                if (getViewportHeight()>(y+$(panel_id+'_top').getSize().y)&&$$('.window')[$$('.window').length-1].id==panel_id){
                    top=getViewportHeight()/2-(y+$(panel_id+'_top').getSize().y)/2;
                    top = getBodyScrollTop()+top;
                    $(panel_id+'_form').setStyle('top', top+"px")
                }
            }
            else clearInterval(ij);
        }, 1000);

    document.addEvent('scroll', function(){
        clearInterval(ij);
    })
}



function addScript(url) {
    var xmlhttp;

    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
            xmlhttp = false;
        };
    };

    if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
        xmlhttp = new XMLHttpRequest();
    };

    xmlhttp.open('GET', url, false);
    xmlhttp.send(null);

    if (navigator.appName=="Microsoft Internet Explorer") {
        window.execScript(xmlhttp.responseText);
    } else {
        var obj = document.createElement('script');
        var textScr = document.createTextNode(xmlhttp.responseText);
        document.body.appendChild(obj);
        obj.appendChild(textScr);
    };
}

