
$(function(){
	
	if ($.browser.msie && $.browser.version < 7)
		$('img[src$=.png]').pngfix();

	$('img[hover],input[hover]').imghover();

	$('a[href$=pdf], a.external').click(function() {
	   window.open(this.href); 
	   return false;
	});

	// Menu
	$('#m1,#m2,#m3,#m4,#m5').hover(function(){
		$('#mt'+this.id.replace('m','')).show();
	},function(){
		$('#mt'+this.id.replace('m','')).hide();
	});

	// Home
	var tips = [];
	tips['reduce'] = ['lamp','fax','desklamp','heater','monitor','kitchen','charger','printer','exit','stationery','window'];
	tips['reuse'] = ['drawer','printer','box','fan','fax'];
	tips['recycle'] = ['printer','fax','drawer','plant','trash','box'];
	
	var active_tip = 'reduce';
	var tip_clicked = {};
	var tip_count = 0;
	
	for(var i in tips) {
		for(var j in tips[i]) {
			tip_clicked[tips[i][j]+'_'+i]= false;
			tip_count ++;
		}
	}

	$('#m_reduce,#m_reuse,#m_recycle').click(function(){
		$('#m_'+active_tip).attr('src','img/blank.gif');
		active_tip = $(this).attr('id').split('_')[1];
		$('#m_'+active_tip).attr('src','img/m_'+active_tip+'.gif');
	}).hover(function(){
		this.src = 'img/m_'+$(this).attr('id').split('_')[1]+'.gif';
	},function(){
		if ($(this).attr('id').split('_')[1] != active_tip)
			this.src = 'img/blank.gif';
	});

	// init reduce
	$('#m_'+active_tip).attr('src','img/m_'+active_tip+'.gif');

	$('img.tipitem').hover(function(){
		var tipname = $(this).attr('id').split('_')[1];
		if ($.inArray(tipname,tips[active_tip]) >= 0) {
			this.src = 'img/a_'+tipname+'.gif';
			$(this).css('cursor','pointer');
		}
	},function(){
		this.src = 'img/blank.gif';
		$(this).css('cursor','default');
	}).click(function(){
		var tipname = $(this).attr('id').split('_')[1];
		if ($.inArray(tipname,tips[active_tip]) >= 0) {
			$('img.tb').hide();
			$('#t_'+tipname+'_'+active_tip).show();
			tip_clicked[tipname+'_'+active_tip] = true;
			progressBar();
		}
	});

	function progressBar () {
		var click_count = 0;
		for (var i in tip_clicked)
			if (tip_clicked[i]) click_count++;
        
		$('div#greenbar').css(
		  'width',
		  (Math.ceil( (click_count / tip_count) * 138)+1)+'px'
	    );
	}


	/*
	for (var i in tips) {
		$('img#m_'+i).bind('mouseover',{t:i},function(e){
			for (var j=0; j<tips[e.data.t].length; j++) {
				var ele = $('img#a_'+tips[e.data.t][j]);
				$(ele).attr('src',$(ele).attr('hover'));
			}
		}).bind('mouseout',{t:i},function(e){
			for (var j=0; j<tips[e.data.t].length; j++) {
				var ele = $('img#a_'+tips[e.data.t][j]);
				$(ele).attr('src',$(ele).data('__img__original'));
			}
		});

		for (var j in tips[i]) {
			$('img#a_'+tips[i][j]).bind('mouseover',{t:i},function(e){
				var ele = $('img#m_'+e.data.t);
				$(ele).attr('src',$(ele).attr('hover'));
			}).bind('mouseout',{t:i},function(e){
				var ele = $('img#m_'+e.data.t);
				$(ele).attr('src',$(ele).data('__img__original'));
			}).bind('click',{t:i},function(e){
				tip_clicked[$(this).attr('alt')] = true;
				progressBar();
			});
		}
	}
	*/


	// Paper
	
	var states = []; // tl, br, bl
	states['desk'] = ['truck','tree','factory'];
	states['tree'] = ['desk','factory','truck'];
	states['factory'] = ['tree','truck','desk'];
	states['truck'] = ['factory','desk','tree'];
	var currentState = 'desk';

	$('img#pb_'+currentState).fadeIn('fast');

	$('div#cr_top_left').click(function(){
		var newState = states[currentState][0];
		$('img#ap_placeholder').attr('src','img/ap_'+newState+'.gif');
		$('img.pa').hide();
		$('img#pa_'+newState).show();
		currentState = newState;
	});

	$('div#cr_bottom_right').click(function(){
		var newState = states[currentState][1];
		$('img#ap_placeholder').attr('src','img/ap_'+newState+'.gif');
		$('img.pa').hide();
		$('img#pa_'+newState).show();
		currentState = newState;
	});

	$('div#cr_bottom_left').click(function(){
		var newState = states[currentState][2];
		$('img#ap_placeholder').attr('src','img/ap_'+newState+'.gif');
		$('img.pa').hide();
		$('img#pa_'+newState).show();
		currentState = newState;
	});

	
	// Products

	var rgcombo = [];
	rgcombo[1] = '4:4';
	rgcombo[2] = '3:3';
	rgcombo[3] = '3:3';
	rgcombo[4] = '2:1';
	rgcombo[5] = '1:2';
	rgcombo[6] = '1:2';
	rgcombo[7] = '5:3';
	rgcombo[8] = '5:1';

	var rval = 0;
	var gval = 0;
	function setRval(v) {
		if (rval) $('img#r'+rval).attr('src','img/blank.gif');
		switch (v) {
			case 'min': rval = 1; break;
			case 'max': rval = 4; break;
			case 'add': rval++; if (rval > 4) rval = 4; break;
			case 'sub': rval--; if (rval < 1) rval = 1; break;
			default: rval = parseInt (v); if (rval < 1) rval = 1; if (rval > 4) rval = 4; break;
		}		
		$('img#r'+rval).attr('src','img/r'+rval+'.gif');
		setRGcombo();
	}

	function setGval(v) {
		if (gval) $('img#g'+gval).attr('src','img/blank.gif');
		switch (v) {
			case 'min': gval = 1; break;
			case 'max': gval = 5; break;
			default: gval = parseInt (v); if (gval < 1) gval = 1; if (gval > 5) gval = 5; break;
		}
		$('img#g'+gval).attr('src','img/g'+gval+'.gif');
		setRGcombo();
	}

	function setRGcombo () {
		$('#p_1,#p_2,#p_3,#p_4,#p_5,#p_6,#p_7,#p_8').attr('src','img/blank.gif');
		$('#pp_1,#pp_2,#pp_3,#pp_4,#pp_5,#pp_6,#pp_7,#pp_8,#pb_1,#pb_2,#pb_3,#pb_4,#pb_5,#pb_6,#pb_7,#pb_8').hide();
		$('img#pb_0').show();

		for (var i in rgcombo) {
			var j = rgcombo[i].split(':');	
			if (j[0] == gval && j[1] == rval) {
				$('img#p_'+i).attr('src','img/p_'+i+'.gif');
			}
		}
	}

	function setProduct (p) {
		$('#p_1,#p_2,#p_3,#p_4,#p_5,#p_6,#p_7,#p_8').attr('src','img/blank.gif');
		$('#p_'+p).attr('src','img/p_'+p+'.gif');
		
		$('#pp_1,#pp_2,#pp_3,#pp_4,#pp_5,#pp_6,#pp_7,#pp_8,#pb_1,#pb_2,#pb_3,#pb_4,#pb_5,#pb_6,#pb_7,#pb_8,#pb_0').hide();
		$('img#pp_'+p+',img#pb_'+p).show();

		for (var i in rgcombo) {
			if (i == p) {
				var j = rgcombo[i].split(':');
				if (gval) $('img#g'+gval).attr('src','img/blank.gif');
				if (rval) $('img#r'+rval).attr('src','img/blank.gif');
				gval = j[0];
				rval = j[1];				
				$('img#g'+gval).attr('src','img/g'+gval+'.gif');
				$('img#r'+rval).attr('src','img/r'+rval+'.gif');
			}
		}
	}

	$('div#r_min').click(function(){setRval('min')});
	$('div#r_max').click(function(){setRval('max')});
	$('img#r1').click(function(){setRval(1)});
	$('img#r2').click(function(){setRval(2)});
	$('img#r3').click(function(){setRval(3)});
	$('img#r4').click(function(){setRval(4)});

	$('div#g_min').click(function(){setGval('min')});
	$('div#g_max').click(function(){setGval('max')});
	$('img#g1').click(function(){setGval(1)});
	$('img#g2').click(function(){setGval(2)});
	$('img#g3').click(function(){setGval(3)});
	$('img#g4').click(function(){setGval(4)});
	$('img#g5').click(function(){setGval(5)});

	$('img#p_1').click(function(){setProduct(1)});
	$('img#p_2').click(function(){setProduct(2)});
	$('img#p_3').click(function(){setProduct(3)});
	$('img#p_4').click(function(){setProduct(4)});
	$('img#p_5').click(function(){setProduct(5)});
	$('img#p_6').click(function(){setProduct(6)});
	$('img#p_7').click(function(){setProduct(7)});
	$('img#p_8').click(function(){setProduct(8)});

	setRval(3);
	setGval(3);


	// Contact
	$('map#m_c_flag area').hover(function(){
		$('img#c_flag').attr('src','img/c_flag.gif');
	},function(){
		$('img#c_flag').attr('src','img/blank.gif');
	});
});


;(function($){

	$.fn.pngfix = function () {	
		if ($.browser.msie && $.browser.version < 7) {
			return this.each (function () {
				$(this).css({
					filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+this.src+"')"
				});
				this.src = 'img/blank.gif';
			});
		} else {
			return this;
		}
	};

	$.prefetch = function() {
		for(var i = 0; i<arguments.length; i++)  {
			$("<img>").attr("src", arguments[i]);
		}
	}

	$.fn.imghover = function () {
		return this.each (function () {
			$(this).data('__img__original', this.src);
			$.prefetch($(this).attr('hover'));
		}).hover(function(){
			this.src = $(this).attr('hover');
		},function(){
			this.src = $(this).data('__img__original');
		});
	};

})(jQuery);