var max_back_wnd = 10;

function get_playlist() {
    $.ajax({
        type: "GET",
        url: "http://" + xc_gethost() + "/get_playlistex",
        dataType: "xml",
        cache: false,
        error: function () {
        },
        success: function (data) {
            var uri = "";
            var uri_audio = "";
			var uri_pindex = 0;
            for (var i = 0; i < max_back_wnd; i++) {
                uri = $(data).find("uri" + i).text();
                $('#uri' + i).attr("value", uri);
                /*uri_audio = $(data).find("uri" + i).attr('audio');
                if (uri_audio == "true" || uri_audio == "1") {
                    document.all.audioselect[i].checked = true;
                    $('#uri_audio_' + i).attr("custom_selected","1");
                }
				uri_pindex = $(data).find("uri" + i).attr("pindex");
				$('#videoswitch_pid_select' + i).attr("value", uri_pindex); */
            }
        }
    });
}

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

function videoswitch_setting() {
    var wnd = $('#wnd').val();
    var chc = $("input[name='audioselect']:checked").val();
    var ptmp = "";
    for (var i = 0; i < max_back_wnd; i++) {
        var url = $('#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;
        }

		//var audio = "0";
        //if (i == chc) {
        //    audio = "1";
        //}

		//var url_playlist_pid = $('#videoswitch_pid_select' + i).val();
        //ptmp += ("uri" + i + "=" + encodeURIComponent(url) + "&uri" + i + "_audio=" + encodeURIComponent(audio) + "&uri" + i + "_pindex=" + url_playlist_pid + "&");
		
		ptmp += ("uri" + i + "=" + encodeURIComponent(url) + "&");
    }
    $.ajax({
        type: "GET",
        url: "http://" + xc_gethost() + "/set_playlistex" + "?" + ptmp,
        cache: false,
        success: function (data) {
        	alert($.i18n.prop('playlist_setting_success'));
        },
        error: function (){
        	alert($.i18n.prop('playlist_setting_failed'));
        }
    });
}

function click_setplay_btn(id) {
	var url = $('#uri' + id).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;
	}

    $.ajax({
        type: "GET",
        url: "http://" + xc_gethost() + "/set_playlist" + "?wnd=1&uri0_audio=1&uri0=" + encodeURIComponent(url),
        cache: false,
        success: function (data) {
        	alert($.i18n.prop('playlist_setting_success'));
        },
        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_back_wnd; i++) {
               	$('#videoswitch_url' + i).html($.i18n.prop('videoswitch_url')+(i+1)+$.i18n.prop('videoswitch_colon'));

                /*$('#videoswitch_audio' + i).html($.i18n.prop('playlist_audio'));
                
				$('#videoswitch_pid' + i).html($.i18n.prop('playlist_pid'));
				for (var pid = 0; pid < 32; pid++) {
					$('#videoswitch_pid_select' + i).append('<option value="' + pid + '">' + $.i18n.prop('playlist_pid_txt') + ' ' + (pid + 1) + '</option>');
				}*/

				$('#setplay_btn_' + i).val($.i18n.prop('videoswitch_setplay'));
            }
            $('#videoswitch_setting').val($.i18n.prop('playlist_setting'));
            $('#header_text').html($.i18n.prop('index_videoswitch'));
        }
    });
}

function init_input_ratio_click(){
	$("input[name='audioselect']").click(function(){
			if($(this).attr("custom_selected")=="1"){
				$(this).attr("checked",false);
				$(this).attr("custom_selected","0");
			}else{
				$(this).attr("checked",true);
				$("input[name='audioselect']").attr("custom_selected","0");
				$(this).attr("custom_selected","1");
			}
		}
	);
}

$(function () {
	language_setup();
    get_playlist();
    //init_input_ratio_click();
});
