﻿
function isUrl(v) {
	return (typeof v == 'string' && v.match(/^(?:http|ftp)s?\:\/\/(?:[^\<\>\*"]+|[a-z0-9\/\._\- !]+)$/i));
}
function popup(url, width, height, name) {
	if (isUrl(url)) {
		return window.open(url.replace(/&amp;/g, '&'),
				name ? name: 'PopUp',
				'statusbar=no,menubar=no,toolbar=no,scrollbars=yes,resizable=yes'
				+ (width ? ',width=' + parseInt(width) : '')
				+ (height ? ',height=' + parseInt(height) : '')
		);
	}
	return false;
}
function resize(id, maxWidth, maxHeight) {
	var image = jQuery(id);
	if (image.attr('width') && image.attr('height')) {
		if (!maxWidth) maxWidth = 620;
		if (!maxHeight) maxHeight = 620;
		var args = new Array(maxWidth, maxHeight, image.attr('width'), image.attr('height')); // max_width, max_height, cur_width, cur_height
		var ret  = new Array(args[2], args[3]);
		if (args[2] > args[0]) {
			ret[0]  = args[0]; ret[1]  = Math.ceil((args[3] * ((args[0] * 100) / args[2])) / 100);
			args[2] = ret[0] ; args[3] = ret[1];
		}
		if (args[3] > args[1]) {
			ret[1] = args[1]; ret[0] = Math.ceil((args[2] * ((args[1] * 100) / args[3])) / 100);
		}
		image.attr('width', ret[0]); image.attr('height', ret[1]);
	}
}
function scale(width) {
	jQuery.each(jQuery('#entries').find('img'), function() {
		if (this.width > width) {
			var _height = this.height;
			var _width = this.width;
			resize(this, width, this.height);
			jQuery(this).before('<a href="javascript:;" onclick="return popup(\'' + this.src + '\', ' + (_width + 20) + ', ' + (_height + 20) + ')" title="Xem hình với kích thước đầy đủ"><img src="' + this.src + '" alt="" width="' + width + '"/></a>');
			jQuery(this).remove();
		}
		
	
	});
	
	jQuery.each(jQuery('#entries').find('table'), function() {
		if (this.width > width) {
			this.width = width;
		}	
	});
	
	jQuery.each(jQuery('#entries').find('div'), function() {
		if (this.width > width) {
			this.width = width;
		}	
	});
	
	jQuery.each(jQuery('#entries').find('span'), function() {
		if (this.width > width) {
			this.width = width;
		}	
	});
	
	jQuery.each(jQuery('#entries').find('a'), function() {
		if (this.width > width) {
			this.width = width;
		}	
	});
	
	jQuery.each(jQuery('#entries').find('table'), function() {
		if (this.width > width) {
			this.width = width;
		}	
	});
}

jQuery.event.add(window, 'load', function(){scale(620)});
