var JumpLinks = {
	sites: {},
	programs: {},
	url: '',

	removeJumpLinkChooser: function()
	{
		jQuery('#programsSelect').unbind('change');
		jQuery('#jumpLinkChooserBox').dialog('destroy');
		jQuery('#jumpLinkChooserBox').remove();
	},

	fillProgramsSelect: function(siteId)
	{
		var current = 0;
		jQuery("#programsSelect").removeOption(/./);
		jQuery.each(this.sites[siteId].programs, function(programId, jumpLinkId) {
			if (current == 0) current = programId;
			jQuery('#programsSelect').addOption(programId, JumpLinks.programs[programId].name);
		});
		jQuery('#programsSelect').selectOptions(current, true);
		jQuery('#jumpLinkSelect').removeOption(/./);
		jQuery.each(this.sites[siteId].programs[current], function(index, jumpLinkId) {
			jQuery('#jumpLinkSelect').addOption(jumpLinkId, jumpLinkId, false);
		});
		jQuery('#programsSelect').change(function() {
			jQuery('#jumpLinkSelect').removeOption(/./);
			jQuery.each(JumpLinks.sites[siteId].programs[jQuery('#programsSelect').val()], function(index, jumpLinkId) {
				jQuery('#jumpLinkSelect').addOption(jumpLinkId, jumpLinkId, false);
			});
		});
	},
	
	showJumpLinkChooser: function(type)
	{
		this.removeJumpLinkChooser();
		jQuery.getJSON(this.url + 'getdata.php', function(result) {
			var buttonsHash = { 'Close': function() { JumpLinks.removeJumpLinkChooser(); }};
			var height = 120;
			if (parseInt(result.countSites) > 0) {
				height = 300;
				jQuery('body').append('<div id="jumpLinkChooserBox" title="Choose jump link!"><span class="beforeSelect">Site:</span><select id="sitesSelect"></select><br /><br /><span class="beforeSelect">Program:</span><select id="programsSelect"></select><br /><br /><span class="beforeSelect">Jump Link ID:</span><select id="jumpLinkSelect"></select></div>');
				var first = 0;
				JumpLinks.sites = result.sites;
				JumpLinks.programs = result.programs;
				jQuery.each(JumpLinks.sites, function(siteId, site) {
					if (first == 0) first = siteId;
					jQuery('#sitesSelect').addOption(siteId, site.name);
				});
				if (parseInt(result.countSites) == 1) jQuery('#sitesSelect').attr('disabled', true);
				jQuery('#sitesSelect').selectOptions(first, true);
				jQuery('#programsSelect').unbind('change');
				JumpLinks.fillProgramsSelect(first);
				jQuery('#sitesSelect').change(function() {
					jQuery('#programsSelect').unbind('change');
					JumpLinks.fillProgramsSelect(jQuery('#sitesSelect').val());
				});
				buttonsHash.Insert = function() {
					JumpLinks.insertJumpLink(type);
					JumpLinks.removeJumpLinkChooser();
				};
			} else jQuery('body').append('<div id="jumpLinkChooserBox" title="There isn\'t any sites with jumplinks!">There isn\'t any sites with jumplinks!</div>');
			jQuery('#jumpLinkChooserBox').dialog({ 
			    modal: true,
			    resizable: false, 
			    overlay: { 
			        opacity: 0.5, 
			        background: 'black'
			    },
				height: height,
		        width: 500,
		        buttons: buttonsHash
	        });
		});
	},
	
	insertJumpLink: function(type)
	{
		var tagStart = '<a href="' + JumpLinks.sites[jQuery('#sitesSelect').val()].url + jQuery('#jumpLinkSelect').val() + '.html" rel="nofollow">';
		var tagEnd = '</a>';
		var defaultText = 'jumpLink' + jQuery('#jumpLinkSelect').val();
		switch(type) {
			case 'mce':
				var selection = tinyMCE.activeEditor.selection.getContent({format : 'raw'});
				var replacement = tagStart;
				replacement += selection.length > 0 ? selection : defaultText;
				replacement += tagEnd;
				tinyMCE.execCommand('mceInsertContent', false, replacement);
				break;
			case 'textarea':
				if (document.selection) {
					edCanvas.focus();
				    sel = document.selection.createRange();
					if (sel.text.length > 0) {
						sel.text = tagStart + sel.text + tagEnd;
					} else {
						sel.text = tagStart + defaultText + tagEnd;
					}
					edCanvas.focus();
				} else if (edCanvas.selectionStart || edCanvas.selectionStart == '0') {
					var startPos = edCanvas.selectionStart;
					var endPos = edCanvas.selectionEnd;
					var cursorPos = endPos;
					if (startPos != endPos) {
						edCanvas.value = edCanvas.value.substring(0, startPos)
						              + tagStart
						              + edCanvas.value.substring(startPos, endPos)
						              + tagEnd
						              + edCanvas.value.substring(endPos, edCanvas.value.length);
						cursorPos += tagStart.length + tagEnd.length;
					} else {
						edCanvas.value = edCanvas.value.substring(0, startPos)
						              + tagStart
						              + defaultText
						              + tagEnd
						              + edCanvas.value.substring(endPos, edCanvas.value.length);
						cursorPos = tagStart.length + defaultText.length + tagEnd.length;
					}
					edCanvas.focus();
					edCanvas.selectionStart = cursorPos;
					edCanvas.selectionEnd = cursorPos;
				} else {
					edCanvas.value = edCanvas.value.substring(0, startPos)
							            + tagStart
							            + defaultText
							            + tagEnd
							            + edCanvas.value.substring(endPos, edCanvas.value.length);
					edCanvas.focus();
				}
				break;
		}
	}

};
jQuery(window).ready(function(){
	if (jQuery('#ed_toolbar').length == 1) {
		jQuery('#ed_toolbar').append('<input type="button" id="ed_insetjumplink" class="ed_button" title="Insert jump link" value="jump link" />');
		jQuery('#ed_insetjumplink').click(function(){
			JumpLinks.showJumpLinkChooser('textarea');
		});
	}
});