var CB = Class.create({
	build:Object(),
	initialize:function(){
		//default values
		this.build = Object();
		this.build[1003] = 1402; //profile:half round
		this.build[1000] = 1475; //metal:18 white
		this.build[1004] = 1418; //width:6mm
		this.build[1005] = 1422; //finish:polish
		this.build[1002] = 1505; //size:7
		this.selectedHash = '';
		this.origModel = 'WBB';

	},
	setbuild:function(area,value){
		CB.build[area] = value;

		if( area == 1010 || area == 1011 ){
			//ignore text fields
		}else{
			if( $('values_'+area) ){
				var children = $('values_'+area).childElements()
				for (var j=0; j < children.length; j++) {
					var currentChild = children[j];
					currentChild.removeClassName('cl_selected');
				}
			}
			if( $('cl_'+value) ){
				$('cl_'+value).addClassName('cl_selected');
			}
		}
		CB.update(area,value);
	},
	update:function(area,attr){
		var value = "";
		for( var i in CB.build ) {
			value += '&'+i+'='+CB.build[i];
		}
		new Ajax.Request('/ajax.php?load=builder&area='+encodeURIComponent(area)+'&attr='+encodeURIComponent(attr)+'&value='+encodeURIComponent(value),{});
	},
	loadModel:function(model){
		new Ajax.Request('/ajax.php?load=builder&model='+encodeURIComponent(model),{});
	},
	show_specific:function(color){
		Element.hide('specific_y');
		Element.hide('specific_b');
		Element.hide('specific_w');
		Element.show('specific_'+color);
	},
	reset:function(){
		for( var i in CB.build ) {
			var attr = CB.build[i];
			if($('cl_'+attr)){
				$('cl_'+attr).removeClassName('cl_selected');
				if($('cl_'+attr).checked ){
					$('cl_'+attr).checked = false;
				}
			}
		}		

		Element.update('bb_1000','18kt White Gold');
		Element.update('bb_1004','6 mm');
		Element.update('bb_1005','Polished');
		Element.update('bb_1002','7');

		Element.update('values_1006','Please select a ring profile first');
		Element.update('values_1007','Please select a diamond layout first ');
		Element.update('values_1008','Please select a ring profile, ring width and diamond layout first ');
		Element.hide('specific_y');
		Element.hide('specific_b');
		Element.hide('specific_w');
		Element.update('bb_1006','&nbsp;');
		Element.update('bb_1007','&nbsp;');
		Element.update('bb_1008','&nbsp;');
		Element.update('bb_1009','&nbsp;');

		$('cl_1010').value='';
		$('cl_1011').value='';
		Element.update('bb_1010','&nbsp;');
		Element.update('bb_1011','&nbsp;');
		Element.update('bb_1012','&nbsp;');

		$('cl_1402').addClassName('cl_selected');
		$('values_1002').selectedIndex = 13; //size 7; 1505
		CB.initialize();
		CB.update(1002,1402);
		PB.toPage(1);
	},
	hash:function(){
		var ie_ver = parseFloat(navigator.appVersion.split("MSIE")[1]);
		var use_iframe = false;
		var update_iframe = true;
		if( ie_ver < 8 && ie_ver > 0 ){use_iframe = true;}
		if( use_iframe ){
			var iFrame   = document.createElement('iframe');
			iFrame.id = 'iehistory';
			document.body.appendChild(iFrame);
			$('iehistory').contentWindow.document.open();
			$('iehistory').contentWindow.document.write('');
			$('iehistory').contentWindow.document.close();
		}
		window.setInterval(function () {
			var mismatch = false;		
			var currentHash = window.location.hash;
			
			if(this.selectedHash != currentHash) {
				mismatch = true;
			}else if( use_iframe && $('iehistory').contentWindow.document.body.innerHTML != currentHash ){
				//back button support for IE
				mismatch = true;
				update_iframe = false;
				var hash = $('iehistory').contentWindow.document.body.innerHTML;
				if( hash.slice(0,1) == '<' || hash == '' ){ hash = '#'; }
				if( hash != currentHash ){
					currentHash = hash;
					window.location.hash = currentHash;
				}
			}
			
			if( mismatch === true ){
				if( currentHash.length > 1 ){
					var model = currentHash.slice(1);
				}else{
					var model = CB.origModel;
				}
				if( model != '' && $('prod_model').innerHTML != model ){
					CB.loadModel(model);
				}
				this.selectedHash = currentHash;
			}
		}, 500);

	}
});
