
/**
 * Header steps
 * Usage: $(<selector>).headersteps();
 */

jQuery.fn.extend({
	headersteps: function () {
		
		if (this) {
			
			var expandEl = $(this).find('.hdr a.more'),
				closeEl = $(this).find('.hdr a.close'),
				steps = $(this).find('.container');
			
			expandEl.click(function () {
				expandEl.hide();
				closeEl.show();
				steps.slideDown();
			});
			
			closeEl.click(function () {
				expandEl.show();
				closeEl.hide();
				steps.slideUp();
			});
			
		}
		
	}
});


/**
 * Top news
 * Usage: $(<selector>).topnews();
 */

jQuery.fn.extend({
	topnews: function () {
		
		if (this) {
			
			var me = this;
			
			$(this).find('.title').each(function (idx,el) {
				
				$(el).mouseover(function () {
					
					$(me)
						.find('.img img:visible').hide().end()
						.find('.img img:eq(' + idx + ')').show().end()
						.find('.title.active').removeClass('active');
					$(this).addClass('active');
					
				});
				
			});
			
			$(me)
				.find('.img img:visible').hide().end()
				.find('.img img:first').show().end()
				.find('.title:first').addClass('active');
			
		}
		
	}
});


/**
 * Search form decorator
 * Usage: $(<selector>).searchFormDecorator();
 */

jQuery.fn.extend({
	searchFormDecorator: function () {
		
		if (this) {
			
			var me = this;
			
			$(this).find('p a').click(function () {
				$(me).find('input:text').val($(this).text());
			});
			
		}
		
	}
});


/**
 * Tell truth button decorator
 * Usage: $(<selector>).tellTruthDecorator();
 */

jQuery.fn.extend({
	tellTruthDecorator: function () {

		if (this) {
			
			var me = this,
				overlay = $('#ol-telltruth');
			
			var alignOverlay = function () {
				var offset = $(me).offset(),
					hd = overlay.find('.hd');
				overlay.css('left', offset.left - parseInt(hd.css('left')));
				overlay.css('top', offset.top - parseInt(hd.css('top')));
			}
			
			var blockUI = function () {
				$.blockUI({
					overlayCSS: {
						backgroundColor: '#000', 
						opacity: 0.5, 
						cursor: 'default'
					},
					message: null
				});
			}
			
			var unblockUI = function () {
				$.unblockUI();
			}
			
			$(this).click(function () {
				
				overlay.show();
				alignOverlay();
				blockUI();
				
				$(window).resize(function () {
					alignOverlay();
				});
				
			});
			
			overlay.find('.close').click(function () {
				overlay.hide();
				unblockUI();
			});
			
		}

	}
});



/**
 * News navigator link decorator
 * Usage: $(<selector>).newsNavigatorDecorator();
 */

jQuery.fn.extend({
	newsNavigatorDecorator: function () {

		if (this) {
			
			var me = this,
				overlay = $('#ol-newsnav');
			
			overlay.find('.close').click(function () {
				overlay.hide();
			});
			
			$(this).click(function () {
				overlay.show();
			});
			
		}

	}
});

/**
 * RSS link decorator
 * Usage: $(<selector>).newsNavigatorDecorator();
 */

jQuery.fn.extend({
	RSSDecorator: function () {

		if (this) {
			
			var me = this,
				overlay = $('#ol-rss');
			
			overlay.find('.close').click(function () {
				overlay.hide();
			});
			
			$(this).click(function () {
				overlay.show();
			});
			
		}

	}
});


/**
 * Calendar link decorator
 * Usage: $(<selector>).calendarLinkDecorator();
 */

jQuery.fn.extend({
	calendarLinkDecorator: function () {

		if (this) {
			
			var me = this,
				overlay = $('#ol-calendar');
			
			overlay.find('.close').click(function () {
				overlay.hide();
			});
			
			$(this).click(function () {
				overlay.show();
			});
			
		}

	}
});


/**
 * More places link decorator
 * Usage: $(<selector>).morePlacesLinkDecorator();
 */

jQuery.fn.extend({
	morePlacesLinkDecorator: function () {

		if (this) {
			
			var me = this,
				overlay = $('#ol-places'),
				closeLink = overlay.find('.close');
			
			var alignOverlay = function () {
				overlay.css('left', $(me).offset().left - parseInt(closeLink.css('left')));
				overlay.css('top', $(me).offset().top - parseInt(closeLink.css('top')));
			}
			
			$(this).click(function () {
				overlay.show();
				alignOverlay();
			});
			
			overlay.find('.close').click(function () {
				overlay.hide();
			});
			
			$(window).resize(function () {
				alignOverlay();
			});
			
		}

	}
});


/**
 * Tell truth button decorator
 * Usage: $(<selector>).tellTruthDecorator();
 */

jQuery.fn.extend({
	tellTruthDecorator: function () {

		if (this) {
			
			var me = this,
				overlay = $('#ol-telltruth');
			
			var alignOverlay = function () {
				var offset = $(me).offset(),
					hd = overlay.find('.hd');
				overlay.css('left', offset.left - parseInt(hd.css('left')));
				overlay.css('top', offset.top - parseInt(hd.css('top')));
			}
			
			var blockUI = function () {
				$.blockUI({
					overlayCSS: {
						backgroundColor: '#000', 
						opacity: 0.5, 
						cursor: 'default'
					},
					message: null
				});
			}
			
			var unblockUI = function () {
				$.unblockUI();
			}
			
			$(this).click(function () {
				
				overlay.show();
				alignOverlay();
				blockUI();
				
				$(window).resize(function () {
					alignOverlay();
				});
				
			});
			
			overlay.find('.close').click(function () {
				overlay.hide();
				unblockUI();
			});
			
		}

	}
});

jQuery.fn.extend({
	socialsDecorator: function () {

		if (this) {
			
			$(this).find('li')
				.mouseover(function () {
					$(this).find('span').show();
				})
				.mouseout(function () {
					$(this).find('span').hide();
				});
			
		}

	}
});


jQuery.fn.extend({
	newsNav: function () {
		var selected = [];
		function select() {
			var slash = this.href.lastIndexOf('/');
			var tid = this.href.substr(slash+1);
			selected.push(tid);
			
			var selected_term = $('<span>'+$(this).text()+'&nbsp;<a class="rem" href="'+this.href+'"><img src="/sites/all/themes/truth/images/ico-remove.gif" /></a></span>');
			selected_term.find('a').click(deselect);
			$(this).replaceWith(selected_term);
			return false;
		}
		
		function deselect() {
			var slash = this.href.lastIndexOf('/');
			var tid = this.href.substr(slash+1);
			for (index in selected) {
				if (selected[index] == tid) {
					selected.splice(index, 1);
				}
			}
			var span = $(this).parent();
			
			var deselected_term = $('<a href="'+this.href+'">'+span.text().trim()+'</a>').click(select);
			span.replaceWith(deselected_term);
			return false;
		}
		
		function submit() {
			if (selected.length) {
				var query = [];
				for (index in selected) {
					query.push('term[]='+selected[index]);
				}
				
				window.location = '/navigator/?' + query.join('&');
			}
			
			return false;
		}

		if (this) {
			$(this)
				.find('table a').click(select).end()
				.find('input').click(submit);
		}

	}
});

/**
 * Phototruth
 * Usage: $(<selector>).phototruth();
 */

jQuery.fn.extend({
	phototruth: function () {
		
		if (this) {
			
			var $this = $(this),
				tabs = $this.find('td'),
				img = $this.find('.img img');
				
			tabs.each(function (idx, tab) {
				$(tab).mouseover(function () {
					tabs.removeClass('active');
					$(tab).addClass('active');
					setTimeout(function () {
						img[0].src = $(tab).find('a')[0].rel;
					})
				});
			})
			
		}
		
	}
});


/**
 * Hot news
 * Usage: $(<selector>).hotnews();
 */

jQuery.fn.extend({
	hotnews: function () {
		
		if (this) {
			
			var $this = $(this),
				tabs = $this.find('.tabs a'),
				blocks = $this.find('.block');
				
			tabs.each(function (idx, link) {
				$(link).click(function () {
					tabs.parent().removeClass('active');
					$(this).parent().addClass('active');
					blocks.hide();
					$(blocks[idx]).show();
				});
			})
			
		}
		
	}
});
