var core_url = '/index.php';
var create_itog = 0;
$(document).ready(function(){
	$('div.colors').find('a').click(function(){
		$(this).parent().find('a').removeClass('active');
		$(this).parent().find('input').val($(this).attr('id'));
		$(this).attr('class', 'active');
	});
	$('div.selectDop').find('.upakovka a').click(function(){
		$(this).parent().parent().find('a').removeClass('active');
		$(this).parent().parent().find('input').val($(this).attr('id'));
		$(this).attr('class', 'active');
		LoadForm('create_buket');
	});
	$('div#set_count_items').find('input').blur(function(){
		sum = $(this).val() * $(this).parent().parent().find('div span#price_item').html();
		$(this).parent().parent().find('div#price span').html(sum);
		LoadForm('create_buket')
	});
});

function LoadForm(form) {
	jQuery.ajax({
		url: core_url,
		type: "POST",
		data: jQuery('#'+form).formToArray(true),
		enctype: "multipart/form-data",
		dataType : "json",
		beforeSend: function() {
			return php.beforeSend();
		},
		success: function(data, textStatus) {
			return php.success(data, textStatus);
		},
		error: function (xmlEr, typeEr, except) {
			return php.error(xmlEr, typeEr, except);                  
		},
		complete: function (XMLHttpRequest, textStatus) {
			return php.complete(XMLHttpRequest, textStatus);
		}
	});
	return false;
}

function LoadData(op, module_name, value) {
	jQuery.ajax({
		url: core_url,
		type: "POST",
		data: "op=" + op + "&module_name=" + module_name + "&value=" + value,
		dataType : "json",
		beforeSend: function() {
			return php.beforeSend();
		},
		success: function(data, textStatus) {
			return php.success(data, textStatus);
		},
		error: function (xmlEr, typeEr, except) {
			return php.error(xmlEr, typeEr, except);                  
		},
		complete: function (XMLHttpRequest, textStatus) {
			return php.complete(XMLHttpRequest, textStatus);
		}
	});
	return false;
}

function LoadDataURL(url) {
	jQuery.ajax({
		url: core_url,
		type: 'POST',
		data: url,
		dataType : 'json',
		beforeSend: function() {
			return php.beforeSend();
		},
		success: function(data, textStatus) {
			return php.success(data, textStatus);
		},
		error: function (xmlEr, typeEr, except) {
			return php.error(xmlEr, typeEr, except);                  
		},
		complete: function (XMLHttpRequest, textStatus) {
			return php.complete(XMLHttpRequest, textStatus);
		}
	});
	return false;
}

function initLoadDataURL(url, label) {
	jQuery.ajax({
		url: core_url,
		type: 'POST',
		data: url,
		dataType : 'json',
		beforeSend: function() {
			jQuery('span#'+label).attr('display','inline');
			return php.beforeSend();
		},
		success: function(data, textStatus) {
			jQuery('span#'+label).attr('display','none');
			return php.success(data, textStatus);
		},
		error: function (xmlEr, typeEr, except) {
			jQuery('span#'+label).attr('display','none');
			return php.error(xmlEr, typeEr, except);                  
		},
		complete: function (XMLHttpRequest, textStatus) {
			jQuery('span#'+label).attr('display','none');
			return php.complete(XMLHttpRequest, textStatus);
		}
	});
	return false;
}

function intval (mixed_var, base) {
    var type = typeof( mixed_var );
 
    if (type === 'boolean') {        return (mixed_var) ? 1 : 0;
    } else if (type === 'string') {
        tmp = parseInt(mixed_var, base || 10);
        return (isNaN(tmp) || !isFinite(tmp)) ? 0 : tmp;
    } else if (type === 'number' && isFinite(mixed_var) ) {        return Math.floor(mixed_var);
    } else {
        return 0;
    }
}

function initMinus(element, send) {
	product_count = jQuery('a#'+element).next();
	count = intval(product_count.val()) - 1;
	if (count >= 1) {
		product_count.val(count);
		if (send) LoadForm('basket_form');
	}
}

function initPlus(element, send) {
	jQuery('a#'+element).prev().val(intval(jQuery('a#'+element).prev().val()) + 1);
	if (send) LoadForm('basket_form');
}
