var addthis_pub = "";

var FancyTips = new Class({
	initialize: function (elements, options) {
		this.setOptions({
			"tipcontents": ".tipcontents"
		},options);
		
		elements.each(function (element) {
			contents_element = element.getElement(this.options.tipcontents);
			if (contents_element) {
				var currentTitle = element.getProperty("title");
				element.setProperty("title", currentTitle + " :: " + contents_element.innerHTML);
			}
		}, this);
		
		this.tips = new Tips(elements, options);
	}
});
FancyTips.implement(new Options);

window.addEvent("domready", function(){
	


  $$("#spotlight").each( function() {
    var count = 1;
    var max = $$("#spotlight_content li").length;
    
     function update( count ) {
          var img = "spotlight_img_" + count.toString();
          var h = "spotlight_body_" + count.toString();
          var u = "spotlight_uri_" + count.toString();
          $("spotlight_image").src = $(img.toString()).src;
          $("spotlight_body").setHTML( $(h.toString()).innerHTML);
          $("spotlight_uri").href = $(u.toString()).href;
     }
  
      update(count);

      
      $('spotlight_previous').addEvent("click", function(e) {
        e = new Event(e).stop();
        if (count == 1) {
          count = max;
        } else {
          count--;
        }
        update(count);   
        return false;     
      });
      
      $('spotlight_next').addEvent("click", function(e) {
        e = new Event(e).stop();
        if (count == max) {
          count = 1;
        } else {
          count++;
        }
        update(count);
        return false;        
      });      
      
  });
  
	$$(".favourites").each(function(el){
		$(el).addEvent("click", function(){
        if (window.sidebar) { // firefox
              window.sidebar.addPanel('Red Ant', 'http://www.redant.co.uk/',"");
        } else if( document.all ) { //MSIE
                window.external.AddFavorite( 'http://www.redant.co.uk', 'Red Ant');
        } else {
               alert("Sorry, your browser doesn't support this");
        }
          
		});
	});  
	
	new FancyTips($$(".tooltip li a"), {
		"tipcontents":"span",
		"maxTitleChars": 100,
		"offsets":{ "x":15, "y":-70 }
	});

	new SmoothScroll({"links": $$(".scroll")});
	new AlphaPng();

	$$(".volatile").each(function(el){
		$(el).addEvent("focus", function(){
			if(this.getValue() == this.getProperty("title")) {
				this.value = "";
			}
		});
	});
	
	$$(".print").each(function(el){
		$(el).addEvent("click", function(){
        window.print();
		});
	});	

	$$("a[rel*='external']").each(function(a){ 
		$(a).setProperty("target", "_blank");
	});
	

	$$(".pods li, .archive li, .clients li").each(function(el){
		new ListItemSelector($(el),{ "fxMin": 1, "fxMax": 0.5 });
	});
		
	var togglers = $$("#rightcol .openform");
	var elements = $$("#rightcol .form");
	
	togglers.addEvent("click", function(e){ 
		new Event(e).stop();
	});
	
	var rightColAccordian = new Accordion(togglers, elements,{ "alwaysHide":true, "show":-1 });
	$$("#rightcol .form").each(function(el, i){
		if ($ES(".fielderror", el).length > 0) {
			rightColAccordian.display(i);
		}
	});
	
	$$(".socialize").addEvent("click", function(e){
		new Event(e).stop();
		addthis_url = location.href;
		addthis_title = document.title;
		addthis_click(this);
	});
		
	if($("slideshow")){
		var slideIndicators = $ES("li",$("slideshow"));
	
		var slideshow = new MooSlideshow("slideshow", "#slideshow img", {
			"wait": 5000,
			"fxDuration": 1000,
			"onChange": function(index){
				if(!$(slideIndicators[index])) return;
				slideIndicators.each(function(el){ el.removeClass("selected"); });
				slideIndicators[index].addClass("selected");				
			}
		});
		
		slideIndicators.each(function(el, index){
			el.addEvent("click", function(e){
				slideshow.jumpTo(index);
			});
		});
	}

	if($("homebanner")){
		var flash = new FlashObject("/_images/flash/banner.swf", "lou", "810", "210", "9", "#ffffff");
		flash.addParam("menu","false");
		flash.addParam("quality","high");
		flash.addParam("WMODE","transparent");
		flash.write("homebanner");
	}

	embedCharacter("lou");
	embedCharacter("richard");
	embedCharacter("tim");
	embedCharacter("matt");
	embedCharacter("neil");
	embedCharacter("fitz");
	embedCharacter("sarah");
	embedCharacter("zaf");
	embedCharacter("will");
	embedCharacter("gem");
	embedCharacter("em");
	embedCharacter("paul");
	embedCharacter("shane");
	embedCharacter("francois");
	embedCharacter("dan");
	embedCharacter("ben");
	embedCharacter("luke");
	embedCharacter("craig");
	embedCharacter("rob");
	embedCharacter("chris");

	toggleCharacter();
	
	if ($("meetCharacter")) {	
	 $("meetCharacter").addEvent("click", toggleCharacter);
	}
	if ($("hideCharacter")) {
	 $("hideCharacter").addEvent("click", toggleCharacter);
	}

});

function embedCharacter(charName){
	if($(charName)){
		var flash = new FlashObject("/_images/flash/meet-the-team/" + charName + ".swf", charName, "990", "575", "9", "#ffffff");
		flash.addParam("menu","false");
		flash.addParam("quality","high");
		flash.addParam("WMODE","transparent");
		flash.write(charName);
	}
}

function toggleCharacter(){
	
	if ($("characterAnim")) {
  	var div = $("characterAnim");
  	
  	if (div.getStyle("visibility") == "hidden"){
  		div.setStyle("visibility", "visible");
  		$("meetCharacter").setStyle("display", "none");
  		$("hideCharacter").setStyle("display", "block");
  	} else {
  		div.setStyle("visibility", "hidden");
  		$("meetCharacter").setStyle("display", "block");
  		$("hideCharacter").setStyle("display", "none");
  	}
	}
	
	
	
	
}
