var homepageChoice = ['http://www.facebook.com/pages/Poway-CA/L7-Creative-Communications/43058081453?sid=b2d86f00d372d0841b9105acc346fa1f&ref=s'];
com.l7creative.defaultRefer = homepageChoice[Math.floor( Math.random()*(homepageChoice.length-1) )];
if (com.l7creative.refer.substring(0,4)!='http' 
	|| com.l7creative.refer.substring(0,25)=='http://l7creative.com' 
	|| com.l7creative.refer.substring(0,21)=='http://l7creative.com'){
	com.l7creative.refer = com.l7creative.defaultRefer;
}
com.l7creative.m_urls = {
	"home" : com.l7creative.refer
}
function copy_clip(text2copy)
{
	if (window.clipboardData && window.clipboardData.setData) {
    	window.clipboardData.setData("Text",text2copy);
    } else {
		if($('_copyclipDiv')){
			$('_copyclipDiv').destroy();
		}
		swfobject.removeSWF('_copyclip');
		var divcontainer = new Element('div',{ 'id':'_copyclipDiv' });
		Element.insert(document.body, divcontainer );
		swfobject.embedSWF('udm-resources/_clipboard.swf', 
						   '_copyclipDiv', 
						   '0', 
						   '0', 
						   '7', 
						   false, 
						   {'clipboard':escape(text2copy)}, 
						   {}, 
						   {'id':'_copyclip', 
						    'name':'_copyclip'});
	}
}
 
com.l7creative.throwSpeed = [];
com.l7creative.mouseEventHandler = function(event)
{
	var element = Event.element(event);
	
	var l7nav_container = $('l7nav_container');
	var l7nav_udm = $('udm');
	var move_handle = $('move_handle');
	var swfoverlay = $('_swfoverlay');
	
	if(!element.draggableMenu){
		l7nav_container.originalPosition = [2,2];
		l7nav_container.lastPosition = l7nav_container.cumulativeOffset();
		element.draggableMenu = new Draggable(l7nav_container,{'onDrag':com.l7creative.onDrag, 'onEnd':com.l7creative.onDragEnd, 'starteffect' : null, 'endeffect' : null, 'reverteffect' : null});
	}
		
	var rect = {};
	var viewportDimensions = document.viewport.getDimensions();
	rect.x = l7nav_container.cumulativeOffset()[0]-viewportDimensions.width;
	rect.y = l7nav_container.cumulativeOffset()[1]-viewportDimensions.height;
	rect.width = viewportDimensions.width*2;
	rect.height = viewportDimensions.height*2;
	
	Element.setStyle('swfoverlay',{'left':(rect.x + 'px'), 'top':(rect.y + 'px'), 'width':(rect.width + 'px'), 'height':(rect.height + 'px')});
	
	switch(event.type){
		case "mouseover" :
			if( com.l7creative.currentURL == "home" 
			   || com.l7creative.currentURL == "" ){
				element.setStyle({'cursor':'move'});
			}
			break;
		case "mouseout" :
			if(!element.mouseDown 
			   && !element.isDragging){
				element.setStyle({'cursor':'default'});
			}
			break;
		case "mousedown" :
			element.mouseDown = true;
			element.isDragging = false;
			element.setStyle({'cursor':'move'});
			break;
		case "mousemove" :
			if(element.mouseDown){
				element.isDragging = true;
			}
			//com.l7creative.throwSpeed = [ l7nav_container.cumulativeOffset()[0]-l7nav_container.lastPosition[0], l7nav_container.cumulativeOffset()[1]-l7nav_container.lastPosition[1] ];
			//l7nav_container.lastPosition = l7nav_container.cumulativeOffset();
			element.setStyle({'cursor':'move'});
			break;
		case "mouseup" :
			if(element.isDragging){
				element.mouseDown = false;
				element.isDragging = false;
				
				var currentPosition = [ parseInt( l7nav_container.getStyle('left') ), parseInt( l7nav_container.getStyle('top') ) ];
 
				currentPosition[0] = Math.max( 0 , currentPosition[0] );
				currentPosition[1] = Math.max( 0 , currentPosition[1] );
 
				
				var viewportDimensions = document.viewport.getDimensions();	
			
				currentPosition[0] = Math.min( ( viewportDimensions.width - l7nav_udm.getWidth() ) , currentPosition[0] );
				currentPosition[1] = Math.min( ( viewportDimensions.height - ( l7nav_udm.getHeight() + move_handle.getHeight() ) ) , currentPosition[1] );
				
 
				if( currentPosition[0] <= 25
						&& currentPosition[1] <= 25 ) {
					currentPosition = [ 0, 0 ];
				}
				
				rect.x = currentPosition[ 0 ]-viewportDimensions.width;
				rect.y = currentPosition[ 1 ]-viewportDimensions.height;
				
				new Effect.Move( l7nav_container, {duration:0.5, x: currentPosition[ 0 ], y: currentPosition[ 1 ], mode: 'absolute' });
				new Effect.Move( $('swfoverlay'), {duration:0.5, x: rect.x, y: rect.y, mode: 'absolute' });
				l7nav_container.lastPosition = currentPosition;
				 
				element.setStyle({'cursor':'default'});
				// l7nav_container.lastPosition = l7nav_container.cumulativeOffset();
				
			} else {
				if(l7nav_container.lastPosition[0] == l7nav_container.cumulativeOffset()[0] 
					&& l7nav_container.lastPosition[1] == l7nav_container.cumulativeOffset()[1]){
					var ancestors = element.ancestors();
					var i=0;
					while(i<ancestors.length){
						var ancestor = ancestors[i];
						if(ancestor.getAttribute('href')){
							window.location.href = element.ancestors()[0].getAttribute('href');
							break;
						}
						i++;
					}
				}
			}
			break;
	}
}
 
com.l7creative.onDragStart = function(element, event)
{
	var l7nav_container = $('l7nav_container');
	l7nav_container.lastPosition =  l7nav_container.cumulativeOffset();
	com.l7creative.throwSpeed = [0,0]
}
 
com.l7creative.onDrag = function(element, event)
{
	var l7nav_container = $('l7nav_container');
	com.l7creative.throwSpeed = [ l7nav_container.cumulativeOffset()[0]-l7nav_container.lastPosition[0], l7nav_container.cumulativeOffset()[1]-l7nav_container.lastPosition[1] ];
	l7nav_container.lastPosition = l7nav_container.cumulativeOffset();
}
com.l7creative.onDragEnd = function(element, event)
{
	// Event.stopObserving(document, 'mouseup', com.l7creative.documentMouseEventHandler);
	// alert(com.l7creative.throwSpeed);
}
 
com.l7creative.timeoutID = []
com.l7creative.l7navMouseEventHandler = function(event)
{
	var element = Event.element(event);
	while(com.l7creative.timeoutID.length>0){
		clearTimeout(com.l7creative.timeoutID[com.l7creative.timeoutID.length-1]);
		com.l7creative.timeoutID.pop();
	}
	var l7nav_container = $('l7nav_container');
	var swfoverlay = $('_swfoverlay');
	
	var rect = {};
	var viewportDimensions = document.viewport.getDimensions();
	rect.x = l7nav_container.cumulativeOffset()[0]-viewportDimensions.width;
	rect.y = l7nav_container.cumulativeOffset()[1]-viewportDimensions.height;
	rect.width = viewportDimensions.width*2;
	rect.height = viewportDimensions.height*2;
	
	switch(event.type){
		case "mouseover" :
			if(swfoverlay.showAt){
				swfoverlay.showAt(l7nav_container.cumulativeOffset());
			}
			com.l7creative.timeoutID[com.l7creative.timeoutID.length] = setTimeout("Element.setStyle('swfoverlay',{'left':'" + rect.x + "px', 'top':'" + rect.y + "px', 'width':'" + rect.width + "px', 'height':'" + rect.height + "px'});", 50);
			break;
		case "mouseout" :
			if(swfoverlay.hideAt){
				com.l7creative.timeoutID[com.l7creative.timeoutID.length] = setTimeout("$('_swfoverlay').hideAt($('l7nav_container').cumulativeOffset());", 1250);
			}
			com.l7creative.timeoutID[com.l7creative.timeoutID.length] = setTimeout("Element.setStyle('swfoverlay',{'left':'" + rect.x + "px', 'top':'" + rect.y + "px', 'width':'1px', 'height':'1px'});", 50);
			break;
	}
}
 
com.l7creative.wikipediaWindow = null;
com.l7creative.twitterWindow = null;
 
com.l7creative.navItemClickHandler = function(event)
{
	var element = Event.element(event);
	var id = element.getAttribute('id');
	var rel = '/' + id + '/';
	
	if(rel){
		if(com.l7creative.wikipediaWindow){
			com.l7creative.wikipediaWindow.close();
			com.l7creative.wikipediaWindow = null;
		}
		
		if(com.l7creative.twitterWindow){
			com.l7creative.twitterWindow.close();
			com.l7creative.twitterWindow = null;
		}
				
		if( SWFAddress.getValue()==rel ){
			com.l7creative.goHREF(rel);
		} else {
			SWFAddress.setValue(rel);
			if(id == 'wikipedia'){
				setTimeout(com.l7creative.openWikipediaWindow, 100);
			}
		}
		
		if(id == 'twitter'){
			setTimeout(com.l7creative.openTwitterWindow, 100);				
		}
	}
	
	Event.stop(event);
}
 
com.l7creative.openWikipediaWindow = function(){
	com.l7creative.wikipediaWindow = window.open(com.l7creative.m_urls['wikipedia'],'wikipediaWindow','toolbar=yes, location=yes, directories=no, status=yes, menubar=yes, scrollbars=yes, resizable=yes, copyhistory=yes') 
	com.l7creative.wikipediaWindow.focus();
}
 
com.l7creative.openTwitterWindow = function(){
	com.l7creative.twitterWindow = window.open(com.l7creative.m_urls['twitter'],'twitterWindow','toolbar=yes, location=yes, directories=no, status=yes, menubar=yes, scrollbars=yes, resizable=yes, copyhistory=yes') 
	com.l7creative.twitterWindow.focus();
}
 
com.l7creative.configureNav = function()
{
	Element.setStyle('l7nav_container',{
							'width': 'auto',
							'overflow': 'visible',
							'zIndex':'3'
						});
	
	//var navtoggle = new Element('div',{ 'id':'navtoggle' }).update('<a href="javascript://" onclick="com.l7creative.toggleNav(); return false;" title="Minimize Nav"><img src="udm-resources/white_minus.gif" alt="Minimize Nav" name="navTog" width="5" height="5" id="navTog" /></a>');
										
	//Element.insert($('l7nav_container'), navtoggle );
 
	Event.observe('l7nav_container', 'mouseover', com.l7creative.l7navMouseEventHandler );
	Event.observe('l7nav_container', 'mouseout', com.l7creative.l7navMouseEventHandler );
 
	Event.observe('move_handle', 'mouseover', com.l7creative.mouseEventHandler );
	Event.observe('move_handle', 'mouseout', com.l7creative.mouseEventHandler );
	Event.observe('move_handle', 'mousedown', com.l7creative.mouseEventHandler );
	Event.observe('move_handle', 'mousemove', com.l7creative.mouseEventHandler );
	Event.observe('move_handle', 'mouseup', com.l7creative.mouseEventHandler );
	
	var anchorLinks = document.getElementsByTagName("a");
	for(var i=0; i<anchorLinks.length; i++){
		var anchorLink = anchorLinks[i];
		if(anchorLink.getAttribute('href') && anchorLink.getAttribute('id')){
			
				// old way using static m_urls object
				for(var j in com.l7creative.m_urls) {
					var m_url = com.l7creative.m_urls[j];
					if(href == m_url){
						var rel = '/'+j+'/';
						Element.writeAttribute(anchorLink,{'rel':rel});
						Event.observe( anchorLink, 'click', com.l7creative.navItemClickHandler );
						break;
					}
				}
			
			
			// new way uses anchor tag's id as m_urls object key and swfaddress hash, 
			// and href as m_urls object value
			var id = anchorLink.getAttribute('id');
			var href = anchorLink.getAttribute('href');
			com.l7creative.m_urls[id] = href;
			Event.observe( anchorLink, 'click', com.l7creative.navItemClickHandler );
		}
	}
}
 
com.l7creative.configureWelcomeCloseButton = function()
{
	Element.setStyle('hello', {
						   'left': '153px',
						   'top': '22px',
						   'height': 'auto',
						   'zIndex':'2'
						});
	
	var closehello = new Element('div',{ 'class':'closehello', 'id':'closehello' }).update('<a href="javascript://" onclick="com.l7creative.toggleLayer(\'hello\');" title="Close Welcome Message"><img src="images/closebttn.gif" alt="Close Welcome Message" width="40" height="9" border="0" /></a>');
										
	Element.insert($('hello'), closehello );
}
 
com.l7creative.configureEscapeButton = function()
{
	//var escapeDiv = new Element('div',{ 'id':'escape' }).update('<a href="'+ com.l7creative.refer +'" onclick="com.l7creative.runAway(); return false;"><img src="" title="" width="" height="" /></a>');
	//Element.insert(document.body,escapeDiv);
}
 
com.l7creative.createIFrame = function()
{
	// check the referrer and write the iframe accordingly
	var currentHash = window.location.hash.substring(1).split('/').join('');
	var srcURL;
	if(currentHash==""){
		currentHash = "home";
		srcURL = com.l7creative.refer;
	} else if(currentHash != "twitter"){
		srcURL = com.l7creative.m_urls[currentHash];
	} 
	
	
	com.l7creative.currentURL = currentHash;
 
	this._myiframe = new Element('iframe',{
										'id':'_myiframe', 
										'name':'_myiframe',
										'width':'100%', 
										'height':'100%',
										'frameborder':'0',
										'src':srcURL
										});
	this._myiframe.setStyle({
							'top':'0',
							'left':'0', 
							'width':'100%', 		
							'height':'100%',
							'border':'0'
							});
	
	Element.insert(document.body,this._myiframe);
	
	if(com.l7creative.wikipediaWindow){
		com.l7creative.wikipediaWindow.close();
		com.l7creative.wikipediaWindow = null;
	}
	
	if(com.l7creative.twitterWindow){
		com.l7creative.twitterWindow.close();
		com.l7creative.twitterWindow = null;
	}
	
	if(com.l7creative.currentURL == 'wikipedia'){
		com.l7creative.moveMenuToLowerLeft();
	}
	
	if(com.l7creative.currentURL == 'twitter'){
		setTimeout(com.l7creative.openTwitterWindow, 100);
	}
}
com.l7creative.swfoverlay = null;
com.l7creative.createSWFOverlay = function()
{
	var swfoverlayDiv = new Element('div',{ 'id':'swfoverlay' }).update('<div id="_swfoverlay"></div>');
	Element.insert(document.body,swfoverlayDiv);
	/*
	if (swfobject.hasFlashPlayerVersion("9.0.0")) {
		var vars = {
					'name1':'hello',
					'name2':'world',
					'name3':'foobar'
				  };
		var att = { 
					'data':'', 
					'width':'100%', 
					'height':'100%',
					'id':'_swfoverlay',
					'name':'_swfoverlay'
				  };
		var par = { 
					'wmode':'transparent', 
					'menu':'false',
					'seamlesstabbing':'false',
					'salign':'LT',
					'scale':'noscale',
					'allowfullscreen':'false'
				  };
		var id = "_swfoverlay";
		SWFObject.embedSWF('', 
						   '_swfoverlay', 
						   '100%', 
						   '100%', 
						   '9', 
						   false, 
						   vars, 
						   par, 
						   att);
		if($('_swfoverlay')){
			com.l7creative.swfoverlay = $('_swfoverlay');
		}
	}
	*/
}
 
com.l7creative.createCopyToClipboardLinks = function()
{
	var locpath = location.protocol+"//"+location.host+location.pathname;

}
 
com.l7creative.toggleNav = function()
{
	if($('udm_nav').visible()){
		Element.hide('udm_nav');
		Element.writeAttribute($('navTog').ancestors()[0], {'title':'Expand Nav'});
		Element.writeAttribute('navTog',{'src':'udm-resources/white_plus.gif', 'alt':'Expand Nav'});
	} else {
		Element.show('udm_nav');
		Element.writeAttribute($('navTog').ancestors()[0], {'title':'Minimize Nav'});
		Element.writeAttribute('navTog',{'src':'udm-resources/white_minus.gif', 'alt':'Minimize Nav'});
	}
}
 
com.l7creative.toggleLayer = function(layerID)
{
	if(Element.visible(layerID)){
		Element.hide(layerID);
	} else {
		Element.show(layerID);
	}
}
 
com.l7creative.goHREF = function(hrefKey){
	com.l7creative.currentURL = hrefKey.split('/').join('');
	
	
	if(Element.visible('hello') && com.l7creative.currentURL != "")
	{
		Element.hide('hello');
	}
	
	// find the iframe
	var myIFrame = window.frames[0];
	
	// add it to the history and the address bar
	if(com.l7creative.currentURL 
	    && com.l7creative.currentURL != "") {
		// change the iframe's location
		window.location.hash = '/'+com.l7creative.currentURL+'/';
 
		if (com.l7creative.currentURL != "twitter" ) { 
			myIFrame.location.replace( com.l7creative.m_urls[ com.l7creative.currentURL ] );
			Element.writeAttribute($('escape').descendants()[0], {'href':com.l7creative.m_urls[ com.l7creative.currentURL ] });
		}
		
		if(com.l7creative.currentURL == 'wikipedia'){
			com.l7creative.moveMenuToLowerLeft();
		}
	}
	
	if(com.l7creative.currentURL.substring(0,2) == 'tv'){
		com.l7creative.youTubeMessage();
	}
	
}
 
com.l7creative.moveMenuToLowerLeft = function(){
	var l7nav_container = $('l7nav_container');
	var l7nav_udm = $('udm');
	var move_handle = $('move_handle');
	
	var currentPosition = [ parseInt( l7nav_container.getStyle('left') ), parseInt( l7nav_container.getStyle('top') ) ];
	
 
	currentPosition[0] = Math.max( 0 , currentPosition[0] );
	currentPosition[1] = Math.max( 0 , currentPosition[1] );
 
	
	var viewportDimensions = document.viewport.getDimensions();	
 
	currentPosition[0] = Math.min( ( viewportDimensions.width - l7nav_udm.getWidth() ) , currentPosition[0] );
	currentPosition[1] = Math.min( ( viewportDimensions.height - ( l7nav_udm.getHeight() + move_handle.getHeight() ) ) , currentPosition[1] );
	
 
	if( currentPosition[0] < 155
			&& currentPosition[1] < 155 ) {
		currentPosition = [ Math.max( ( viewportDimensions.width - l7nav_udm.getWidth() - 18 ) , 155 ), 37 ];
	}
	
	var secs = (com.l7creative.youCanDragIt) ? 5 : 7;
	
	new Effect.Move( $('l7nav_container'), {delay: secs, duration:1, x: currentPosition[ 0 ], y: currentPosition[ 1 ], mode: 'absolute' });
}
 
com.l7creative.runAway = function (){
	if(com.l7creative.m_urls[com.l7creative.currentURL] != "" && com.l7creative.currentURL){
		window.location = com.l7creative.m_urls[com.l7creative.currentURL];
	} else {
		window.location = 'http://www.facebook.com/pages/Poway-CA/L7-Creative-Communications/43058081453?sid=b2d86f00d372d0841b9105acc346fa1f&ref=s';
	}
}
 
//animated text alert
com.l7creative.type = function()
{
  if ($(this.destination))
  {
    var dest = $(this.destination);
    if (dest)// && dest.innerHTML)
    {
	  if(dest.innerHTML.length>com.l7creative.text.length){
	  	dest.innerHTML = dest.innerHTML.substr(0,dest.innerHTML.length-1);
		currentChar--;
		if(currentChar<=1 || dest.innerHTML.length<=1) {
			Element.setStyle(dest,{'display':'none'});
			return;
	    }
		setTimeout("com.l7creative.type()", 5);
	  } else {
      	dest.innerHTML=com.l7creative.text.substr(0, currentChar);
      	//dest.innerHTML+=text[currentChar-1];
      	currentChar++
		if (currentChar>com.l7creative.text.length)
     	{
       	currentChar=1;
      	}
	  	else
        {
        setTimeout("com.l7creative.type()", 25);
      	}
	  }
    }
  }
}
 
com.l7creative.startTyping = function(textParam, destinationParam)
{
 this.text = textParam;
 this.destination = destinationParam;
 var dest=$(destinationParam);
  if(textParam.length>1){
  	currentChar=1;
  	Element.setStyle(dest,{'display':'none'});
  }
  else
  {
  	currentChar = dest.innerHTML.length;
  }
  com.l7creative.type();
}
 
com.l7creative.noYouTube = false;
com.l7creative.youTubeMessage = function ()
{
	return;
	if(com.l7creative.noYouTube){
		return;
	};
	if(!$('youtube')){
		var youtubeDiv = new Element('div',{ 'id':'youtube' });
		Element.insert(document.body, youtubeDiv);
	}
	var t=setTimeout("com.l7creative.startTyping('To access the menu again, pause the video player.', 'youtube');",1500);
	t=setTimeout("com.l7creative.startTyping('', 'youtube')",12000);
	com.l7creative.noYouTube=true;
}
 
com.l7creative.youCanDragIt = false;
com.l7creative.youCanDragItMessage = function ()
{
 
	if(com.l7creative.youCanDragIt){
		return;
	};
	if(!$('youtube')){
		var youtubeDiv = new Element('div',{ 'id':'youtube' });
		Element.insert(document.body, youtubeDiv);
	}
	var t=setTimeout("com.l7creative.startTyping('&lt;- Psst! You can drag and drop the menu.', 'youtube');",2000);
	t=setTimeout("com.l7creative.startTyping('', 'youtube')",9000);
	com.l7creative.youCanDragIt=true;
}
 
com.l7creative.handleSWFAddressChange = function(event) {
	com.l7creative.goHREF(SWFAddress.getValue());
};
 
com.l7creative.handleResize = function()
{
	var l7nav_container = $('l7nav_container');
	var l7nav_udm = $('udm');
	var move_handle = $('move_handle');
	
	var currentPosition = [ parseInt( l7nav_container.getStyle('left') ), parseInt( l7nav_container.getStyle('top') ) ];
	
 
	currentPosition[0] = Math.max( 0 , currentPosition[0] );
	currentPosition[1] = Math.max( 0 , currentPosition[1] );
	
	var viewportDimensions = document.viewport.getDimensions();	
 
	currentPosition[0] = Math.min( ( viewportDimensions.width - l7nav_udm.getWidth() ) , currentPosition[0] );
	currentPosition[1] = Math.min( ( viewportDimensions.height - ( l7nav_udm.getHeight() + move_handle.getHeight() ) ) , currentPosition[1] );
	
 
	new Effect.Move( $('l7nav_container'), {delay: 0, duration:0.5, x: currentPosition[ 0 ], y: currentPosition[ 1 ], mode: 'absolute' });
};
 
com.l7creative.init = function ()
{
	this.configureNav();
	this.configureWelcomeCloseButton();
	this.configureEscapeButton();
	this.createCopyToClipboardLinks();
	this.createIFrame();
	this.createSWFOverlay();
	SWFAddress.addEventListener(SWFAddressEvent.CHANGE, com.l7creative.handleSWFAddressChange);
	
	this.youCanDragItMessage();
};
 
 
 
document.observe("dom:loaded", function() { com.l7creative.init(); });
 
Event.observe(window, "resize", com.l7creative.handleResize.bindAsEventListener(this));
