/*
 * 	Author: Jacob Tadros
 */
var DIVERUI = {
		ticker: null,
		
		windowChange:function (e){
				window.onresize = function(e) {
					DIVERUI.displayCheck();
					DIVERTOOLS.panTool(e);
				};
				
				window.onscroll = function(e) {
					DIVERTOOLS.panTool(e);

					var left =document.viewport.getScrollOffsets().left;
					var top = document.viewport.getScrollOffsets().top;
					var check_scroll = 0;
					var minimap = $('mapHighlight').children;
					
					for (var i =0, iLength = minimap.length; i<iLength; i++){
						if	(top<1300){
							if (left<1900){
								check_scroll = 0;
							}
							else if (left>1899 && left<3900){
								check_scroll = 1;
							}
							else 
								check_scroll =2;
						}
						else	{
							if (left<1900){
								check_scroll =3;
							}
							else if (left>1899 && left<3900){
								check_scroll =4;
							}
							else
								check_scroll =5;
						}
						minimap [i].style.display ='none';
						minimap [check_scroll].style.display = 'block';
					}	
				};
			},			

			containerClassCheck: function(){
				
				if ($('minimap-panel-control-2').className=='panelOpened'){
					$('minimap_panel').style.left = document.viewport.getWidth()-317+'px';
					$('minimap_panel').style.top = document.viewport.getHeight()-168+6+'px';
				}
				else{
					$('minimap_panel').style.left = document.viewport.getWidth()-5+'px';
					$('minimap_panel').style.top = document.viewport.getHeight()-168+6+'px';
				}
				
				if ($('portfolio-panel-control').className=='panelOpened'){
					$('portfolio_container').style.left = document.viewport.getWidth()-300 +'px';
					$('portfolio_container').style.top = document.viewport.getHeight()-198-332+10+'px';
				}
				else{
					$('portfolio_container').style.left = document.viewport.getWidth()-5 +'px';
				}
				
				if ($('map-data-panel-control').className=='panelOpened'){
					$('mapDataDesc_container').style.left = document.viewport.getWidth()-300 +'px';
					$('mapDataDesc_container').style.top = document.viewport.getHeight()-582-101+27+'px';
					
				}
				else{
					$('mapDataDesc_container').style.left = document.viewport.getWidth()-5 +'px';
					$('mapDataDesc_container').style.top = document.viewport.getHeight()-582-101+27+'px';
				}
				if ($('legend-panel-control').className=='panelOpened'){
					$('legend-title').style.left = document.viewport.getWidth() - 300 +'px';
					$('legend-title').style.top = 10 + 'px';
				}
				else{
					$('legend-title').style.left = document.viewport.getWidth() - 5 +'px';
					$('legend-title').style.top = 10 + 'px';
				
				}
				
				if ($('horizontal-panel-control').className=='leftPanelOpened'){
					$('horizontal-menu').style.left = 0 + 'px';
					$('horizontal-menu').style.top = document.viewport.getHeight() - 145-3 +'px';
				}
				else{
					$('horizontal-menu').style.left = -545 + 'px';
					$('horizontal-menu').style.top = document.viewport.getHeight() - 145-3 +'px';
				}
				
			},
			
			smallDisplays: function (){
				/*
				if (document.viewport.getHeight() <537 && miniMapShow ==1){
					Effect.DropOut('minimap_panel');
					$('panTool').style.display = 'none';
					miniMapShow=0;
				}
				else if (document.viewport.getHeight() > 537 && miniMapShow ==0){
					$('minimap_panel').appear();
					$('panTool').style.display = 'block';
					miniMapShow =1;
				}*/
				$$('.left_panel_body')[0].style.top = document.viewport.getHeight()- $$('.left_panel_body')[0].getDimensions().height +'px';
				
				$$('.panel_body').each(function(it) {
					
					var topPos = it.style.top.substr(0,it.style.top.length-2);
					var topDiff = document.viewport.getHeight() - it.getDimensions().height ;
					
					if( topDiff < 0)
						it.style.top=11+'px';
					else{
						if (it.id =='minimap_panel'){
							//if (topDiff > 380)
								it.style.top = document.viewport.getHeight()- it.getDimensions().height-11 +'px';
							//else
								//it.style.top = '369px';
						}
						else if (it.id =='mapDataDesc_container'){
							
							if (topDiff > 628)
								it.style.top = document.viewport.getHeight()- (it.getDimensions().height) - 50-
										$('portfolio_container').getHeight()- $('minimap_panel').getHeight()-11+'px';
							else
								it.style.top ='77px';
						}
						
						else if (it.id =="legend-title")
							it.style.top = '11px';
						
						else if (it.id =="portfolio_container"){
							if (topDiff > 322)
								it.style.top = document.viewport.getHeight()- it.getDimensions().height - 
								$('minimap_panel').getHeight() - 25 +'px';
							else
								it.style.top = '140px';
						}

					}
					if (it.children[0].className == 'panelOpened'){
						//it.style.top = document.viewport.getHeight()- it.getDimensions().height-11 +'px';
						it.style.left = document.viewport.getWidth()- it.getDimensions().width +'px';		
					}
					else{
						//it.style.top = document.viewport.getHeight()- it.getDimensions().height -11+'px';
						it.style.left = document.viewport.getWidth()-5 +'px';	
					}

					/*
					it.observe('mouseover',function(){
						this.setStyle('z-index:'+this.getStyle('z-index')* 2);
					});
					it.observe('mouseout',function(){
						this.setStyle('z-index:'+this.getStyle('z-index') / 2);
					});
					*/
				});
			},
			
			panelPositionOnload: function(){
					$('minimap_panel').style.left = document.viewport.getWidth()-317+'px';
					$('minimap_panel').style.top = document.viewport.getHeight()-168 +6 +'px';
					$('portfolio_container').style.left = document.viewport.getWidth()-300 +'px';
					$('portfolio_container').style.top = document.viewport.getHeight()-198-332+10 +'px';
					$('mapDataDesc_container').style.left = document.viewport.getWidth()-300 +'px';
					$('mapDataDesc_container').style.top = document.viewport.getHeight()-581-101+27 +'px';
					$('legend-title').style.left = document.viewport.getWidth() - 300 +'px';
					$('legend-title').style.top = 10 + 'px';
					$('horizontal-menu').style.left = 0 + 'px';
					$('horizontal-menu').style.top = document.viewport.getHeight() - 145-3 +'px';
			},
			
			displayCheck: function(){
				if (window.innerHeight < 768 || window.innerWidth < 1024){
					DIVERUI.smallDisplays();
				}
				else{
					DIVERUI.panelPositionOnload();
					DIVERUI.containerClassCheck();
				}
				
			},
			
			containersObserve: function(){
				$('portfolio-panel-control').observe('click',function(){
					$('canvas_hover').hide();
					if (this.className == 'panelOpened'){
						var tempShow = function (){
//				    		$('portfolio_container').hide();
				    		$('portfolio_container').show();
				    		$('portfolio_container').style.left = document.viewport.getWidth()-5+'px';
//					        $('canvas_circles').show();
//							$('canvas_search').show(); 
				    	};
						this.className = 'panelClosed';
						new Effect.Move ('portfolio_container', {x:295, y: 0, duration: 0.5, afterFinish: tempShow});
					}
				    else{
				    	var tempShow = function(){
							$('portfolio_container').show();
//					        $('canvas_circles').show();
//							$('canvas_search').show(); 
						};
						var tempBefore = function (){
							$('portfolio_container').hide();
						};
						
				        this.className = 'panelOpened';
				        new Effect.Move ('portfolio_container',{ x:-295, y: 0, duration: 0.5,beforeStart: tempBefore, afterSetup: tempShow});
					    }
				});
							
				$('map-data-panel-control').observe('click',function(){
					$('canvas_hover').hide();
					//$('canvas_circles').hide();
					//$('canvas_search').hide();
					if (this.className == 'panelOpened'){
						var tempShow = function (){
//				    		$('mapDataDesc_container').hide();
				    		//$('mapDataDesc_container').show();
				    		$('mapDataDesc_container').style.left = document.viewport.getWidth()-5+'px';
					        //$('canvas_circles').show();
							//$('canvas_search').show(); 
				    	};
				        this.className = 'panelClosed';
				        new Effect.Move ('mapDataDesc_container', {x:295, y: 0, duration: 0.5, afterFinish: tempShow});
				    }
				    else{
				    	var tempShow = function(){
							$('mapDataDesc_container').show();
					        //$('canvas_circles').show();
							//$('canvas_search').show(); 
						};
						var tempBefore = function (){
							$('mapDataDesc_container').hide();
						};
				        this.className = 'panelOpened';
				        new Effect.Move ('mapDataDesc_container',{ x:-295, y: 0, duration: 0.5,beforeStart: tempBefore, afterSetup: tempShow});
					}
				});
				
				
				$('minimap-panel-control-2').observe('click',function(e){
					$('canvas_hover').hide();
					//$('canvas_circles').hide();
					//('canvas_search').hide();
					$('panTool').style.display='none';
					$('panTool').style.left=-100+'px';
				    if (this.className == 'panelOpened'){
				    	this.className = 'panelClosed';
				    	$('minimap').hide();
				    	var tempHide = function (){
//				    		$('minimap_panel').hide();
				    		$('minimap_panel').show();
				    		$('minimap_panel').style.left = document.viewport.getWidth()-5+'px';
					        $('miniMapControl').hide();
					        $('divSpacer').show();
					       // $('canvas_circles').show();
							//$('canvas_search').show();        
						};
						var tempBefore = function (){
							//$('minimap_panel').fade();	
							
						};
						
				        new Effect.Move ('minimap_panel', {x: 300, y: 0, duration: 0.5,beforeStart: tempBefore, afterFinish: tempHide});
				        
				   }
				   else{
					   this.className = 'panelOpened';
						var tempShow = function(){
							$('minimap_panel').show();
							$('divSpacer').hide();
				    		$("miniMapControl").appear();
				    		
					        //$('canvas_circles').show();
							//$('canvas_search').show(); 
							$('panTool').show();
				    		setTimeout (function (e){ DIVERTOOLS.panTool();$('minimap').show();},1000);
						};
						var tempBefore = function (){
							$('minimap_panel').hide();
						};
					   new Effect.Move ('minimap_panel',{ x: -312, y: 0, duration: 0.5,beforeStart: tempBefore, afterSetup: tempShow});
			        }
				    
				});
							
				$('horizontal-panel-control').observe('click',function(){

					$('canvas_hover').hide();
//					$('canvas_circles').hide();
//					$('canvas_search').hide();
					
				    if (this.className == 'leftPanelOpened'){
				    	var tempShow = function (){
//				    		$('horizontal-menu').hide();
				    		$('horizontal-menu').show();
				    		$('horizontal-menu').style.left = -533-11.5 +'px';
//					        $('canvas_circles').show();
//							$('canvas_search').show();

				    	};
				        new Effect.Move ('horizontal-menu', {x: -500, y: 0, duration: 0.5, afterFinish: tempShow});
				        this.className = 'leftPanelClosed';
					}
				    else{
						var tempShow = function(){
							$('horizontal-menu').show();
//					        $('canvas_circles').show();
//							$('canvas_search').show(); 
						};
						var tempBefore = function (){
							$('horizontal-menu').hide();
						};
				        this.className = 'leftPanelOpened';
				        new Effect.Move ('horizontal-menu',{ x: 545, y: 0, duration: 0.5,beforeStart: tempBefore, afterSetup: tempShow});
				    }
				});

				$('legend-panel-control').observe('click',function(){
					$('canvas_hover').hide();
//					$('canvas_circles').hide();
//					$('canvas_search').hide();
					
					if (this.className == 'panelOpened'){
						var tempShow = function (){
//				    		$('legend-title').hide();
				    		$('legend-title').show();
				    		$('legend-title').style.left = document.viewport.getWidth()-5+'px';
//					        $('canvas_circles').show();
//							$('canvas_search').show(); 
				    	};
				    	this.className = 'panelClosed';
						new Effect.Move ('legend-title', {x:295, y: 0, duration: 0.5, afterFinish: tempShow});
					}
					else{
						var tempShow = function(){
							$('legend-title').show();
//					        $('canvas_circles').show();
//							$('canvas_search').show(); 
						};
						var tempBefore = function (){
							$('legend-title').hide();
						};
						this.className = 'panelOpened';
						new Effect.Move ('legend-title',{ x:-295, y: 0, duration: 0.5,beforeStart: tempBefore, afterSetup: tempShow});
					}
				});
			},
			
			viewPortCheck: function(){
				
			}
};



function prototypeRSS(l1, l2, l3) {
	var Feed = [];
	Feed[0] = 'url=' + l1;
	Feed[1] = 'url=' + l2;
	Feed[2] = 'url=' + l3;

	for ( var i = 0, iLength = Feed.length; i < iLength; i++) {
		if (i > 0 && Feed[i].length > 48 && Feed[i - 1].length > 48)
			Feed[i] = '&' + Feed[i];
		else if (Feed[i].length > 48)
			Feed[i] = '?' + Feed[i];
		else
			Feed[i] = "";
	}
	var jsonLink = './diver/ajax-retrieve-RssFeed'+Feed[0]+Feed[1]+Feed[2];
	new Ajax.Request(jsonLink, { // retrieve the current state name and county name
		method : 'GET',
		onSuccess : function(transport) {
			var data = (transport.responseText).evalJSON();
			var newLink = [];
			var newTitle = [];
			var newID = [];
			var jsonContent = data['RssFeed'][0]['entry'];
			for ( var i = 0, iLength = jsonContent.length; i < iLength; i++) {
				newLink[i] = jsonContent[i]['string'][1];
				newTitle[i] = jsonContent[i]['string'][0];
				$('js-news').insert(
						"<li class='news-item'><a href=" + newLink[i]
								+ " target='_blank'>" + newTitle[i]
								+ "</a></li>");
			}
			DIVERUI.ticker = new Ticker($$("#js-news li"));
		}
	});
}

function rssOnLoad() {
	
	$('rss-option').observe('click',function(){
		$('options-content').show();	
	});
	$('rss-update1').observe('click',function() {
			$('js-news').remove();
			$('ticker-wrapper').insert("<ul id='js-news' class='js-hidden'></ul>");
			
			var feed1 = "http://feeds.feedburner.com/StatelineorgRss-"
					+ $('feed1').value;
			
			var feed2 = "http://feeds.feedburner.com/StatelineorgRss-"
					+ $('feed2').value;
			
			var feed3 = "http://feeds.feedburner.com/StatelineorgRss-"
					+ $('feed3').value;
			
			if (		$('feed1').value == 0
					&& 	$('feed2').value == 0
					&& 	$('feed3').value == 0) {
				prototypeRSS('http://feeds.feedburner.com/StatelineorgRss-Headlines',null, null);
			} else {
				prototypeRSS(feed1, feed2, feed3);
			}
			;
			$('options-content').hide();
		});

	new Ajax.Request('./diver/ajax-all-states', { // retrieve the current state name and county name
		method : 'GET',
		onSuccess : function(transport) {
			var data = (transport.responseText).evalJSON();
			var newLink = [];
			var newTitle = [];
			var newID = [];
			var jsonContent = data['states'][0]['entry'];

			for ( var i = 0, iLength = jsonContent.length; i < iLength; i++) {
				var temp = jsonContent[i]['com.ambac.diver.model.State'].state;
				
				if (temp != 'District of Columbia' && temp != 'American Samoa'
						&& temp != 'N. Mariana Islands' && temp != 'Guam'
						&& temp != 'Puerto Rico' && temp != 'Virgin Islands' && temp != 'Midway Islands') {

					newLink[i] = temp;
					newID[i] = newLink[i].replace(/\s/g, "");

					var feed2OptionEle = new Element('option', { value: newID[i] }).update(newLink[i]);
					var feed3OptionEle = new Element('option', { value: newID[i] }).update(newLink[i]);
					$('feed2').insert(feed2OptionEle);
					$('feed3').insert(feed3OptionEle);

				}
			}
		}
	});
}
