var popup_id;

function get_random() {
	return Math.round(Math.random() * Math.random() * 100000);
}

function open_window(url, width, height, name) {
	var w = (width) ? width : 700;
	var h = (height) ? height : 560;
	var window_name = (name) ? name : 'Fly_' + get_random();

	window.open(url, window_name, 'width=' + w + ',height=' + h + ',location=no,status=yes,resizable=yes,scrollbars=yes,titlebar=yes');
}

function phone_gallery(url, max_width) {
	var width = window.screen.width;
	if (max_width + 200 < width) width = max_width + 200;

	var height = window.screen.height - 100;
	if (height > 1000) height = 1000;

	open_window(url, width, height);
}

function filterPhoneList() {
	getFilterMatrix();

	var form_ele = document.getElementById('phone_filter');
	if (form_ele) {
		var containerEle = document.getElementById('phone_list');
		var heightWas = parseInt(containerEle.offsetHeight);
		var types = 0;
		var cameras = 0;
		var features = 0;
		var statuses = 0;
		var inputs = new Array('types', 'cameras', 'features', 'statuses');
		var isList = false;

		for (var i = 0; i < inputs.length; i++) {
			var value = 0;
			var input = form_ele.elements['filter_' + inputs[i]];
			if (!input) input = form_ele.elements['filter_' + inputs[i] + '[]'];

			if (input && input.length > 0) for (var j = 0; j < input.length; j++) {
				if (input[j].checked) value += parseInt(input[j].value);
			}
			eval(inputs[i] + ' = ' + value);
		}

		for (i = 0; i < phoneFilter.length; i++) {
			var obj = document.getElementById(phoneFilter[i][0]);
			if (obj) {
				obj.style.display = (types == 0 || (phoneFilter[i][1] & types)) && (cameras == 0 || (phoneFilter[i][2] & cameras)) && (features == 0 || (phoneFilter[i][3] & features)) && (statuses == 0 || (phoneFilter[i][4] & statuses)) ? 'block' : 'none';
				if (obj.style.display == 'block') isList = true;
			}
		}

		var noPhonesEle = document.getElementById('no_phones');
		if (noPhonesEle) noPhonesEle.style.display = isList ? 'none' : 'block';

/*
		var heightNow = parseInt(containerEle.offsetHeight);
		if (navigator.userAgent.indexOf('MSIE') != -1 && heightNow < heightWas) {
			var contentEle = document.getElementById('page_content');
			if (contentEle) {
				var a = parseInt(contentEle.style.height) - (heightWas - heightNow);
				contentEle.style.height = (a + 25) + 'px';
			}
		}
*/
	}
}

function content_height() {
	if (navigator.userAgent.indexOf('MSIE') != -1) {
		var navigation_column_ele = document.getElementById('page_navigation_column');
		var head_ele = document.getElementById('page_head');
		var content_ele = document.getElementById('page_content');
		var navigation_column_height = navigation_column_ele.offsetHeight;

		if (navigation_column_ele && head_ele && content_ele) {
			content_ele.style.height = navigation_column_height;
			content_ele.style.height = navigation_column_height - head_ele.offsetHeight;
		}
	}
}

function getFilterMatrix() {
	matrix = [[1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], [1, null, null, null, null, null]];

	for (var i = 0; i < matrix.length; i++) {
		for (var j = 0; j < matrix[i].length; j++) {
			if (matrix[i][j] != null) {
				matrix[i][j] = false;

				var container = document.getElementById('filter_item_' + (i + 1) + '-' + (j + 1));
				if (container) {
					var inputs = container.getElementsByTagName('input');
					if (0 < inputs.length) matrix[i][j] = inputs[0].checked;
				}
			}
		}
	}

	for (var i = 0; i < matrix.length; i++) {
		for (var j = 0; j < matrix[i].length; j++) {
			var container = document.getElementById('filter_item_' + (i + 1) + '-' + (j + 1));
			if (container) {
				var class_name = '';
				var left = get_filter_item_left_style(i, j);
				var right = get_filter_item_right_style(i, j);

				if (matrix[i][j]) class_name += 'selected';
				if (left) class_name += (class_name != '' ? ' ' : '') + 'sel_l' + left;
				if (right) class_name += (class_name != '' ? ' ' : '') + 'sel_r' + right;

				container.className = class_name;
			}
		}
	}
}

function get_filter_item_left_style(r, c) {
	if (matrix[r][c]) {
		if (c == 0) {
			var col = get_filter_column_selection(r, c);

			if (col.is_upper) return 4;
			else {
				if (col.is_lower) return 3;
				else return 2;
			}

		} else {
			if (matrix[r][c - 1]) return 1;
			else {
				var col = get_filter_column_selection(r, c);
				var col_next = get_filter_column_selection(r, c - 1);

				if ((col.is_upper && col.is_lower) || col_next.is_upper || col_next.is_lower) return 1;
				else {
					if (col.is_upper || col_next.is_upper) return 4;
					else {
						if (col.is_lower || col_next.is_lower) return 3;
						else return 2;
					}
				}
			}
		}

	} else {
		if (c != 0) {
			var col = get_filter_column_selection(r, c);
			var col_next = get_filter_column_selection(r, c - 1);
			if (col.is_upper && col.is_lower && !matrix[r][c - 1] && !col_next.is_upper && !col_next.is_lower) return 1;
		}
	}

	return 0;
}

function get_filter_item_right_style(r, c) {
	if (matrix[r][c]) {
		if (c == matrix[0].length - 1) {
			var col = get_filter_column_selection(r, c);

			if (col.is_upper && col.is_lower) return 1;
			else {
				if (col.is_upper) return 4;
				else {
					if (col.is_lower) return 3;
					else return 2;
				}
			}

		} else {
			if (matrix[r][c + 1]) return 1;
			else {
				var col = get_filter_column_selection(r, c);
				var col_next = get_filter_column_selection(r, c + 1);

				if ((col.is_upper && col.is_lower) || (col_next.is_upper && col_next.is_lower) || (col.is_upper && col_next.is_lower) || (col.is_lower && col_next.is_upper)) return 1;
				else {
					if (col.is_upper || col_next.is_upper) return 4;
					else {
						if (col.is_lower || col_next.is_lower) return 3;
						else return 2;
					}
				}
			}
		}

	} else {
		var col = get_filter_column_selection(r, c);

		if (col.is_upper && col.is_lower) return 5;
		else {
			if (c != matrix[0].length - 1) {
				var col_next = get_filter_column_selection(r, c + 1);

				if (matrix[r][c + 1] && ((col.is_upper && !col_next.is_lower) || (col.is_lower && !col_next.is_upper))) {
					if (col.is_upper) return 7;
					else return 6;

				} else {
					if ((col.is_upper && col.is_lower) || (col.is_lower && col_next.is_upper) || (col.is_upper && col_next.is_lower)) return 5;
				}
			}
		}
	}

	return 0;
}

function get_filter_column_selection(r, c) {
	var is_upper = false;
	var is_lower = false;
	var i = 0;

	while (true) {
		if (matrix[i] && matrix[i][c]) {
			if (i < r) is_upper = true;
			if (i > r) is_lower = true;
		}

		if (!matrix[i] || matrix[i][c] == null || (is_upper && is_lower)) {
			break;
		} else {
			i++;
		}
	}

	return {is_upper : is_upper, is_lower : is_lower};
}

function get_element_position(ele) {
	var x = ele.offsetLeft;
	var y = ele.offsetTop;

	while (ele.offsetParent != null) {
		ele = ele.offsetParent;
		x += ele.offsetLeft;
		y += ele.offsetTop;
	}

	return {x : x, y : y};
}

function add_event(ele, type, func) {
	if (ele.addEventListener) {
		ele.addEventListener(type, func, false);

	} if (ele.attachEvent) {
		ele.attachEvent('on' + type, func);
	}
}

function remove_event(ele, type, func) {
	if (ele.removeEventListener) {
		ele.removeEventListener(type, func, false);

	} if (ele.detachEvent) {
		ele.detachEvent('on' + type, func);
	}
}

function cancel_event(e) {
	var evt = e ? e : window.event;
	if (evt) evt.cancelBubble = true;
}

function select_a_phone(e) {
	cancel_event(e);
	hide_popup(e);

	var ele = document.getElementById('select_phone');
	if (ele) {
		ele.blur();
		ele.style.display = ele.style.display == 'none' ? 'block' : 'none';

		if (ele.style.display == 'block') {
			var caller_ele = document.getElementById('select_phone_switcher');
			if (caller_ele) {
				var pos = get_element_position(caller_ele);
				ele.style.left = pos.x + 'px';
				ele.style.top = (pos.y + caller_ele.offsetHeight + 5) + 'px';
			}

			add_event(ele, 'click', cancel_event);
			add_event(document, 'click', hide_phone_chooser);

		} else {
			remove_event(document, 'click', hide_phone_chooser);
		}
	}
}

function hide_phone_chooser(e) {
	cancel_event(e);

	var ele = document.getElementById('select_phone');
	if (ele) ele.style.display = 'none';
}

function popup(e, ele_id, caller) {
	cancel_event(e);
	hide_phone_chooser(e);
	if (ele_id != popup_id) hide_popup(e);

	var ele = document.getElementById(ele_id);
	if (ele) {
		ele.blur();
		ele.style.display = ele.style.display == 'none' ? 'block' : 'none';

		if (ele.style.display == 'block') {
			popup_id = ele_id;

			var pos = get_element_position(caller);
			var window_width = 0;
			var window_height = 0;
			var scroll_top = document.body.scrollTop;
			var scroll_left = document.body.scrollLeft;

			if (self.innerWidth) {
				window_width = self.innerWidth;
				window_height = self.innerHeight;

			} else if (document.documentElement && document.documentElement.clientWidth) {
				window_width = document.documentElement.clientWidth;
				window_height = document.documentElement.clientHeight;

			} else if (document.body) {
				window_width = document.body.clientWidth;
				window_height = document.body.clientHeight;
			}

			if (window_width > 0 && window_height > 0) {
				if (pos.x - Math.ceil(ele.clientWidth / 2) > 0 && pos.x + Math.ceil(ele.clientWidth / 2) < window_width) {
					ele.style.left = scroll_left + (pos.x - Math.ceil(ele.clientWidth / 2)) + 'px';
				} else if (window_width - ele.clientWidth > 0) {
					ele.style.left = scroll_left + Math.ceil((window_width - ele.offsetWidth) / 2) + 'px';
				}

				if (pos.y - Math.ceil(ele.clientHeight / 2) > 0 && pos.y + Math.ceil(ele.clientHeight / 2) < window_height) {
					ele.style.top = scroll_top + (pos.y - Math.ceil(ele.clientHeight / 2)) + 'px';
				} else if (window_height - ele.clientHeight > 0) {
					ele.style.top = scroll_top + Math.ceil((window_height - ele.clientHeight) / 2) + 'px';
				}
			}

			add_event(ele, 'click', cancel_event);
			add_event(document, 'click', hide_popup);

		} else {
			popup_id = null;
			remove_event(document, 'click', hide_popup);
		}
	}
}

function hide_popup(e) {
	cancel_event(e);

	if (popup_id) {
		var ele = document.getElementById(popup_id);
		if (ele) ele.style.display = 'none';
	}
}

function change_phone_color(color) {
	var frame = document.getElementById('phone_about_image_frame');

	if (frame && phone_colors) {
		for (var i = 0; i < phone_colors.length; i++) {
			var color_ele = document.getElementById('phone_color_' + phone_colors[i]);
			if (color_ele) {
				color_ele.className = 'phone_color_frame';
				if (color == phone_colors[i]) color_ele.className += ' phone_color_frame_selected';
			}

			var color_label_ele = document.getElementById('phone_color_' + phone_colors[i] + '_label');
			if (color_label_ele) color_label_ele.style.display = color == phone_colors[i] ? 'block' : 'none';

			var color_set_ele = document.getElementById('phone_color_' + phone_colors[i] + '_set');
			if (color_set_ele) color_set_ele.style.display = color == phone_colors[i] ? 'block' : 'none';

			if (color == phone_colors[i]) {
				frame.style.backgroundImage = 'url("' + eval('phone_img_' + phone_colors[i] + '.src') + '")';
				fixMsieBackgroundPng(frame.getAttribute('id'));
			}
		}
	}
}

function fixMsieBackgroundPng(eleId) {
	var ele = document.getElementById(eleId);
	if (ele && navigator.appVersion.indexOf('MSIE') != -1 && parseFloat(navigator.appVersion.split('MSIE')[1]) < 7) {
		var uri = ele.style.backgroundImage.replace(/url\((.+\.png)\)/, '$1');
		if (uri) {
			ele.style.background = 'none';
			ele.style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + uri + '", sizingMethod="crop")';
		}
	}
}

function selectTab(ele, name) {
	var tabs = new Array('phones', 'smartphones', 'gps');

	for (var i = 0; i < tabs.length; i++) {
		var tabEle = document.getElementById('tab_' + tabs[i]);
		if (tabEle) tabEle.style.display = 'none';
	}

	var tabs = ele.parentNode.getElementsByTagName('DIV');
	for (var i = 0; i < tabs.length; i++) {
		tabs[i].className = ele == tabs[i] ? 'tab tab_selected' : 'tab';
	}

	var tabContainer = document.getElementById('tab_' + name);
	if (tabContainer) tabContainer.style.display = 'block';
}