/* ------------------------------ */
/* Initiate JS				*/
/* ------------------------------ */

//Add this button variables
var addthis_offset_top = -50;
var addthis_offset_left = 4;


function init(){
	//Home page carousel
	if($('#hero-carousel-container .content-item').length > 0) {
		$('#hero-carousel-wrap').addClass('hero-carousel-wrap-js');
		$('#hero-carousel-container .browse').addClass('show');
		$(".hero-carousel-wrap-js").scrollable({'circular':true,'speed':1000}).autoscroll(7000);
	}

	//spotlight for get creative
	if($('.small-spotlight-wrap .content-item').length > 0) {
		$('.small-spotlight-wrap').scrollable({'circular':true,'speed':900}).navigator({span:'.navi',naviItem:'a'});
	}
	if($('.large-spotlight-wrap .content-item').length > 0) {
		$('.large-spotlight-wrap').scrollable({'circular':true,'speed':900}).navigator({span:'.navi',naviItem:'a'});
	}

	//homepage events + upcoming
	if($('#new-events .event-slide').length > 0) {
		$('.new-event-wrap').scrollable({'circular':true,'speed':900}).navigator({span:'.navi',naviItem:'a'});
	}
	if($('.coming-soon-wrap .event-slide').length > 0) {
		$('.coming-soon-wrap').scrollable({'circular':true,'speed':900}).navigator({span:'.navi',naviItem:'a'});
	}

	// Explore What's On Form
	$('#exploreWhatsOn form input').focus(function() {
		$(this).blur();
	});

	// Homepage Tabs
	//$('#coming-soon').addClass('hide');
	$('#second-row .block h2').click(function(){
		if ($(this).hasClass('active')) {
			return false;
		} else {
			var that = $(this);
			$(that)
				.parent('.block')
				.children('h2')
				.toggleClass('active')
			$(that)
				.parent('.block')
				.children('.content-group')
				.toggleClass('hide');
		}
		return false;
	});

	//Initialise the h1 tag line
	$('h1').addClass('banner' + Math.ceil(Math.random() * 5));

	//Form overlabels
	$("label[for='searchfield'], label[for='signup_gig1'], label[for='signup_gig2']").overlabel();

	//Add this button
	$('.share-this')
		.click(function(){
			return addthis_sendto();
		})
		.mouseover(function() {
			return addthis_open(this, '', '[URL]', '[TITLE]');
		})
		.mouseout(function() {
			addthis_close();
		});

	// Video transcripts
	jQuery.each($('.transcript'), function() {

		var transcript = $(this);

		// Create toggle link
		var link = $('<a class="view-transcript" href="#">View transcript</a>')
			.click(function() {
				if (transcript.css('display') == 'none') {
					transcript.css('display', 'block');
				} else {
					transcript.css('display', 'none');
				}
				return false;
			});

		// Hide transcript and add link
		transcript
			.css('display', 'none')
			.before(link);

	});

	// Show/hide category and venue drop-downs
	$('#exploreWhatsOn h2').click(function(args) {
		if ($(this).attr('class') === 'all-venues') {
			return;
			}
		var dropped = $('#exploreWhatsOn .dropped');
		$(this)
			.next('.drop')
			.slideDown('slow')
			.addClass('dropped');
		if(dropped && dropped != $(this)) {
			dropped.slideUp().removeClass('dropped');
		}
		return false;
	});

	// Show/hide calendar
	/*$('#exploreWhatsOn #calendar-nav-link').click(function() {
		$('#eventsCalendar')
			.slideToggle('slow');
	});*/

	// Stop manual date entry
	$('#exploreWhatsOn form input').focus(function() {
		$(this).blur();
	});

	var calendarMonthName = [
		'January',
		'February',
		'March',
		'April',
		'May',
		'June',
		'July',
		'August',
		'September',
		'October',
		'November',
		'December'
	];

	var calendarDaysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

	$('#eventsCalendar .prev, #eventsCalendar .next').click(function() {

		// Get today's date
		var curDate = new Date();
		var curYear = curDate.getFullYear();
		var curMonth = curDate.getMonth();
		var curDay = curDate.getDate();

		// Get current calendar date
		var curCalDate = $('.monthLink').attr('href').split('/');
		var curCalMonth = parseInt(curCalDate.pop(), 10)-1;
		var curCalYear = parseInt(curCalDate.pop(), 10);

		var date = new Date(curCalYear, curCalMonth, 1);

		// Increment/decrement date by one month
		if ($(this).hasClass('next')) {
			date.setMonth(date.getMonth()+1);
		} else {
			date.setMonth(date.getMonth()-1);
		}

		var year = date.getFullYear();
		var month = date.getMonth();
		var day = 1;

		// Get first day of month
		var firstDay = new Date(year, month, day);
		var startingDay = firstDay.getDay()-1;

		// If the first day is negative then "wrap around"
		if (startingDay < 0) {
			startingDay = 7 + startingDay;
		}

		// Get number of days in month
		var monthLength = calendarDaysInMonth[month];
		if ((month == 1) && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)) {
			monthLength = 29;
		}

		// Render calendar
		var calendar = $('<tbody></tbody>');

		for(var week = 0; week < 6; week++) {

			var tr = $('<tr></tr>');

			for (var weekDay = 0; weekDay < 7; weekDay++) {

				if (day <= monthLength && (week > 0 || weekDay >= startingDay)) {

					var dateString = year + '/' + jQuery.fn.padNum(month + 1) + '/' + jQuery.fn.padNum(day);
					var link = $('<a href="/whatson/' + dateString + '">' + day + '</a>');

					if (year === curYear && month === curMonth && day === curDay) {
						link.addClass('current');
					}

					tr.append($('<td></td>').append(link));

					day++;

				} else {
					tr.append('<td class="empty"></td>');
				}

			}

			calendar.append(tr);

			if (day > monthLength) {
				break;
			}

		}

		// Update calendar
		$(this)
			.parents('table')
			.find('tbody')
			.replaceWith(calendar);

		// Update heading
		$('.monthLink')
			.attr('href', '/whatson/' + year + '/' + (month + 1))
			.text(calendarMonthName[month] + ' ' + year);

		return false;

	});

}

/* ------------------------------ */
/* Leading zeros for numbers	  */
/* ------------------------------ */
jQuery.fn.padNum = function(num) {
	return (num < 10 ? '0' : '') + num;
};

/* ------------------------------ */
/* Form overlabels			*/
/* ------------------------------ */
jQuery.fn.overlabel = function() {
		this.each(function(index) {
				var label = $(this); var field;
				var id = this.htmlFor || label.attr('for');
				if (id && (field = document.getElementById(id))) {
						var control = $(field);
						label.addClass("overlabel-apply");
						if (field.value !== '') {
								label.fadeOut(100);
						}
						control.focus(function () {label.fadeOut(100);}).blur(function () {
								if (this.value === '') {
										label.fadeIn(100);
								}
						});
						label.click(function() {
								var label = $(this); var field;
								var id = this.htmlFor || label.attr('for');
								if (id && (field = document.getElementById(id))) {
										field.focus();
								}
						});
				}
		});
};

/* ------------------------------ */
/* END Form overlabels */
/* ------------------------------ */


/* ------------------------------ */
/* Home-page twitter-feed */
/* ------------------------------ */

function homePageInit() {
	if($('#twitter').length > 0) {
		$.getScript(	'http://widgets.twimg.com/j/2/widget.js',
						function() {
							new TWTR.
								Widget({	version:	2,
											type:		'profile',
											id:			'twitter',
											rpp:		3,
											interval:	5000,
											width:		'auto',
											height:		'auto',
											theme:		{	shell:	{	background:	'transparent',
																		color:		'#000'
																		},
															tweets:	{	background:	'transparent',
																		color:		'#000',
																		links:		'#383838'
																		}
																},
											features:	{	scrollbar:	false,
															loop:		false,
															live:		false,
															hashtags:	true,
															timestamp:	true,
															avatars:	true,
															behavior:	'default'
															}
								}).
								render().
									setUser('colston_hall').
										start();
						});
	}
}

/* ------------------------------ */
/* END Home-page twitter-feed */
/* ------------------------------ */

