i = 0;

function getCartTop() {
  $('#carttop').load('/ajax/carttop/');
}

function AddToCart(itemid) {
$.ajax({
    url: '/ajax/cartadd/',
    data: { 'itemid': itemid },
    dataType : 'text',
    success: function (data, textStatus) {
		$('#DivCart_'+itemid).html(data); 
		fly_to_basket('item_'+itemid, 'carttop');
		getCartTop();
    } 
});
}

function fly_to_basket(who, where) {
// коэф пропорциональности
var prop = 4;

// координаты перемещаемого объекта
left_who = $('#' + who).offset().left;
top_who = $('#' + who).offset().top;

// координаты объекта, в который "летим"
left_where = $('#' + where).offset().left;
top_where = $('#' + where).offset().top;

// габариты перемещаемого объекта
width_who = $('#' + who).width();
height_who = $('#' + who).height();

// габариты объекта, в который "летим"
width_where = $('#' + where).width();
height_where = $('#' + where).height();

// клонируем  перемещаемый объект
$('#' + who).clone(true).insertBefore('#' + who).addClass('insert_wherek_' + i).css({
	'width' : (width_who/prop) + 'px',
	'height' : (height_who/prop) + 'px',
	'position':'absolute',
	'left':left_who + 'px',
	'top':top_who + 'px'
});

// анимация
$('.insert_wherek_' + i)
	.animate({				
		'left':left_where + width_where/2 - width_who/(2*prop)
	}, 500)
	.animate({
		'top':top_where + height_where/2 - height_who/(2*prop)
	}, 300)
	.animate({
		'opacity':'0'
	}, 400)
	.animate({
		'width':'0px',
		'height':'0px'
	}, 1)
	.attr('id', '');
	i++;
}

function NewWindow(mypage, myname, w, h, scroll, resize, statusbar, menubar, toolbar, personalbar, titlebar) {
  var winl =  10;
  var wint =  10;
  scroll = scroll || 'no';
  statusbar = statusbar || 'no';
  menubar=menubar || 'no';
  toolbar= toolbar || 'no';
  personalbar= personalbar || 'no';
  titlebar= titlebar || 'no';
  resizable= resize || 'no';
  winprops = "height="+h+",width="+w+",top="+wint+",left="+winl+",scrollbars="+scroll+", resizable="+resizable+",status="+statusbar+",menubar="+menubar+",toolbar="+toolbar+",personalbar="+personalbar+",titlebar="+titlebar;
  win = window.open(mypage, myname, winprops)
  if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

document.getElementsByClassName = function(cl) {
	var retnode = [];
	var myclass = new RegExp('\\b'+cl+'\\b');
	var elem = this.getElementsByTagName('*');
	for (var i = 0; i < elem.length; i++) {
		var classes = elem[i].className;
		if (myclass.test(classes)) retnode.push(elem[i]);
	}
	return retnode;
};

function setWidthByClassName(className) {
	var maxWidth = 0;
	var element;
	for(i=0; (element = document.getElementsByClassName(className)[i]); i++) {				
		if(maxWidth < parseInt(getElementWidth(element))) maxWidth = parseInt(getElementWidth(element));
	}	
	for(i=0; (element = document.getElementsByClassName(className)[i]); i++) {
		element.style.width = maxWidth - 2 + "px";		
	}	
	
}

function getElementWidth(element) {	
	if(element.clientWidth) {
		return element.clientWidth;
	}
	else {
		if(element.offsetWidth) {
			return element.offsetWidth;
		}
	}
	return 10;
}