var PXL = PXL || {};

$(document).ready(function(){
	PXL.anchors.addBehaviors();
	PXL.txtBoxClear();

	$('#newsTickerContent').cycle({ 
		delay:  2000, 
		speed:  500,
		fx: 'custom', 
		cssBefore: { top: 22 }, 
		animIn:  { top: 0 }, 
		animOut: { top: -22 }
	});
	
	/* Investments */
	var investmentIndex = -1;
	var investments = new Array();
	var currentInvestmentIndex = 0;
	
	$("body").append('<input id="currentInvestmentIndex" name="currentInvestmentIndex" value="-1" />');
	
	$(".investments .details").each(function(i) {
		investmentIndex++;
		investments[investmentIndex] = $(this).attr("id");
		if (!$(this).hasClass("hiddencontent")) {
			currentInvestmentIndex = investmentIndex;
		}
	});

	$("#currentInvestmentIndex").val(currentInvestmentIndex);

	$(window).hashchange( function(){
		if (location.hash.length > 0) {
			updateCurrentInvestmentIndex(location.hash.replace("#",""),investments);
		}
	})

	$(window).hashchange();

	$(".investments p.links a.back").click(function(e) {
		e.preventDefault();
		goBack(investments, investmentIndex);
	});
	
	$(".investments p.links a.next").click(function(e) {
		e.preventDefault();
		goForward(investments, investmentIndex);
	});
});

function updateCurrentInvestmentIndex(hashtag, investments) {
	var newInvestmentIndex = $.inArray(hashtag, investments);
	$("#currentInvestmentIndex").val(newInvestmentIndex);
	
	$(".investments .details").addClass("hiddencontent");
	$(".investments #"+hashtag).removeClass("hiddencontent");
}

function goBack(investments, investmentIndex) {
	var currentInvestmentIndex = $("#currentInvestmentIndex").val();

	if (currentInvestmentIndex == 0) {
		currentInvestmentIndex = investmentIndex;
	} else {
		currentInvestmentIndex--;
	}
	$(".investments .details").addClass("hiddencontent");
	$(".investments #"+investments[currentInvestmentIndex]).removeClass("hiddencontent");

	$("#currentInvestmentIndex").val(currentInvestmentIndex)
}

function goForward(investments, investmentIndex) {
	var currentInvestmentIndex = $("#currentInvestmentIndex").val();

	if (currentInvestmentIndex >= investmentIndex) {
		currentInvestmentIndex = 0;
	} else {
		currentInvestmentIndex++;
	}
	$(".investments .details").addClass("hiddencontent");
	$(".investments #"+investments[currentInvestmentIndex]).removeClass("hiddencontent");

	$("#currentInvestmentIndex").val(currentInvestmentIndex)
}
/* =================================================================== */

/**
 * Various link functionality (popups, external, onstate)
 * (original script inspired by Jeremy Keith)
 * @requires jQuery
 */
 PXL.anchors = function() {
	var openWin = function(o,params) {
		window.open(o.href, "newwin","" + params + "");
		return false;
	};
	
	var addBehaviors = function() {
		$('a').live('click', function(e) {
			if (
				$(this).attr('rel') == 'external'
				|| $(this).hasClass('external')
				|| $(this).hasClass('pdf')
				|| $(this).hasClass('popupFull')
				// || this.href.indexOf(location.hostname) == -1
			) {
				// external links
				e.preventDefault();
				return openWin(this,"");
			} else if ($(this).hasClass('popup')) {
				// popup
				e.preventDefault();
				return openWin(this,"height=550,width=600,scrollbars=yes");
			} 
		});
	};
	
	return {
		addBehaviors : addBehaviors
	};
}();

/* =================================================================== */

/**
 * checks for support of a given attribute on a given element
 * @param {Object} element
 * @param {Object} attribute
 */
PXL.elementSupportsAttribute = function(element, attribute){
	var test = document.createElement(element);
	if (attribute in test) {
		return true;
	}
	else {
		return false;
	}
};

/* =================================================================== */

/**
 * check for browser support of the placeholder attribute
 * and if no support exists, add support through script
 * @requires elementSupportsAttribute
 */
PXL.txtBoxClear = function() {
	if (!PXL.elementSupportsAttribute('input', 'placeholder')) {
		$('input[placeholder], textarea[placeholder]')
	        .focus(function() {
	            var $input = $(this);
	            if ($input.val() == $input.attr('placeholder')) {
	                $input.val('');
	                $input.removeClass('placeholder');
	            }
	        })
	        .blur(function() {
	            var $input = $(this);
	            if ($input.val() == '') {
	                $input.addClass('placeholder');
	                $input.val($input.attr('placeholder'));
	            }
	
	        })
	        //Run once on load
	        .blur();
	
	    // Clear all 'placeholders' on submit
	    $('input[placeholder], textarea[placeholder]').parents('form').submit(function() {
	        $(this).find('[placeholder]').each(function() {
	            var $input = $(this);
	            if ($input.val() == $input.attr('placeholder')) {
	                $input.val('');
	            }
	        });
	    });
	}
};

/* =================================================================== */
