$(document).ready(function(){
	initTooltip();
	prepareHtml();
	if (cufon) {
		initCufon();
	} else {
		initTitles();
	}
	initSelect();
	initValidate();
	initFancyBox();
	initHover();
	initLinks();
	initStoryNav();
	initBlockHeight();
});

function initBlockHeight(){
	$.fn.maxHeight = function(){
		var max = 0;
		this.each(function() {
			max = Math.max( max, $(this).height() );
		});
		return max;
	};
	if($('#content div.broll h4').get(0)){
		$('#content div.broll-row').each(function(){
			var max_height = '';
			$(this).find('h4').each(function(){
				var height = $(this).height();
				if (height > max_height){
					max_height = height;
				}
			});
			$(this).find('h4').css('height', max_height + 'px');
		});
	}
}

function initLinks() {
	var links = $('a[rel*="external"]');
	$.each(links, function(index, item) {
		$(item).attr('target','_blank');
	}); 
}

function initStoryNav(){	
	$('#stories div.story-item div.text').each(function(){
		var top = parseInt($(this).css('top'));
		$(this).attr('_top',top);
	});
	$('#stories ul.story-links a').click(function(){
		$('#story-navigation').fadeOut();
	});
}

$(window).load(function(){
	initImageContent();
	initCycle();
	initStories();
	
});
function initImageContent(){
	$('#content div.image img').each(function(){
		var alt = $(this).attr('alt');
		var width = $(this).width();
		if (alt) {
			$(this).after('<span>'+alt+'</span>');
			$(this).next().width(width);
		}
	});
}

function prepareHtml(){
	$('#stories div.story-item').each(function(){
		var title = $(this).find('h3').clone();
		var title_story = $(this).find('h2').clone().addClass('skipcufon');
		var links = $(this).find('ul.story-links').clone();
		$(this).append('<div class="nav"></div>');
		$(this).find('.nav').hide();
		title.appendTo($(this).find('.nav'));
		title_story.appendTo($(this).find('.nav'));
		links.appendTo($(this).find('.nav'));
	});
}

function initCycle(){
	if ($('#main div.facts div.slider').get(0)) {
		$('#main div.facts div.slider').cycle({
			fx : 'scrollHorz',
			timeout : 0,
			speedIn : 500,
			speedOut : 500,
			easing : 'swing',
			prev : $('#main div.facts #previous'),
			next : $('#main div.facts #next')
		});
	}
}
function initHover(){
	$('#main ul.story-links a').hover(
		function(){ $(this).next().css('visibility','visible');},
		function(){ $(this).next().css('visibility','hidden');}
	);
}
$(window).load(function(){
	$('#main div.story-item img').each(function(index) {
		var img_width = $(this).width();
		$(this).width(img_width);
		var half_img_width = parseInt(img_width/2);
		$(this).css('marginLeft', (-half_img_width)+'px');
	});
})
function initTooltip(){
	$('#main ul.story-links a').each(function(i,link){
		var title = $(this).attr('title');
		var tooltip = '<span class="tooltip"><span>'+title+'</span></span>';
		$(this).after(tooltip);
		$(this).attr('title','');
	});
}
function initCufon(){
	Cufon.replace('#main h2, #main ul.buttons a,#main div.scroll span, #stories p, #main div.facts div.slider h3',  {fontFamily: 'Merriam',ignoreClass : 'skipcufon'});
	Cufon.replace('#main a.play, #main .cufon, #footer h3, #footer div.language label',  {fontFamily: 'Merriam'});
	Cufon.replace('#header ul.main-menu li a,#footer ul.main-menu li a,',  {fontFamily: 'Merriam', hover:true});
	Cufon.replace('#main div.block h3, #main a.more',  {fontFamily: 'Franklin Gothic Demi'});
	Cufon.replace('#main div.title-petition h3,#stories div.text h3',  {fontFamily: 'Franklin Gothic Heavy',ignoreClass : 'skipcufon'});
	Cufon.replace('#main div.title-container h2, #main div.facts div.title h2,#main .text ul.story-links span.tooltip span',  {fontFamily: 'Merriam',onAfterReplace: function() {initTitles();}});
	//sub pages
	Cufon.replace('#main h1,#content h3',  {fontFamily: 'Merriam', ignoreClass : 'skipcufon'});
	Cufon.replace('#sidebar ul.menu li a, #logo a',  {fontFamily: 'Merriam', hover:true, ignoreClass : 'skipcufon'});
}
function initTitles() {
	if($('#main div.title-container').get(0)) {
		$('#main div.title-container').each(function(index) {
			var $this = $(this);
			var title_wrapper_width = $this.width();
			var title_width = $this.find('h2').width();
			var span_width = parseInt((parseInt(title_wrapper_width)-title_width)/2);
			span_width = span_width-10;
			$this.find('span.border').width(span_width);
		});
	}
	if($('#main div.facts div.title').get(0)) {
		$('#main div.facts div.title').each(function(index) {
			var $this = $(this);
			var title_wrapper_width = $this.width();
			var title_width = $this.find('h2').width();
			var span_width = parseInt((parseInt(title_wrapper_width)-title_width)/2);
			span_width = span_width-10;
			$this.find('span.border').width(span_width);
		});
	}
	if($('ul.story-links span.tooltip span').get(0)) {
		$('ul.story-links span.tooltip span').each(function(index) {
			var $this = $(this);
			var width  = $this.width()+36;
			var a = $this.parent().prev().width();
			var left = parseInt((width)/2) - parseInt((a)/2);
			$this.parent().css({'left':-left});
		});
	}
}

function initSelect(){
	if ($('#subForm #Country').get(0)){
		$('#subForm #Country').selectbox({
			className: 'country-selectbox'
		});
		$('#main div.country-selectbox-list').wrapInner('<div class="country-selectbox-list-holder"><div class="scroll-pane"></div></div>');
		$('#main div.country-selectbox-list').show();
		$('#main div.country-selectbox-list .scroll-pane').jScrollPane({
			verticalGutter : 9
		});
		$('#main div.country-selectbox-list').hide();
	}
	
	if ($('#language select').get(0)){
		$('#language select').selectbox({
			className: 'language-selectbox',
			animationSpeed : 'fast'
		});
		$('#language select').change(function(){
			$('#language').submit();
		});
	}
}
function initFancyBox(){
	$('#stories ul.story-links a, #stories div.story-item a.play').click(function(){
		$(window).scrollTo($(this).parents('.story-item'), 1000, {offset:300});
	});
	$('#main #video a.play').live('click',function(){
		var href = $(this).attr('href');
		$.fancybox.showActivity();
		$.ajax({
			url : href,
			success : function(msg) {
				$.fancybox(msg,{
					width : '940',
					height : 'auto',
					autoScale : false,
					overlayOpacity : 0.8,
					overlayColor : '#000',
					autoDimensions : false
				});
			}
		});
		return false;
	});
	$('#main div.text ul.story-links a,#main div.text a.play').live('click',function(){
		var type = $(this).attr('class');
		var link = $(this);
		var href = $(this).attr('href');
		var story = $(this).parents('div.story-item');
		var header = story.find('.nav').html();
		
		var content;
		if (type == 'play') {
			type = 'video';
			href = story.find('a.video').attr('href');
		}
		var share = story.find('.share-box .'+type+'-share').html();
		if (story.find('div.'+type).get(0)) {
			content = story.find('div.'+type).html(); 
			/*$.fancybox(header+'<div id="ajax-content">'+content+'</div>',{
				width : '940',
				height : 'auto',
				autoScale : false,
				overlayOpacity : 0.8,
				overlayColor : '#000',
				autoDimensions : false
			});*/
			initFader();
			initPopupStory();
			if (cufon) {
				Cufon.replace('#fancybox-content h3',  {fontFamily: 'Franklin Gothic Heavy'});
				Cufon.replace('#fancybox-content h2',  {fontFamily: 'Merriam'});
				Cufon.replace('#fancybox-content ul.story-links span.tooltip span',  {fontFamily: 'Merriam',onAfterReplace: function() {initTitles();}});
			} else {
				initTitles();
			}
			$('#fancybox-content ul.story-links a[class='+type+']').parent().addClass('active');
			initNav();
			initGallery();
		} else {
			$.fancybox.showActivity();
			$.ajax({
				url : href,
				success : function(msg) {
					content = msg;
					story.append('<div class="'+type+'">'+msg+'</div>');
					story.find('div.'+type).hide();
					initFader();
					initPopupStory();
					if (cufon) {
						Cufon.replace('#fancybox-content h3',  {fontFamily: 'Franklin Gothic Heavy'});
						Cufon.replace('#fancybox-content h2',  {fontFamily: 'Merriam'});
						Cufon.replace('#fancybox-content ul.story-links span.tooltip span',  {fontFamily: 'Merriam',onAfterReplace: function() {initTitles();}});
					} else {
						initTitles();
					}
					$('#fancybox-content ul.story-links a[class='+type+']').parent().addClass('active');
					initNav();
					initGallery();
				}
			});
		}
		function initFader(){
			$('#fader-story').remove();
			story.append('<div id="fader-story"></div>');
			$('#fader-story').css({
				opacity : 0.8,
				backgroundColor : '#000',
				height : story.height()
			});
			story.find('div.text').fadeOut();
		}
		
		function initPopupStory(){
			$.fancybox.hideActivity();
			if ($('#popup-story').get(0)){
				$('#popup-story').parents('div.story-item').find('div.text').fadeIn();
			}
			$('#popup-story').remove();
			content = content + '<div class="share-block">' + share + '</div>';
			story.append('<div id="popup-story"><div id="fancybox-content"></div><a id="fancybox-close" style="display: inline;"></a></div>');
			$('#popup-story #fancybox-content').html(header+'<div id="ajax-content">'+content+'</div>');
			$('#popup-story a#fancybox-close,#fader-story').live('click',function(){
				$('#popup-story').fadeOut(500,function(){
					$(this).remove();
					$('#story-navigation').fadeIn();
				});
				$('#fader-story').fadeOut(500,function(){
					$(this).remove();
				});
				story.find('div.text').fadeIn();
						
			});
		}
		return false;
	});
}

function initNav(){
	$('#fancybox-content ul.story-links a').live('click',function(){
		$('#fancybox-content ul.story-links li').removeClass('active');
		$(this).parent().addClass('active');
		var type = $(this).attr('class');
		var href = $(this).attr('href');
		var link = $('#main div.text ul.story-links a[href='+href+']');
		var story = link.parents('div.story-item');
		var header = story.find('.nav').html();
		var share = story.find('.share-box .'+type+'-share').html();
		if (story.find('div.'+type).get(0)) {
			content = story.find('div.'+type).html() + '<div class="share-block">' + share + '</div>' ; 
			$('#fancybox-content #ajax-content').html(content);
			if (cufon) {
					Cufon.replace('#fancybox-content #ajax-content h2',  {fontFamily: 'Merriam'});
				}
			
			initGallery();
			//$.fancybox.resize();
		} else {
			$.ajax({
				url : href,
				success : function(msg) {
					
					story.append('<div class="'+type+'">'+msg+'</div>');
					story.find('div.'+type).hide();
					content = msg + '<div class="share-block">' + share + '</div>'; 
					$('#fancybox-content #ajax-content').html(content);
					Cufon.replace('#fancybox-content #ajax-content h2',  {fontFamily: 'Merriam'});
					initGallery();
					//$.fancybox.resize();
				}
			});
		}
		return false;
	});
}

function initGallery(){
	if ($('#fancybox-content #slideshow').get(0)) {
		$('#fancybox-content #slideshow').galleria({
			showCounter : false,
			transition: 'fade',
			extend: function(options){
				$('#fancybox-content #slideshow .galleria-thumbnails-container .galleria-image').append('<div class="image-frame"></div>');
			}
		});
	}
}

function initValidate(){
	//focus
 	$('#main input[type=text]').focus(function(){$(this).addClass('focus-input');});
	$('#main input[type=text]').blur(function(){$(this).removeClass('focus-input');});
	
	if ($('#subForm').length == 0) return false;
	$("#subForm .checkbox").dgStyle();
	var email_address = $('#subForm').find('input#email-address');
	if (email_address.attr('type') != 'text' && email_address.get(0) ) {
		var email_text = $('#subForm').find('input[name=email]');
		var hostname_text = $('#subForm').find('input[name=hostname]');
		var domain_text = $('#subForm').find('input[name=domain]');
		function composite_email(){
			var email = email_text.val();	
			var hostname = hostname_text.val();
			var domain = domain_text.val();
			email = email.replace(/\s+/g, '');
			hostname = hostname.replace(/\s+/g, '');
			domain = domain.replace(/\s+/g, '');
			email = email +'@'+ hostname +'.'+ domain;
			email_address.val(email);
			$('#subForm').validate().element(email_address);
		}
		email_text.keydown(function(){
			composite_email();
		});
		hostname_text.keydown(function(){
			composite_email();
			
		});
		domain_text.keydown(function(){
			composite_email();
		});
		
	 	$('#subForm').validate({
	 		meta: "validate",
			errorPlacement: function(label, element) {
				var div = element.parents('div.form-item');
				label.appendTo(div);
			},
			invalidHandler : function(form, validator) {
				var email = email_text.val();	
				var hostname = hostname_text.val();
				var domain = domain_text.val();
				email = email.replace(/\s+/g, '');
				hostname = hostname.replace(/\s+/g, '');
				domain = domain.replace(/\s+/g, '');
				email = email +'@'+ hostname +'.'+ domain;
				email_address.val(email);
				
			},
	 		submitHandler: function(form) {
	 			var email = email_text.val();	
				var hostname = hostname_text.val();
				var domain = domain_text.val();
				email = email.replace(/\s+/g, '');
				hostname = hostname.replace(/\s+/g, '');
				domain = domain.replace(/\s+/g, '');
				email = email +'@'+ hostname +'.'+ domain;
				$(form).find('input[name="cm-kdudkd-kdudkd"]').val(email);
				//do other stuff for a valid form
					form.submit();
				}
		 });
	} else {
		$('#subForm').validate({
	 		meta: "validate",
			errorPlacement: function(label, element) {
				var div = element.parents('div.form-item');
				label.appendTo(div);
			},
			submitHandler: function(form) {
				//do other stuff for a valid form
					form.submit();
				}
		 });	
	}
	$('#subForm div.checkbox').click(function(){
		var item = $(this).find('input');
		$('#subForm').validate().element(item);
	});
	$('#subForm #Country').change(function(){
		var item = $(this);
		$('#subForm').validate().element(item);
	});
} 
function initStories(){
	if ($('#stories').get(0)) {
		var story = $.getUrlVars()['story'];
		var type = $.getUrlVars()['type'];
		if (story && typeof(story) != undefined && type && typeof(type) != undefined ) {
			$(window).scrollTo('#'+story,800,function(){
				if ($('#'+story+' a.'+type).get(0)) {
					$('#'+story+' a.'+type).click();
				}
				
			});
		}
		
	}
}


