var hitachi 	= hitachi || {};
hitachi.product 		= hitachi.product || {};

hitachi.product.LCD = function(id, tv_screensize, tv_freeview_hd, tv_led, tv_hd, tv_freeview, tv_dvd, tv_usb, dr_harddrive, dr_freeview_hd, dr_twin, dr_pause, dr_freeview, dr_hdmi,projector_wireless, projector_wired, projector_optional, projector_brightness, projector_resolution, projector_contrastratio) {
    this.id = id;
    
    this.tv_screensize = tv_screensize; //select
    this.tv_freeview_hd = tv_freeview_hd;
    this.tv_led = tv_led;
    this.tv_hd = tv_hd;
    this.tv_freeview = tv_freeview;
    this.tv_dvd = tv_dvd;
    this.tv_usb = tv_usb;
    
    this.dr_harddrive = dr_harddrive;//select
    this.dr_freeview_hd = dr_freeview_hd;
    this.dr_twin = dr_twin;
    this.dr_pause = dr_pause;
    this.dr_freeview = dr_freeview;
    this.dr_hdmi = dr_hdmi;
    
    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) {
	//tv
	var ts1 = false;
	var ts2 = false;
	var ts3 = false;
	var ts4 = false;
	var ts5 = false
	var ts6 = false;
	var ts7 = false;
	//digital
	var ts8 = false;
	var ts9 = false;
	var ts10 = false;
	var ts11 = false;
	var ts12 = false;
	var ts13 = false;
  //lcd projector
 	var ts14 = false;
	var ts15 = false;
	var ts16 = false;
	var ts17 = false;
	var ts18 = false;
	var ts19 = false;
	
	//tv
	if (ref.tv_screensize != null && ref.tv_screensize>this.tv_screensize && ref.tv_screensize>1){
		ts1 = true;
	}
	if ((ref.tv_freeview_hd!= null && ref.tv_freeview_hd==true && ref.tv_freeview_hd != this.tv_freeview_hd)) {
		ts2 = true;
	}
	if ((ref.tv_led!= null && ref.tv_led==true && ref.tv_led != this.tv_led)) {
		ts3 = true;
	}
	if ((ref.tv_hd!= null && ref.tv_hd==true && ref.tv_hd != this.tv_hd)) {
		ts4 = true;
	}
	if ((ref.tv_freeview!= null && ref.tv_freeview==true && ref.tv_freeview != this.tv_freeview)) {
		ts5 = true;
	}
	if ((ref.tv_dvd!= null && ref.tv_dvd==true && ref.tv_dvd != this.tv_dvd)) {
		ts6 = true;
	}
	if ((ref.tv_usb!= null && ref.tv_usb==true && ref.tv_usb != this.tv_usb)) {
		ts7 = true;
	}
	///digital
	if (ref.dr_harddrive != null && ref.dr_harddrive>this.dr_harddrive && ref.dr_harddrive>1){
		ts8 = true;
	}
	if ((ref.dr_freeview_hd!= null && ref.dr_freeview_hd==true && ref.dr_freeview_hd != this.dr_freeview_hd)) {
		ts9 = true;
	}
	if ((ref.dr_twin!= null && ref.dr_twin==true && ref.dr_twin != this.dr_twin)) {
		ts10 = true;
	}
	if ((ref.dr_pause!= null && ref.dr_pause==true && ref.dr_pause != this.dr_pause)) {
		ts11 = true;
	}
	if ((ref.dr_freeview!= null && ref.dr_freeview==true && ref.dr_freeview != this.dr_freeview)) {
		ts12 = true;
	}
	if ((ref.dr_hdmi!= null && ref.dr_hdmi==true && ref.dr_hdmi != this.dr_hdmi)) {
		ts13 = true;
	}
	//lcd projector
	
	if ((ref.projector_wireless!= null && ref.projector_wireless==true && ref.projector_wireless != this.projector_wireless)) {
		ts14 = true;
	}
	if ((ref.projector_wired!= null && ref.projector_wired==true && ref.projector_wired != this.projector_wired)) {
		ts15 = true;
	}
	if ((ref.projector_optional!= null && ref.projector_optional==true && ref.projector_optional != this.projector_optional)) {
		ts16 = true;
	}
	if (ref.projector_brightness != null && ref.projector_brightness>this.projector_brightness && ref.projector_brightness>=1){
		ts17 = true;
	}
	if (ref.projector_resolution != null && ref.projector_resolution!=this.projector_resolution && ref.projector_resolution>=1){
		ts18 = true;
	}
	if (ref.projector_contrastratio != null && ref.projector_contrastratio>this.projector_contrastratio && ref.projector_contrastratio>1){
		ts19 = true;
	}	
	
	var rval = false;
	

	if (currentproducttype==6){
		if (ts14 || ts15 || ts16 || ts17 || ts18 || ts19){
			rval = true;
		}else{
			rval = false;
		}
	}else if (currentproducttype==23){
		if (ts8 || ts9 || ts10 || ts11 || ts12 || ts13){
			rval = true;
		}else{
			rval = false;
		}
	}else{
		if (ts1 || ts2 || ts3 || ts4 || ts5 || ts6 || ts7){
			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;
}

function getWIDTHNew(num1){
	var lastnum = 100;
	for (var i=100;i<150;i++){
		if (((i%num1)<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-1),0);
	    B.SLIDERSTEP3 = format_number(B.SLIDERWIDTH/(procontrast.length-2),0);
	  }else if (currentproducttype==23){
    	B.SLIDERWIDTH = getWIDTHNew(drharddrives.length-2);
    	$('slider1Background').style.width=(B.SLIDERWIDTH+10)+"px";
	    B.SLIDERSTEP1 = format_number(B.SLIDERWIDTH/(drharddrives.length-2),0);
	    
    }else{
    	B.SLIDERWIDTH = getWIDTHNew(tvscreensizes.length-2);
    	$('slider1Background').style.width=(B.SLIDERWIDTH+10)+"px";
	    B.SLIDERSTEP1 = format_number(B.SLIDERWIDTH/(tvscreensizes.length-2),0);
    }
    
    B.mintv_screensize  = 1000000;    
    B.maxtv_screensize  = 0;
    
    B.mindr_harddrive  	= 1000000;    
    B.maxdr_harddrive  	= 0;
    
    B.minprojector_brightness  = 1000000; 
    B.maxprojector_brightness  = 0;
     
    B.minprojector_resolution = 1000000;
    B.maxprojector_resolution = 0;
    
    B.minprojector_contrastratio  = 1000000;
    B.maxprojector_contrastratio  = 0;

		B.maxtv_screensize = tvscreensizes.length-1;
		B.mintv_screensize = 0;
		
		B.maxdr_harddrive = drharddrives.length-1;
		B.mindr_harddrive = 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.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_screensize;
    var new_tv_freeview_hd;
    var new_tv_led;
    var new_tv_hd;
    var new_tv_freeview;
    var new_tv_dvd;
    var new_tv_usb;

    var new_dr_harddrive;
    var new_dr_freeview_hd;
    var new_dr_twin;
    var new_dr_pause;
    var new_dr_freeview;
    var new_dr_hdmi;
    
    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;
			
			
			//new_projector_resolution += 1;
			
			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,'111');
			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 = Math.ceil(new_projector_resolution);
			}
			
			//if (new_projector_resolution==5)new_projector_resolution=4;
			
			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];
	    $('slider1Value').innerHTML = tp1;
	    $('slider2Value').innerHTML = tp2;
	    $('slider3Value').innerHTML = tp3;
	    
    }else if (currentproducttype==23){
    	
    	new_dr_freeview_hd 	= B.nv( $("dr_freeview_hd_checkbox") );
    	new_dr_twin 				= B.nv( $("dr_twin_checkbox") );
    	new_dr_pause 				= B.nv( $("dr_pause_checkbox") );
   		new_dr_freeview 		= B.nv( $("dr_freeview_checkbox") );
    	new_dr_hdmi 				= B.nv( $("dr_hdmi_checkbox") );
    	new_dr_harddrive 		= ((B.maxdr_harddrive-B.mindr_harddrive) * (B.slider1.getValue()/B.SLIDERWIDTH)) + B.mindr_harddrive;
			
			if (new_dr_harddrive>=(drharddrives.length-1)){
				new_dr_harddrive = parseInt(new_dr_harddrive);
			}else if (new_dr_harddrive<1){
				new_dr_harddrive = 1;
			}else{
				new_dr_harddrive = parseInt(new_dr_harddrive)+1;
			}
    	tp1 = drharddrives[new_dr_harddrive];
    	$('slider1Value').innerHTML = tp1;
    	
    }else{
    	new_tv_freeview_hd = B.nv( $("tv_freeview_hd_checkbox") );
    	new_tv_led = B.nv( $("tv_led_checkbox") );
    	new_tv_hd = B.nv( $("tv_hd_checkbox") );
    	new_tv_freeview = B.nv( $("tv_freeview_checkbox") );
    	new_tv_dvd = B.nv( $("tv_dvd_checkbox") );
    	new_tv_usb = B.nv( $("tv_usb_checkbox") );
			
    	new_tv_screensize = ((B.maxtv_screensize-B.mintv_screensize) * (B.slider1.getValue()/B.SLIDERWIDTH)) + B.mintv_screensize;
    	
			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 = tvscreensizes[new_tv_screensize];
    	$('slider1Value').innerHTML = tp1;
    	
    }
    

    var ref = new hitachi.product.LCD(0, new_tv_screensize,new_tv_freeview_hd,new_tv_led,new_tv_hd,new_tv_freeview,new_tv_dvd,new_tv_usb,new_dr_harddrive,new_dr_freeview_hd,new_dr_twin,new_dr_pause,new_dr_freeview,new_dr_hdmi,new_projector_wireless,new_projector_wired,new_projector_optional,new_projector_brightness,new_projector_resolution,new_projector_contrastratio);
    
    //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});
        }
    }
}
