$(document).ready(function() {
	// first let page init in document ready
	if (typeof GD.documentReady === 'function') {
		GD.documentReady();
	}
	
	$('#login_link').click(function() {
		var loginForm = $('#login_box_form');
		if (!loginForm.is(':visible')) {
			loginForm.slideDown('fast');
		} else {
			loginForm.slideUp('fast');
		}
		return false;
	});
	
	// init link search box
	var linkInput = $('#link_search_input');
	var prevCode = 0;
	linkInput.keydown(function(event) {
		// do not allow a-z
		if (event.keyCode >= 65 && event.keyCode <= 90) {
			return false;
		} else if (event.keyCode == 27) {
			// hide on esc
			hideLinkInfo();
			return false;
		}
	});
	linkInput.keyup(function(event) {
		var linkInfoVisible = $('#link_info').is(':visible');
		if (linkInfoVisible && event.keyCode == 13) {
			openLink();
		} else if (event.keyCode == 27) {
			// exit on esc
			return false;
		} else {
			var code = $('#link_search_input').val();
			if (code.length == 3) {
				if (!linkInfoVisible || code != prevCode) {
					$('#link_search_box .indicator').show();
					$.getJSON(GD.SITE_URL+'links/show/'+code+'/json',
						function (data) {
							if (data.success) {
								$('#link_description').html(data.link.description);
								$('#link_open').attr('href', data.link.url);
								$('#link_info').slideDown('fast');
								$('#link_info').click(openLink);
								prevCode = data.link.code;
							} else {
								hideLinkInfo();
							}
							$('#link_search_box .indicator').hide();
						}
					);
				}
			} else {
				hideLinkInfo();
			}
		}
	});
	
	linkInput.blur(function () {
		hideLinkInfo();
	});
	
	function openLink() {
		//document.location.href = $('#link_open').attr('href');
		window.open($('#link_open').attr('href'));
		return false;
	}
	
	function hideLinkInfo() {
		$('#link_info').slideUp('fast');
		$('#link_search_box .indicator').hide();
		prevCode = 0;
	}
	
	// book teaser paging tooltips 
	$("#preview_container #prev").sltip({
		content: $('#prev_teaser_tip'),
		xOffset: -97,
		yOffset: 18
	});
	$("#preview_container #next").sltip({
		content: $('#next_teaser_tip'),
		xOffset: 1,
		yOffset: 18
	});
	
	// book teaser pages cycle
	$('#slideshow').cycle({ 
		prev:   '#preview_container #prev', 
		next:   '#preview_container #next', 
		timeout: 0,
		speed:  200
	});
});

GD.showVideo = function(video) {
	$('#'+video.containerId).flash({
        swf: GD.BASE_URL+'swf/player.swf',
        width: video.width,
        height: video.height,
        hasVersion: 9,
        expressInstaller: GD.BASE_URL+'swf/expressInstall.swf', 
        flashvars: {
            file: video.file,
            image: video.image
        }   
    });
};

GD.initTinyMCE = function(options) {
	var settings = { enableCode: false };
	$.extend(settings, options || {});
	
	$('textarea.tinymce').tinymce({
		script_url : GD.BASE_URL+'js/tiny_mce/tiny_mce.js',
		theme : 'advanced',
		plugins : 'processing,paste,safari',
		theme_advanced_buttons1 : 'bold,italic,underline,strikethrough,|,undo,redo,|,link,unlink,image,|,processing'+(settings.enableCode ? ',code' : ''),
		theme_advanced_buttons2 : '',
		theme_advanced_buttons3 : '',
		theme_advanced_toolbar_location : 'top',
		theme_advanced_toolbar_align : 'center',
		theme_advanced_statusbar_location : 'bottom',
		theme_advanced_resizing : true,
		theme_advanced_path : false,
		height : '230px',
        width : '550px',
		content_css : GD.BASE_URL+'css/styles_rte.css',
		entity_encoding : 'raw',
		verify_html : false,
		paste_create_paragraphs : true,
		paste_create_linebreaks : true,
		paste_use_dialog : false,
		paste_auto_cleanup_on_paste : true,
		paste_convert_middot_lists : false,
		paste_unindented_list_class : "",
		paste_convert_headers_to_strong : true,
		paste_remove_styles : true,
		paste_strip_class_attributes : "all",
		cleanup_callback : GD.mceCleanUp,
		valid_elements : ""
			+"a[accesskey|charset|class|coords|dir<ltr?rtl|href|hreflang|id|lang|name"
			  +"|onblur|onclick|ondblclick|onfocus|onkeydown|onkeypress|onkeyup"
			  +"|onmousedown|onmousemove|onmouseout|onmouseover|onmouseup|rel|rev"
			  +"|shape<circle?default?poly?rect|style|tabindex|title|target|type],"
			+"br[class|clear<all?left?none?right|id|style|title],"
			+"em/i[class|dir<ltr?rtl|id|lang|onclick|ondblclick|onkeydown|onkeypress"
			  +"|onkeyup|onmousedown|onmousemove|onmouseout|onmouseover|onmouseup|style"
			  +"|title],"
			+"img[align<bottom?left?middle?right?top|alt|border|class|dir<ltr?rtl|height"
			  +"|hspace|id|ismap<ismap|lang|longdesc|name|onclick|ondblclick|onkeydown"
			  +"|onkeypress|onkeyup|onmousedown|onmousemove|onmouseout|onmouseover"
			  +"|onmouseup|src|style|title|usemap|vspace|width],"
			+"p[align<center?justify?left?right|class|dir<ltr?rtl|id|lang|onclick"
			  +"|ondblclick|onkeydown|onkeypress|onkeyup|onmousedown|onmousemove"
			  +"|onmouseout|onmouseover|onmouseup|style|title],"
			+"pre[align|class|dir<ltr?rtl|id|lang|onclick|ondblclick"
			  +"|onkeydown|onkeypress|onkeyup|onmousedown|onmousemove|onmouseout"
			  +"|onmouseover|onmouseup|style|title|width],"
			+"span[align<center?justify?left?right|class|dir<ltr?rtl|id|lang|onclick|ondblclick|onkeydown"
			  +"|onkeypress|onkeyup|onmousedown|onmousemove|onmouseout|onmouseover"
			  +"|onmouseup|style|title],"
			+"strong/b[class|dir<ltr?rtl|id|lang|onclick|ondblclick|onkeydown|onkeypress"
			  +"|onkeyup|onmousedown|onmousemove|onmouseout|onmouseover|onmouseup|style"
			  +"|title]"
	});
};

GD.mceCleanUp = function(type, content) {
	switch (type) {
		// gets passed when user submits the form
		case "get_from_editor":
			// remove empty paragraphs before opening pre which get inserted automatically
			content = content.replace(/<p>\s*<\/p><pre/gi, "<pre"); 
			break;
		// gets passed when new content is inserted into the editor
		case "insert_to_editor":
			// replace MS Word quotes with standard ASCII quotes
			content = content.replace(/“|„|”|«|»/gi, '"'); 
		break;
	}
	return content;
};