var last_dividing_pointer_X=0;

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

function low_delay_hint_handle(){
	var vo_low_delay=$("#low_delay").val();
	if(vo_low_delay==0){
		$("#system_low_delay_hint_text").attr("style","display:none;");
	}else if(vo_low_delay==1){
		$("#system_low_delay_hint_text").removeAttr("style");
	}
}

function get_vo() {
    $.ajax({
        type: "GET",
        url: "http://" + xc_gethost() + "/get_vo",
        dataType: "xml",
        cache: false,
        error: function () {
        },
        success: function (data) {
        	var hdr_mode = $(data).find("hdmi").find("hdr_mod").text();
        	if(hdr_mode>=0&&hdr_mode<=4){
        		$("#hdr_mode").find("option[value='"+hdr_mode+"']").attr("selected","selected");
        	}
        	var vo_low_delay = $(data).find("hdmi").find("low_delay").text();
	    	if(vo_low_delay==0){
	    		$("#low_delay").find("option[value='0']").attr("selected","selected");
	    	}else if(vo_low_delay==1){
	    		$("#low_delay").find("option[value='1'").attr("selected","selected");
	    	}
	    	low_delay_hint_handle();
	    	
            $('#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);
			}else{
				$('#vo_source').attr("checked",false);
			}
            $('#vo_source').val(vo_source);
            $('#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();
            $("#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);
            $("#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").attr("value", cvbs_x);
			$("#system_cvbs_y").attr("value", cvbs_y);
			$("#system_cvbs_w").attr("value", cvbs_w);
			$("#system_cvbs_h").attr("value", cvbs_h);
            $("#main_content").removeAttr("class");
        }
    });
}

function system_setting() {
    var hdr = "hdr_mod=" + $('#hdr_mode').val();
	var vo_low_delay = "low_delay=" + $('#low_delay').val();
    var vo = "vo=" + $('#vo').val();
    var vo_source="vo_source=" + "no";
    //if($('#vo_source').prop('checked')){
    //	vo_source="vo_source=" + "yes";
    //}
    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 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();
	var valid = true;
	if(valid == true) {
		//var cvbs_pos = "&cvbs_x=" + cvbs_x + "&cvbs_y=" + cvbs_y + "&cvbs_w=" + cvbs_w + "&cvbs_h=" + cvbs_h;
		$.ajax({
			type: "GET",
			url: "http://" + xc_gethost() + "/set_vo" + "?" + hdr + "&" + vo + "&" + vo_source + "&" + vo_cvbs + "&" + luma + "&" + contrast + "&" + hue + "&" + saturation + "&" + vo_low_delay,
			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 init_language() {
    jQuery.i18n.properties({
        name: 'common',
        path: '../lang/',
        mode: 'map',
        language: window.parent.get_language_setup(),
        callback: function () {
            $('#system_hdr_mode_txt').html($.i18n.prop('system_hdr_mode_txt'));
            $('#system_low_delay_txt').html($.i18n.prop('system_low_delay_txt'));
            $("#low_delay option[value='0']").html($.i18n.prop('system_low_delay_disable'));
            $("#low_delay option[value='1']").html($.i18n.prop('system_low_delay_enable'));
            $('#system_low_delay_hint_text').html($.i18n.prop('system_low_delay_hint_text'));
            
            $('#system_vo_txt').html($.i18n.prop('system_vo_txt'));
            $('#system_vo_source_txt').html($.i18n.prop('system_vo_source_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_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_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();
    $("#low_delay").change(function(){
    	low_delay_hint_handle();
    });
});
