$(function(){
	equalHeight();
	equalHeight2();
	cycleBox();
	sameTableRowHeight();
	pollErrorMsg();
	dealsProfileShow();
	dealsBoxPosition();
	closeFB();	
	showLogin();
	modifyEditProfile();
	toggleOther();
	hideLoginLink();
});



function equalHeight() { 
	var tallest = 0;
	$('.box').each(function() {
		var thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	$('.box').height(tallest);
}


function equalHeight2() { 
	var tallest = 0;
	$('.infrablock .slogan').each(function() {
		var thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	$('.infrablock .slogan').height(tallest);
}


/**
 * Cycle the homepage box using our custom paginator
 * Enables clicking on caption to go to article on homepage cycle box
 */
function cycleBox() {
	var $cycle =  $('#cycle');
	
	// show the slide
	$cycle.delegate('#headlines div','mouseover',
		function(event) {
			$(this).addClass('on').siblings().removeClass('on');
			
			var storyToShow = $(this).attr('id').replace('tab-','#cycle-');
			
			$(storyToShow).stop(true,true).fadeIn().siblings(':not(#cycle-container_leftShadow, #cycle-container_bottomShadow)').stop(true,true).fadeOut();
		}
	);
}


function sameTableRowHeight() {
		
	var thisTable;
	
	//extend Array object to return max value
	Array.prototype.max = function() { return Math.max.apply(Math,this); };
	
	//which table has most rows?
	$('.transaction-table').each(function() {
		var thisTable = $(this);
		if ($('.transaction-table').not(this).filter(function() { return $(this).find('tr').length > thisTable.find('tr').length; }).length == 0) table = thisTable;
	});
	
	//harmonise row heights
	if (thisTable)
		thisTable.find('tr').each(function() {
			var thisRowHeights = [];
			$('.transaction-table').find('tr:eq('+$(this).index()+')').each(function() { thisRowHeights.push($(this).height()); }).css('height', thisRowHeights.max());
			
		});

}

function pollErrorMsg() {
	$("#market-poll input[type='submit']").click(function(){
		if(!$("#market-poll input[type='radio']:checked").val()) {
			$('#market-poll form').prepend('<p class="error">Please enter correct value</p>')
			return false;
		}
	});
}



function dealsProfileShow(){
	if ($('#article-content .deals-box .content-wrapper').children().size() > 0) {
		$('#article-content .deals-box').css("display", "block");
	}
}



function dealsBoxPosition() {
	var article = $('#article-copy').children('p');
	if (article.length) {
		var articleHeight = 0;
		for (var i = 0; i < article.length; i++) {
			articleHeight = articleHeight + article[i].offsetHeight;
		}
		var percentCalc = (50 / 100) * articleHeight;	
		if (articleHeight > 200 && articleHeight < 400) {
			$('.push-box').css("height", "70px");
		} else if (articleHeight > 400 && articleHeight < 600 ) {
			$('.push-box').css("height", "120px");
		} else {
			$('.push-box').css("height", percentCalc);
		}
	}
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}


function closeFB() {
	fbPageOptions = {
		outsideClickCloses: false,
		overlayOpacity: 80,
		shadowType: 'none',
		innerBorder: 0,
		infoPos: 'tl',
		controlPos: 'tr'
	};	
}


function showLogin() {

	if(!$('#reg-notloggedin')) { 
		return false ;
	}

	//check if access cookie is set
	var infra_access = readCookie('infra_access') ; 
	
	if(infra_access == null) {	
		$('#reg-notloggedin').show() ;
		$('#reg-loggedin').hide() ;
	} else {
		$('#reg-notloggedin').hide() ;
		$('#reg-loggedin').show() ;	
		
		var infra_products = readCookie('infra_products') ; 
		
		if(infra_products) {
			var aInfraProducts = infra_products.split('%3A') ;
			
			$.each(aInfraProducts,
				function(index, value) { 
  					if (value === 'InfraEurope' || value === 'InfraNews') {
						$('#reg-infraeurope').show() ;
					}
					if (value === 'InfraAmericas') {
						$('#reg-infraamericas').show();
					}
					if (value === 'InfraDeals') {
						$('#reg-infradeals').show() ;
					}
					if (value === 'InfraLatinAmerica') {
						$('#reg-infralatinamerica').show();
					}
				}
			);
		}

	}
	
	return true ;

}


//if we're on the edit profile form show/ hide DP questions depending on what product the user is signed up to
function modifyEditProfile() {

		if(!$('#dp-reg_email_infradaily')) {
			return false ;
		} 

		var infra_products = readCookie('infra_products') ; 
		
		if (infra_products) {
			var aInfraProducts = infra_products.split('%3A') ;
		}

		$('#dp-reg_email_infradaily').hide() ;
		$('#dp-reg_email_infraweekly').hide() ;
		$('#dp-reg_email_infraamericadaily').hide() ;
		$('#dp-reg_email_infraamericaweekly').hide() ;
		
		//return if aInfraProducts never got set
		if (!infra_products) return false;
		
		$.each(aInfraProducts,
			function(infraProduct) {	
				if(infraProduct == 'InfraEurope' || infraProduct == 'InfraNews') {
					$('#dp-reg_email_infradaily').show() ;
					$('#dp-reg_email_infraweekly').show() ;
				}
				if(infraProduct == 'InfraAmericas') {
					$('#dp-reg_email_infraamericadaily').show() ;
					$('#dp-reg_email_infraamericaweekly').show() ;
				}
			}
		) ;
				
}


function toggleOther() { 
	var $otherCheckbox = $('#state-other'),
		$subscribeOther = $('#subscribe-other');
		
	$otherCheckbox.click(
		function() {
			var $checkbox = $(this);
			
			if ($checkbox.is(':checked')) {
				$subscribeOther.show();
			} else {
				$subscribeOther.hide();
			}
		}
	);
}


function hideLoginLink() {
	if('#reg-loggedin:visible'){
		$('.infrablock a.login').hide();
	}
}


