  if (top.location != location) {
    top.location.href = document.location.href;
  }
var ss_onload  = null;
var existingOnLoad = window.onload;
window.onload = function(){
		bhm_poster = new bhm_poster_comp();
		if(ss_onload)
		{
			ss_onload();
		}	
		if(existingOnLoad) { existingOnLoad(); };
};

(function(){ 
	var _posterBlock = null;
	var _submissionForm = null;
	var _selectedPoster = null;
	
	bhm_poster_comp = function() {
			_posterBlock = document.getElementById('bhm-poster-select');
			_submissionForm = document.getElementById('bhm-poster-form');
			this._errBlock = document.getElementById('errorBlock');
			if(!_posterBlock)
			{
				return;
			}	
			
			var posterChildren = _posterBlock.childNodes;
			for(inc in posterChildren)
			{
				if(posterChildren[inc].firstChild
				&& posterChildren[inc].firstChild.nodeName == 'IMG')
					{
						posterChildren[inc].firstChild.onclick = function() { bhm_poster.visuals.poster_selected(this); }
						
						posterChildren[inc].onmouseover = function() 
						{ 
							this.style.marginTop = '8px'; 
							this.style.marginBottom = '12px';
							if(this.firstChild.style.opacity != undefined)
							{
								this.firstChild.style.opacity = '1.0';
							} else if(this.firstChild.style.filter != undefined) {	
								this.firstChild.style.filter =  'alpha(opacity=100)';
							}	
						} 
						posterChildren[inc].onmouseout = function() 
						{ 
							this.style.marginTop = '10px'; 
							this.style.marginBottom = '10px';
							if(this.firstChild.style.opacity != undefined)
							{
								this.firstChild.style.opacity = '0.6';
							} else if(this.firstChild.style.filter != undefined) {	
								this.firstChild.style.filter =  'alpha(opacity=60)';
							}
						}
					} 
			}
	};	
	bhm_poster_comp.prototype = {
		flowSelect : {
				_selectBlocks : [
						'bhm-1',
						'bhm-2',
						'bhm-3',
				],
				_prevBlock : null,
				selectSection : function(section)
				{
					return;
					var _maxsize = 450;
					var _frms = 40;
					var _tm = 1000;
					var _animateLoop = function(count,openEle,closeEle)
					{
						var incAmt = (count*(_maxsize/_frms));
						if(openEle)
						{
							openEle.style.height = incAmt+'px';
						}
						if(closeEle)
						{	
							closeEle.style.height = (_maxsize - incAmt)+'px';
						}	
						count++;
						if(count <= _frms)
						{
							setTimeout(function(){ _animateLoop(count,openEle,closeEle);},(_tm/_frms));
						} else {
							if(closeEle)
							{
								closeEle.style.height = '1px'; 
								closeEle.style.display = 'none';
							}	
						}
					};
					if(this._prevBlock == section)
					{
							_animateLoop(0,null,document.getElementById(section));
							this._prevBlock = null; 
							return;
					}
					for(inc in this._selectBlocks)
					{
						if(this._selectBlocks[inc] == section || this._selectBlocks[inc] ==  this._prevBlock)
						{
							document.getElementById(section).style.display = 'block';
						} else {
							document.getElementById(this._selectBlocks[inc]).style.display = 'none';
						}
					}
					_animateLoop(0,document.getElementById(section),document.getElementById(this._prevBlock));
					this._prevBlock = section;
				}	
		},
		visuals : {	
			_currentPoster : null,
			poster_selected : function(ele) {
				if(this._currentPoster)
				{
					this._currentPoster.parentNode.className = this._currentPoster.parentNode.className.replace('selected','');
				}
				this._currentPoster = ele;
				ele.parentNode.className = 'selected'; 
				var srcStr = ele.src;
				srcStr = srcStr.substring(0,srcStr.lastIndexOf('.'))+'_large'+srcStr.substring(srcStr.lastIndexOf('.'));
				document.getElementById('bhm-poster-selected-img').src = srcStr;
				document.getElementById('bhm-poster-selected-img').style.display = 'block';
				
				if(document.getElementById('bhmchoice'))
				{
					document.getElementById('bhmchoice').value = ele.parentNode.id.replace('bhm_poster_','');
				}
			},
			updateWordCount : function()
			{
				var wordLimit = (99 - document.getElementById('bhmcomment-input').value.length);
				if(wordLimit <= 0)
				{
					document.getElementById('bhm-wordcount').innerHTML = 'Character limit reached';
				} else {
					document.getElementById('bhm-wordcount').innerHTML = wordLimit;
				}	
			}
		},
		snInteraction : false,
		validation : function() {
			this.errState = false;
			/* Validate the input elements and textarea */
			var _validateInput = function(oChk)
			{
					  this.clearErr(oChk.errBlock);
					  var checks = [{chk : (oChk.str.length == undefined),
													 err : 'Incorrect input type to '+oChk.name},
													{chk : (oChk.str.length == 0),
													 err : oChk.name+' cannot be empty.'},
													{chk : (oChk.str.length > oChk.maxsize),
													 err : oChk.name+' is too large must be under '+oChk.maxsize+' characters.'}];
	          for(var inc in checks)
	          {
	          	if(checks[inc].chk)
	          	{
	          		this.appendErr(oChk.errBlock,checks[inc].err);	
	          	} 
	          }																		 	
	    };
			var checkEles = [{ str : document.getElementById('bhmname').value,
				                 name : 'Name',
				                 errBlock : 'bhmname-errorBlock',
												 maxsize : 200},
												 { str : document.getElementById('bhmemail').value,
												 name : 'Email',
				                 errBlock : 'bhmemail-errorBlock',
												 maxsize : 200},
												 { str : document.getElementById('bhmcomment').value,
												 name : 'Comment',
				                 errBlock : 'bhmcomment-errorBlock',
												 maxsize : 99}];
			for(var eleInc in checkEles)
		 	{
		 		_validateInput.call(this,checkEles[eleInc]);
			}
			/* Validate extras, poster select and gigya interaction */
			this.clearErr('bhm-posters-errorBlock');
			if(!this.visuals._currentPoster)
			{
				this.appendErr('bhm-posters-errorBlock','A Poster needs to be selected');	
			} 
			this.clearErr('bhm-snPost-errorBlock');
			if(!this.snInteraction)
			{
				this.appendErr('bhm-snPost-errorBlock','Post to a social network required.');	
			} 
			return (!this.errState);
		},
		errState : false,
		appendErr : function(errEleStr,msg)
		{
			this.errState = true;
			var errEle = document.getElementById(errEleStr);
			var errMsg = document.createElement('div');
				errMsg.className = 'errorRow';
				errMsg.innerHTML = msg;
			errEle.appendChild(errMsg);
		},
		clearErr : function(errEleStr)
		{
			document.getElementById(errEleStr).innerHTML = '';
		},
		formsubmit : function()
		{
			return this.validation();
		}
	};
})();

(function(){
				var _to = false;
				var _comment = '';
				var _title = 'Broken Hill - The Movie, Poster Competition';
				var _url = 'http://www.brokenhillthemovie.com/';
				var _compUrl = 'http://is.gd/1ISnd';
				
				var _sn = {
					bhm_twitter:
					 {
						name : 'Twitter',
						buildUrl : function(title,comment,url)
						{
							var urlBase = 'http://twitter.com/home';
							var param_str = '?status=';
							var twit_titleStr = '#BrokenHillMovie Vote';
							return urlBase+param_str+encodeURIComponent(twit_titleStr+' '+comment+' '+_compUrl);
						}
					},
					bhm_fb: {		
						name : 'Facebook',
						buildUrl : function(title,comment,url)
						{
							var urlBase = 'http://www.facebook.com/sharer.php';
							//we need to pass the comment and title back to the php so it can set the meta tags for the fb request.
							if(bhm_poster.visuals._currentPoster != null 
							&& bhm_poster.visuals._currentPoster.src)
							{
								//FB will take the url from start to the end of the next http:// in this as the display url so need to remove
								var fbimg = '&fbimg='+encodeURIComponent(bhm_poster.visuals._currentPoster.src.replace(/\//g,'__'));
							}
							var ajoin = (url.indexOf('?') != -1)	 ? '&' : '?';	
							var param_str = '?u='+encodeURIComponent(url+ajoin+'fbt='+encodeURIComponent(title)+'&fbd='+encodeURIComponent(comment)+fbimg);
							param_str += '&t='+encodeURIComponent(title);
							return urlBase+param_str;
						}	
					},
					bhm_ms: {
						name : 'MySpace',
						buildUrl : function(title,comment,url)
						{
							var urlBase = 'http://www.myspace.com/Modules/PostTo/pages/';
							var param_str = '?u='+encodeURIComponent(url);
							param_str += '&c='+encodeURIComponent(comment);
							param_str += '&t='+encodeURIComponent(title);
							param_str += '&l=3';
							return urlBase+param_str;						
						}	
					}
				};
				var _getEle = function(e)
				{
					var ele = !(e.target) ? e.srcElement : e.target;
					return (ele.nodeName != 'DIV') ? ele.parentNode : ele;
				};
				bhm_snPost = {
					bhmsnbutclick : function (e)
					{
						var ele = _getEle(e);
						var eId = ele.id;
						if(_sn[eId])
						{
							window.open(_sn[eId].buildUrl(_title,_comment,_url));
							bhm_poster.snInteraction = true;
						}		
						return false;
					},
					bhmsnbuthover : function (e,hoverOff)
					{
						var ele = _getEle(e);
						if(_sn[ele.id])
						{
							var aTag = ele.firstChild;
							while(aTag.nodeName != 'A')
							{
								aTag = aTag.nextSibling;
								if(!aTag.nextSibling)
								{ return; }
							}
							if(!hoverOff)
							{
								aTag.className = aTag.className+'new hover';
								_sn[ele.id].hover = true;
							} else {
								if(_sn[ele.id].hover)
								{
									aTag.className = aTag.className.replace(/\s?hover/,'');
								}
							}	
						}
					},
					bhmcommentupdatePause : function(ele)
					{
								if(_to)
								{
									clearTimeout(_to);
								}
								_to = setTimeout(function(){ bhm_snPost.bhmcommentupdated(ele); },250); 
					},
					bhmcommentupdated : function(ele)
					{
						_comment = document.getElementById('bhmcomment-input').value;
						document.getElementById('bhmcomment').value = _comment;
					}
				};	 
})();


