// Global-scope array containing dates to show in the calendar
var datesWithComics = new Array();
var currentDate = '';

// ADDTHIS configuraton
var addthis_config =
{
   ui_508_compliant: true,
   "data_track_clickback":true,
   ui_header_color: "#ffffff",
   ui_header_background: "#000000"
}

// document.ready
$(document).ready(function () {	
	currentDate = $('#current-comic-date').val();
	
	// Fix the comic div height
	var comicHeight = $('#comic-image').height();
	if(comicHeight > 300) {
		$('#comic').height(comicHeight+160);
	}
	
	// NAVIGATION
	$('.navigation-default').hover(OnTabIn, OnTabOut);
	
	var path = window.location.pathname;
    if(path == '/fcdsdev/' || path == '/') {
        $('nav ul li a[href$="index.php"]').parent().attr('class', 'navigation-active round-corners');
    }
	else {
        var file = path.substring(path.lastIndexOf('/')+1);
        if (file) {
            $('nav ul li a[href$="' + file + '"]').parent().attr('class', 'navigation-active round-corners');
        }	
    }
    
    // BUTTONS
	$('#firstButton').button({
		icons: {
			primary: "ui-icon-seek-first"
		}
	});
	$('#prevButton').button({
		icons: {
			primary: "ui-icon-seek-prev"
		}
	});
	$('#categories-dropdown-button')
			.button()
			.next()
				.button( {
					text: false,
					icons: {
						primary: "ui-icon-triangle-1-s"
					}
				})
				.parent()
					.buttonset();
	$('#nextButton').button({
		icons: {
			secondary: "ui-icon-seek-next"
		}
	});
	$('#lastButton').button({
		icons: {
			secondary: "ui-icon-seek-end"
		}
	});
	$('#categories-dropdown-button').addClass('category-link');
	$('#categories-dropdown-opener').removeClass('hide').addClass('show');
	// Check to disable buttons
	$('a[href="#"]').button( "option", "disabled", true );
	// Search button
	$('#ohnorobot-search-button').button();
	
	// TABS
	$('#tabs').tabs();
	
	// CALENDARS
	KludgeTheDatepicker();
	
	$('#calendar').datepicker({
		gotoCurrent: false,
		showOtherMonths: true,
		dateFormat: 'yy-mm-dd',
		defaultDate: currentDate,
		beforeShowDay: function(dateToShow) {
			testDate = $.datepicker.formatDate('yy-mm-dd', dateToShow);
			isInArray = $.inArray(testDate,datesWithComics);
			retVal = (isInArray >= 0);
			return [retVal, ''];
		},
		onChangeMonthYear: function (year, month, inst) {
			if(month < 10){
				month = '0' + month;
			}
						
			jQuery.ajax({
		 		url:    '_calendar-service.php?year=' + year + '&month=' + month,
		 		success: function(result) {
		 			datesWithComics = jQuery.parseJSON(result);
				},
		 		async:   false
			});
		},
		onSelect: function (date, inst) {
			location = 'archive.php?comic=' + date;
		}
	});
	
	// MENUS 
	$('#categories-dropdown-opener').menu({
		content: $('#categories-dropdown').html(),
		width: 260,
		positionOpts: { posX: 'right', posY: 'bottom', directionV: 'down', offsetX: -270, offsetY: -5, detectH: true, detectV: true },
		backLink: false
	});

	// Nicetitles
	$('a').niceTitles();
});

// Load the default dates
var KludgeTheDatepicker = function(){
	jQuery.ajax({
		url:    '_calendar-service.php?year=' + currentDate.substr(0, 4) + '&month=' + currentDate.substr(5, 2),
		success: function(result) {
			datesWithComics = jQuery.parseJSON(result);
		},
		async:   false
	});
}

// Hover over a nav element
var OnTabIn = function() {
	if($(this).hasClass('navigation-active')) {
	}
	else {
		$(this).addClass('navigation-hover');
	}
}

// Hover out of a nav element
var OnTabOut = function() {
	if($(this).hasClass('navigation-active')) {
	}
	else {
		$(this).removeClass('navigation-hover');
	}				
}
