var addquotebutton     		= ipb_var_image_url + "/p_mq_add.gif";
var removequotebutton  		= ipb_var_image_url + "/p_mq_remove.gif";
var unselectedbutton 		= ipb_var_image_url + "/topic_unselected.gif";
var selectedbutton   		= ipb_var_image_url + "/topic_selected.gif";
var lang_gobutton    		= "With selected";
var gallery_lang_prompt		= "Link to Image:";
var text_area 			= new Array();
var image 			= new Array();
var ids_to_imgs			= new Array();
var gal_ignore_cache		= new Array();
var quote_start_x 			= 0;
var quote_start_y 			= 0;
var var_image_height 		= 0;
var var_image_width 		= 0;
var rate_value			= 0;
var centerdiv;
var the_image;
var actual_image;


function init_image_editor( img_h, img_w ) {
	var_image_height = img_h;
	var_image_width = img_w;
	actual_image = document.getElementById( 'actualImage' );
	actual_image.style.height = var_image_height + 'px';
	actual_image.style.width = var_image_width + 'px';
	document.getElementById( 'imageContainer' ).style.height = actual_image.style.height + 10 + 'px';
}

function save_tags() {
	var url = ipb_base_url;
	do_request_function = function() {
		if ( ! xmlobj.readystate_ready_and_ok() ) {
			xmlobj.show_loading( 'Saving Tags...' );
			return;
		}
		xmlobj.hide_loading();
		var html = xmlobj.xmlhandler.responseText;
	}
	xmlobj = new ajax_request();
	xmlobj.onreadystatechange( do_request_function );
	xmlobj.process( url );
	document.getElementById( 'tag_edit_box' ).style.display = 'none';
}

function create_quote_box() {
	quote_div = document.getElementById( 'quote_div' );
	quote_div.style.display = 'block';
	image['top'] = _get_obj_toppos( actual_image );
	image['left'] = _get_obj_leftpos( actual_image );
	quote_div.style.top = image['top'] + 5 + 'px';
	quote_div.style.left = image['left'] + 5 + 'px';
	image['right'] = image['left'] + parseInt( var_image_width );
	image['bottom'] = image['top'] + parseInt( var_image_height );
	image['right_barrier'] = image['right'] - parseInt( quote_div.style.width );
	image['bottom_barrier'] = image['bottom'] - parseInt( quote_div.style.height );
	Drag.init( quote_div, quote_div, image['left'], image['right_barrier'], image['top'], image['bottom_barrier'] );
	quote_div.onmouseup = pop_text_area;
	document.getElementById( 'quote_url' ).href = 'javascript:alert( "You have to save the current quote box first." );';
	document.getElementById( 'quote_text' ).innerHTML = '<s>Create Quote Box</s>';
}

function pop_tag_box() {
	document.getElementById( 'tag_edit_box' ).style.display = 'block';
}

function pop_text_area( event ) {
	text_area['box'] = document.getElementById( 'quote_textarea' );
	text_area['container'] = document.getElementById( 'textarea_div' );
	text_area['container'].style.top =  parseInt( _get_obj_toppos( quote_div ) ) + parseInt( quote_div.style.height ) + 'px';
	text_area['container'].style.left = parseInt( quote_div.style.left ) - 5 + 'px';
	text_area['container'].style.display = 'block';
	text_area['box'].style.display = 'block';
}

function end_quote_box() {	
	var url = ipb_base_url;
	do_request_function = function() {
		if ( ! xmlobj.readystate_ready_and_ok() ) {
			xmlobj.show_loading( 'Saving Quote Box...' );
			return;
		}
		xmlobj.hide_loading();
		var html = xmlobj.xmlhandler.responseText;
	}
	xmlobj = new ajax_request();
	xmlobj.onreadystatechange( do_request_function );
	xmlobj.process( url );
	text_area['container'].style.display = 'none';
	quote_div.style.display = 'none';
	document.getElementById( 'quote_url' ).href = 'javascript:create_quote_box();';
	document.getElementById( 'quote_text' ).innerHTML = 'Create Quote Box';
}

function track_quote_box( event ) {
	change_cursor( 'move' );
}

function upload_pane_init() {
	var pane_main = document.getElementById( 'gallery_upload' );
	var pane_drag = document.getElementById( 'upload-drag' );
	Drag.init( pane_drag, pane_main );
}

function gallery_upload_pane() {
	upload_pane_init();
	centerdiv = new center_div();
	centerdiv.divname = 'gallery_upload';
	centerdiv.move_div();
}

function gallery_link_to_post(pid,img) {
	temp = prompt( gallery_lang_prompt, ipb_var_base_url + "automodule=gallery&cmd=si&img=" + img + "#" + pid );
	return false;
}

function delete_img(theURL) {
	if (confirm( ipb_lang_js_del_1 )) {
		window.location.href=theURL;
	} else {
		alert ( ipb_lang_js_del_2 );
	} 
}

function gallery_toggle_pid( id ) {
	saved = new Array();
	clean = new Array();
	add   = 1;
	tmp = document.modform.selectedgcids.value;
	saved = tmp.split(",");
	for( i = 0 ; i < saved.length; i++ ) {
		if ( saved[i] != "" ) {
			if ( saved[i] == id) {
				 add = 0;
			} else {
				clean[clean.length] = saved[i];
			}
		}
	}
	if ( add ) {
		clean[ clean.length ] = id;
		eval("document.pid"+id+".src=selectedbutton");
	} else {
		eval(" document.pid"+id+".src=unselectedbutton");
	}
	newvalue = clean.join(',');
	my_setcookie( 'modgcids', newvalue, 0 );
	document.modform.selectedgcids.value = newvalue;
	newcount = stacksize(clean);
	document.modform.gobutton.value = lang_gobutton + '(' + newcount + ')';
	return false;
}

function multiquote_add(id) {
	saved = new Array();
	clean = new Array();
	add   = 1;
	if ( tmp = my_getcookie('gal_pids') ) {
		saved = tmp.split(",");
	}
	for( i = 0 ; i < saved.length; i++ ) {
		if ( saved[i] != "" ) {
			if ( saved[i] == id ) {
				 add = 0;
			} else {
				clean[clean.length] = saved[i];
			}
		}
	}
	if ( add ) {
		clean[ clean.length ] = id;
		eval("document.mad_"+id+".src=removequotebutton");
	} else {
		eval(" document.mad_"+id+".src=addquotebutton");
	}
	my_setcookie( 'gal_pids', clean.join(','), 0 );
	return false;
}

function gallery_toggle_img( id ) {
	saved = new Array();
	clean = new Array();
	add   = 1;
	tmp = document.modform.selectedimgids.value;
	saved = tmp.split(",");
	for( i = 0 ; i < saved.length; i++ ) {
		if ( saved[i] != "" ) {
			if ( saved[i] == id) {
				 add = 0;
			} else {
				clean[clean.length] = saved[i];
			}
		}
	}
	if ( add ) {
		clean[ clean.length ] = id;
		eval("document.img"+id+".src=selectedbutton");
	} else {
		eval(" document.img"+id+".src=unselectedbutton");
	}
	newvalue = clean.join(',')
	my_setcookie( 'modimgids', newvalue, 0 );
	document.modform.selectedimgids.value = newvalue;
	newcount = stacksize(clean);
	document.modform.gobutton.value = lang_gobutton + '(' + newcount + ')';
	return false;
}

add_onload_event( fix_linked_image_sizes );

function fix_linked_image_sizes() {
	var images   = document.getElementsByTagName( 'IMG' );
	var _padding = 2;
	var _count   = 0;
	var _img     = '<img src="' + ipb_var_image_url + '/img-resized.png" style="vertical-align:middle" border="0" alt="" />';
	for ( i = 0 ; i < images.length; i++ ) {
		if ( images[i].className == 'galattach' ) {
			_count++;
			var _width   = images[i].width;
			var _height  = images[i].height;
			var _percent = 0;
			var _href	 = images[i].parentNode.href;
			if( _href != 'undefined' && images[i].parentNode.className == 'gal' ) {
				var img = document.createElement( 'img' );
				//img.onmouseover = fix_linked_images_mouseover;
				//img.onclick = fix_linked_images_onclick;
				img.id = '--ipb-img-resizer-' + _count;
				img.title = ipb_global_lang['click_to_view'];
				img.src = images[i].src;
				img.className = images[i].className;
				img.width = images[i].width;				
				img.height = images[i].height;
				var div = document.createElement( 'div' );
				div.innerHTML = _img + '&nbsp;' + lang_clickme;
				div.style.width = img.width + ( _padding * 2 ) + 'px';
				div.className = 'resized-linked-image';
				div.style.paddingTop = _padding + "px";
				div.style.paddingBottom = _padding + "px";
				div.style.paddingLeft = _padding + "px";
				div.style.paddingRight = _padding + "px";
				div.style.position = 'relative';
				div._is_div = 1;
				div._resize_id = _count;
				div._src = _href;
				div.onclick = fix_linked_images_onclick;
				div.onmouseover = fix_linked_images_mouseover;
				div.title = ipb_global_lang['click_to_view'];
				div.appendChild( img );
				images[i].parentNode.href = '#';
				images[i].parentNode.className = '';
				images[i].parentNode.parentNode.appendChild( div, images[i].parentNode );
				images[i].parentNode.parentNode.removeChild( images[i].parentNode );
			}
		}
	}
}

function fix_linked_images_onclick(e) {
	PopUp( this._src, 'popup',screen.width,screen.height,1,1,1);
	return false;
}

function fix_linked_images_mouseover(e) {
	try {
		this.style.cursor='pointer';
	}
	catch(acold) {}
}

function show_ignored_post( pid ) {
	try {
		var post_main   = document.getElementById( 'post-main-'   + pid );
		var post_ignore = document.getElementById( 'post-ignore-' + pid );
		post_main.innerHTML = gal_ignore_cache[ pid ];
	}
	catch( e ) {
		//alert( e );
	}
	return false;
}

function init_ignored_post( pid ) {
	try {
		var post_main   = document.getElementById( 'post-main-'   + pid );
		var post_ignore = document.getElementById( 'post-ignore-' + pid );
		gal_ignore_cache[ pid ] = post_main.innerHTML;
		post_main.innerHTML = post_ignore.innerHTML;
	}
	catch( e ) {
		//alert( e );
	}
}
