var hitachi 	= hitachi || {};
hitachi.product 		= hitachi.product || {};

hitachi.product.LCD = function(id, tv_usb, tv_pcvga, tv_dvd, tv_hdmi, tv_resolution, tv_screensize, projector_wireless, projector_wired, projector_optional, projector_brightness, projector_resolution, projector_contrastratio) {
    this.id = id;
    this.tv_usb = tv_usb;
    this.tv_pcvga = tv_pcvga;
    this.tv_dvd = tv_dvd;
    this.tv_hdmi = tv_hdmi;
    this.tv_resolution = tv_resolution;
    this.tv_screensize = tv_screensize;
    
    this.projector_wireless = projector_wireless;
    this.projector_wired = projector_wired;
    this.projector_optional = projector_optional;
    this.projector_brightness = projector_brightness;
    this.projector_resolution = projector_resolution;
    this.projector_contrastratio = projector_contrastratio;
} 

var TPC 	= hitachi.product.LCD.prototype;

TPC.filter = function(ref) {
	var ts1 = false;
	var ts2 = false;
	var ts3 = false;
	var ts4 = false;
	var ts5 = false
	var ts6 = false;
	var ts7 = false;
	var ts8 = false;
	var ts9 = false;
	var ts10 = false;
	var ts11 = false;
	var ts12 = false;
	if ((ref.tv_usb!= null && ref.tv_usb==true && ref.tv_usb != this.tv_usb)) {
		ts1 = true;
	}
	if ((ref.tv_pcvga!= null && ref.tv_pcvga==true && ref.tv_pcvga != this.tv_pcvga)) {
		ts2 = true;
	}
	if ((ref.tv_dvd!= null && ref.tv_dvd==true && ref.tv_dvd != this.tv_dvd)) {
		ts3 = true;
	}
	if (ref.tv_hdmi != null && ref.tv_hdmi>this.tv_hdmi && ref.tv_hdmi>1){
		ts4 = true;
	}
	if (ref.tv_resolution != null && ref.tv_resolution>this.tv_resolution && ref.tv_resolution>1){
		ts5 = true;
	}
	if (ref.tv_screensize != null && ref.tv_screensize>this.tv_screensize && ref.tv_screensize>1){
		ts6 = true;
	}
	
	if ((ref.projector_wireless!= null && ref.projector_wireless==true && ref.projector_wireless != this.projector_wireless)) {
		ts7 = true;
	}
	if ((ref.projector_wired!= null && ref.projector_wired==true && ref.projector_wired != this.projector_wired)) {
		ts8 = true;
	}
	if ((ref.projector_optional!= null && ref.projector_optional==true && ref.projector_optional != this.projector_optional)) {
		ts9 = true;
	}
	if (ref.projector_brightness != null && ref.projector_brightness>this.projector_brightness && ref.projector_brightness>=1){
		ts10 = true;
	}
	if (ref.projector_resolution != null && ref.projector_resolution!=this.projector_resolution && ref.projector_resolution>=1){
		ts11 = true;
	}
	if (ref.projector_contrastratio != null && ref.projector_contrastratio>this.projector_contrastratio && ref.projector_contrastratio>1){
		ts12 = true;
	}	
	
	var rval = false;
	

	if (currentproducttype==6){
		if (ts7 || ts8 || ts9 || ts10 || ts11 || ts12){
			rval = true;
		}else{
			rval = false;
		}
	}else{
		if (ts1 || ts2 || ts3 || ts4 || ts5 || ts6){
			rval = true;
		}else{
			rval = false;
		}
	}

	return rval;
}

hitachi.product.nv = function(input) {
				var val = false;
				try{
					if (input.checked){
						val = true;
					}
				}catch(err){
					val = false;
				}
        return val;
}
function format_number(pnumber,decimals){
	return parseInt(pnumber);
}

function getWIDTH(num1,num2,num3){
	var lastnum = 100;
	for (var i=100;i<150;i++){
		if (((i%num1)<3) && ((i%num2)<3) && ((i%num3)<3)){
			lastnum = i;
		}
	}
	return lastnum;
}

hitachi.product.init = function() {
    var B = hitachi.product;
    B.SLIDERWIDTH = 150;
    if (currentproducttype==6){
    	B.SLIDERWIDTH = getWIDTH(probrightness.length-2,proresolutions.length,procontrast.length-2);
    	$('slider1Background').style.width=(B.SLIDERWIDTH+10)+"px";
    	$('slider2Background').style.width=(B.SLIDERWIDTH+10)+"px";
    	$('slider3Background').style.width=(B.SLIDERWIDTH+10)+"px";
    	
	    B.SLIDERSTEP1 = format_number(B.SLIDERWIDTH/(probrightness.length-2),0);
	    B.SLIDERSTEP2 = format_number(B.SLIDERWIDTH/(proresolutions.length-2),0);
	    B.SLIDERSTEP3 = format_number(B.SLIDERWIDTH/(procontrast.length-2),0);
    }else{
    	B.SLIDERWIDTH = getWIDTH(tvhdmis.length-2,tvresolutions.length-2,tvscreensizes.length-2);
    	$('slider1Background').style.width=(B.SLIDERWIDTH+10)+"px";
    	$('slider2Background').style.width=(B.SLIDERWIDTH+10)+"px";
    	$('slider3Background').style.width=(B.SLIDERWIDTH+10)+"px";
    	
	    B.SLIDERSTEP1 = format_number(B.SLIDERWIDTH/(tvhdmis.length-2),0);
	    B.SLIDERSTEP2 = format_number(B.SLIDERWIDTH/(tvresolutions.length-2),0);
	    B.SLIDERSTEP3 = format_number(B.SLIDERWIDTH/(tvscreensizes.length-2),0);
    }
    
    B.mintv_hdmi  = 1000000; 
    B.mintv_resolution = 1000000;
    B.mintv_screensize  = 1000000;
    B.maxtv_hdmi  = 0;
    B.maxtv_resolution = 0;
    B.maxtv_screensize  = 0;
    
    B.minprojector_brightness  = 1000000; 
    B.minprojector_resolution = 1000000;
    B.minprojector_contrastratio  = 1000000;
    B.maxprojector_brightness  = 0;
    B.maxprojector_resolution = 0;
    B.maxprojector_contrastratio  = 0;
    
    B.maxprojector_brightness = probrightness.length-1;
    B.maxprojector_resolution = proresolutions.length-1;
    B.maxprojector_contrastratio = procontrast.length-1;
    
    B.minprojector_brightness = 0;
    B.minprojector_resolution = 0;
    B.minprojector_contrastratio = 0;
    
    
    B.maxtv_hdmi = tvhdmis.length-1;
    B.maxtv_resolution = tvresolutions.length-1;
    B.maxtv_screensize = tvscreensizes.length-1;
    
    B.mintv_hdmi = 0;
    B.mintv_resolution = 0;
    B.mintv_screensize = 0;

    B.slider1 = YAHOO.widget.Slider.getHorizSlider("slider1Background", "slider1Image", 0, B.SLIDERWIDTH, B.SLIDERSTEP1); 
    B.slider1.subscribe("change", hitachi.product.update);
    B.slider1.setValue(0);
    
    B.slider2 = YAHOO.widget.Slider.getHorizSlider("slider2Background", "slider2Image", 0, B.SLIDERWIDTH, B.SLIDERSTEP2); 
    B.slider2.subscribe("change", hitachi.product.update); 
    B.slider2.setValue(0);
    
    B.slider3 = YAHOO.widget.Slider.getHorizSlider("slider3Background", "slider3Image", 0, B.SLIDERWIDTH, B.SLIDERSTEP3); 
    B.slider3.subscribe("change", hitachi.product.update);
    B.slider3.setValue(0);
}


hitachi.product.update = function(ele) {
    var B = hitachi.product;
    
    var new_tv_usb;
    var new_tv_pcvga;
    var new_tv_dvd;
    var new_tv_hdmi;
    var new_tv_resolution;
    var new_tv_screensize;
    var new_projector_wireless;
    var new_projector_wired;
    var new_projector_optional;
    var new_projector_brightness;
    var new_projector_resolution;
    var new_projector_contrastratio;
    
    var tp1,tp2,tp3;
    if (currentproducttype==6){
    	new_projector_wireless = B.nv( $("projector_wireless_checkbox") );
    	new_projector_wired = B.nv( $("projector_wired_checkbox") );
    	new_projector_optional = B.nv( $("projector_optional_checkbox") );
    	
    	
    	new_projector_brightness = ((B.maxprojector_brightness-B.minprojector_brightness) * (B.slider1.getValue()/B.SLIDERWIDTH)) + B.minprojector_brightness;
    	new_projector_resolution = ((B.maxprojector_resolution-B.minprojector_resolution) * (B.slider2.getValue()/B.SLIDERWIDTH)) + B.minprojector_resolution;
    	new_projector_contrastratio = ((B.maxprojector_contrastratio-B.minprojector_contrastratio) * (B.slider3.getValue()/B.SLIDERWIDTH)) + B.minprojector_contrastratio;
			
			if (new_projector_brightness>=(probrightness.length-1)){
				new_projector_brightness = parseInt(new_projector_brightness);
			}else if (new_projector_brightness<1){
				new_projector_brightness = 1;
			}else{
				new_projector_brightness = parseInt(new_projector_brightness)+1;
			}
			
		//	console.log("new_projector_resolution",new_projector_resolution);
			
			if (new_projector_resolution>=(proresolutions.length-1)){
				new_projector_resolution = parseInt(new_projector_resolution);
			}else if (new_projector_resolution<=0){
				new_projector_resolution = 0;
			}else{
				new_projector_resolution = parseInt(new_projector_resolution);
			}
			if (new_projector_resolution==5)new_projector_resolution=4;
			//console.log("new_projector_resolution",new_projector_resolution);
			
			
			if (new_projector_contrastratio>=(procontrast.length-1)){
				new_projector_contrastratio = parseInt(new_projector_contrastratio);
			}else if (new_projector_contrastratio<1){
				new_projector_contrastratio = 1;
			}else{
				new_projector_contrastratio = parseInt(new_projector_contrastratio)+1;
			}
    	tp1 = probrightness[new_projector_brightness];
    	tp2 = proresolutions[new_projector_resolution];
    	tp3 = procontrast[new_projector_contrastratio];
    	//console.log("tp2",tp2);
    }else{
    	new_tv_usb = B.nv( $("tv_usb_checkbox") );
    	new_tv_pcvga = B.nv( $("tv_pcvga_checkbox") );
    	new_tv_dvd = B.nv( $("tv_dvd_checkbox") );

			
    	new_tv_hdmi = ((B.maxtv_hdmi-B.mintv_hdmi) * (B.slider1.getValue()/B.SLIDERWIDTH)) + B.mintv_hdmi;
    	new_tv_resolution = ((B.maxtv_resolution-B.mintv_resolution) * (B.slider2.getValue()/B.SLIDERWIDTH)) + B.mintv_resolution;
    	new_tv_screensize = ((B.maxtv_screensize-B.mintv_screensize) * (B.slider3.getValue()/B.SLIDERWIDTH)) + B.mintv_screensize;
    	
    	/*
			console.log("B.maxtv_hdmi",B.maxtv_hdmi);
			console.log("B.mintv_hdmi",B.mintv_hdmi);
			console.log("B.slider1.getValue()",B.slider1.getValue());
			console.log("B.SLIDERWIDTH",B.SLIDERWIDTH);
			console.log("new_tv_hdmi",new_tv_hdmi);
			*/
			if (new_tv_hdmi>=(tvhdmis.length-1)){
				new_tv_hdmi = parseInt(new_tv_hdmi);
			}else if (new_tv_hdmi<1){
				new_tv_hdmi = 1;
			}else{
				new_tv_hdmi = parseInt(new_tv_hdmi)+1;
			}
			
			if (new_tv_resolution>=(tvresolutions.length-1)){
				new_tv_resolution = parseInt(new_tv_resolution);
			}else if (new_tv_resolution<1){
				new_tv_resolution = 1;
			}else{
				new_tv_resolution = parseInt(new_tv_resolution)+1;
			}
			
			if (new_tv_screensize>=(tvscreensizes.length-1)){
				new_tv_screensize = parseInt(new_tv_screensize);
			}else if (new_tv_screensize<1){
				new_tv_screensize = 1;
			}else{
				new_tv_screensize = parseInt(new_tv_screensize)+1;
			}
    	
    	tp1 = tvhdmis[new_tv_hdmi];
    	tp2 = tvresolutions[new_tv_resolution];
    	tp3 = tvscreensizes[new_tv_screensize];
    }
    
    $('slider1Value').innerHTML = tp1;
    if (tp2=="ALL"){
    	//$('minP').style.display = "none";
    }else{
    	//$('minP').style.display = "block";
    }
    $('slider2Value').innerHTML = tp2;
    $('slider3Value').innerHTML = tp3;
	    
    var ref = new hitachi.product.LCD(0, new_tv_usb,new_tv_pcvga,new_tv_dvd,new_tv_hdmi,new_tv_resolution,new_tv_screensize,new_projector_wireless,new_projector_wired,new_projector_optional,new_projector_brightness,new_projector_resolution,new_projector_contrastratio);
    for(var i=0,len=hitachitvprojects.length;i<len;++i) {
        var p = hitachitvprojects[i];
        if(typeof p != "object" || !$(p.id)) {
            continue;
        }
        var pd = $(p.id);
        if(p.filter(ref)) {
            p.valid = false;
            pd.setStyle( {opacity: 0.33} );
        } else {
            p.valid = true;
            pd.setStyle({opacity: 1});
        }
    }
}