var spefull_search_type = 'articles';

function check_clear_field(field, val, clear_class) {
	if (field.value == val) {
		field.value = '';
		if (clear_class) {
			arr = Array();
			classes = field.className.split(' ');
			for (var c in classes) {
				if (classes[c] != 'empty') {
					arr.push(classes[c]);
				}
			}
			field.className = arr.join(' ');
		}
	}
}
function check_reset_field(field, val, clear_class) {
	if (field.value == '') {
		if (clear_class) {
			field.className += ' empty';
		}
		field.value = val;
	}
}
function set_search_type(type) {
	self.focus();
	//self.blur();
	var all_types = new Array('articles', 'forums', 'accessories');
	all_types = new Array('articles', 'forums', 'accessories');

	for (i = 0; i < all_types.length; i++) {	//	reset all types
		document.getElementById('under_' + all_types[i]).className = all_types[i];
	}
	
	document.getElementById('under_' + type).className = type + ' ' + type + '_active';
	spefull_search_type = type;
}
function submit_search_form() {
	document.getElementById('search_' + spefull_search_type + '_input').value = document.getElementById('search_input_field').value;
	document.getElementById('search_' + spefull_search_type + '_form').submit();
	return false;
}
function wallpaper_setup() {
	wallpaper = new Object();
	wallpaper.load = function() {
		this.img = $('#wallpaper_image');
		this.cropper = $('#wallpaper_maker_cropper');
		this.cropper_bkg = $('#wallpaper_maker_cropper_bkg');
		this.img_width = this.img.width();
		this.img_height = this.img.height();
		this.img_base_width = 0;
		this.img_base_height = 0;
		this.cropper_height = 0;
		this.cropper_width = 0;
		this.stencil = $('#wallpaper_maker_bkg');
		this.stencil_height = this.stencil.height();
		this.stencil_width = this.stencil.width();
		this.keep_aspect = $('#wallpaper_keep_aspect_ratio').attr('checked');
		this.keep_aspect_addtl = $('#wallpaper_aspect_additional');
		this.dragger_element = document.getElementById('wallpaper_drag_overlay');
		this.dragger_image = document.getElementById('wallpaper_image');
		$('#wallpaper_aspect_additional div.color_picker').farbtastic(wallpaper.set_background_color);
		
		this.set_background_color('#7F7F7F');
		var selected_size = $('.wallpaper_dimension:checked').val();
		selected_size = selected_size.split('x');
		this.cropper_size(selected_size[0], selected_size[1]);
		this.cropper_center(this.stencil_width / 2, this.stencil_height / 2);
		
		// setup overlay for moving image
		Drag.init(this.dragger_element, this.dragger_image, -5000, this.stencil_width - 25, -5000, this.stencil_height - 25);
		this.dragger_image.onDrag = function(x, y) {
			if (wallpaper.keep_aspect) {
				var min_left = wallpaper.img.width() * -1 + 25;
				var min_top = wallpaper.img.height() * -1 + 25;
				
				if (parseInt(wallpaper.img.css('top')) < min_top) {
					wallpaper.img.css('top', min_top);
				}
				if (parseInt(wallpaper.img.css('left')) < min_left) {
					wallpaper.img.css('left', min_left);
				}
			}
			else {
				wallpaper.img.css('left', parseInt(wallpaper.cropper.css('left')) + 1);
				wallpaper.img.css('top', parseInt(wallpaper.cropper.css('top')) + 1);
			}
			
			
		}
		this.dragger_image.onDragEnd = function(x, y) {
			wallpaper.set_form_vals();
		}
		this.toggle_aspect_addtl();
		
		this.cropper_bkg.show();
		this.cropper.show();
		this.img.css('visibility', 'visible');
	}
	wallpaper.cropper_size = function(w, h) {
		w = parseInt(w);
		h = parseInt(h);
		var middle_left;
		var middle_top;
		
		this.cropper_width = w;
		this.cropper_height = h;
		if (this.keep_aspect) {
			middle_left = parseInt(this.cropper.css('left'), 10) + (parseInt(this.cropper.width(), 10) / 2) + 1;
			middle_top = parseInt(this.cropper.css('top'), 10) + (parseInt(this.cropper.height(), 10) / 2) + 1;
			
			var percent_left = w / this.img_width;
			var percent_top = h / this.img_height;
			var base_width;
			var base_height;
			
			if (percent_left < percent_top) {
				base_width = (this.img_width < w) ? this.img_width : w;
				base_height = 'auto';
			}
			else {
				base_height = (this.img_height < h) ? this.img_height : h;
				base_width = 'auto';
			}
			
			this.img.css('width', base_width);
			this.img.css('height', base_height);
			this.img_base_width = this.img.width();
			this.img_base_height = this.img.height();
		}
		else {
			middle_left = this.stencil_width / 2;
			middle_top = this.stencil_height / 2
			
			this.img.css('width', w);
			this.img.css('height', h);
		}
		this.img.css('left', this.stencil_width / 2 - this.img.width() / 2);
		this.img.css('top', this.stencil_height / 2 - this.img.height() / 2);
		
		this.cropper.css('width', w);
		this.cropper.css('height', h);
		this.cropper_bkg.css('width', w);
		this.cropper_bkg.css('height', h);
		this.cropper_center(middle_left, middle_top);
		this.toggle_aspect_addtl();
		this.set_form_vals();
	}
	wallpaper.set_form_vals = function() {
		$('#form_image_width').val(this.img.width());
		$('#form_image_height').val(this.img.height());
		$('#form_image_top').val(parseInt(this.img.css('top')) - parseInt(this.cropper.css('top')) - 1);
		$('#form_image_left').val(parseInt(this.img.css('left')) - parseInt(this.cropper.css('left')) - 1);
	}
	wallpaper.cropper_center = function(middle_left, middle_top) {
		if (!this.keep_aspect) {
			middle_left = this.stencil_width / 2;
			middle_top = this.stencil_height / 2;
		}
		var left = middle_left - (this.cropper.width() / 2) - 1;
		var top = middle_top - (this.cropper.height() / 2) - 1;
		if (left < 0) {
			left = 0;
		}
		if (top < 0) {
			top = 0;
		}
		if (left + this.cropper_width > this.stencil_width) {
			left = this.stencil_width - this.cropper_width;
		}
		if (top + this.cropper_height > this.stencil_height) {
			top = this.stencil_height - this.cropper_height;
		}
		this.cropper.css('left', left);
		this.cropper.css('top', top);
		this.cropper_bkg.css('left', left + 1);
		this.cropper_bkg.css('top', top + 1);
	}
	wallpaper.resize_by = function(p) {
		p = parseFloat(p);
		this.img.css('width', this.img_base_width * p);
		this.img.css('height', this.img_base_height * p);
		this.img.css('left', this.stencil_width / 2 - this.img.width() / 2);
		this.img.css('top', this.stencil_height / 2 - this.img.height() / 2);
		this.set_form_vals();
	}
	wallpaper.toggle_aspect_ratio = function() {
		this.keep_aspect = !this.keep_aspect;
		this.toggle_aspect_addtl();
		this.cropper_size(this.cropper.width(), this.cropper.height());
	}
	wallpaper.toggle_aspect_addtl = function() {
		this.keep_aspect_addtl.css('display', this.keep_aspect ? '' : 'none');
		if (this.keep_aspect) {
			this.resize_by($('#wallpaper_aspect_additional select').val());
		}
	}
	wallpaper.set_background_color = function(color) {
		$('#wallpaper_aspect_additional input.color_picker').val(color);
		wallpaper.cropper_bkg.css('background-color', color);
	}
	wallpaper.load();
}
function update_wallpaper_css() {
	$('#body .entry').css('background', 'none');
	$('#body .title').css('display', 'none');
}





function spe_select_all(obj)
{ var text_val=eval(obj);
text_val.focus();
text_val.select();
if(!document.all) return; // IE only
r= text_val.createTextRange();
r.execCommand('copy');
}

