var ua=navigator.userAgent.toLowerCase();
var KO = ua.match(/konqueror\/(\d+)/);
var KO3 = KO && KO[1] == 3;
//var SA = ua.match(/safari\/(\d+)/)
//var SA1 = SA && SA[1]<86;
//KO=!!KO; SA=!!SA;
//var MAC = /mac/.test(ua);
//var OP = !!self.opera;
//var WEBTV = /webtv/.test(ua);
//var IE = /msie/.test(ua) && !OP && !WEBTV && !SA && !KO;
//var GK = /gecko/.test(ua)
//var OP8 = /opera 8/.test(ua) && OP;
//var NN4 = IE4 = NS6 = false;
//var IE5 = IE && ![].push && !IE4;

/*********** Big Image View ***********/

var bidiv = null;
var bitable = null;
var bidiv_l = 0;
var bidiv_t = 0;
var bimg = null;

var biwidth = null;
var biheight = null;

/*
var winw = 0;
var winh = 0;
var scrollw = 0;
var scrollh = 0;
*/
var winsize = null;

var img_cash = new Image;
var timo = null;

var resw = 0;
var resh = 0;

function get_win_size(w)
{
	if ( w.innerWidth ) {
		return [w.innerWidth-16, w.innerHeight, w.pageXOffset, w.pageYOffset];
	} else if ( w.document.compatMode=='CSS1Compat' ) {
		with ( w.document.documentElement ) {
		    return [clientWidth,clientHeight,scrollLeft,scrollTop];
        }
	} else {
		with ( w.document.body ) {
		    return [clientWidth,clientHeight,scrollLeft,scrollTop];
        }
    }
}

function show_big_img(img_url)
{
    if ( !bidiv ) {
        bidiv = document.getElementById('big_img_div');
        if ( !bidiv ) return;
    }

    if ( !bitable ) {
        bitable = document.getElementById('big_img_table');
        if ( !bitable ) return;
    }

    if ( !biwidth ) {
        biwidth = document.getElementById('bi_width');
        if ( !biwidth ) return;
    }

    if ( !biheight ) {
        biheight = document.getElementById('bi_height');
        if ( !biheight ) return;
    }

    if ( !bimg ) {
        bimg = document.getElementById('big_img');
        if ( !bimg ) return;
    }

    winsize = get_win_size(window);
    bidiv_t = winsize[3] + parseInt((winsize[1] - 100) / 2);
    bidiv_l = winsize[2] + parseInt((winsize[0] - 100) / 2);


    bidiv.style.zIndex = 65535;
    bidiv.style.top = bidiv_t;
    bidiv.style.left = bidiv_l;
    bidiv.style.width = '100px';
    bidiv.style.height = '100px';
    bidiv.style.visibility = "visible";

    bimg.src = '/img/shim.gif';
    bimg.width = 32;
    bimg.height = 32;

    biwidth.width = 84;
    biheight.height = 72;

    img_cash.onload = display_img;
    img_cash.src = img_url;
}

function display_img()
{
    hide_top_controls()

    if ( !bidiv ) return;
    if ( !bitable ) return;
    if ( !biwidth ) return;
    if ( !biheight ) return;
    if ( !bimg ) return;

    setTimeout('resize_img_div()', 20);

    resw = img_cash.width;
    resh = img_cash.height;
}

function resize_img_div()
{
    if ( bidiv.style.visibility != "visible" ) return;

    var w = parseInt(biwidth.width) + 10;
    var h = parseInt(biheight.height) + 10;

    if ( w > resw ) w = resw;
    if ( h > resh ) h = resh;

    if ( biwidth.width != w ) {
        biwidth.width = w;
        if ( bidiv_l > 0 ) {
            bidiv_l = parseInt((winsize[0] - w - 16) / 2);
            if ( bidiv_l < 0 ) bidiv_l = 0;
            bidiv_l += winsize[2];
            bidiv.style.left = bidiv_l + 'px';
        }
    }

    if ( biheight.height != h ) {
        biheight.height = h;
        if ( bidiv_t > 0 ) {
            bidiv_t = parseInt((winsize[1] - h - 28) / 2);
            if ( bidiv_t < 0 ) bidiv_t = 0;
            bidiv_t += winsize[3];
            bidiv.style.top = bidiv_t + 'px';
        }
    }

    if ( w < resw || h < resh )
    {
        setTimeout('resize_img_div()', 20);
    }
    else
    {
        bimg.src = img_cash.src;
        bimg.width = img_cash.width;
        bimg.height = img_cash.height;
    }
}

function hide_big_img()
{
    show_top_controls();

    if ( !bidiv ) return;
    if ( !bitable ) return;
    if ( !bimg ) return;

    bimg.src = '/img/shim.gif';
    bimg.width = 32;
    bimg.height = 32;

    bidiv.style.zIndex = 0;
    bidiv.style.top = -1000;
    bidiv.style.left = -1000;
    bidiv.style.visibility = "hidden";
}

function hide_top_controls()
{
    hidden_top_controls = document.getElementsByTagName('select');
    for ( var i = 0; i < hidden_top_controls.length; i++  )
    {
        if ( hidden_top_controls.item(i).style.visibility = "visible" )
        {
            hidden_top_controls.item(i).style.visibility = "hidden";
        } else {
            hidden_top_controls.item(i) = null;
        }
    }
}

function show_top_controls()
{
    if ( !hidden_top_controls ) return;

    for ( var i = 0; i < hidden_top_controls.length; i++  )
    {
        if ( hidden_top_controls.item(i) ) {
            hidden_top_controls.item(i).style.visibility = "visible";
        }
    }

    hidden_top_controls = null;
}

/*********** PopUp div ***********/

var popupdiv = null;
//var popuptable = null;
var popupwidth = null;
var popupheight = null;
var popupcontent = null;

var popupdiv_l = 0;
var popupdiv_t = 0;
var popupdiv_w = 0;
var popupdiv_h = 0;

function show_popup_div(w, h)
{
    if ( !popupdiv ) {
        popupdiv = document.getElementById('popup_div');
        if ( !popupdiv ) return;
    }
    if ( !popupwidth ) {
        popupwidth = document.getElementById('popup_width');
        if ( !popupwidth ) return;
    }

//    if ( !popupheight ) {
//        popupheight = document.getElementById('popup_height');
//        if ( !popupheight ) return;
//    }

    if ( !popupcontent ) {
        popupcontent = document.getElementById('popup_content');
        if ( !popupcontent ) return;
    }

    winsize = get_win_size(window);
    popupdiv_t = winsize[3] + parseInt((winsize[1] - 100) / 2);
    popupdiv_l = winsize[2] + parseInt((winsize[0] - 100) / 2);


    popupdiv.style.zIndex = 65535;
    popupdiv.style.top = bidiv_t;
    popupdiv.style.left = bidiv_l;
    popupdiv.style.width = '100px';
    popupdiv.style.height = '100px';
    popupdiv.style.visibility = "visible";

    popupwidth.width = 84;
    popupcontent.style.height = 72;

    popupdiv_w = w;
    popupdiv_h = h;

    hide_top_controls();

    setTimeout('resize_popup_div()', 20);
}

function resize_popup_div()
{
    if ( popupdiv.style.visibility != "visible" ) return;

    var w = parseInt(popupwidth.width) + 10;
    var h = parseInt(popupcontent.style.height) + 10;

    if ( w > popupdiv_w ) w = popupdiv_w;
    if ( h > popupdiv_h ) h = popupdiv_h;

    if ( popupwidth.width != w ) {
        popupwidth.width = w;
        if ( popupdiv_l > 0 ) {
            popupdiv_l = parseInt((winsize[0] - w - 16) / 2);
            if ( popupdiv_l < 0 ) popupdiv_l = 0;
            popupdiv_l += winsize[2];
            popupdiv.style.left = popupdiv_l + 'px';
        }
    }

    if ( parseInt(popupcontent.style.height) != h ) {
        popupcontent.style.height = h + "px";
        if ( popupdiv_t > 0 ) {
            popupdiv_t = parseInt((winsize[1] - h - 28) / 2);
            if ( popupdiv_t < 0 ) popupdiv_t = 0;
            popupdiv_t += winsize[3];
            popupdiv.style.top = popupdiv_t + 'px';
        }
    }

    if ( w < popupdiv_w || h < popupdiv_h )
    {
        setTimeout('resize_popup_div()', 20);
    }
    else
    {
        popupcontent.style.visibility = "visible";
        popupcontent.style.width = popupdiv_w + "px";
        popupcontent.style.height = popupdiv_h + "px";
    }
}

function hide_popup_div()
{
    show_top_controls();

    if ( !popupdiv ) return;
    if ( !popupcontent ) return;

    popupcontent.style.visibility = "hidden";
    popupcontent.style.width = "32px";
    popupcontent.style.height = "32px";

    popupdiv.style.zIndex = 0;
    popupdiv.style.top = -1000;
    popupdiv.style.left = -1000;
    popupdiv.style.visibility = "hidden";
}

/*********** Shoping Cart ***********/

function add_shopping_cart_item(id, type)
{
    var tmp = (new Date()).getTime();
    var js = document.createElement('script');
    js.id = 'product_' + type + '_' + id;
    js.type = "text/javascript";
    js.charset = "windows-1251";
    js.src = '/shopping/?iid=' + id + '&type=' + type + '&tmp=' + tmp;
    document.body.appendChild(js);
}

function count_shopping_cart_items(cnt)
{
    document.getElementById('shopping_cart_items').innerHTML = cnt;
}

function shopping_done(id)
{
    var prod_cell = document.getElementById('cell_product_' + id);
    if ( prod_cell ) {
        prod_cell.innerHTML = cell_shopping_done;
        prod_cell.className = 'basketdone';
    }
}

/*********** Search form ***********/

function check_search_value(frm)
{
    if ( frm.fld_search.value == 'поиск' ) {
        return false;
    }

    if ( frm.fld_search.value.length == 0 ) {
        frm.fld_search.focus();
        alert('Поле "Поиск" должно быть заполненно.');
    }

    return true;
}


