try {
    document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}

var CoverTooltip = function() {
	  
	  var pub = {};

	  var yOffset = 15;
	  var xOffset = 10;

	  var viewFrameWidth;
	  var viewFrameHeight;
	  var toolTipWidth;
	  
	  
	  var hover_over = function(e)
	  {
		  
	      viewFrameWidth = $(window).width();
	      viewFrameHeight = $(window).height();
	      
	      
	      this.t = this.title;
	      this.title = "";
	      if(this.t != ""){
	    	  description = this.t
	    	  
	    	  bigsrc = this.src.replace("90x90", "150x150");
	    	  bigsrc = bigsrc.replace("50x50", "150x150");
	    	  
	    	  parts = this.t.split('|');
	    	  if(parts.length == 2)
	    	  {
	    		  description = '<h1>'+parts[0]+'</h1>'+'<p>'+parts[1]+'</p>';
	    	  } else {
	    		  description = '<h1>'+this.t+'</h1>';  
	    	  }
	    	  
	    	  tt = $("body").append('<div id="cover_tooltip" class="clearfix"><div class="cover"><img src="'+bigsrc+'"/></div><div class="text">'+ description +'</div></div>');
	    	  current_width = $('#cover_tooltip').width();
	    	  
	    	  
	    	  x = (e.pageX+350 > viewFrameWidth) 
	    	  	  ? viewFrameWidth-current_width-50 
	    	  	  : e.pageX+xOffset;
	    	  
	    	  
	    	  $("#cover_tooltip")
	    	  	.css("bottom",(viewFrameHeight - e.pageY + yOffset) + "px")
	    	  	.css("left", x + "px")
	    	  	.fadeIn("fast");
	      }
	      
	      
	 
	  };
	  
	  var hover_out = function(e)
	  {
		  this.title = this.t;
	      $("#cover_tooltip").remove();
	  };
	  
	  
	  pub.initTooltip = function() {
		  $(".cover_tooltip").hover(hover_over, hover_out);
		  
		  $(".cover_tooltip").mousemove(function(e){
	    	  x = (e.pageX+350 > viewFrameWidth) 
    	  	  ? viewFrameWidth-current_width-50 
    	  	  : e.pageX+xOffset;
	    	  
	    	  $("#cover_tooltip")
	    	  	.css("bottom",(viewFrameHeight - e.pageY + yOffset) + "px")
	    	  	.css("left", x + "px")
	    	  	.fadeIn("fast");
			  
		  });

		  
	  };
	  
	  return pub;
}();


//Dispatcher actions
var page_actions = {
  'home_index': function()
  {
	$('#teaser_cover_image').hide().show();
	teasers = $("#home_teasers .teaser");
	teasers.css('float', 'left');
	
	$("#home_teasers").css("width", teasers.length*550 + "px");
	
	$(".tab").click(function(evt){
		$(".tab.active").toggleClass('active');
		$(this).toggleClass('active');
		pageTracker._trackPageview(this.href);
	});
  
	$.localScroll.defaults.axis = 'x';
		  
	// Scroll initially if there's a hash (#something) in the url 
	$.localScroll.hash({
		target: '#home_teasers_wrap', // Could be a selector or a jQuery object too.
		queue:true,
		duration:1500
		
	});
	
	$.localScroll({
		target: '#home_teasers_wrap', // could be a selector or a jQuery object too.
		queue:true,
		duration:1000		
	});
	$('#teaser_cover').show().hide().show();
  },
  'history_index': function()
  {
	
	new HistoryAccordion($('#decades'));

	
	
  },
  'artist_index': function() {
	  $('.quote .short').each(function(elem){
		  var short = $(this);
		  short.show();
		  var full = short.next();
		  full.hide();

		short.find('.toggle').click(function(e){
		 e.preventDefault();
		 full.slideToggle();
		 short.hide();
		
	  });
	  
	  });
		$.localScroll.hash({
			  axis:'y'
			, duration:2000
			, onBefore: function(e, t)
			{
				pageTracker._trackPageview('testimonial:'+t.name);
			}
		});

  }
};

$(document).ready(function() {
	
	body_id = document.body.id;
	if(page_actions[body_id])
	{
		page_actions[body_id]();
	}
	
	// things to do for all pages
	pp = $("a[rel^='prettyPhoto']");
	pp.prettyPhoto({
			animationSpeed: 'normal',
			padding: 30,
			opacity: 0.8,
			showTitle: false,
			allowresize: true,
			counter_separator_label: '/',
			theme: 'dark_rounded',
			callback: function(){}
	});
	pp.click(function(){
		//track photo click
		pageTracker._trackPageview(this.href);
	});
	
	Tooltip.initTooltip();
	CoverTooltip.initTooltip();
//	Countdown.initialize();
	$('#language_select').show();
	$('#language_list').hide();
	
});



var Countdown = function() {
	
	// static properties
	var $public = {};
	
	$public.initialize = function() {
		var spanify = function(v)
		{
			return '<span>'+v.toString().split('').join('</span><span>')+'</span>';
		};
		
		var update_counter = function() {
			
			if(counter > 0)
			{
				diffDate = counter--;
				
				// floor() liefert nur den Anteil vor dem Komma
				d = Math.floor(diffDate / 24 / 60 / 60 );
				d = d < 10 ? "0"+d : d;
				
				diffDate = diffDate - (d*24*60*60);
				h = Math.floor(diffDate / 60 / 60);
				h = h < 10 ? "0"+h : h;
				
				diffDate = (diffDate - (h*60*60));
				m = Math.floor(diffDate/60);
				m = m < 10 ? "0" + m : m;
				
				diffDate = diffDate - (m*60);
				s = Math.floor(diffDate);
				s = s < 10 ? "0"+s : s;
					
				$('#days').html(spanify(d));
				$('#hours').html(spanify(h));
				$('#minutes').html(spanify(m));
				$('#seconds').html(spanify(s));
				
			}
			
		};
		update_counter();
		
		var interval = window.setInterval(update_counter, 1000);

	}

	return $public;
}();



//Inspired by base2 and Prototype
(function(){
var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;

// The base Class implementation (does nothing)
this.Class = function(){};

// Create a new Class that inherits from this class
Class.extend = function(prop) {
 var _super = this.prototype;

 // Instantiate a base class (but only create the instance,
 // don't run the init constructor)
 initializing = true;
 var prototype = new this();
 initializing = false;

 // Copy the properties over onto the new prototype
 for (var name in prop) {
   // Check if we're overwriting an existing function
   prototype[name] = typeof prop[name] == "function" &&
     typeof _super[name] == "function" && fnTest.test(prop[name]) ?
     (function(name, fn){
       return function() {
         var tmp = this._super;

         // Add a new ._super() method that is the same method
         // but on the super-class
         this._super = _super[name];

         // The method only need to be bound temporarily, so we
         // remove it when we're done executing
         var ret = fn.apply(this, arguments);       
         this._super = tmp;

         return ret;
       };
     })(name, prop[name]) :
     prop[name];
 }

 // The dummy class constructor
 function Class() {
   // All construction is actually done in the init method
   if ( !initializing && this.init )
     this.init.apply(this, arguments);
 }

 // Populate our constructed prototype object
 Class.prototype = prototype;

 // Enforce the constructor to be what we expect
 Class.constructor = Class;

 // And make this class extendable
 Class.extend = arguments.callee;

 return Class;
};
})();


var HistoryAccordionItem = Class.extend({
	 init: function(accordion, container){
		this._accordion = accordion;
		this._container = container;
		this._loaded = container.hasClass('open');

		// find items
		this._opener = container.find(".decade_opener");
		this._opener.bind("click", {self: this}, accordion.click);
		this._content = container.find(".decade_content");

	  },

	  hideContent: function()
	  {
		  this._container.hide();
	  },
	  toggle: function() {
		this._container.toggleClass('open');	 
		myself = this;
		
		if(!this._loaded)
		 {
			
			pageTracker._trackPageview(this._opener[0].rel);
			 $.ajax({
				   url: this._opener.attr('rel')
				 , cache: false
				 , success: function(html) {
				 	myself._content.hide();
				 	myself._content.append(html);
				 	myself._loaded = true;
				 	myself._content.slideDown("slow");
			 	}
			 
			 });
			 
		 } else {
			 this._content.slideToggle();	 
		 }
		 
	  }
	  
});



var HistoryAccordion = Class.extend({
	 init: function(container){
	
		this.container = container;
		this.items = [];
		this.active = null; 
			
		var self = this;
		
		container.find(".decade").each(function(i) {
			
			ai = new HistoryAccordionItem(self, $(this));
			self.items.push(ai);
		});
		/*
		// find open one and close it
		if(this.active)
		{
			content = this.active._content;
			content.hide();
			setTimeout(function(){content.slideDown(1000)}, 500);	
		}
*/		
	  },
	  
	  click: function(e)
	  {
		  
		  e.preventDefault();
		  e.data.self.toggle();
	  }
});


var Tooltip = function() {
	  
	  var pub = {};

	  /* CONFIG */
	  xOffset = 10;
	  yOffset = -17;

	  var viewFrameWidth;
	  var viewFrameHeight;
	  var toolTipWidth;

	  pub.initTooltip = function() {
	    tooltip = $(".tooltip");
	    
	    tooltip.hover(function(e){
	      
	      //get current viewframe width & height
	      viewFrameWidth = $(window).width();
	      viewFrameHeight = $(window).height();
	      if(this.title)
	      {
	    	  this.t = this.title;
	    	  $(this).attr('title', '');
	    	  $(this).attr('alt', '');
	      }
	      
	      if(this.t != ""){
	      $("body").append("<p id='tooltip'><span></span>"+ this.t +"</p>");
	      $("#tooltip")
	        .css("bottom",(viewFrameHeight - e.pageY + xOffset) + "px")
	        .css("left",(e.pageX + yOffset) + "px")
	        .fadeIn("fast");
	      }
	      current_width = $('#tooltip').width();
	      toolTipWidth = current_width > 250 ? 250 : current_width;
	      },
	    function(){
	      this.title = this.t;
	      $("#tooltip").remove();
	    });
	    
	    tooltip.mousemove(function(e){
	      var platz = (e.pageX - viewFrameWidth)*-1;

	      if(platz - toolTipWidth < 50) {
	    	w = (platz-50) > 250 ? 250 : (platz-50);
	        $("#tooltip")
	          .css("bottom",(viewFrameHeight - e.pageY + xOffset) + "px")
	          .css("left",(e.pageX + yOffset) + "px")
	          .css("width", w + "px");
	          
	      } else {
	        $("#tooltip")
	          .css("bottom",(viewFrameHeight - e.pageY + xOffset) + "px")
	          .css("left",(e.pageX + yOffset) + "px")
	          .css("width", toolTipWidth + "px");
	      };
	    });
	  }

	  return pub;
	}();