// JavaScript Document

//extend prototypes browser detection
Prototype.Browser.IE6 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 6;
Prototype.Browser.IE7 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 7;
Prototype.Browser.IE8 = Prototype.Browser.IE && !Prototype.Browser.IE6 && !Prototype.Browser.IE7;

function tryReplaceFlash(){
	if(($('player')==undefined)&&($('vid-still')==undefined)){
		return;
	}
	if(typeof pageVars.vid != 'undefined'){
		if((typeof swfobject != 'undefined')&&swfobject.hasFlashPlayerVersion("9")){
			// show an image still and click to reveal flash
			$('player').update('<div class="handy" id="vid-still" style="display:none; background-image:url(images/get_image.php?id='+pageVars.vid+'&action=resize&args=410x270); background-repeat:no-repeat"><img src="images/video-click-play.png" width="410" heigh="270"/></div>');
			$('vid-still').appear();
			$('player').observe("click",function(){
				var atts = {};
				var pars = { allowFullScreen:"true", scale:"noscale", salign:"lt", wmode:"opaque" };
				var vars = { media:'/'+pageVars.lang+'/stream/'+pageVars.vid+'.flv' };
				var id = "player";
				swfobject.embedSWF("flvplayer.swf", id, "409", "269", "9.0.0", false, vars, pars, atts);
			});
		} else {
			// no flash
			$('player').update('<a href="http://www.adobe.com/go/EN_US-H-GET-FLASH" target="_blank"><img src="/international-new/images/no-flash.jpg" alt="" width="411" height="271" border="0"/></a>');
		}
	}
}

function getRep(id){
				new Ajax.Updater('rep-data', 'get_representatives.php?country='+id, {method: 'get', onComplete: scrollToBottom});
			}
			function scrollToBottom(){
				new Effect.ScrollTo('footer');
			}
			
var hepco = {};
hepco.IE = 0,
hepco.init = function(){
	hepco.productMenu.init();
	tryReplaceFlash();
	hepco.forms.init();
},
hepco.forms = {
	init: function () {
		$$('.autosubmit').each(function (el){
			var form = el;
			var go = function () {
				form.submit();
			};
			$(form).select('input, select').each(function (el) {
				$(el).observe('change', function () {	
                    go();
				});
			});
			$(form).select('input[type=submit]').invoke('hide');
		});
	}
},
hepco.productMenu = {},
hepco.productMenu.init = function(){
	hepco.productMenu.initItems();
	hepco.productMenu.initChildItems();
},
hepco.productMenu.initItems = function(){
	$$('.nav-product-item.unselected a').each(function(el){
		$(el).observe('mouseover', function(evt){
			var tar = evt.target || evt.srcElement;
			hepco.fx.transition(tar,{backgroundColor:'#666',color:'#FFF'},0);
			if(!$('googlesearchresults').visible()){
				if(hepco.isHome == true){
					hepco.arrowMarker.show($(tar));
				}
			}
			});
			$(el).observe('mouseout', function(evt){
				var tar = evt.target || evt.srcElement;
				hepco.fx.transition(tar,{backgroundColor:'#CCC',color:'#900'});
				if(hepco.isHome == true){
					hepco.arrowMarker.hide();
				}
		});
	});
},
hepco.productMenu.initChildItems = function(){
	$$('.nav-product-child-item a').each(function(el){
		var bg,col;
		bg = $(el).up('.nav-product-child-item').getStyle('background-color');
		col = ($(el).hasClassName('light'))? '#FFF' : '#000';
		$(el).observe('mouseover',function(evt){
			hepco.fx.transition(evt.target,{backgroundColor:bg,color:col},0);
		});
		$(el).observe('mouseout',function(evt){
			hepco.fx.transition(evt.target,{backgroundColor:'#666',color:'#FFF'});
		});
	});
},
hepco.utils = (function(){
	var utils = {
		objToCSS: function(obj){
			var arr = [];
			for(prop in obj){
				if(obj.hasOwnProperty(prop)){
					arr.push(prop.replace('backgroundColor','background-color') + ':' + obj[prop]);
				}
			}
			return arr.join(';');
		},
		attachJS: function(src){
			var script = document.createElement("script");
			script.type = "text/javascript";
			script.src = src;
			document.getElementsByTagName("head")[0].appendChild(script);
		},
		autoSubmit: function(inputId, submitID){
			var submit = $(submitID);
			$(inputId).observe('change',function(evt){
				var tar = evt.target || evt.srcElement;
				var parent = $(tar).parentNode;
				while(parent.nodeName != 'FORM'){
					parent = parent.parentNode;
				}
				parent.submit();
			});
			if(typeof submit != 'undefined'){
				submit.style.display = 'none';
			}
		},
		mouseOutOfArea: function(evt, targetArea){
			var toTar = evt.relatedTarget || evt.toElement;
			while(toTar && toTar != targetArea && toTar.nodeName != 'BODY'){
				toTar = toTar.parentNode;
			}
			if(toTar == targetArea){
				return false;
			}
			return true;
		},
		visibility : function (el, visible) {
			if(!visible){
				el.style.visibilty = 'hidden';
				return;
			}
			el.style.visibilty = 'visible';
		}
	};
	return utils;
})(),
hepco.fx = (function(){
	var fx = {
		transition: function(el,style,duration){
			if(typeof duration === 'undefined'){
				var duration = 0.3;
			}
			if(typeof Effect.Queues.get('global')[el] !== 'undefined'){
				Effect.Queues.get('global')[el].invoke('cancel');
			}
			if(duration > 0){
				$(el).morph(hepco.utils.objToCSS(style),{duration:duration});
			}else{
				$(el).setStyle(style);
			}
		}
	};
	return fx;
})(),
hepco.ignition = {
	bootlist: [],
	register: function(func, obj, args){
		if(typeof func == 'function'){
			this.bootlist.push([func, obj, args]);
		}
	},
	start: function(){
		for(var i = 0, len = this.bootlist.length;i<len;i++){
			try{
				this.bootlist[i][0].apply(this.bootlist[i][1], this.bootlist[i][2]);
			}catch(err){

			}
		}
	}
};
hepco.getVideoPlayer = function(lang, vidID, width, height){
	var width = width || 410,
	height = height || 270,
	that = {};
	if(swfobject.hasFlashPlayerVersion("9")){
		that.insertInto = function(canvas) {
			$(canvas).update('<img src="images/video-click-play.png" width="'+width+'" heigh="'+height+'"/>');
			canvas.style.backgroundImage = 'url(images/get_image.php?id='+vidID+'&action=resize&args='+width+'x'+height+')';
			canvas.style.width = width + 'px';
			canvas.style.height = height + 'px';
			canvas.style.cursor = 'pointer';
			$(canvas).observe('click',function(){
				var atts = {};
				var pars = {allowFullScreen:"true", scale:"noscale", salign:"lt", wmode:"opaque"};
				var vars = {media:'/'+lang+'/stream/'+vidID+'.flv'};
				var id = "player";
				swfobject.embedSWF("flvplayer.swf", id, "409", "269", "9.0.0", false, vars, pars, atts);
			});
			$(canvas).setStyle({opacity:0});
			$(canvas).appear();
		}
	}else{
		that.insertInto = function(canvas) {
			$(canvas).update('<a href="http://www.adobe.com/go/EN_US-H-GET-FLASH" target="_blank"><img src="/international-new/images/no-flash.jpg" alt="" width="'+width+'" height="'+height+'" border="0"/></a>');
			canvas.style.width = width + 'px';
			canvas.style.height = height + 'px';
		}
	}
	return that;
};
hepco.arrowMarker = {
	config: {
		width: '24'
	},
	draw: function () {
		if(!this.theArrow){
			this.theArrow = document.createElement('div');
			this.theArrow.className = 'arrowMarker';
			this.theArrow.style.width = this.config.width + 'px';
			this.theArrow.style.display = 'none';
			this.theArrow.style.position = 'absolute';
			document.body.appendChild(this.theArrow);
		}
	},
	show: function (el) {
		var $el = $(el),
		pos = $el.positionedOffset();
		this.draw();
		this.theArrow.style.display = 'block';
		this.theArrow.style.left = (pos[0] - this.config.width) + 'px';
		this.theArrow.style.top = pos [1] + 'px';
		this.theArrow.style.height = $el.getHeight() + 'px';
	},
	hide: function () {
		if(this.theArrow){
			this.theArrow.style.display = 'none';
		}
	}
};
hepco.ignition.register(hepco.init, hepco, []);
document.observe('dom:loaded', function () {hepco.ignition.start(); });
