var CoreTemplate = new Object();
CoreTemplate.workspaces = 'div#olefa_template_holder'
CoreTemplate.excludes = CoreTemplate.workspaces+', div#ajaxCounter, div#browserupdate, div#blur, div#project_views, div#project_SEARCH,' +
						' div#apps-holder, div#project_add, ul.sf-olefatoolbar, script, [class*="ui-"], .donthide, applet';
CoreTemplate.jHidden = jQuery();
CoreTemplate.templateBackground = 'none';
var twoHoursInMilliseconds = 720000

jQuery(document).ready(function() {
	var localTemplate = CoreTemplate;
	var parents = jQuery(localTemplate.workspaces).parents();
	if (jQuery.cookie('hide_template') == 'yes') {
		jQuery('body').css({
			'background-image': 'none'
		})
		localTemplate.jHidden = jQuery('body')
		.children('*')
		.not(localTemplate.excludes)
			.not(parents)
				.hide(); 
	}
	
	jQuery('.ViewTemp').click(function(){
		showTemplate();
	});

	jQuery('.HideTemp').click(function(){
		hideTemplate();
	});
	
	jQuery(".sf-olefatoolbar").superfish({
		autoArrows	:false,
		delay: 500
	});

	//stuff for link generation, rewritten in proper unobstrusive JS
	//If I ever see a <script> tag in the <body>-tag again I will kick you in the BALLS!
	jQuery('.internal_value').live('click',function(){
		jQuery('.internal_value').select();
	});
	jQuery('.external_value').live('click',function(){
		jQuery('.external_value').select();
	});
	
	jQuery('#menuitem_Link').live('click', function() {
		jQuery('.copypaste_button').trigger('click');
	});
	
	jQuery('.copypaste_button').live('click', function() {
		var this_link = new Object;
		jQuery(this).siblings('input[type="hidden"]').each(function() {
			var key = jQuery(this).attr('class');
			this_link[key] = jQuery(this).val();
		});
		
		var dialog_content = '<table>' +
			'<tr>' +
				'<td>'+this_link.dialog_link_desc+'</td>' +
			'</tr>' +
			'</table>' +
			'<table class="link-table">' +
				'<tr>' +
					'<td><b>'+this_link.dialog_internal_lang+'</b></td>'+
					'<td><input class="copy_value internal_value" type="text" value="[['+this_link.dialog_internal+']]" style="width:320px"></td>'+
					'<td><div id="copy_1" class="copypaste" style="position:relative;"><input id="button_1" class="copypaste_button" type="button" value="Copy"></div></td>' +
				'</tr>' +
				'<tr>' +
					'<td><b>'+this_link.dialog_external_lang+'</b></td>'+
					'<td><input class="copy_value external_value" type="text" value="'+this_link.dialog_external+'" style="width:320px"></td>' +
					'<td><div id="copy_2" class="copypaste" style="position:relative;"><input id="button_2" class="copypaste_button" type="button" value="Copy"></div></td>' +
				'</tr>' +
			'</table>';
		
		dialog(this_link.dialog_title, dialog_content, 0).dialog ({
			buttons: {
				'Back' : function() {
					jQuery(this).dialog('close');
				}
			},
			width: 600
		});
		init_copypaste();
	});
})

function hideTemplate() {
	var localTemplate = CoreTemplate;
	var parents = jQuery(localTemplate.workspaces).parents();
	localTemplate.templateBackground = jQuery('body').css('background-image');
	jQuery('body').css({
		'background-image': 'none'
	})
	localTemplate.jHidden = jQuery('body')
	.children('*')
	.not(localTemplate.excludes)
		.not(parents)
			.fadeOut();
	var expiration = new Date();
	expiration.setTime(expiration.getTime()+twoHoursInMilliseconds);
	jQuery.cookie('hide_template', 'yes', {expires: expiration});
}

function showTemplate() {
	var localTemplate = CoreTemplate;
	jQuery('body').css({
		'background-image': localTemplate.templateBackground
	})
	localTemplate.jHidden.fadeIn();
	var expiration = new Date();
	expiration.setTime(expiration.getTime()+twoHoursInMilliseconds);
	jQuery.cookie('hide_template', 'no', {expires: expiration});
}
