//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);
				} else if(i.hasClassName('dropdown-bg')){
				  i.clonePosition($$('.css-sb-dropdown')[0]);
				  i.show();
				}
			});
		};
		
		//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);
				} else if(i.hasClassName('dropdown-bg')){
				  i.hide();
				}
			});
		}
		
		//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 and Partners Bar
// Turned this into a function since it also applies to the #partnersBar.
// Also note changes to associates bar HTML. -LinusR 2011/4/7
function initAssociatesBar(bar_div_id, is_slideshow)
{
	var associates = Array();

	$$('#' + bar_div_id + ' .asc_details').each(function(e){
		associates.push(e);
	});
	
	associates.pointer = 0;
	associates.is_slideshow = is_slideshow;
    associates.tm = "";
	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;
		}
	}

	var next_link = $(bar_div_id + '_next');
	var prev_link = $(bar_div_id + '_prev');
	
	if (next_link)
	{
		next_link.clickFX = function(){
			if (associates.length > 1)
			{
				var c = associates.current();
				
				new Effect.Opacity(c.identify(), {
					from: 1,
					to: 0,
					duration: 0.5,
					beforeSetup: function(){
						next_link.stopObserving('click', next_link.clickFX);
						prev_link.stopObserving('click', prev_link.clickFX);
						
						var n = associates.next();
						n.show();
						
						new Effect.Opacity(n.identify(), {
							from: 0,
							to: 1,
							duration: 0.5,
							afterFinish: function(){
								c.hide();
								next_link.observe('click', next_link.clickFX);
								prev_link.observe('click', prev_link.clickFX);
							}
						});
					}
				});
                if(associates.is_slideshow) {
                    associates.slide();
                }
			}
		};
		next_link.observe('click', next_link.clickFX);
	}
	
	if (prev_link)
	{
		prev_link.clickFX = function(){
			if (associates.length > 1)
			{
				var c = associates.current();
				
				new Effect.Opacity(c.identify(), {
					from: 1,
					to: 0,
					duration: 0.5,
					beforeSetup: function(){
						next_link.stopObserving('click', next_link.clickFX);
						prev_link.stopObserving('click', prev_link.clickFX);
						
						var p = associates.prev();
						p.show();
						
						new Effect.Opacity(p.identify(), {
							from: 0,
							to: 1,
							duration: 0.5,
							afterFinish: function(){
								c.hide();
								next_link.observe('click', next_link.clickFX);
								prev_link.observe('click', prev_link.clickFX);
							}
						});
					}
				});
			}
		};
		prev_link.observe('click', prev_link.clickFX);
	}

    associates.slide = function() {
        if(next_link) {
            clearTimeout(associates.tm);
            associates.tm = setTimeout(next_link.clickFX, 5000);
        }
    }

    if(associates.is_slideshow) {
        associates.slide();
    }
}

// Bind the events for the associates and partners bars.
document.observe('dom:loaded', function(){
	initAssociatesBar('associatesBar');
	initAssociatesBar('partnersBar',true);
});


//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 = [];
	if($(slideshow)!=undefined && $(slideshow)!=null){
  var nl = $(slideshow).getElementsByTagName('div');
  for (var i = 0; i < nl.length; i++) {
    if (Element.hasClassName(nl[i], 'slide')) {	  
      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;
	this.slides[i].style.display = '';
  }

  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 + "')";
    }
}

function cl_url(t, lps, oid, cid, url) {
        var a_url = 'templates/partners/helpers/partners_offer_cl.php';
        new Ajax.Request(a_url, {
            parameters: {'type': t, 'lps':lps, 'oid':oid, 'cid':cid },
            onComplete: function (t) {
                window.location = url;
            }
        });
        //window.location = url;
}

