//Common Functions
function ghostInputText(e, text)
{           
	if ((e.value.blank()) || (e.value == text))  
	{               
		e.value = text;
		e.addClassName('ghosted');
		e.blur();
	}

	e.observe('focus', function(){                       
		if ((e.value == text) && (e.hasClassName('ghosted')))
		{
			e.value = '';
		}
		e.removeClassName('ghosted');
	});

	e.observe('blur', function(){
		if (e.value.blank())
		{
			e.value = text;
			e.addClassName('ghosted');
		}
	});        
}

function showSocialTooltip(id)
{
	$('sm_tooltip_'+id).show();
}

function hideSocialTooltip(id)
{
	$('sm_tooltip_'+id).hide();
}

//CSS Dropdown (Language Dropdown)
document.observe('dom:loaded', function(){
	$$('.css-select-box').each(function(e){
		
		//Dropdown function
		e.ddFX = function(){
			e.ancestors().first().addClassName('open');
			e.addClassName('open');
			e.childElements().each(function(i){
				if (i.hasClassName('css-sb-dropdown'))
				{
					//Position the dropdown correctly
					i.clonePosition(e, {setWidth: false, setHeight: false, offsetTop: e.getHeight()});
					
					//Show dropdown
					i.show();
					//i.makePositioned();
					
					//Fix width
					if (e.getWidth() > i.getWidth())
					{
						i.style.width = e.getWidth()+'px';
					}
					
					//Stop observing mouse-click event on select box
					e.stopObserving('click', e.bindDdFX);
					
					e.bindHideFX = e.hideFX.bindAsEventListener(e);
					//Observe mouse-click event on document (slight delay to avoid conflicts)
					setTimeout(function(){document.observe('click', e.bindHideFX)},10);
				}
			});
		};
		
		//Hide dropdown function
		e.hideFX = function(){
			e.ancestors().first().removeClassName('open');
			e.removeClassName('open');
			e.childElements().each(function(i){
				if (i.hasClassName('css-sb-dropdown'))
				{
					//Stop observing mouse-click event on the document
					document.stopObserving('click', e.bindHideFX);
					//Hide dropdown
					i.hide();
					//Bind and observe mouse-click event on select box
					e.bindDdFX = e.ddFX.bindAsEventListener(e);
					e.observe('click', e.bindDdFX);
				}
			});
		}
		
		//Make positioned
		//e.makePositioned();
		
		//Bind and observe mouse-click event on select box
		e.bindDdFX = e.ddFX.bindAsEventListener(e);
		e.observe('click', e.bindDdFX);
		
		//Enable hover effects for dropdown
		e.descendants().each(function(i){
			i.observe('mouseover', function(){ i.addClassName('hover'); });
			i.observe('mouseout', function(){ i.removeClassName('hover'); });
		});
		
	});
});

//Associates Bar
var associates = Array();

document.observe('dom:loaded', function(){
	$$('#associatesBar .asc_details').each(function(e){
		associates.push(e);
	});
	
	associates.pointer = 0;
	
	associates.current = function() {
		return associates[associates.pointer];
	};
	
	associates.next = function() { 
		if (associates.length > 0)
		{
			associates.pointer += 1;  
			if (associates.pointer >= associates.length)
			{
				associates.pointer = 0;
			}
			return associates.current();
		} else {
			return false;
		}
	};
	
	associates.prev = function() {
		if (associates.length > 0)
		{
			associates.pointer -= 1;  
			if (associates.pointer < 0)
			{
				associates.pointer = associates.length - 1;
			}
			return associates.current();
		} else {
			return false;
		}
	};
	
	associates.goto = function(i) {
		if (associates.length > 0)
		{
			associates.pointer = i;  
			if (associates.pointer < 0)
			{
				associates.pointer = 0;
			} else if (associates.pointer >= associates.length) {
				associates.pointer = associates.length - 1;
			}
			return associates.current();
		} else {
			return false;
		}
	}
	
	if ($('asc_next'))
	{
		$('asc_next').clickFX = function(){
			if (associates.length > 1)
			{
				var c = associates.current();
				
				new Effect.Opacity(c.identify(), {
					from: 1,
					to: 0,
					duration: 0.5,
					beforeSetup: function(){
						$('asc_next').stopObserving('click', $('asc_next').clickFX);
						$('asc_prev').stopObserving('click', $('asc_prev').clickFX);
						
						var n = associates.next();
						n.show();
						
						new Effect.Opacity(n.identify(), {
							from: 0,
							to: 1,
							duration: 0.5,
							afterFinish: function(){
								c.hide();
								$('asc_next').observe('click', $('asc_next').clickFX);
								$('asc_prev').observe('click', $('asc_prev').clickFX);
							}
						});
					}
				});
			}
		};
		$('asc_next').observe('click', $('asc_next').clickFX);
	}
	
	if ($('asc_prev'))
	{
		$('asc_prev').clickFX = function(){
			if (associates.length > 1)
			{
				var c = associates.current();
				
				new Effect.Opacity(c.identify(), {
					from: 1,
					to: 0,
					duration: 0.5,
					beforeSetup: function(){
						$('asc_next').stopObserving('click', $('asc_next').clickFX);
						$('asc_prev').stopObserving('click', $('asc_prev').clickFX);
						
						var p = associates.prev();
						p.show();
						
						new Effect.Opacity(p.identify(), {
							from: 0,
							to: 1,
							duration: 0.5,
							afterFinish: function(){
								c.hide();
								$('asc_next').observe('click', $('asc_next').clickFX);
								$('asc_prev').observe('click', $('asc_prev').clickFX);
							}
						});
					}
				});
			}
		};
		$('asc_prev').observe('click', $('asc_prev').clickFX);
	}
});

//Slideshow
/*
  Simple slideshow using prototype and scriptaculous.
  
  Usage:
  
    <script src="prototype.js"></script>
    <script src="effects.js"></script>
    <script src="slideshow.js"></script>
    <style type="text/css">
      #slideshow { position: relative; width: 100px; height: 100px; }
      #slideshow div { position: absolute; left: 0; top: 0; }
    </style>
    <div class="slideshow" id="slideshow">
      <div class="slide"><img src="slide1.jpg"></div>
      <div class="slide"><img src="slide2.jpg"></div>
      <div class="slide"><img src="slide3.jpg"></div>
    </div>
    <script type="text/javascript">new Slideshow('slideshow', 3000);</script>
*/

function Slideshow(slideshow, timeout) {
  this.slides = [];
  var nl = $(slideshow).getElementsByTagName('div');
  for (var i = 0; i < nl.length; i++) {
    if (Element.hasClassName(nl[i], 'slide')) {
	  nl[i].style.display = '';
      this.slides.push(nl[i]);
    }
  }
  this.timeout = timeout;
  this.current = 0;

  for (var i = 0; i < this.slides.length; i++) {
    this.slides[i].style.zIndex = this.slides.length - i;
  }

  Element.show(slideshow);
  setTimeout((function(){this.next();}).bind(this), this.timeout + 850);
}
Slideshow.prototype = {
  next: function() {
    for (var i = 0; i < this.slides.length; i++) {
      var slide = this.slides[(this.current + i) % this.slides.length];
      slide.style.zIndex = this.slides.length - i;
    }

    Effect.Fade(this.slides[this.current], {
      afterFinish: function(effect) {
        effect.element.style.zIndex = 0;
        Element.show(effect.element);
        Element.setOpacity(effect.element, 1);
      }
    });
    
    this.current = (this.current + 1) % this.slides.length;
    setTimeout((function(){this.next();}).bind(this), this.timeout + 850);
  }
}

//PNG Fix
function fixPNG(img) {
	if (navigator.userAgent.indexOf("MSIE") > -1 && parseInt(navigator.appVersion) <= 6) {
      var pSrc = img.src;
      img.onload = null;
      img.src = "assets/no-index/blank.gif";
      img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, src='" + pSrc + "')";
    }
}