$j(document).ready(function() {

// Homepage refresh
var currentLoc = location.href;
if (currentLoc.indexOf('stage.') == -1) {
	$j(document).oneTime(500000, function() {
		if (currentLoc.indexOf('?') != -1) {
			currentLoc = currentLoc.substring(0,currentLoc.lastIndexOf('?'));
		}
		location.href = currentLoc; 
	});
}

$j('#latest-news .entry-title:gt(0)').hover(
	function() {
		$j(this).nextAll('.preview').css('display', 'block');
	},
	function() {
		$j(this).nextAll('.preview').css('display', 'none');
	}
);

$j('#todays-advocate .sections a').fancybox({
	autoDimensions: false,
	width: 550,
	height: 492,
	margin: 0,
	scrolling: 'no',
	overlayOpacity: 0.5,
	overlayColor: '#000'
});

// Navigation
$j('#nav .primary a').mouseover(function() {
	var id = $j(this).attr('rel');
	$j('#nav .current').removeClass('current');
	$j('#sub-nav .current').css('display', 'none').removeClass('current');
	$j(this).addClass('current');
	$j('#sub-nav #' + id).css('display', 'block').addClass('current');
});

$j('#mid-nav a').click(function() {
	var id = $j(this).attr('href').substring(1, $j(this).attr('href').length);
	$j('#mid-nav .current').removeClass('current');
	$j('#center-content .current').css('display', 'none').removeClass('current');
	$j(this).addClass('current');
	$j('#center-content #' + id).css('display', 'block').addClass('current');
	return false;
});

$j('#mid-nav-2 a').click(function() {
	var id = $j(this).attr('href').substring(1, $j(this).attr('href').length);
	$j('#mid-nav-2 .current').removeClass('current');
	$j('#center-content-2 .current').css('display', 'none').removeClass('current');
	$j(this).addClass('current');
	$j('#center-content-2 #' + id).css('display', 'block').addClass('current');
	return false;
});

// Latest news
function updateLatest(request) {
	var firstID = $j('#latest-news .hentry:eq(0)').attr('id');
	firstID = parseInt(firstID.substring(firstID.lastIndexOf('-') + 1, firstID.length));
	var lastID = $j('#latest-news .hentry:eq(9)').attr('id');
	lastID = parseInt(lastID.substring(lastID.lastIndexOf('-') + 1, lastID.length));
	if (request == 'newer') {
		var start = lastID + 1;
	} else if (request == 'older') {
		var start = firstID - 10;
	}
	var hfeed = '#latest-news .hfeed';
	$j(hfeed).html('');
	var counter = 10;
	$j.getJSON('http://www.2theadvocate.com/templates/articles.index.json?start=' + start + '&quantity=10', function(data) {
		$j.each(data.entries, function(i, entry) {
			if (request == 'newer') {
				var id = lastID + (i + 1);
			} else if (request == 'older') {
				var id = firstID - counter;
				counter--;
			}
			var link = $j('<a/>').attr('class', 'entry-title bookmark').attr('href', entry.url).text(entry.title);
			if (i == 0) {
				var title = $j('<h2/>').html(link);
				if (entry.author != null) {
					var vcard = $j('<div/>').addClass('vcard').html($j('<a/>').addClass('fn url author').attr('href', '#').text(entry.author));
				}
				if (entry.published != null) {
					var time = $j('<time/>').addClass('updated').attr('timedate', entry.published).text(entry.published);
				}
				var summary = $j('<p/>').attr('class', 'entry-summary').text(entry.summary);
				$j('<li/>').attr({ 'id': 'latest-' + id, 'class': 'hentry featured' }).append(title).append(summary).appendTo(hfeed);
			} else {
				$j('<li/>').attr({ 'id': 'latest-' + id, 'class': 'hentry' }).html(link).appendTo(hfeed);
			}

		});
	});
}

$j('#latest-news .older').click(function() {
	updateLatest('newer');
	return false;
});

$j('#latest-news .newer').click(function() {
	if ($j('#latest-news .hentry:eq(0)').attr('id') != 'latest-1') {
		updateLatest('older');
	}
	return false;
});

// Hide/show lead caption
$j('#featured-content .featured-item.big-photo figcaption a').click(function() {
	var visibility = $j('#lead-caption').css('display');
	if(visibility == 'block') {
		$j('#featured-content .featured-item.big-photo figcaption a').text('Show caption');
		$j('#lead-caption').css('display', 'none');
	} else {
		$j('#featured-content .featured-item.big-photo figcaption a').text('Hide caption');
		$j('#lead-caption').css('display', 'block');
	}
	return false;
});

// Dynamic panels
function getItems(panel, section) {
	var hfeed = '#' + panel + ' .hfeed';
	$j(hfeed).html('');
	if (section == '/features/food/recipes') {
		var jsonURL = 'http://www.2theadvocate.com/templates/articles.index.json?type=Recipe&quantity=6&section=' + section;
	} else {
		var jsonURL = 'http://www.2theadvocate.com/templates/articles.index.json?quantity=6&section=' + section; 
	}
	$j.getJSON(jsonURL, function(data) {
		$j.each(data.entries, function(i, entry) {
			var type = entry.type.toLowerCase();
			var link = $j('<a/>').attr('class', 'entry-title bookmark').attr('href', entry.url).text(entry.title);
			if (i == 0) {
				var title = $j('<h1/>').html(link);
				var summary = $j('<p/>').attr('class', 'entry-summary').text(entry.summary);
				if (entry.mediaURL != null) {
					$j('<img/>').attr({ 'alt': entry.mediaAltText, 'src': entry.mediaURL }).css('width', '130px').prependTo(summary);
				}
				$j('<div/>').attr('class', 'hentry featured').append(title).append(summary).appendTo(hfeed);
			} else {
				if (i == 1) {
					$j('<ul/>').attr('class', 'more').appendTo(hfeed);
				}
				$j('<li/>').attr('class', 'hentry ' + type).html(link).appendTo(hfeed + ' ul');
			}
		});
	});
}

$j('.center-col .panel nav a').click(function() {
	var section = $j(this).attr('href');
	var panel = $j(this).parents('.panel:eq(0)').attr('id');
	$j('#' + panel + ' nav a.current').removeClass('current');
	$j(this).addClass('current');
	getItems(panel, section);
	return false;
});

// Zvents widget
$j('#zvents-widget .zvents_invertednav a').click(function() {
	$j('#zvents-widget .zvents_invertednav a').removeClass('current');
	$j(this).addClass('current');
	return false;
});

// Carousel
var pos = 0;
var max = $j('#carousel .slides').css('width');
if (max) {
	max = parseInt(max.substring(0, max.indexOf('px')));
	max = (max / 138) / 6;
	$j('#carousel .prev').click(function() {
		if (pos > 0) {
			$j('#carousel .slides ul').animate({ 'margin-left': '+=900px' }, 500);
			$j(this).css('background-position', 'left top');
			pos--;
			// $j('#carousel .next').css('background-position', 'right top');
		} else {
			// $j('#carousel .next').css('background-position', 'right bottom');	
		}
		return false;
	});
	$j('#carousel .next').click(function() {
		if (pos <= max) {
			$j('#carousel .slides ul').animate({ 'margin-left': '-=900px' }, 500);
			pos++;
			// $j('#carousel .prev').css('background-position', 'left top');
		} else {
			// $j('#carousel .prev').css('background-position', 'left bottom');	
		}
		return false;
	});
}

// Verticals widget
$j('#verticals-widget a').click(function() {
	$j('#verticals-widget a').removeClass('current');
	$j(this).addClass('current');
	return false;
});

var iframe = $j('#verticals-widget .content iframe');

$j('#verticals-widget a.jobs').click(function() {
	iframe.attr({
		src: 'http://2theadvocate.com/includes/verticals-widget.html',
		height: '114'
	});
});

$j('#verticals-widget a.wheels').click(function() {
	iframe.attr({
		src: 'http://advocate.motors.adicio.com/autos/widgets/featuredAutos.php?clientId=advocate&pp=2&cols=2&nopageview=y&nouniquevisitor=y',
		height: '190'
	});
});

$j('#verticals-widget a.shop').click(function() {
	iframe.attr({
		src: 'http://2theadvocate.com/includes/shop-widget.html',
		height: '120'
	});
});


// Search panel
$j('#site-search nav a').click(function() {
	var id = $j(this).attr('href').substring(1, $j(this).attr('href').length);
	$j('#site-search .current').removeClass('current');
	$j(this).addClass('current');
	$j('#site-search #' + id).addClass('current');
	return false;
});

var today = new Date();
if (document.getElementById('archives-month')) {
	var months = document.getElementById('archives-month').getElementsByTagName('option');
	var thisMonth = today.getMonth();
	for( var i = 0; i < months.length; i++ ) {
		var month = months[i];
		if ( i == thisMonth ){
			month.selected = true;
		}
	}
	document.getElementById('archives-day').value = today.getDate();
	document.getElementById('archives-year').value = today.getFullYear();
}

$j('#site-search #by-keywords').submit(function() {
	var source = $j('#site-search #by-keywords select').val();
	var action = 'http://2theadvocate.com/search';
	if (source == 'zvents') {
		action = 'http://calendar.2theadvocate.com/search';
		$j('#site-search #by-keywords input[name=searchKeywords]').attr('name', 'swhat');
		$j('<input/>').attr({ type: 'hidden', name: 'where', value: 'Baton+Rouge%2C+LA' }).appendTo($j(this));
		$j('<input/>').attr({ type: 'hidden', name: 'srad', value: '50.0' }).appendTo($j(this));
		$j('<input/>').attr({ type: 'hidden', name: 'st', value: 'event' }).appendTo($j(this));
		$j('<input/>').attr({ type: 'hidden', name: 'search', value: 'true' }).appendTo($j(this));
	} else if (source == 'classifieds') {
		action = 'http://2theadvocate.ca.kaango.com/feListAds';
		$j('#site-search #by-keywords input[name=searchKeywords]').attr('name', 'search');
		$j('<input/>').attr({ type: 'hidden', name: 'where', value: 'Baton+Rouge%2C+Louisiana' }).appendTo($j(this));
	} else if (source == 'jobs') {
		action = 'http://advocate.careers.adicio.com/careers/jobsearch/results';
		$j('#site-search #by-keywords input[name=searchKeywords]').attr('name', 'kAndEntire');
		$j('<input/>').attr({ type: 'hidden', name: 'city', value: 'Baton+Rouge' }).appendTo($j(this));
		$j('<input/>').attr({ type: 'hidden', name: 'state', value: 'Louisiana' }).appendTo($j(this));
	} else if (source == 'wheels') {
		action = 'http://advocate.motors.adicio.com/autos/search/results.php';
		$j('#site-search #by-keywords input[name=searchKeywords]').attr('name', 'keywords');
		$j('<input/>').attr({ type: 'hidden', name: 'zip', value: '70810' }).appendTo($j(this));
	} else if (source == 'shop') {
		action = 'http://shop.2theadvocate.com/mkt/BatonRouge/local/Results.aspx';
		$j('#site-search #by-keywords input[name=searchKeywords]').attr('name', 'k');
		$j('<input/>').attr({ type: 'hidden', name: 'f', value: '1' }).appendTo($j(this));
	}
	$j(this).attr('action', action);
});


// Poll
if(location.href.indexOf('submitted=y') != -1) {
	location.href = location.href + '#_2theadvocate-poll';
}

$j('#_2theadvocate-poll header ul a').click(function() {
	var current = $j('#_2theadvocate-poll header ul a.current').attr('href');
	var selected = $j(this).attr('href');
	$j(current).removeClass('current');
	$j(selected).addClass('current');
	$j('#_2theadvocate-poll header ul a.current').removeClass('current');
	$j(this).addClass('current');
	return false;
});

if (location.href.indexOf('grid=y') != -1) {
	$j('<div.>').css({
		'width': '100%',
		'height': '100%',
		'position': 'fixed',
		'left': '0',
		'top': '0',
		'background': 'url(http://advocate.ftp.clickability.com/adv_weather/assets/v2/img/grid.png) center repeat-y'
	}).appendTo('body');
}

});
