function openwindow(url, width, height) {
	// hack for IE 7
	if(navigator.appVersion.indexOf('MSIE 7') != -1) {
		height += 20;
	}

	xposition = 20; yposition = 20;
	if ((parseInt(navigator.appVersion) >= 4 )){
		xposition = (screen.width - width) / 2;
		yposition = (screen.height - height) / 2;
	}

	args = "width=" + width + ","
		    + "height=" + height + ","
			+ "location=0,"
			+ "menubar=0,"
			+ "resizable=0,"
			+ "scrollbars=no,"
			+ "status=0,"
			+ "titlebar=0,"
			+ "toolbar=0,"
			+ "hotkeys=0,"
			+ "screenx=" + xposition + ","  //NN Only
			+ "screeny=" + yposition + ","  //NN Only
			+ "left=" + xposition + ","     //IE Only
			+ "top=" + yposition;           //IE Only
	ww = window.open(url, '', args);
}

function openproduct(url) {
	width = 740;
	height = 600;

	// hack for IE 7
	if(navigator.appVersion.indexOf('MSIE 7') != -1) {
		height += 20;
	}

	xposition = 20; yposition = 20;
	if ((parseInt(navigator.appVersion) >= 4 )){
		xposition = (screen.width - width) / 2;
		yposition = 20;
	}

	args = "width=" + width + ","
		    + "height=" + height + ","
			+ "location=0,"
			+ "menubar=0,"
			+ "resizable=0,"
			+ "scrollbars=yes,"
			+ "status=0,"
			+ "titlebar=0,"
			+ "toolbar=0,"
			+ "hotkeys=0,"
			+ "screenx=" + xposition + ","  //NN Only
			+ "screeny=" + yposition + ","  //NN Only
			+ "left=" + xposition + ","     //IE Only
			+ "top=" + yposition;           //IE Only
	ww = window.open(url, new Date().getTime(), args);
}



function addEvent(obj, sType, fn, rmFn)
{
	if(obj.addEventListener) {
		try
		{
			obj.removeEventListener(sType, rmFn, false);
		}
		catch(e)
		{
		}
		obj.addEventListener(sType, fn, false);
	}
	else if (obj.attachEvent)
	{
		try
		{
			var r = obj.detachEvent('on'+sType, rmFn);
		}
		catch(e)
		{
		}
		var r = obj.attachEvent('on'+sType, fn);
	}
	else
	{
		alert("Neimanoma prisieti veiksmo!");
	}
}

function removeEvent(obj, sType, fn) {
	if (obj.removeEventListener) {
		obj.removeEventListener(sType, fn, false);
	}
	else {
		obj.detachEvent('on' + sType, fn);
	}
}

function removeAllEditors() {
	for(i = 0; i < document.forms.length; i++) {
		if(document.forms[i] != null)
			for(j = 0; j < document.forms[i].elements.length; j++) {
				element = document.forms[i].elements[j];
				if (tinyMCE.getInstanceById(element.id) != null) {
					element.value = tinyMCE.getInstanceById(element.id).getHTML();
					tinyMCE.execCommand('mceRemoveControl', false, element.id);
				}
			}
	}
}

function toggleEditor(id) {
	var elm = document.getElementById(id);
	// submitinant tevine forma reikia nuimti redaktoriu
	if (tinyMCE.getInstanceById(id) == null) {
		tinyMCE.execCommand('mceAddControl', false, id);
	}
	else
		tinyMCE.execCommand('mceRemoveControl', false, id);
}


function changeBlock(pBlockId, pProductId, pProductTitle, pProductPhoto)
{
    lLink = document.createElement('a');
    lLink.href = 'catalog/view_product/?id='+pProductId+'&height=500&width=446';
    lLink.className = 'image';

    $(lLink).addClass('thickbox');
    tb_init(lLink);
    $(lLink).append('<img src="'+pProductPhoto+'" alt="" width="136" />');
    $(lLink).append('<span>'+pProductTitle+'</span>');

    $('#'+pBlockId).html('').append(lLink);
}


function sscroll(pId)
{
	lMarginTop = parseInt($('#'+pId).css('margin-top'));

	lSize = $('#'+pId).children().size();

	if(lMarginTop > lSize - (15 * 15) && lSize * 15 > 200)
		$('#'+pId).animate({marginTop: (lMarginTop-30)+'px'}, 500);
}

function getCategories(pType)
{
	$.getJSON('/'+lang+'-catalog-get_categories.html', {type: pType}, function(data) {
		$('#techCategory').html('');
		$('#techSubCategory').html('');
		for(_i in data) {
			if(data[_i].depth < 3) {
				option = document.createElement('option');
				option.value = data[_i].id;
				option.innerHTML = '';
				for(j = 0; j < data[_i].depth; j++) {
					option.innerHTML = option.innerHTML + '--';
				}
				option.innerHTML = option.innerHTML + ' ' + data[_i].title;
				
				$('#techCategory').append(option);
			}
		}
	});
}

function getSubCategories(pCatId)
{
	$.getJSON('/'+lang+'-catalog-get_categories.html?cid='+pCatId, function(data) {
		$('#techSubCategory').html('');
		for(_i in data) {
			if(data[_i].depth >= 3) {
				option = document.createElement('option');
				option.value = data[_i].id;
				option.innerHTML = '';
				for(j = 0; j < data[_i].depth; j++) {
					option.innerHTML = option.innerHTML + '--';
				}
				option.innerHTML = option.innerHTML + ' ' + data[_i].title;
				
				$('#techSubCategory').append(option);
			}
		}
	});
}


