addEvent(window, 'load', user_init, false);

function user_init()
{
    popups_init();
    shop_init();
}

//
//
//
function mywindow(src, width, height, name, reload) {
    	if (ow && ow.close) {
	    	ow.close();
    	}
    	
        // calculate window position
        var left = (screen.width-width)/2;
        var top  = (screen.height-height)/2;
        features = 'width='+width+', height='+height+', location=no, menubar=no, personalbar=no, resizable=yes, scrollbars=yes, status=no, titlebar=yes, toolbar=no, left='+left+', top='+top;
		openedWindow = window.open(src, name, features);
		openedWindow.focus();
		if (reload) {
		    // var wopener = (openedWindow.opener)?(openedWindow.opener):(ow.opener);
		    // openedWindow.onload = alert(wopener);
		    // wopener.location.reload();
		}
        return openedWindow;
}


//
// Init popup windows for images
//
function popups_init()
{
    var ancs = document.getElementsByTagName("a");
    for (i=0; i<ancs.length; i++) {
        if ((ancs.item(i).className).indexOf("popup")!=-1) {
            // popup-width-height
            parts = ancs.item(i).className.split("-");
            addEvent(ancs.item(i), 'click', create_show_image_func(ancs.item(i).href, parts[1], parts[2]), false);
        }
    }
}

function create_show_image_func(href, width, height)
{
    return function(e) { 
        if (e.preventDefault) {
            e.preventDefault();
        }
        show_image(href, width, height); 
        return false;
    };
}

function show_image(href, width, height)
{
        var left = (screen.width-width)/2;
        var top  = (screen.height-height)/2;
        
        height = 30 + parseInt(height);
        features = 'width='+width+', height='+height+', location=no, menubar=no, personalbar=no, resizable=yes, scrollbars=no, status=no, titlebar=yes, toolbar=no, left='+left+', top='+top;
		openedWindow = window.open('/pic.php?p='+href, 'mywind', features);
		openedWindow.focus();
        return false;
}



//
// Init 'add-to-cart' functionality
//
function shop_init()
{
    if (!document.getElementsByTagName) {
        return false;
    }
    
    var inputs  = new Array();
    var tables = document.getElementsByTagName('table');
    for (i=0; i<tables.length; i++) {
        if (tables.item(i).className.indexOf('shop-items')!=-1) {
            /*
            inputs = tables.item(i).getElementsByTagName('input');
            for (j=0; j<inputs.length; j++) {
                if (inputs.item(j).className.indexOf('buy')!=-1) {
                    inputs.item(j).onclick = function() {alert('"Корзина" временно недоступна'); return false;};
                }
            }
            */
            forms = tables.item(i).getElementsByTagName('form');
            for (j=0; j<forms.length; j++) {
                if (forms.item(j).className.indexOf('buy-form')!=-1) {
                    func = create_add_function(forms.item(j));
                    addEvent(forms.item(j), 'submit', func, false);
                    //forms.item(j).onsubmit = function() {return shop_add_to_cart()};
                }
            }
        }

    }
}

var ow = false;
function shop_add_to_cart(form_obj)
{
    if (form_obj.nodeName=='FORM') {
        ow = mywindow(form_obj.action, 400, 200, 'addToCart', true);
        form_obj.target = 'addToCart';
        form_obj.submit();
        form_obj.target = '_blank';
    
    }
    
    return false;
}

function create_add_function(form_obj)
{
    return function(e) { 
        if (e.preventDefault) {
            e.preventDefault();
        }
        shop_add_to_cart(form_obj); 
        return false;
    };
}




function views(url,name)
{
	foto=window.open('/img/view.php?foto='+url+'&name='+name,'foto','width=300,height=300');
	foto.focus();
}
