/**
 * FIX IE6 IMAGE FLICKER PROBLEM
 *
 */
try {
	document.execCommand('BackgroundImageCache', false, true);
} catch(e) {}


/**
 * DOM READY
 *
 */
$(document).ready(Init);


function Init()
{
	($('#political-parties').size() > 0) ? TabbedContent('#political-parties') : '';
	($('#polling-results').size() > 0) ? PollingResults() : '';
}



function TabbedContent(e)
{
	/*
	Hide all panels and setup display properties.
	*/
	$('div.panel', e).css({
		opacity: 0,
		position: 'absolute',
		top: 0
	});

	/*
	Set height of container to the height of the first visible panel.
	*/
	$('div.panels', e).height($('div.panel:first').height() + 'px');

	/*
	Hide all panels except the first within the parent argument.
	*/
	$('div.panel:first', e).css({opacity: 1, zIndex: 20});

	/*
	Determine currently "lit" panel.
	*/
	var visiblePanel = $('ul.panel-nav li a.lit', e);

	/*
	Retrieve the ID of "lit" panel.
	*/
	var visiblePanelID = $(visiblePanel).attr('href');

	/*
	Setup click events for panel navigation.
	*/
	$('ul.panel-nav li a', e).click(function(){
		// Retrieve the ID of the requested panel.
		var hiddenPanelID = $(this).attr('href');

		// Fade out old panel, fade in new panel.
		$(visiblePanelID, e).animate({opacity: 0}, 900).css({zIndex : 10});
		$('div.panels', e).animate({height: $(hiddenPanelID).height() + 'px'}, 1000);
		$(hiddenPanelID, e).animate({opacity: 1}, 1500).css({zIndex : 20});

		// Update lit state of the selected panel.
		$('ul.panel-nav li a[@href=' + visiblePanelID + ']', e).removeClass('lit');
		$('ul.panel-nav li a[@href=' + hiddenPanelID + ']', e).addClass('lit');

		// Update the variable with the newly-visible panel.
		visiblePanelID = hiddenPanelID;

		return false;
	});
}



function PollingResults()
{
	$('#polling-results, #nav-poll').click(function(){
		PopupOverlay.init('popup-polling');

		$.get($(this).attr('href'), function(data, textStatus){
			PopupOverlay.loadContent($(data).filter('div#wrapper').html());

			/*
			Setup events for closing the overlay.
			*/
			$('#popup-polling a.hdr-graphic').click(function(e){
				return PopupOverlay.close('click', null);
			});
			$(document).bind('keydown', function ClosePopup(e){
				var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
				(key == 27) ? PopupOverlay.close('keydown', ClosePopup) : false;
			});

		});

		return false;
	});
}



/**
 * Popup Overlay
 *
 * Grabs URL within anchor tag and loads the page, filtering out all
 * content except what has been provided as an anchor target.
 *
 * i.e. <a href="popuppage.html#targetID"> ... </a>
 *
 */
var PopupOverlay = {
	offsetToTop : 0,
	offsetOfElement : 0,
	popupID : '',
	status : false,

	init: function(id) {
		var obj = this;

		$('<div id="overlay"></div>').appendTo('body');
		$('#overlay').css({
			height: $(document).height() + 'px',
			width: $(window).width() + 'px'
		});

		this.popupID = (id !== '') ? id : 'PopupDefault';

		$(window).scroll(this._offset);
		$(window).resize(function(){
			$('#overlay').css({
				height: $(document).height() + 'px',
				width: $(window).width() + 'px'
			});

			this._offset();
		});

		this.status = true;

		$('<div class="popup"></div>').attr('id', this.popupID).appendTo('body').addClass($('#wrapper').attr('class'));

		this.loadMessage();

		$('#overlay').click(function(){
			obj.close('click', 'obj.init()');
		});
	},

	loadContent: function(e) {
		$('div.popup').html(e).show();
		this._offset();
	},

	close: function(evt, evtfunc) {
		$('div.popup').remove();
		$("#overlay").remove();
		this.status = false;

		$(document).unbind(evt, evtfunc);

		return false;
	},

	loadMessage: function() {
		$('div.popup').html('<div id="polling-results-overlay"><h1 class="hdr-graphic"></h1><p style="text-align:center;"><br /><strong>Loading... please wait.</strong></p></div>')
		$('div.popup').show();

		this._offset();
	},

	_offset: function() {
		this.offsetToTop = $(window).scrollTop();
		this.offsetOfElement = parseInt(($(window).height() - $('div.popup').height()) / 2);
		$('div.popup').css('top', parseInt(this.offsetToTop + this.offsetOfElement) + 'px');
	}
};