var Mitsubishi = {};
Mitsubishi.Behaviour = {
	rulesets: [],
	add: function(rules) {
		Mitsubishi.Behaviour.rulesets.push(rules);
	},
	go: function(d) {
		d = d || document;
		Mitsubishi.Behaviour.rulesets.each(function(ruleset) {
			$H(ruleset).each(function(pair) {
				Element.getElementsBySelector(d,pair.key).each(function(node) {
					pair.value($(node));
				});
			});
		});
	}	
};
Event.observe(window,"load",function(e) {
	Mitsubishi.Behaviour.go();	
});


Mitsubishi.rules = {
	'#search_box':function(input){
		var def_text = $F(input);
		input.observe('focus', function(e){
			if($F(this) == def_text){
				this.clear();
			}
		});
		input.observe('blur', function (e){
			if($F(this) == ''){
				this.value = def_text;
			}
		});
	},
	'#messages, #errors': function(ul) {
		ul.observe('click',function() {	ul.fade({delay:0.2});});
	},
	'#messages': function(ul) {
		//ul.fade.bind(ul).delay(3);
		ul.fade({delay:3});
	},
	'#close_contact': function (span){
		span.observe('click', function(e){
			Effect.BlindUp('contact',{afterFinish: function(e){
				$('contact_form').reset();
				$('contact').innerHTML = '<p>'+$F('quick_contact_instructions').replace('&quot;','"').replace('&lt;','<').replace('&gt;','>')+'</p>';
				Effect.BlindDown('contact');
			}});
		});
	},
	'#preferred_language':function(el){
		el.observe('change', function(e){
			window.location = '/'+this.value+'/';
		});
	},
	'#contact_country, #contact_product':function(select){
		select.observe('change', function(e){
			if($F('contact_country') != '' && $F('contact_product') != ''){
				if($('contact').visible()){
					Effect.BlindUp('contact',{afterFinish: function(e){
						new Ajax.Updater('contact', '/contact_form_request', {
							parameters: {
								country: $F('contact_country'), 
								product: $F('contact_product')
							},
							onComplete: function(transport){
								//pageTracker._trackPageview('/contact_us/form');
								//pageTrackerContact._trackPageview('/contact_us/'+$F('contact_country')+'/'+$F('contact_product'));
								Effect.BlindDown('contact');
							}
						});
					}});
				}
			}
		});
	},
	'#left_menu ul li.top':function(li){
		if(!li.hasClassName('top_selected')){
			li.observe('mouseover', function(e){
				this.addClassName('hover');
				this.childElements().each(function(img){
					if(img.tagName == 'IMG' && img.hasClassName('top')){
						img.src = '/graphics/menu-level-1-corner-top-open.png';
					}
					if(img.tagName == 'IMG' && img.hasClassName('bottom')){
						img.src = '/graphics/menu-level-1-corner-bottom-open.png';
					}
				});
			});
			li.observe('mouseout', function(e){
				this.removeClassName('hover');
				this.childElements().each(function(img){
					if(img.tagName == 'IMG' && img.hasClassName('top')){
						img.src = '/graphics/menu-level-1-corner-top.png';
					}
					if(img.tagName == 'IMG' && img.hasClassName('bottom')){
						img.src = '/graphics/menu-level-1-corner-bottom.png';
					}
				});
			});
		}
	},
	'#render_cell':function(el){
		tinyMCEPopup.onInit.add(function(ed) {
			var dom = tinyMCEPopup.dom;
		
			// Load editor content_css
			tinymce.each(ed.settings.content_css.split(','), function(u) {
				dom.loadCSS(ed.documentBaseURI.toAbsolute(u));
			});
		
			// Place contents inside div container
			dom.setHTML('middle_content', ed.getContent());
		});
	},
	'#print':function(a){
		a.observe('click', function(e){
			$('middle_content').setStyle({height:'auto'});
			Event.stop(e);
			window.print();
		});
	},
	'#map_map area':function(area) {
		area.observe('mouseover', function(e){			
			// Hide div
			$('map_hidden').style.display = 'none';
			
			var address;
			var image;
			
			// Edit hidden div content
			if (area.alt == "Guildford") {
				address = "Mitsubishi Electric R&amp;D Centre<br />Europe (MERCE)<br />20 Frederick Sanger Road<br />The Surrey Research Park<br />Guildford GU2 7YD";
				image 	= "/graphics/offices/guildford.jpg";
			} else if (area.alt == "Uxbridge") {
				address = "MEU Corporate Office<br />Harman House<br />1 George Street<br />Uxbridge<br />Middlesex UB8 1QQ";
				image	= "/graphics/offices/uxbridge.jpg";
			} else if (area.alt == "Hatfield") {
				address = "UK Branch Office<br />Mitsubishi Electric Europe B.V.<br />Travellers Lane<br />Hatfield<br />Herts AL10 8XB";
				image	= "/graphics/offices/hatfield.jpg";
			} else if (area.alt == "Canning Town") {
				address = "Lift and Escalator Division<br />Unit 8<br />Electra Park<br />Bidder Street<br />Canning Town<br />London E16 4ES";
				image	= "/graphics/offices/canning_town.jpg";
			} else if (area.alt == "Croydon") {
				address = "Power Systems Group<br />Stephenson House, 7th Floor<br />2 Cherry Orchard Road<br />Croydon<br />Surrey<br />CR0 6BA";
				image	= "/graphics/offices/croydon.jpg";
			} else if (area.alt == "Orpington") {
				address = "Mitsubishi Electric Europe<br />Diamond Vision<br />Unit 13, Crayfield Industrial Estate<br />Main Road, St Pauls Cray<br />Orpington<br /> Kent BR5 3HP";
				image	= "/graphics/offices/orpington.jpg";
			} else if (area.alt == "Dublin") {
				address = "Mitsubishi Electric Ireland,<br />Westgate Business Park,<br />Ballymount,<br />Dublin 24,<br />Ireland";
				image	= "/graphics/headers/about_us.jpg";
			}
			
			$('map_hidden').innerHTML 	= address;
			$('header_image').src 		= image;
			
			// Show a hidden div
			$('map_hidden').style.display = 'block';
		});
	},
	'#map':function(img) {
		img.observe('mouseout', function(e){
			$('map_hidden').style.display = 'none';
		});
	},
	'.rhs_hover':function(h) {
		h.observe('mouseover', function(e){
			// Hide div
			$('map_hidden').style.display = 'none';
			
			var address;
			
			if (h.id == "rhs_Guildford") {
				address = "Mitsubishi Electric R&amp;D Centre<br />Europe (MERCE)<br />20 Frederick Sanger Road<br />The Surrey Research Park<br />Guildford GU2 7YD";
			} else if (h.id == "rhs_Uxbridge") {
				address = "MEU Corporate Office<br />Harman House<br />1 George Street<br />Uxbridge<br />Middlesex UB8 1QQ";
			} else if (h.id == "rhs_Hatfield") {
				address = "UK Branch Office<br />Mitsubishi Electric Europe B.V.<br />Travellers Lane<br />Hatfield<br />Herts AL10 8XB";
			} else if (h.id == "rhs_Canning_Town") {
				address = "Lift and Escalator Division<br />Unit 8<br />Electra Park<br />Bidder Street<br />Canning Town<br />London E16 4ES";
			} else if (h.id == "rhs_Croydon") {
				address = "Power Systems Group<br />Stephenson House, 7th Floor<br />2 Cherry Orchard Road<br />Croydon<br />Surrey<br />CR0 6BA";
			} else if (h.id == "rhs_Orpington") {
				address = "Mitsubishi Electric Europe<br />Diamond Vision<br />Unit 13, Crayfield Industrial Estate<br />Main Road, St Pauls Cray<br />Orpington<br /> Kent BR5 3HP";
			} else if (h.id == "rhs_Dublin") {
				address = "Mitsubishi Electric Ireland,<br />Westgate Business Park,<br />Ballymount,<br />Dublin 24,<br />Ireland";
			}
			
			$('map_hidden').innerHTML 	= address;
			//$('header_image').src 		= image;
			
			// Show a hidden div
			$('map_hidden').style.display = 'block';
		});
	},
	'#distrib_map_map area':function(area) {
		area.observe('mouseover', function(e){			
			// Hide div
			$('map_hidden').style.display = 'none';
			
			var address;
			var image;
			
			// Edit hidden div content
			if (area.alt == "1") {
				address = "James-Morrow Home<br />Entertainment Systems<br />1 Home Street<br />Edinburgh EH3 9JR<br />0131 229 8777<br />www.james-morrow.com";

			} else if (area.alt == "2") {
				address = "The Home Cinema Centre<br />234-240 Morrison Street<br />Edinburgh EH3 8EA<br />0131 229 9350<br />www.thehomecinemacentre.co.uk";
				
			} else if (area.alt == "3") {
				address = "Roy Jowett<br />42-44 Albert Road<br />Colne<br />Lancashire BB8 0AD<br />01282 864 048<br />www.royjowetthomecinema.co.uk";
				
			} else if (area.alt == "4") {
				address = "HiFiX<br />163 Spon Street<br />Coventry CV1 3BB<br />02476 222622<br />www.hifix.co.uk";
				
			} else if (area.alt == "5") {
				address = "Kent Home Cinema<br />69 London Road<br />Southborough<br />Tunbridge Wells<br />Kent TN4 0PA<br />01892 535007<br />www.kenthomecinema.co.uk";
				
			} else if (area.alt == "6") {
				address = "Stone Audio<br />The Axium Centre<br />Dorchester Road<br />Lytchett Minster, Poole<br />Dorset BH16 6FE<br />01202 630066<br />www.stoneaudio.co.uk";
			
			} else {
				address = "";
			}
			
			$('map_hidden').innerHTML 	= address;
			
			// Show a hidden div
			$('map_hidden').style.display = 'block';
		});
	},
	'#distrib_map':function(img) {
		img.observe('mouseout', function(e){
			$('map_hidden').style.display = 'none';
		});
	}
	
}



Mitsubishi.Behaviour.add(Mitsubishi.rules);