function qwe_dct_rebuild_fabrics_select()
{
	qwe_dct_reset_fabrics_select();
	
	$("#fabric[name='fabric']").removeAttr('disabled');
	
	var file_class = $("#class[name='class']").val();
	var fabric_val = 0;
	$("#qwe_dct_fabrics_list > option").each(function(){
		var val = String($(this).val()).split('_');
		if(val && val[0] && val[0] == file_class) {
			$("#fabric[name='fabric']").append(this);
			fabric_val = fabric_val ? fabric_val : $(this).val();
		}
	});
	
	if(fabric_val) {
		$("#fabric[name='fabric']").val(fabric_val);
	}
	else {
		$("#fabric[name='fabric']").attr('disabled', 'true');
	}

	if(qwe_isset('QWE_DOC_EDIT_FABRIC_ID') && QWE_DOC_EDIT_FABRIC_ID) {
		$("#fabric[name='fabric']").val(QWE_DOC_EDIT_FABRIC_ID);
		QWE_DOC_EDIT_FABRIC_ID = '';
	}
}

function qwe_dct_reset_fabrics_select()
{
	$("#fabric[name='fabric'] > option").each(function(){
		$('#qwe_dct_fabrics_list').append(this);
	});
}

function qwe_find_pos(obj)
{
	var curleft = 0;
	var curtop = 0;
	
	if(obj && obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	
	return [curleft,curtop];
}

function qwe_find_size(elem)
{
	var width = 0;
	var height = 0;
	
	if(elem) {
		width = elem.offsetWidth;
		height = elem.offsetHeight;
	}
	
	return [width, height];
}

function qwe_find_cursor_position(e)
{
    e = e || window.event;
    var cursor = {x:0, y:0};
    if (e.pageX || e.pageY) {
        cursor.x = e.pageX;
        cursor.y = e.pageY;
    } 
    else {
        var de = document.documentElement;
        var b = document.body;
        cursor.x = e.clientX + (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
        cursor.y = e.clientY + (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
    }
    return [cursor.x, cursor.y];
}

function qwe_is_cursor_in(elem, event)
{
	var cur_pos = qwe_find_cursor_position(event);
	var el_pos = qwe_find_pos(elem);
	var el_size = qwe_find_size(elem);
	
	var res = true;
	
	if(cur_pos[0] <= el_pos[0] || cur_pos[0] >= el_pos[0] + el_size[0]) {
		res = false;
	}
	
	if(cur_pos[1] <= el_pos[1] || cur_pos[1] >= el_pos[1] + el_size[1]) {
		res = false;
	}
	
	return res;
}

function qwe_doc_fave(doc_id)
{
	var sBaseUrl = document.getElementsByTagName('base')[0].href;
	var url = sBaseUrl + 'qwe_mod/qwe_main.php?location=qwe_doc_add_to_fave&doc_id=' + doc_id;
	$.getJSON(url, function(json){
		   alert(json.message);
	});
	
	return false;			
}

function qwe_doc_delete_fave(doc_id)
{
	var sBaseUrl = document.getElementsByTagName('base')[0].href;
	var url = sBaseUrl + 'qwe_mod/qwe_main.php?location=qwe_doc_delete_fave&doc_id=' + doc_id;
	$.getJSON(url, function(json){
		   alert(json.message);
	});
	
	return false;			
}

function qwe_isset(varname)
{
	return(typeof(window[varname])!='undefined');
}

function qwe_show_preview(e, doc_id)
{
	$('#qwe_doc_preview_div').remove();
	
	$(document.body).append('<div id="qwe_doc_preview_div" class="qwe_doc_preview"> </div>');
	var prev_div = $('#qwe_doc_preview_div');
	prev_div.css('diplay', 'block');
	
	var cur_pos = qwe_find_cursor_position(e);
	prev_div.css('left', cur_pos[0] + 'px');
	prev_div.css('top', cur_pos[1] + 'px');
	
	var sBaseUrl = document.getElementsByTagName('base')[0].href;
	var url = sBaseUrl + 'qwe_mod/qwe_main.php?location=qwe_doc_preview_code&doc_id=' + doc_id;
	$.getJSON(url, function(data){
		if(data.type == "image") {
			$('#qwe_doc_preview_div').html('<img src="'+data.src+'" />');
		}
		else if(data.type == "text") {
			$('#qwe_doc_preview_div').html('<img src="'+data.src+'" /><br />Layout Source<br /><textarea rows="10" cols="40">' + data.text + '</textarea>');
			//$('#qwe_doc_preview_div').html('Layout Source<br /><textarea rows="10" cols="40">' + data.text + '</textarea>');
		}
		$('#qwe_doc_preview_div').append('<br />');
		
		$('#qwe_doc_preview_div').append('<a href=\'javascript:void(0);\' onclick=\'$("#qwe_doc_preview_div").remove(); return false;\'><b>Close</b></a>');
	});
	
	return false;			
}

function qwe_show_comment_popup(e, comment_id, user_id, user_name)
{
	if($('#qwe_comment_popup' + comment_id).get(0)) {
		$('#qwe_comment_popup' + comment_id).remove();
		return;
	}

	$("div[id*='qwe_comment_popup']").remove();

	var sBaseUrl = document.getElementsByTagName('base')[0].href;
	var url_visit_user = sBaseUrl + user_name;
	var url_goto_art = sBaseUrl + 'stuff/by_member/' + user_id;

	$(document.body).append('<div class="qwe_doc_comment_popup" id="qwe_comment_popup' + comment_id + '"> </div>');
	var div = $('#qwe_comment_popup' + comment_id);
	div.css('diplay', 'block');
	
	var cur_pos = qwe_find_cursor_position(e);
	div.css('left', cur_pos[0] + 'px');
	div.css('top', cur_pos[1] + 'px');
	div.html('<a href="'+url_visit_user+'">visit user</a><a href="'+url_goto_art+'">goto art</a><a href="#" onclick="current_comments_engine.cmtRemove(this, '+comment_id+'); qwe_hide_comment_popup('+comment_id+'); return false;">delete comment</a>');
}

function qwe_hide_comment_popup(comment_id, user_id)
{
	$('#qwe_comment_popup' + comment_id).remove();
	return false;
}

$(document).ready(function(){
	$("body").append("<div id='qwe_dct_fabrics_list'> </div>");
	$('#qwe_dct_fabrics_list').css('display', 'none');
	
	qwe_dct_rebuild_fabrics_select();
	$("#class[name='class']").change(qwe_dct_rebuild_fabrics_select);
});