/* CUSTOM FUNCTIONS
*************************************************************/
function facts_fader() {
	var self = this;
	self.current_slide = 0;
	
	self.init = function() {
		$('.fact strong:gt(0)').hide();
		    setInterval(function(){
				var next = (self.current_slide +1 >= $('.fact strong').length) ? 0 : self.current_slide + 1;
				self.next_slide(self.current_slide, next);
				self.current_slide = next;
			}, 
		      10000);
	
	}
	
	self.next_slide = function(index, next_slide){
		$('.fact strong:eq('+index+')').fadeOut('fast', function(){
			$('.fact strong:eq('+next_slide+')').fadeIn();
			sIFR.replace(
				frutiger_black, {
					selector: '.fact strong',
					css: ['.sIFR-root{ color: #AAAAAA; font-size: 24px; leading: 5;}',
					'.sIFR-root a{ color: #bb0000; font-size: 17px; text-decoration: none;}',
					'.sIFR-root a:hover{ color: #bb0000; text-decoration: underline;}'],
				  	wmode: 'transparent'
			});
		});
	}
	self.init();
}

function overlay() {
	var self = this;
	self.popURL = $('.overlay[href^=#]').attr('href');
	self.popWidth = "";
	
	self.init = function() {
		$('.overlay').click(function() {
			self.popID = $(this).attr('rel');
			self.popMargTop = parseInt($('#' + self.popID).height() + 30) / 2;
			self.popMargleft = parseInt($('#' + self.popID).width() + 120) / 2;
			
			if($('#' + self.popID).find('.video').length > 0){
				self.popWidth = 495;
				
				var tmp = $('#' + self.popID).find('.video:eq(0)').attr('href');		
				$('#' + self.popID).find('.video:eq(0)').media({flashvars:{image:'/t/img/video_background.jpg'}});
				$('#' + self.popID).find('.video:eq(0)').attr('rel', tmp)
			} else if($('#' + self.popID + ' .image_reel').find('.radio').length > 0) {
				self.popWidth = 350;
				$('#radio_box .paging a:eq(0)').addClass('current');	
				var tmp = $('#' + self.popID + ' .image_reel').find('.radio:eq(0)').attr('href');
				$('#' + self.popID + ' .image_reel').find('.radio:eq(0)').media({width: 350, height: 117, flashvars:{image:'/t/img/radio_thumb.jpg'}});
				$('#' + self.popID + ' .image_reel').find('.radio:eq(0)').attr('rel', tmp);
			} else {
				self.popWidth = 350;
				$('#print_box .paging a:eq(0)').addClass('current');		
				
				sIFR.replace(
					frutiger_bold, {
						selector: '#campaign_description h3',
						css: ['.sIFR-root{ color: #cccccc; font-size: 17px; leading: 0; }',
							'.sIFR-root a { color: #cccccc; text-decoration: none; }',
							'.sIFR-root a:hover { color: #cccccc; text-decoration: underline; }'
							],
					  	wmode: 'transparent'
				});
				
			}
			
    	$('#' + self.popID).fadeIn().css({'width': Number( self.popWidth ) }).prepend('<a href="#" class="close" title="Close">x</a>');
			$('#' + self.popID).css({
				'margin-top' : -self.popMargTop,
				'margin-left' : -self.popMargleft
			});
				
			$('body').append('<div id="fade"></div>');
			$('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn();
	
			$('.close, #fade').live('click', function() {
				self.close();
				return false;
			});
		
			return false; 
		});
	}
	
	self.close = function() {
		$('.radio').stop();
		
		$('.popup_block:visible div.radio').removeClass('radio').each(function(){
			var temp_rel = $(this).attr('rel');
			$(this).replaceWith('<a href="'+temp_rel+'" class="radio" rel="' + temp_rel + '">&nbsp;</a>');
		});
		
		$('paging a').removeClass('current');		
			
		var tmp_id = $('.popup_block:visible').attr('id');
		$('#fade, .popup_block').fadeOut(function() {
			$('#fade, .close').remove();
			
				$('.paging a').removeClass('current');
				$('#' + self.popID + " .image_reel").css({
					'left' : 0
				});
			
			$('#' + self.popID + ' .image_reel').find('.radio:eq(0)').media({width: 350, height: 117, flashvars:{image:'/t/img/radio_thumb.jpg'}});
			$('#' + tmp_id + ' .video').removeClass('video').each(function(){
				
				var temp_rel = $(this).attr('rel');
				$(this).replaceWith('<a href="'+temp_rel+'" class="video" rel="' + temp_rel + '" style="width:640px;height:360px">&nbsp;</a>');
			});
		});
		
		return false;
	}
	self.init();
}

function overlay_slider() {
	var self = this;
//	self.imageWidth = $('.image_reel a').width();
	self.imageWidth = 350;
	self.imageSum = $('.image_reel img').length;
	self.imageReelWidth = self.imageWidth * self.imageSum;
	self.active = $('.paging a.active').next();
	self.triggerID = self.active.attr('rel') - 1;
	self.image_reelPosition = self.triggerID * self.imageWidth;
	self.currentSlider = "";

	self.init = function() {
		$('.paging').show();
		$('.paging').each(function(){
			$(this).find('a:first').addClass("current");
		});
		
		$('.image_reel').each(function(){
			var total_images = $(this).find('img').length;
			$(this).css({'width' : total_images * self.imageWidth + 120})
		});
		
		$('.paging a').each(function() {
			$(this).click(function() {
				$('.paging a').removeClass('current');
				$(this).addClass('current');
				self.currentSlider = $(this).parent().parent().parent().parent().attr('id');
				self.rotate(self.currentSlider);
				return false;
			});
		});
	}
	
	//Paging + Slide Function
	self.rotate = function(currentSlider) {
		var slider = currentSlider;
		var obj_index = $('.popup_block:visible .paging a').index($('.popup_block:visible .paging a.current'));
 		var move_to = obj_index * self.imageWidth;
		$("#" + slider + " .image_reel").animate({'left' : "-" + move_to + "px"}, 500);
		self.setup_media(obj_index);
		return false;
	}
	
	//Rotation + Timing Function
	self.rotateSwitch = function() {
		self.play = setInterval(function() {
			if(self.active.length == 0) {
				self.active = $('.paging a:first');
			}
		}, 7000);
	}
	
	self.setup_media = function(obj_index){
		if($('.popup_block:visible').length > 0){
			if($('.popup_block:visible').find('.radio').length > 0){
				$('.popup_block:visible div.radio').removeClass('radio').each(function(){
					var temp_rel = $(this).attr('rel');
					$(this).replaceWith('<a href="'+temp_rel+'" class="radio" rel="' + temp_rel + '">&nbsp;</a>');
				});
				
				var tmp = $('.popup_block:visible .image_reel').children('.radio:eq(' + obj_index +')').attr('href');
				$('.popup_block:visible .image_reel').children('.radio:eq(' + obj_index +')').media({width: 350, height: 117, flashvars:{image:'/t/img/radio_thumb.jpg'}});
				$('.popup_block:visible .image_reel').children('.radio:eq(' + obj_index +')').attr('rel', tmp);
			}
			
			if($('.popup_block:visible .video').length > 0){
				$('.popup_block div.video').removeClass('radio').each(function(){
					var temp_rel = $(this).attr('rel');
					$(this).replaceWith('<a href="'+temp_rel+'" class="video" rel="' + temp_rel + '">&nbsp;</a>');
				});
				
				var tmp = $('.popup_block:visible').children('.video:eq(0)').attr('href');
				$('.popup_block:visible').children('.video:eq(0)').media();
				$('.popup_block:visible').children('.video:eq(0)').attr('rel', tmp);
				
				$('.popup_block:visible .video:eq(' + obj_index +')').media();
			}
		}
	}
	
	self.init();
//	self.rotateSwitch();
}

function toggle() {
	var self = this;
	
	self.init = function() {
		$('.toggle_container').hide();
		$('.trigger').click(function() {
			$(this).toggleClass('toggle_active').next().slideToggle('slow');
			return false;
		});
		self.initLinks();
	}
	
	self.initLinks = function() {
		$('.show_all').click(function() {
			$('.toggle_container').slideDown('slow');
			$('.show_all').replaceWith('<a href="#" class="close_all" title="Close all">Hide all the laws</a>');
			$('.trigger').addClass('toggle_active');
			self.initLinks();
			
			return false;
		});

		$('.close_all').click(function() {
			$('.toggle_container').slideUp('slow');
			$('.close_all').replaceWith('<a href="#" class="show_all" title="Show all">Show all the laws</a>');
			$('.trigger').removeClass('toggle_active')
			self.initLinks();
			
			return false;
		});
		
		$('.show_all_myths').click(function() {
			$('.toggle_container').slideDown('slow');
			$('.show_all_myths').replaceWith('<a href="#" class="close_all_myths" title="Close all">Hide all the myths</a>');
			$('.trigger').addClass('toggle_active');
			self.initLinks();

			return false;
		});

		$('.close_all_myths').click(function() {
			$('.toggle_container').slideUp('slow');
			$('.close_all_myths').replaceWith('<a href="#" class="show_all_myths" title="Show all">Show all the myths</a>');
			$('.trigger').removeClass('toggle_active')
			self.initLinks();

			return false;
		});
	}
	
	self.init();
}

function back_to_top() {
	var self = this;
	self.vHeight = 0;
	
	self.init = function() {
		$('#back_to_top').hide();
		
		if(document.all) {
		  if(document.documentElement) {
		    self.vHeight = document.documentElement.clientHeight;
		  } else {
		    self.vHeight = document.body.clientHeight
		  }
		} else {
		  self.vHeight = window.innerHeight;
		}

		if(document.body.offsetHeight > self.vHeight + 30) {
			$('#back_to_top').show();
		}
	}
	self.init();
}

/* DOCUMENT READY FUNCTION
*************************************************************/
$(document).ready(function(){
	if($(".media").length > 0){
		$(".media").media();
	}	
	
	var facts = new facts_fader();
	var slider = new overlay_slider();
	var lightbox = new overlay();
	var accordion = new toggle();
	var top_link = new back_to_top();
	//var paginate = new pagination();
	
	$('.hover_over').fadeTo('fast', 0.5);
	
	$('.hover_over').mouseover(function() {
		$(this).fadeTo('slow', 0.8);
	});
	
	$('.hover_over').mouseout(function() {
		$(this).fadeTo('slow', 0.5);
	});
});