var $j = jQuery.noConflict();
// Use jQuery via $j(...)
$j(document).ready(function(){
	 initHead();
});

function initHead(){
	created = false;
	 $j('#editTagsDialog').jqm({
    		trigger: '.tagsInlineEdit',
			onShow: startTagsForm});
}

var startTagsForm=function(h){
	var temp = new Array();
	temp = h.t.id.split('_');
	hashT = temp[2];
	var tagsCont = $j('#tags_cont_'+hashT).text();
	tagsCont = (tagsCont=='--no tags--')?'':tagsCont;
	var topsite = $j(h.t).parent().parent().find('.topsite').text();
	$j(h.w).find('.urlTitle').html(topsite);
	$j('#TopsiteTags').val(tagsCont);
	$j('#TopsiteHash').val(hashT);
	if ( !created ) {
		$j('#editTagsForm').ajaxForm({
			error:			dumpAjaxError,
			beforeSubmit:	function( formData, jqForm, options ){
				if(formData[1].value == '') {
					if( tagsCont == '' ){
						alert('Please enter tags first');
						return false;
					}else if(!confirm('Are you sure you want to clear those tags ?')){
						return false;
					}
				}
				$j('#editTagsForm')
					.find('input[@type=text]').attr('disabled','disabled').end()
					.find('input[@type=submit]').attr('disabled','disabled').val('Saving...')
					.find('input[@type=button]').attr('disabled','disabled');
			},
			success:		function(response, status) {
				//$j('#TopsiteTags').val('');
				//$j('#TopsiteHash').val('');
				$j('#editTagsForm')
					.clearForm()
					.find('input[@type=text]').attr('disabled','').end()
					.find('input[@type=submit]').attr('disabled','').val('Save')
					.find('input[@type=button]').attr('disabled','');
				$j('#tags_cont_'+hashT).html(response);
				$j.jqm.close(h.s);
				},
				
			complete:		function(){
				//do some stuff
			}
		});
		created = true;
	}
	h.w.show();
};
function testCall()
{
	alert('dfgrerh');
}
function dump( obj )
{
	var txt = '';
	for( var i in obj ) {
		txt += i+' = '+obj[i]+"\n";
	}
	return txt;
}

function dumpAjaxError(xml, error, exception ) {
	alert( dump( exception ) );
}
/*
(function($) {
$.fn.tsTagEditor=function(o){
var _o = {
zIndex: 3000,
overlay: 50,
overlayClass: 'jqmOverlay',
closeClass: 'jqmClose',
trigger: '.jqModal',
ajax: false,
target: false,
modal: false,
onShow: false,
onHide: false,
onLoad: false
};
}

$.fn.jqmAddClose=function(e){hs(this,e,'jqmHide'); return this;}

$.jqm = {
open:function(s,t){
},
close:function(s){return false;
}};
})(jQuery);
*/