var max_turn_wnd = 64;
var turn_cache_list = [];
var turn_audio_list = [];
var turn_pindex_list = [];

function check_url_valid(url) {
	var temp_url = $.trim(url);
	if (temp_url.indexOf(" ") != -1) {
		return -1;
	}
	return temp_url;
}

function check_turn_time_valid(time_val) {
	var text = $.trim(time_val);
	if (!/^\d+$/.test(text)) {
		return false;
	}
	var seconds = parseInt(text, 10);
	return seconds > 0;
}

function get_turn() {
	$.ajax({
		type: "GET",
		url: "http://" + xc_gethost() + "/get_turn",
		dataType: "xml",
		cache: false,
		error: function () {
		},
		success: function (data) {
			var playlist = $(data).find("playlist");
			$('#turn_enable').attr("value", playlist.attr("enable"));
			$('#turn_time').attr("value", playlist.attr("time"));
			for (var i = 0; i < max_turn_wnd; i++) {
				var uri_obj = $(data).find("uri" + i);
				$('#turn_uri' + i).attr("value", uri_obj.text());
				turn_cache_list[i] = uri_obj.attr("cache") || "40";
				turn_audio_list[i] = uri_obj.attr("audio") || "0";
				turn_pindex_list[i] = uri_obj.attr("pindex") || "0";
			}
		}
	});
}

function videoturn_setting() {
	var turn_enable = $('#turn_enable').val();
	var turn_time = $('#turn_time').val();
	if (!check_turn_time_valid(turn_time)) {
		alert($.i18n.prop('videoturn_time_error'));
		return;
	}

	var ptmp = "enable=" + encodeURIComponent(turn_enable) + "&time=" + encodeURIComponent($.trim(turn_time)) + "&";
	for (var i = 0; i < max_turn_wnd; i++) {
		var url = $('#turn_uri' + i).val();
		url = check_url_valid(url);
		if (url == -1) {
			alert($.i18n.prop("playlist_format_error_start_info") + i + $.i18n.prop("playlist_format_error_end_info"));
			return;
		}
		ptmp += ("uri" + i + "=" + encodeURIComponent(url)
			+ "&uri" + i + "_cache=" + encodeURIComponent(turn_cache_list[i] || "40")
			+ "&uri" + i + "_audio=" + encodeURIComponent(turn_audio_list[i] || "0")
			+ "&uri" + i + "_pindex=" + encodeURIComponent(turn_pindex_list[i] || "0")
			+ "&");
	}

	$.ajax({
		type: "POST",
		url: "http://" + xc_gethost() + "/set_turn",
		data: ptmp,
		cache: false,
		success: function () {
			alert($.i18n.prop('playlist_setting_success'));
			get_turn();
		},
		error: function () {
			alert($.i18n.prop('playlist_setting_failed'));
		}
	});
}

function parentpage_adjust_height() {
	window.parent.setup_iframe_height();
}

function language_setup() {
	var current_language = window.parent.get_language_setup();
	jQuery.i18n.properties({
		name: 'common',
		path: '../lang/',
		mode: 'map',
		language: current_language,
		callback: function () {
			for (var i = 0; i < max_turn_wnd; i++) {
				$('#videoturn_url' + i).html($.i18n.prop('videoturn_url') + (i + 1) + $.i18n.prop('videoswitch_colon'));
			}
			$('#videoturn_header').html($.i18n.prop('videoturn_header'));
			$('#videoturn_enable_txt').html($.i18n.prop('videoturn_enable_txt'));
			$('#videoturn_time_txt').html($.i18n.prop('videoturn_time_txt'));
			$('#videoturn_time_unit').html($.i18n.prop('videoturn_time_unit'));
			$('#turn_enable_disable').html($.i18n.prop('index_common_disable'));
			$('#turn_enable_enable').html($.i18n.prop('index_common_enable'));
			$('#videoturn_setting').val($.i18n.prop('playlist_setting'));
			$('#header_text').html($.i18n.prop('index_videoturn'));
			parentpage_adjust_height();
		}
	});
}

$(function () {
	language_setup();
	get_turn();
});
