/**** SHOP-SCRIPT ****/

function Redirect(url) {
	window.location = REAL_PATH + url;
	return false;
}

function open_window(link,w,h) //opens new window
{
	var win = "width="+w+",height="+h+",menubar=no,location=no,resizable=yes,scrollbars=yes";
	newWin = window.open(link,'newWin',win);
	newWin.focus();
}

function confirmDelete(id, ask, url) //confirm order delete
{
	temp = window.confirm(ask);
	if (temp) //delete
	{
		window.location=url+id;
	}
}

function validate_disc() // review form verification
{
	if (document.formD.nick.value.length<1)
	{
		alert("Пожалуйста, введите Ваш псевдоним");
		return false;
	}

	if (document.formD.topic.value.length<1)
	{
		alert("Пожалуйста, введите тему сообщения");
		return false;
	}

	return true;
}

function activatePlaceholders() {
	var detect = navigator.userAgent.toLowerCase();
	if (detect.indexOf("safari") > 0) return false;
	var inputs = document.getElementsByTagName("input");
	for (var i=0;i<inputs.length;i++) {
		if (inputs[i].getAttribute("type") == "text") {
			if (inputs[i].getAttribute("placeholder") && inputs[i].getAttribute("placeholder").length > 0) {
				inputs[i].value = inputs[i].getAttribute("placeholder");
				inputs[i].onclick = function() {
					if (this.value == this.getAttribute("placeholder")) {
						this.value = "";
					}
					return false;
				}
				inputs[i].onblur = function() {
					if (this.value.length < 1) {
						this.value = this.getAttribute("placeholder");
					}
				}
			}
		}
	}
}

/***** SPEEDO *****/

function editCartItem(productID, colorID, sizeID, itemID) {
	$('#cartEditor'+itemID).modal({'close':false});
}

function closeCartEditor() {
	$.modal.close();
}

function switchSize(sizeID, name, uid, productID) {
	$('#size_' + uid).val(sizeID);
	
	$('#cartEditor' + uid + ' .sizes a').removeClass('selected');
	$('#size-' + sizeID + '-' + uid).addClass('selected');
}

function switchColor(colorID, name, uid, productID) {
	$('#color_' + uid).val(colorID);
	
	$('#cartEditor' + uid + ' .colors a').removeClass('selected');
	$('#cartEditor' + uid + ' .colors a#color-' + colorID + '-' + uid).addClass('selected');
	$('#cartEditor' + uid + ' .current-color').html(name);
	
	$.getJSON(REAL_PATH + '/', {
		'module': 'category',
		'event': 'getphotos',
		'productID': productID,
		'colorID': colorID
	}, function(data){
		$('#cartEditor' + uid + ' td.small-pics img').remove();
		
		$.each(data, function(i, picture) {
			if (i == 0) {
				$('#bigPic' + uid).attr('src', REAL_PATH + '/products_pictures/thumb/303/368/' + picture.filename);
			}
			
			var html = "<img style=\"visibility:hidden;\" onload=\"this.style.visibility = 'visible'\" " + (i == 0 ? "class=\"first\"" : '') + " src=\"" + REAL_PATH + "/products_pictures/thumb/96/116/" + picture.filename + "\" class=\"loading\" alt=\"\" width=\"96\" height=\"116\" />";
			
			$('#cartEditor' + uid + ' td.small-pics').html($('#cartEditor' + uid + ' td.small-pics').html() + html);
			
		})
	})
}

function saveEditor(uid, productID, ccid) {
	var count = parseInt($('#count_' + uid).val()), color = parseInt($('#color_' + uid).val()), size = ($('#size_' + uid).val() == "-1" ? -1 : parseInt($('#size_' + uid).val()));

	if (!count) {
		alert('Пожалуйста, укажите количество');
	} else if (!color) {
		alert('Пожалуйста, выберите цвет');
	} else if (!size) {
		alert('Пожалуйста, выберите размер');
	} else {
		Redirect('/changeproduct/' + (ccid ? ccid : uid) + '/' + productID + '/' + color + '/' + size + '/' + count + '.html')
	}
}

function showSizeGuide(sizeguideID) {
	if (!sizeguideID) {
		alert('Извините, для этого продукта нет таблицы размеров');
		return;
	}
	
	$('#sizeguide'+sizeguideID).modal({'close':false});
}

function closeSizeGuide() {
	$.modal.close();
}
