var last_dividing_pointer_X=0;
var system_default_config = {
	playmod: "preview",
	vdecmod: "0",
	vo: "1080P60",
	vo_source: "no",
	vocs: "0",
	vo_cvbs: "PAL",
	vo_rotate: "0",
	vo_scale: "0",
	Luma: 50,
	Contrast: 50,
	Hue: 50,
	Saturationvo: 50,
	ao_volume: 100,
	cvbs_x: 40,
	cvbs_y: 20,
	cvbs_w: 660,
	cvbs_h: 540
};

function IsInteger(interger) {
	var IsInteger = true;
	if (parseInt(interger) == interger) {
		IsInteger = true;
	}
	else {
		IsInteger = false;
	}
	return IsInteger;
}

function get_vo() {
    $.ajax({
        type: "GET",
        url: "http://" + xc_gethost() + "/get_vo",
        dataType: "xml",
        cache: false,
        error: function () {
        },
        success: function (data) {
			$('#vo_mod').val($(data).find("hdmi").find("playmod").text());
			$('#decode_mod').val($(data).find("hdmi").find("vdecmod").text());
            $('#vo').val($(data).find("hdmi").find("vo").text());
            var vo_source=$(data).find("hdmi").find("vo_source").text();
            if(vo_source=="yes"){
				$('#vo_source').attr("checked",true).prop("checked", true);
			}else{
				$('#vo_source').attr("checked",false).prop("checked", false);
			}
            $('#vo_source').val(vo_source);
            $('#vo_color').val($(data).find("hdmi").find("vocs").text());
            $('#vo_cvbs').val($(data).find("hdmi").find("vo_cvbs").text());
            $('#vo_rotate').val($(data).find("hdmi").find("vo_rotate").text());
			$('#vo_scale').val($(data).find("hdmi").find("vo_scale").text());
            var Luma=$(data).find("hdmi").find("Luma").text();
            var Contrast=$(data).find("hdmi").find("Contrast").text();
            var Hue=$(data).find("hdmi").find("Hue").text();
            var Saturationvo=$(data).find("hdmi").find("Saturationvo").text();
			var ao_volume=$(data).find("hdmi").find("ao_volume").text();
			
            $("#system_luma").html(Luma);
            setup_pointer_position($("#system_luma_dividing_rule"),parseInt(Luma)/100*240);
            $("#system_contrast").html(Contrast);
            setup_pointer_position($("#system_contrast_dividing_rule"),parseInt(Contrast)/100*240);
            $("#system_hue").html(Hue);
            setup_pointer_position($("#system_hue_dividing_rule"),parseInt(Hue)/100*240);
            $("#system_saturation").html(Saturationvo);
            setup_pointer_position($("#system_saturation_dividing_rule"),parseInt(Saturationvo)/100*240);
			$("#system_ao_volume").html(ao_volume);
            setup_pointer_position($("#system_ao_volume_dividing_rule"),parseInt(ao_volume)/100*240);

            $("#main_content").removeAttr("class");

			var cvbs_x = $(data).find("hdmi").find("cvbs_x").text();
			var cvbs_y = $(data).find("hdmi").find("cvbs_y").text();
			var cvbs_w = $(data).find("hdmi").find("cvbs_w").text();
			var cvbs_h = $(data).find("hdmi").find("cvbs_h").text();
			$("#system_cvbs_x").val(cvbs_x).attr("value", cvbs_x);
			$("#system_cvbs_y").val(cvbs_y).attr("value", cvbs_y);
			$("#system_cvbs_w").val(cvbs_w).attr("value", cvbs_w);
			$("#system_cvbs_h").val(cvbs_h).attr("value", cvbs_h);
            $("#main_content").removeAttr("class");
        }
    });
}

function system_setting() {
	var playmod = "playmod=" + $('#vo_mod').val();
	var decodemod = "vdecmod=" + $("#decode_mod").val();
    var vo = "vo=" + $('#vo').val();
    var vo_source="vo_source=" + "no";
    if($('#vo_source').prop('checked')){
    	vo_source="vo_source=" + "yes";
    }
    var vo_color = "vocs=" + $('#vo_color').val();
    var vo_cvbs = "vo_cvbs=" + $('#vo_cvbs').val();
    var vo_rotate = "vo_rotate=" + $('#vo_rotate').val();
	var vo_scale = "vo_scale=" + $('#vo_scale').val();
    var luma = "Luma=" + $('#system_luma').attr("value");
    var contrast = "Contrast=" + $('#system_contrast').attr("value");
    var hue = "Hue=" + $('#system_hue').attr("value");
    var saturation = "Saturation=" + $('#system_saturation').attr("value");
    var ao_volume = "ao_volume=" + $('#system_ao_volume').attr("value");
	
	var cvbs_x = $("#system_cvbs_x").val();
	var cvbs_y = $("#system_cvbs_y").val();
	var cvbs_w = $("#system_cvbs_w").val();
	var cvbs_h = $("#system_cvbs_h").val();
	var valid = check_cvbs_position();
	if(valid == true) {
		$.ajax({
			type: "GET",
			url: "http://" + xc_gethost() + "/set_vo" + "?" + playmod + "&" + decodemod + "&" + vo+"&"+vo_source+"&"+vo_color+"&"+vo_cvbs+"&"+vo_rotate+"&"+vo_scale+"&"+luma+"&"+contrast+"&"+hue+"&"+saturation+"&"+ao_volume+"&cvbs_x="+cvbs_x+"&cvbs_y="+cvbs_y+"&cvbs_w="+cvbs_w+"&cvbs_h="+cvbs_h,
			cache: false,
			success: function (data) {
				var ret = $(data).find("return").text();
				if ("ok" == ret) {
					alert($.i18n.prop('system_setting_success'));
				}
				else {
					alert($.i18n.prop('system_setting_failed'));
				}
			},
			error: function (){
				alert($.i18n.prop('system_setting_failed'));
			}
		});
	}
}

function set_dividing_rule_value(rule_id, value) {
	var offset = parseInt(value, 10) / 100 * 240;
	setup_pointer_position($(rule_id), offset);
}

function system_default() {
	$('#vo_mod').val(system_default_config.playmod);
	$('#decode_mod').val(system_default_config.vdecmod);
	$('#vo').val(system_default_config.vo);
	$('#vo_source').attr("checked", system_default_config.vo_source == "yes").prop("checked", system_default_config.vo_source == "yes");
	$('#vo_source').val(system_default_config.vo_source);
	$('#vo_color').val(system_default_config.vocs);
	$('#vo_cvbs').val(system_default_config.vo_cvbs);
	$('#vo_rotate').val(system_default_config.vo_rotate);
	$('#vo_scale').val(system_default_config.vo_scale);

	set_dividing_rule_value("#system_luma_dividing_rule", system_default_config.Luma);
	set_dividing_rule_value("#system_contrast_dividing_rule", system_default_config.Contrast);
	set_dividing_rule_value("#system_hue_dividing_rule", system_default_config.Hue);
	set_dividing_rule_value("#system_saturation_dividing_rule", system_default_config.Saturationvo);
	set_dividing_rule_value("#system_ao_volume_dividing_rule", system_default_config.ao_volume);

	$("#system_cvbs_x").val(system_default_config.cvbs_x).attr("value", system_default_config.cvbs_x);
	$("#system_cvbs_y").val(system_default_config.cvbs_y).attr("value", system_default_config.cvbs_y);
	$("#system_cvbs_w").val(system_default_config.cvbs_w).attr("value", system_default_config.cvbs_w);
	$("#system_cvbs_h").val(system_default_config.cvbs_h).attr("value", system_default_config.cvbs_h);
}

function init_language() {
    jQuery.i18n.properties({
        name: 'common',
        path: '../lang/',
        mode: 'map',
        language: window.parent.get_language_setup(),
        callback: function () {
			$('#system_vo_mod_txt').html($.i18n.prop('system_vo_mod_txt'));
			$('#vo_mod_preview').html($.i18n.prop('vo_mod_preview'));
			$('#vo_mod_playback').html($.i18n.prop('vo_mod_playback'));
			
			$('#system_decode_mod_txt').html($.i18n.prop('system_decode_mod_txt'));
			$('#decode_mod_frame').html($.i18n.prop('decode_mod_frame'));
			$('#decode_mod_url').html($.i18n.prop('decode_mod_url'));

            $('#system_vo_txt').html($.i18n.prop('system_vo_txt'));
            $('#system_vo_source_txt').html($.i18n.prop('system_vo_source_txt'));
            $('#system_vo_color_txt').html($.i18n.prop('system_vo_color_txt'));
            $('#system_vo_cvbs_txt').html($.i18n.prop('system_vo_cvbs_txt'));
            $('#system_vo_rotate_txt').html($.i18n.prop('system_vo_rotate_txt'));
            $('#vo_rotate_unit').html($.i18n.prop('system_vo_rotate_unit_txt'));
			$('#system_vo_scale_txt').html($.i18n.prop('system_vo_scale_txt'));
			$('#vo_scale_disable').html($.i18n.prop('system_vo_scale_disable'));
			$('#vo_scale_enable').html($.i18n.prop('system_vo_scale_enable'));
            $('#system_luma_txt').html($.i18n.prop('system_luma_txt'));
            $('#system_contrast_txt').html($.i18n.prop('system_contrast_txt'));
            $('#system_hue_txt').html($.i18n.prop('system_hue_txt'));
            $('#system_saturation_txt').html($.i18n.prop('system_saturation_txt'));
			$('#system_ao_volume_txt').html($.i18n.prop('system_ao_volume_txt'));
			$('#system_cvbs_x_txt').html($.i18n.prop('system_cvbs_x_txt'));
			$('#system_cvbs_y_txt').html($.i18n.prop('system_cvbs_y_txt'));
			$('#system_cvbs_w_txt').html($.i18n.prop('system_cvbs_w_txt'));
			$('#system_cvbs_h_txt').html($.i18n.prop('system_cvbs_h_txt'));
            
            $('#system_default').val($.i18n.prop('default_setting'));
            $('#system_setting').val($.i18n.prop('system_setting'));
            $('#header_text').html($.i18n.prop('index_systemout'));
        }
    });
}

function setup_pointer_position(element,offset){
	if(offset>0&&offset<241){
		element.find(".rule_pointer").attr("style","margin-left:"+offset+"px;");
		var value=parseInt(offset/240*100);
		element.next(".number").html(value).attr("value",value);
	}
}

function init_dividing_rule(){
	$(".dividing_rule").mousedown(function(e1){
		setup_pointer_position($(this),e1.pageX-$(this).offset().left-5);
		$(this).mousemove(function(e2){
			setup_pointer_position($(this),e2.pageX-$(this).offset().left-5);
		});
	}).mouseup(function(){
		$(this).unbind("mousemove");
	}).mouseleave(function(){
		$(this).unbind("mousemove");
	});
}

function check_cvbs_position()
{
	var x = $("#system_cvbs_x").val();
	var y = $("#system_cvbs_y").val();
	var w = $("#system_cvbs_w").val();
	var h = $("#system_cvbs_h").val();
	//js �ַ�"" == 0
	if(x == "" || y == "" || w == "" || h == "") {
		alert($.i18n.prop('system_cvbs_empty_txt'));
		return false;
	}
	
	var Intx = IsInteger(x);
	var Inty = IsInteger(y);
	var Intw = IsInteger(w);
	var Inth = IsInteger(h);
	x = parseInt(x);
	y = parseInt(y);
	w = parseInt(w);
	h = parseInt(h);
	if(x < 0 || x > 720 || Intx == false) {
		alert("CVBS show X " + $.i18n.prop('system_cvbs_format_txt'));
		return false;
	}
	else if(y < 0 || y > 576 || Inty == false) {
		alert("CVBS show Y " + $.i18n.prop('system_cvbs_format_txt'));
		return false;
	}
	else if(w < 0 || w > 720 || Intw == false) {
		alert("CVBS show W " + $.i18n.prop('system_cvbs_format_txt'));
		return false;
	}
	else if(h < 0 || h > 576 || Inth == false) {
		alert("CVBS show H " + $.i18n.prop('system_cvbs_format_txt'));
		return false;
	}
	else if(x + w < 0 || x + w > 720) {
		alert("CVBS show X and CVBS show W " + $.i18n.prop('system_cvbs_format_txt'));
		return false;
	}
	else if(y + h < 0 || y + h > 576) {
		alert("CVBS show y and CVBS show H " + $.i18n.prop('system_cvbs_format_txt'));
		return false;
	}
	return true;
}

$(function () {
	init_language();
    get_vo();
    init_dividing_rule();
});
